Page 1 of 1

On-the-fly code formatting

Posted: Thu Sep 02, 2010 2:33 pm
by Jezze
I had this "epiphany" the other day. I was browsing around some random C code written by someone who used a code formatting style I really didn't enjoy and I though that there must exist some sort of tool (in my case on Linux) I could use to make the code look the way I am familiar with.

The more I thought about it I began thinking it's weird that I haven't heard of any such tool, probably because it would be hard (and sometimes impossible) to write for certain languages.

I imagine having a tool (preferably integrated into vim) that dynamically changes the formatting of a C file to match my preferences. Another nice feature would be if it could automatically compress the code (basically remove most whitespaces) to the smallest possible before storing it to disk in order to save space.

Does such a tool or plugin exist?

Re: On-the-fly code formatting

Posted: Thu Sep 02, 2010 3:06 pm
by Jezze
Oops... after some more googling i found indent.

Re: On-the-fly code formatting

Posted: Fri Sep 03, 2010 2:15 am
by Candy
berkus wrote:astyle is somewhat more usable than indent (to my taste).
Quote: "Most C code should be indented 6 feet downward and covered with dirt.". I'm not sure code formatters are going to help enough :-)

Re: On-the-fly code formatting

Posted: Fri Sep 03, 2010 8:05 am
by Solar
C source indenters are quite capable. Unfortunately, C++ syntax can become too unreadable even for a source reformatter... I tried my hands at AStyle for some time, and was unable to get all the corner cases covered. The current maintainer does a marvellous job, but still: Don't reformat code when you're not able to check it really behaves like expected afterwards.

Personally I'd use indent for C source (although there are several vi plugins out there that can do a similarily good job). For C++, there's no "free" (as in beer) alternative that I've heard of, other than astyle.