unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Editing Java (with LSP)
@ 2022-06-13 13:43 Alessandro Bertulli
  2022-06-13 15:48 ` Filipp Gunbin
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Alessandro Bertulli @ 2022-06-13 13:43 UTC (permalink / raw)
  To: help-gnu-emacs

Hi all!

I am struggling to instruct Emacs to properly assist me in writing Java
code. Whereas using C/C++ was a breeze, Java simply seems to be more
buggy. I explain: using the integrated server from lsp-mode seems to be
broken. I partially resolved it by manually dowloading a (much) newer
version of the server (which is jdtls). This seemed to solve part of the
problems, but the experience is still buggy. Completion often is not
triggered (not even by explicitly C-M-i), the server sometimes crashes,
and when I modify the buffer I need to write it before error
highlighting properly updates (for instance, if I modify a piece of code
who had an error, that point on screen is still highlightd as an error,
even if it's now in the middle of a word). Moreover, the indentation
sometimes doesn't work.

Now, it seems like:
- I am one of the very, very few people on Earth using Emacs to write
Java
- I am the only one having problems.

Can you please give me your feedback? Does any of you write Java code?
How is it? Do you have the same problems too? If yes, have you found a
solution? Do you use LSP? Or some other package?

Note that my config is simply lsp-mode + lsp-java.

Thanks in advance!

Bertulli




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Editing Java (with LSP)
  2022-06-13 13:43 Editing Java (with LSP) Alessandro Bertulli
@ 2022-06-13 15:48 ` Filipp Gunbin
  2022-06-13 21:53   ` Alessandro Bertulli
  2022-06-13 20:32 ` Milan Zamazal
  2022-06-16  3:29 ` Pankaj Jangid
  2 siblings, 1 reply; 10+ messages in thread
From: Filipp Gunbin @ 2022-06-13 15:48 UTC (permalink / raw)
  To: Alessandro Bertulli; +Cc: help-gnu-emacs

Hi,

On 13/06/2022 15:43 +0200, Alessandro Bertulli wrote:

> Hi all!
>
> I am struggling to instruct Emacs to properly assist me in writing Java
> code. Whereas using C/C++ was a breeze, Java simply seems to be more
> buggy. I explain: using the integrated server from lsp-mode seems to be
> broken. I partially resolved it by manually dowloading a (much) newer
> version of the server (which is jdtls). This seemed to solve part of the
> problems, but the experience is still buggy. Completion often is not
> triggered (not even by explicitly C-M-i), the server sometimes crashes,
> and when I modify the buffer I need to write it before error
> highlighting properly updates (for instance, if I modify a piece of code
> who had an error, that point on screen is still highlightd as an error,
> even if it's now in the middle of a word). Moreover, the indentation
> sometimes doesn't work.
>
> Now, it seems like:
> - I am one of the very, very few people on Earth using Emacs to write
> Java

That seems to be true.  There're very few of us.

> - I am the only one having problems.
>
> Can you please give me your feedback? Does any of you write Java code?
> How is it? Do you have the same problems too? If yes, have you found a
> solution? Do you use LSP? Or some other package?
>
> Note that my config is simply lsp-mode + lsp-java.

I use my package javaimp (http://elpa.gnu.org/packages/javaimp.html),
it's limited in functionality, but it fits my needs.

Current version knows about the project structure and dependencies (from
Maven or Gradle), assists with completion in imports, and implements
imenu better than cc-mode.  Next version is almost ready, it has
improved parsing (for imenu and other stuff), limited xref support
(finding a definition should work), navigation functions, and a minor
mode to enable all that.

Next, I plan to add support for debugging via gud (I already have the
dependencies info), REPL via jshell (jdk 11), and perhaps implement
indentation (this will remove dependency on cc-mode).  However, I don't
know when I'll be able to complete all this.

I don't intend to work on good font-lock support, because I don't use it
myself, and it would require full source parsing, which is better done
with treesitter (there's ongoing work on treesitter integration into
Emacs, hopefully we'll see it soon).  Also, clever source completion is
also not in my plan: for that, you need a compiler, via lsp or directly.

HTH, Filipp



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Editing Java (with LSP)
  2022-06-13 13:43 Editing Java (with LSP) Alessandro Bertulli
  2022-06-13 15:48 ` Filipp Gunbin
@ 2022-06-13 20:32 ` Milan Zamazal
  2022-06-16  3:29 ` Pankaj Jangid
  2 siblings, 0 replies; 10+ messages in thread
