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.
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.
Next I basically did what I was told:
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 do-stuff
, in what is called the
"minibuffer". Intellij has this as find action
typically
invoked by hitting
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
Of course I tried more commands, somewhat randomly picked, to see what
happens. For one, Emacs told me "you could run command ... also with
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 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.