$ \def\Vec#1{\mathbf{#1}} \def\vt#1{\Vec{v}_{#1}(t)} \def\v#1{\Vec{v}_{#1}} \def\vx#1{\Vec{x}_{#1}} \def\av{\bar{\Vec{v}}} \def\vdel{\Vec{\Delta}} $

Harald Kirsch

about this blog
2024-02-15

Starting with Emacs lsp-mode for Java development

First the Rant

In ancient times (the 1980s) I learned programming mostly using Emacs and some key bindings got chiseled into my brain forever. When I started Java programming in Emacs, support for coding actions based on compiler output, like quick fixes suggestions, was not available, sadly. So I switched to Eclipse for Java programming. But my first action was to enable Emacs key bindings as far as available.

Only recently, around 2019, I switched mostly to Intellij, because around that time, Eclipse got weirder by the day. For example on my Linux with XFCE4, menus tended to be non-functional. And I had hated the workspace concept of Eclipse from day one. Again Emacs key bindings were applied as good as possible, several manual rebindings included.

Around the same time I also started using vscode for Typescript programming and again enabled Emacs key bindings, including manual fixes as far as I felt the need for.

Then, recently, I started into a sabbatical and thought, hey, nobody forces you to use Eclipse/Intellij/vscode. How's the support for Java in Emacs these days. Could I go back?

But why should I? Well, I never stopped using Emacs for other editing and all three IDEs have some lacking when it comes to some slickness I got used to in my programmer "infancy":

So lets see if I can make friend with lsp-java which uses the incremental compiler from eclipse to stay informed about the code structure.

As somewhat expected, though, getting started is a bit tough, despite the seemingly good documentation.

Starting with lsp-java

Git the .emacs.d

Reading the Quick Start it is obvious that big things will happen with my to my hand crafted, dry aged emacs configuration files. So I decided to preserve the current state and be able to see all the changes by checking my ~/.emacs.d into a local git repository.

In particular I renamed ~/.emacs to ~/.emacs.d/init.el which, according to the manual are equivalent, so that no tricks were needed to include the initialization file into git.

Follow the installation instructions

Next I basically did what I was told:

  1. install the package
  2. add the magic spells (on the same page as the above link)

Try it and ... ughhh? ... aaaah!

Then I opened up the first Java file of a small project of mine. It looked good, as I noticed that the jdt server was downloaded an started a the syntax highlighting was working.

Next I deleted an import some.Stuff line from the code and nicely, the first use of Stuff was highlighted with an error.

But now comes the point I had somewhat expected where Emacs shows that it does not easily make friends with every odd dev coming along. A bit of peristence is required at the start. Even for long-term users like me. How can I tell it to just fix the missing import?

Even if you don't know the keyboard shortcut of some function do-stuff, in Emacs you can always invoke it by typing Meta-x and then enter the function name, do-stuff, in what is called the "minibuffer". Intellij has this as find action typically invoked by hitting SHIFT twice quickly. So I thought it should suffice to find the right function name and invoke it.

Somehow I got astray on the lsp-ui page and invoked lsp-ui-sideline-apply-code-actions, which sounded right, but it insisted to tell me: there are no code actions on this line. Even the error message seemed to tell me I had the right function, but, hey, there is nothing to offer. If this was to be true, the whole thing would be completely useless. Quickly fixing a missing import is a must.

Next I tried CTRL-x m to describe the Emacs editing "mode" for the current file. This usually comes with long lists of key bindings available. Sadly not for the lsp stuff. Hmm.

Of course I tried more commands, somewhat randomly picked, to see what happens. For one, Emacs told me "you could run command ... also with s-l T a. The "s" is for the Super key. So I tried this and since I was somewhat slow after the s-l, a list of alternatives valid after it appeared at the bottom of the screen, thanks to which-key mode that was installed and activated as part of the whole thing. Somehow I had never come across this minor-mode before, likely because I have used Emacs only for simple text editing in the last years.

But now I am in business because the possibilities are all listed nicely and it is a matter of exploration to find things that I like to use. It will surely also enable me to find more documentation as I'll see the keywords to search for.

Fixing the wrong import, or other mistakes I entered on purpose, was now as simple as s-l a a which invokes lsp-execute-code-action, listed first on the lsp-java page. Stupid me.

To be continued.

...June 2024 addition: use Eglot, not lsp-mode. Though I actually also tried Eglot, but I am back to Intellj. Emacs is still very, very far from what can be done with IDEs such as Intellij or Eclipse. Sorry to say this.