From: Milan Zamazal @ 2022-06-13 20:32 UTC (permalink / raw)
  To: help-gnu-emacs

>>>>> "AB" == Alessandro Bertulli <alessandro.bertulli96@gmail.com> writes:

    AB> Does any of you write Java code?  How is it? Do you have the
    AB> same problems too? If yes, have you found a solution? Do you use
    AB> LSP?

I use lsp-mode relatively successfully to work on a Java project with
~9000 *.java files (https://github.com/oVirt/ovirt-engine).  I find it
easier than using lsp-mode with C because no special compiler
arrangements are needed, it works itself.  I needn’t to use anything
else.  The only special configuration I use is

  (setq lsp-java-vmargs
        '("-noverify" "-Xmx4G" "-XX:+UseG1GC"
          "-XX:+UseStringDeduplication"))

but I can’t recall why it is needed.

From time to time, I experience similar symptoms like those you
describe.  In such a case, I delete ~/.emacs.d/.cache and
~/.emacs.d/eclipse.jdt.ls.  Then it fetches jdtls, reindexes the project
and starts working fine again.

OTOH, dap-mode works much worse and I cannot use it for Java debugging.
IIRC the most annoying thing was collapsing expanded data structures all
the time, making practically impossible watching what happens with data.
And the overall functionality was limited due to inability to work well
with the given maven definitions.  I use IntelliJ IDEA for debugging,
which works perfectly.

Regards,
Milan




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Editing Java (with LSP)
@ 2022-06-13 21:35 Alessandro Bertulli
  2022-06-14 19:13 ` Milan Zamazal
  0 siblings, 1 reply; 10+ messages in thread
From: Alessandro Bertulli @ 2022-06-13 21:35 UTC (permalink / raw)
  To: pdm; +Cc: help-gnu-emacs

Thanks!

> I use lsp-mode relatively successfully to work on a Java project with
> ~9000 *.java files (https://github.com/oVirt/ovirt-engine).  I find it
> easier than using lsp-mode with C because no special compiler
> arrangements are needed, it works itself.

Good to hear that, it means it is usable. By the way, which system are
you on? I'm currently using Arch (non meme intended) and I suspect my
system can be buggy by misconfiguration.

> I needn’t to use anything
> else.  The only special configuration I use is
> 
>   (setq lsp-java-vmargs
>         '("-noverify" "-Xmx4G" "-XX:+UseG1GC"
>           "-XX:+UseStringDeduplication"))
> 
> but I can’t recall why it is needed.

Thanks, I'll investigate those settings.

>From time to time, I experience similar symptoms like those you
> describe.  In such a case, I delete ~/.emacs.d/.cache and
> ~/.emacs.d/eclipse.jdt.ls.  Then it fetches jdtls, reindexes the project
> and starts working fine again.

I figured out that, apparently, a simple M-x lsp-workspace-restart is
sufficient. Meaning that it seems that it is the server that crashes, so
restarting it solves the problem. Of course, it is not normal it crashes
so often, but anyway.

> OTOH, dap-mode works much worse and I cannot use it for Java debugging.
> IIRC the most annoying thing was collapsing expanded data structures all
> the time, making practically impossible watching what happens with data.
> And the overall functionality was limited due to inability to work well
> with the given maven definitions.  I use IntelliJ IDEA for debugging,
> which works perfectly.

Uhm, for me dap-mode (SEEMS to) work well, but to be honest I haven't
had the need to investigate deep data structures yet. In case, I'll use
IntelliJ, it'll simply means that Emacs is not ready yet to be used for
Java editing. Which I think it's a shame, because I think it has the
potentiality to do so. Anyway, right now I think it's pretty standard
opinion that IntelliJ IS the de-facto editor for Java, and it's the
flagship of JetBrains.

Alessandro



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Editing Java (with LSP)
  2022-06-13 15:48 ` Filipp Gunbin
@ 2022-06-13 21:53   ` Alessandro Bertulli
  2022-06-14 19:12     ` Filipp Gunbin
  0 siblings, 1 reply; 10+ messages in thread
From: Alessandro Bertulli @ 2022-06-13 21:53 UTC (permalink / raw)
  To: Filipp Gunbin; +Cc: help-gnu-emacs


Filipp Gunbin <fgunbin@fastmail.fm> writes:

> That seems to be true.  There're very few of us.
Indeed

