We believe that defining formatting rules and coding accordingly is important, especially in multi-person projects, to improve the readability of your code.
On the other hand, it is a costly task for each developer to remember the formatting rules and write the code according to them, and to check whether the code written by other developers follows the formatting rules, so it is automatic. It is better to use a code formatter that has the same format.
Regarding the timing and method of passing the code formatter, "The person who writes the code passes the formatter each time" "Set to be automatically formatted when saving the file" "Set to be automatically formatted when committing" " There are various possibilities such as "formatting using commands from the command line", and to prevent forgetting to format, it is better to format automatically at the time of saving or committing, but that is the command from the command line. In some cases it is useful to be able to format using. That's why (although it seems to be forcible), here I would like to introduce a method of formatting from the command line for C languages.
Eclipse
Eclipse is a well-known integrated development environment. It is mainly used for Java program development, but it can be used for program development in various languages by inserting a plug-in.
In Eclipse, you can set formatting rules for the entire workspace or for each project, and you can format them on the GUI, but you can actually format them from the command line as well.
To format from the command line to follow the formatting rules set in your project:
$ eclipse -nosplash -application org.eclipse.jdt.core.JavaCodeFormatter -config $PROJ_DIR/.settings/org.eclipse.jdt.core.prefs Hoe.java Fuga.java
Where $ PROJ_DIR
is the top directory of the project where the format rules are set.
It seems that only Java can be formatted from the command line at the moment. For example, if you have installed a plugin for C / C ++ program development called CDT, you can format C / C ++ code from the GUI, but it seems that you cannot format it from the command line. Sorry.
ClangFormat
ClangFormat is a formatter for C / C ++ / Objective-C / Objective-C ++ that comes with clang. You can use it from the command line with the clang-format
command.
For example, to format with Google's format style:
$ clang-format -i -style=google Hoe.h Hoe.cpp
Adding -i
will overwrite the original file.
The drawback of ClangFormat is that it's relatively cumbersome to install (you need to build from the LLVM source code: as of March 2014). However, the built clang-format
works independently, so when using it in a project, everyone uses the binary built by one person to prevent the difference in behavior due to the difference in the version of the original source code. Would be good.
Artistic Style
(Artistic Style) [http://astyle.sourceforge.net/] is a code formatting tool for C / C ++ / C # / Objective-C / Objective-C ++ / Java.
For example, to format C # code with Google's format style:
$ astyle --style=google --mode=cs --suffix=none Hoe.cs Fuga.cs
The original file will be overwritten by adding --suffix = none
.
ʻA style --help` will give you a detailed description of the command line options.
SublimeAStyleFormatter
As a front end of Artistic Style, there is Sublime AStyle Formatter which is a plug-in for Sublime Text 2. For this, "Introducing formatter to Sublime Text 2" will be helpful.
The method of customizing the behavior of Sublime AStyle Formatter is relatively undocumented, but you can understand it by looking at the following page.
When processing the source code with a script etc., you may want to suppress formatting for a part of the source code. This section describes the suppression method for each formatter.
Eclipse
Enclose the block for which you want to suppress formatting with // @formatter: off
and // @formatter: on
.
// @formatter:off
...
// @formatter:on
However, this tag must be enabled in the Java formatter settings.
ClangFormat
Enclose the block for which you want to suppress formatting with // clang-format off
and // clang-format on
. (Reference: CLANG-FORMAT STYLE OPTIONS)
// clang-format off
...
// clang-format on
It seems that this specification is valid from 3.6 of ClangFormat. I tried it with 3.5 and it was not recognized.
Artistic Style
Enclose the block for which you want to suppress formatting with // * INDENT-OFF *
and // * INDENT-ON *
. (Reference: Artistic Style 2.05)
// clang-format off
...
// clang-format on
We have introduced Eclipse, ClangFormat, and Artistic Style as tools for formatting files from the command line. Personally, if you are developing with Java + Eclipse, Eclipse, if you are developing with a C language other than C #, Clang Format or Artistic Style, if you are developing with C #, I think it is Artistic Style.
Recommended Posts