When indenting source code, programmers have a tendency to use both spaces and tab characters to create the wanted indentation. Moreover, some editors by default insert spaces instead of tabs when pressing the tab key, and other editors (Emacs for example) have the ability to "pretty up" lines by automatically setting up the white space before the code on the line, possibly inserting spaces in a code that up to now used only tabs for indentation.
Since the NUMBER of space characters showed on screen for each tab character in the source code changes between editors (unless the user sets up the number to his liking...), one of the standard problems programmers are facing when moving from one editor to another is that code containing both spaces and tabs that was up to now perfectly indented, suddenly becomes a mess to look at when changing to another editor. Even if you as a programmer take care to ONLY use spaces or tabs, looking at other people's source code can still be problematic.
To address this problem, Artistic Style was created - a filter written in C++ that automatically re-indents and re-formats C / C++ / C# / Java source files. It can be used from a command line, or it can be incorporated as classes in another C++ program.
Installation:
OpenSuSe user can install Astyle using "1-click" installer - here
Using Astyle:
If you have never used Artistic Style there are a couple of ways to start. One is to run it with no options at all. This will format the file with 4 spaces per indent and will leave the brackets unchanged. Another is to use one of the predefined styles described in the "Predefined Style Options" section (below). Select one with a bracket formatting style you like. Once you are familiar with the options you can customize the format to your personal preference.
Predefined Styling options:
--style=ansi ANSI style formatting/indenting.
--style=kr Kernighan&Ritchie style formatting/indenting.
--style=gnu GNU style formatting/indenting.
--style=java Java mode, with standard java style formatting/indenting.
--style=linux Linux mode (i.e. 8 spaces per indent, break definition-block brackets but attach command-block brackets.
Example to format a single file:
astyle --style=allman /home/user/project/foo.cpp
Example to format all .cpp and .h files recursively:
astyle --style=allman --recursive /home/user/project/*.cpp /home/user/project/*.h
Check out the Astyle documentation for many more options.
0 comments:
Post a Comment