>
> I use my package javaimp (http://elpa.gnu.org/packages/javaimp.html),
> it's limited in functionality, but it fits my needs.
>
> Current version knows about the project structure and dependencies (from
> Maven or Gradle), assists with completion in imports, and implements
> imenu better than cc-mode.  Next version is almost ready, it has
> improved parsing (for imenu and other stuff), limited xref support
> (finding a definition should work), navigation functions, and a minor
> mode to enable all that.

Uhm, interesting! I suppose it's a completely stand-alone package,
independent from LSP?

> Next, I plan to add support for debugging via gud (I already have the
> dependencies info), REPL via jshell (jdk 11), and perhaps implement
> indentation (this will remove dependency on cc-mode).  However, I don't
> know when I'll be able to complete all this.

I have experimented with GUD/RealGUD, but honestly it didn't click in to
me. I felt natural with gdb, but to me the GUD interface with other
debuggers is not as standard as it should be. Let me know if I am naive
here, but: if I can't use the same commands in every debugger, then
whats the point?

Do you feel good with GUD? Did you have to do any special configuration?

> I don't intend to work on good font-lock support, because I don't use it
> myself, and it would require full source parsing, which is better done
> with treesitter (there's ongoing work on treesitter integration into
> Emacs, hopefully we'll see it soon).  Also, clever source completion is
> also not in my plan: for that, you need a compiler, via lsp or directly.

Yap, I don't know treesitter but I suspect it may be an useful
integration also to solve this problem. Unfortunately, completion is one
of the thing that I find very useful (especially when exploring a new
codebase/library/framework, having type-checked completion is really
like having a guide, and it reduces basic, dumb errors)

Alessandro



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Editing Java (with LSP)
  2022-06-13 21:53   ` Alessandro Bertulli
@ 2022-06-14 19:12     ` Filipp Gunbin
  0 siblings, 0 replies; 10+ messages in thread
From: Filipp Gunbin @ 2022-06-14 19:12 UTC (permalink / raw)
  To: Alessandro Bertulli; +Cc: help-gnu-emacs

On 13/06/2022 23:53 +0200, Alessandro Bertulli wrote:

> Filipp Gunbin <fgunbin@fastmail.fm> writes:
>
>> That seems to be true.  There're very few of us.
> Indeed
>
>>
>> I use my package javaimp (http://elpa.gnu.org/packages/javaimp.html),
>> it's limited in functionality, but it fits my needs.
>>
>> Current version knows about the project structure and dependencies (from
>> Maven or Gradle), assists with completion in imports, and implements
>> imenu better than cc-mode.  Next version is almost ready, it has
>> improved parsing (for imenu and other stuff), limited xref support
>> (finding a definition should work), navigation functions, and a minor
>> mode to enable all that.
>
> Uhm, interesting! I suppose it's a completely stand-alone package,
> independent from LSP?

Yes, it's independent from LSP.

It depends on Maven / Gradle to provide dependency and classpath info,
but invokes them as commands.

>> Next, I plan to add support for debugging via gud (I already have the
>> dependencies info), REPL via jshell (jdk 11), and perhaps implement
>> indentation (this will remove dependency on cc-mode).  However, I don't
>> know when I'll be able to complete all this.
>
> I have experimented with GUD/RealGUD, but honestly it didn't click in to
> me. I felt natural with gdb, but to me the GUD interface with other
> debuggers is not as standard as it should be. Let me know if I am naive
> here, but: if I can't use the same commands in every debugger, then
> whats the point?

I think the point is in integration of command-line debugger (jdb in
case of Java) with source files in Emacs buffers.

> Do you feel good with GUD? Did you have to do any special
> configuration?

I haven't used gud for a long time now.  And back then, I only checked
it works.  Without automagical feeding it with project sources /
classpath, it's almost useless, and I certainly wouldn't do that by
hand...

>> I don't intend to work on good font-lock support, because I don't use it
>> myself, and it would require full source parsing, which is better done
>> with treesitter (there's ongoing work on treesitter integration into
>> Emacs, hopefully we'll see it soon).  Also, clever source completion is
>> also not in my plan: for that, you need a compiler, via lsp or directly.
>
> Yap, I don't know treesitter but I suspect it may be an useful
> integration also to solve this problem. Unfortunately, completion is one
> of the thing that I find very useful (especially when exploring a new
> codebase/library/framework, having type-checked completion is really
> like having a guide, and it reduces basic, dumb errors)

Tree-sitter is an efficient parser library, see
https://tree-sitter.github.io/tree-sitter/.  However, for good
completion, I suspect you'd still have to resort to LSP.

Filipp



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Editing Java (with LSP)
  2022-06-13 21:35 Alessandro Bertulli
@ 2022-06-14 19:13 ` Milan Zamazal
  0 siblings, 0 replies; 10+ messages in thread
From: Milan Zamazal @ 2022-06-14 19:13 UTC (permalink / raw)
  To: Alessandro Bertulli; +Cc: help-gnu-emacs

>>>>> "AB" == Alessandro Bertulli <alessandro.bertulli96@gmail.com> writes:

    AB> By the way, which system are you on? I'm currently using Arch
    AB> (non meme intended) and I suspect my system can be buggy by
    AB> misconfiguration.

I’m on NixOS but I don’t think it’s much important.  The distribution
provides Emacs and the rest (add-ons + jdtls) is fetched by Emacs itself.

    >> From time to time, I experience similar symptoms like those you
    >> describe.  In such a case, I delete ~/.emacs.d/.cache and
    >> ~/.emacs.d/eclipse.jdt.ls.  Then it fetches jdtls, reindexes the
    >> project and starts working fine again.

    AB> I figured out that, apparently, a simple M-x
    AB> lsp-workspace-restart is sufficient. Meaning that it seems that
    AB> it is the server that crashes, so restarting it solves the
    AB> problem. Of course, it is not normal it crashes so often, but
    AB> anyway.

If it crashes often, I’d try deleting the caches as suggested above.
It may or may not help but if it starts crashing for me then there is no
other help.

    AB> In case, I'll use IntelliJ, it'll simply means that Emacs is not
    AB> ready yet to be used for Java editing. Which I think it's a
    AB> shame, because I think it has the potentiality to do so.

Sure.  But while lsp-mode is a must, dap-mode is optional.  It’s OK to
run a standalone debugger, there is not that much need for integration
with Emacs there.  Of course, having everything in Emacs would be nice,
but I do Java programming only for living so I prefer getting it done
using what’s available at the moment.  I tried to look into dap-mode
briefly once or twice but it didn’t look like easy fixes.



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Editing Java (with LSP)
  2022-06-13 13:43 Editing Java (with LSP) Alessandro Bertulli
  2022-06-13 15:48 ` Filipp Gunbin
  2022-06-13 20:32 ` Milan Zamazal
@ 2022-06-16  3:29 ` Pankaj Jangid
  2022-06-16 13:34   ` Alessandro Bertulli
  2 siblings, 1 reply; 10+ messages in thread
From: Pankaj Jangid @ 2022-06-16  3:29 UTC (permalink / raw)
  To: Alessandro Bertulli; +Cc: help-gnu-emacs

Alessandro Bertulli
<alessandro.bertulli96@gmail.com> writes:

> Can you please give me your feedback? Does any of you write Java code?
> How is it? Do you have the same problems too? If yes, have you found a
> solution? Do you use LSP? Or some other package?
>

I use eglot+eclipse.jdt.ls. There is a bit of struggle in setting this
up.

Although, eglot tries to download latest eclipse.jdt.ls but it is not
always perfect. There are CLASSPATH issues and all those things which
are typical of Java. And even after full setup I face issues here and
there. Sometimes.

My setup is like this (assuming you have cloned eclipse.jdt.ls
repository inside ~/.emacs.d and built using ‘./mvnw clean package’),

--8<---------------cut here---------------start------------->8---
(unless (package-installed-p 'eglot)
  (package-install 'eglot))
(custom-set-variables
 '(eglot-connect-timeout nil))
(declare-function eglot-ensure "eglot")
(add-hook 'java-mode-hook
		  #'eglot-ensure)				; eclipse.jdt.ls


(defun my/add-to-classpath (item)
  "Add ITEM to CLASSPATH."
  
  (let ((class-path (getenv "CLASSPATH")))
    
    (defvar class-path-list (if class-path
				(split-string class-path ":")))
    (cl-pushnew item class-path-list)
    (setenv "CLASSPATH"
	    (mapconcat 'identity class-path-list ":"))))

(when (file-exists-p (expand-file-name "eclipse.jdt.ls" user-emacs-directory))
  (my/add-to-classpath
   (substring
    (shell-command-to-string
     (format
      "find %s -name 'org.eclipse.equinox.launcher_*jar'"
      (expand-file-name
       "eclipse.jdt.ls/org.eclipse.jdt.ls.product/target/repository/plugins"
       user-emacs-directory)))
    0 -1)))
--8<---------------cut here---------------end--------------->8---

Also I use ‘exec-path-from-shell’ package to setup PATHs in GUI Emacs.

~Pankaj



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Editing Java (with LSP)
  2022-06-16  3:29 ` Pankaj Jangid
@ 2022-06-16 13:34   ` Alessandro Bertulli
  2022-06-17  3:27     ` Pankaj Jangid
  0 siblings, 1 reply; 10+ messages in thread
From: Alessandro Bertulli @ 2022-06-16 13:34 UTC (permalink / raw)
  To: Pankaj Jangid; +Cc: help-gnu-emacs


Pankaj Jangid <pankaj@codeisgreat.org> writes:

> I use eglot+eclipse.jdt.ls. There is a bit of struggle in setting this
> up.

For me, last time I tried eglot, the problem was making it work with
dap-mode (which in turn iirc it's strictly coupled with lsp-mode). I
really need a debugger when developing and I don't feel quite at home
with GUD/RealGUD.

> My setup is like this (assuming you have cloned eclipse.jdt.ls
> repository inside ~/.emacs.d and built using ‘./mvnw clean package’),

Actually I simply set (setq lsp-java-jdt-download-url
"https://download.eclipse.org/jdtls/snapshots/jdt-language-server-latest.tar.gz")
and then I let lsp-mode download and build it. That's why I changed your
code

>      (format
>       "find %s -name 'org.eclipse.equinox.launcher_*jar'"
>       (expand-file-name
>        "eclipse.jdt.ls/org.eclipse.jdt.ls.product/target/repository/plugins"
>        user-emacs-directory))

to
(format
  "find %s -name 'org.eclipse.equinox.launcher_*jar'"
  (expand-file-name
    ".cache/lsp/eclipse.jdt.ls/plugins"
    user-emacs-directory))

However, both with your code and with mine, now when I try to open a
java file I get this error:
Error in post-command-hook (#[0 "\303\304\301\242\305#\210\300\306.!\205.\0r\211q\210
?\205.\0\307\310\311 \")\207" [#<buffer CLIApp.java> (#0)
eglot--managed-mode remove-hook post-command-hook nil buffer-live-p
apply eglot--connect eglot--guess-contact] 4]): (file-missing "Searching
for program" "No such file or directory" "jdtls")

> Also I use ‘exec-path-from-shell’ package to setup PATHs in GUI Emacs.
Maybe this is part of the reason?

Alessandro




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Editing Java (with LSP)
  2022-06-16 13:34   ` Alessandro Bertulli
@ 2022-06-17  3:27     ` Pankaj Jangid
  0 siblings, 0 replies; 10+ messages in thread
From: Pankaj Jangid @ 2022-06-17  3:27 UTC (permalink / raw)
  To: help-gnu-emacs

Alessandro Bertulli
<alessandro.bertulli96@gmail.com> writes:

> However, both with your code and with mine, now when I try to open a
> java file I get this error:
> Error in post-command-hook (#[0 "\303\304\301\242\305#\210\300\306.!\205.r\211q\210
> ?\205.\307\310\311 \")\207" [#<buffer CLIApp.java> (#0)
> eglot--managed-mode remove-hook post-command-hook nil buffer-live-p
> apply eglot--connect eglot--guess-contact] 4]): (file-missing "Searching
> for program" "No such file or directory" "jdtls")
>
>> Also I use ‘exec-path-from-shell’ package to setup PATHs in GUI Emacs.
> Maybe this is part of the reason?

Yes. I forgot that long back I copied

~/.emacs.d/eclipse.jdt.ls/org.eclipse.jdt.ls.product/target/repository/bin/jdtls

to my path somewhere. But that isn’t good; because then the jdtls binary is not
matching with your latest jdtls version. So this is what I did now besides the
previous CLASSPATH settings.

(add-to-list 'exec-path
			   (expand-file-name
				"eclipse.jdt.ls/org.eclipse.jdt.ls.product/target/repository/bin"
				user-emacs-directory))
                
This will fix the above error.




^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2022-06-17  3:27 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-13 13:43 Editing Java (with LSP) Alessandro Bertulli
2022-06-13 15:48 ` Filipp Gunbin
2022-06-13 21:53   ` Alessandro Bertulli
2022-06-14 19:12     ` Filipp Gunbin
2022-06-13 20:32 ` Milan Zamazal
2022-06-16  3:29 ` Pankaj Jangid
2022-06-16 13:34   ` Alessandro Bertulli
2022-06-17  3:27     ` Pankaj Jangid
  -- strict thread matches above, loose matches on Subject: below --
2022-06-13 21:35 Alessandro Bertulli
2022-06-14 19:13 ` Milan Zamazal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).