Continuing the series about of configuring Emacs as a Java development IDE, lets talk about code formatting.
Emacs' lsp-mode uses the Eclipse language server to stay informed about the Java code being edited. This also includes the server's service to format part or all of the Java code according to some rules. It employs Eclipse's code formatter which has lots of options. When using the Eclipse UI, these can be tweaked visually in an extensive settings screen.
To use these settings with the language server, one can export the settings file and use it with lsp-java. The following describes the steps.
To create the configuration file it seems easiest to start a recent
version of Eclipse and navigate to Window -> Preferences ->
Formatter
. If you need this very description, you'll likely see
only one profile, select Edit
, adapt the rules as needed,
change the name to, say, blablub-format
to unlock
the Export
button and save the file to,
say, ~/.emacs.d/eclipse-formatter.xml
.
There are two variables of lsp-java which need to be customized:
lsp-java-format-settings-url
: I provided the full
absolute path to the
file .../.emacs.d/eclipse-formatter.xml
lsp-java-format-settings-profile
should be the
name of the formatting profile. Check the top of the generated file
to be sure, you should see the name you provided as the profile
name: blablub-format
was the example above.You know how to customize variables in Emacs, right? As a reminder,
you can use
Switch to a Java buffer which is
managed by the language server and use
<setting id="org.eclipse.jdt.core.formatter.brace_position_for_block" value="end_of_line" />
between end_of_line
and next_line
by editing
the formatter file. Make sure to restart the language server after
changes, as these do not seem to be picked up automatically.
To avoid the detour through Eclipse it may sometimes be enough to find a configuration value in the Eclipse documentation, but the best bet is probably to store the format settings file in a VCS and import, change and export it with Eclipse.
At one point I thought that the format file should show up in the
argument list of the jdt.ls Java process. But this is not the
case. The settings are shoved into the language server via the
language server protocol. One thing that worked to at least verify
that the file was touched was to watch it
with inotifywait
.