This repo contains instructions and a command line tool to help achieve consistent formatting for hso-autonomy projects (in an IDE-independent way). We use clang-format for Java and C++, and yapf for Python.
- Clone this repository.
- Run
java -jar formattingHelper.jar --init <project dir(s)>
(where<project dir(s)>
is one or multiple project directories to which to add the config files). This should be done again when there are changes to the config / this repo.
In case the repository has not been set up for formatting yet:
-
Add the following to
.gitignore
:.idea/**/watcherTasks.xml .clang-format *.TMP *.java-*
-
Run
java -jar formattingHelper.jar <project dir(s)>
(same as before, but without--init
) to recursively reformat all source files in the repo. With--verbose
, the path of every formatted file is printed.
These instructions assume that the earlier, general setup has already been done.
(these instructions should work for all IDEA-based IDEs, including CLion / PyCharm)
-
Install the File Watchers plugin:
File|Settings|Plugins|Browse Repositories...
, then search for "File Watchers" and install it. -
After restarting IDEA, making a change to a file and pressing Ctrl+S should trigger a reformat:
-
Deactivate the built-in format-on-save if enabled (
Window|Preferences|Java|Editor|Save Actions
, uncheck "Format source code"). -
Install a modified version of the CppStyle plugin by copying
org.wangzw.cppstyle-1.4.0.6.jar
to thedropins
directory of your Eclipse installation. -
You may need to start Eclipse from the command line using
eclipse -clean
(to clear the plugin cache). -
Go to
Window|Preferences|CppStyle
. Here, specify theClang-format path
(one of the files in/binaries
, depending on your OS) and enableRun clang-format on file save
. -
Making a change to a file and pressing Ctrl+S should now trigger a reformat:
-
Install the Clang-Format extension (
View|Extensions
, search for "clang-format") -
enable format-on-save by adding this to
settings.json
(File|Preferences|Settings
):"editor.formatOnSave": true
-
Also set the
"clang-format.executable"
path (on Windows, escape\
with\\
!), or make sureclang-format
is in yourPATH
. -
After restarting VSCode, making a change to a file and pressing Ctrl+S should trigger a reformat:
- On Windows, clang-format may create
TMP
files next to source files sometimes (such asWorldModel.java~RF3494d0.TMP
). This is why*.TMP
should be added to.gitignore
as instructed earlier. More info here.
Overall it works very well, however.