On-the-fly code formatting

All off topic discussions go here. Everything from the funny thing your cat did to your favorite tv shows. Non-programming computer questions are ok too.
Post Reply
User avatar
Jezze
Member
Member
Posts: 395
Joined: Thu Jul 26, 2007 1:53 am
Libera.chat IRC: jfu
Contact:

On-the-fly code formatting

Post 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?
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
User avatar
Jezze
Member
Member
Posts: 395
Joined: Thu Jul 26, 2007 1:53 am
Libera.chat IRC: jfu
Contact:

Re: On-the-fly code formatting

Post by Jezze »

Oops... after some more googling i found indent.
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: On-the-fly code formatting

Post 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 :-)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: On-the-fly code formatting

Post 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.
Every good solution is obvious once you've found it.
Post Reply