Subject: [PATCH] Fix invocation of Java Language Server
From: Philip Kaludercic <[email-masked]>
To: [email-masked]
Date: Sun, 24 Mar 2024 20:34:26 +0000
List-Id: <debbugs-submit.debbugs.gnu.org>

It appears that the Java Language Server "jdtls" unfortunately requires
a "-data" flag to be passed along with some directory to function
properly.  Otherwise it fails or only provides primitive syntax
checking, and no completion or other warnings.  This was discussed on
the Eglot issue tracker [0].

The following patch would invoke the server properly, passing
project-specific cache directory for the program to use.  I use
`xdg-cache-home' to determine what directory to use, but didn't want to
load it at the top of the file.  Are there any strong opinions on that
question?

From dae847f979084134effa256b95de7219c1d5dbf6 Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <[email-masked]>
Date: Sun, 24 Mar 2024 21:26:08 +0100
Subject: [PATCH] Fix invocation of Java Language Server

* lisp/progmodes/eglot.el (eglot-server-programs): Invoke
"jdtls" with a "-data" flag, passing a project-specific cache
directory.
---
 lisp/progmodes/eglot.el | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index f341428cac3..f2298b2e079 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -226,6 +226,7 @@ eglot-alternatives
                       when probe return (cons probe args)
                       finally (funcall err)))))))
 
+(declare-function xdg-cache-home "xdg" ())
 (defvar eglot-server-programs `(((rust-ts-mode rust-mode) . ("rust-analyzer"))
                                 ((cmake-mode cmake-ts-mode) . ("cmake-language-server"))
                                 (vimrc-mode . ("vim-language-server" "--stdio"))
@@ -264,7 +265,13 @@ eglot-server-programs
                                  . ("gopls"))
                                 ((R-mode ess-r-mode) . ("R" "--slave" "-e"
                                                         "languageserver::run()"))
-                                ((java-mode java-ts-mode) . ("jdtls"))
+                                ((java-mode java-ts-mode) .
+                                 ,(lambda (_interactive project) ;see github#1008
+                                    (require 'xdg)
+                                    (let ((cache (file-name-concat
+		                                  (xdg-cache-home) "jdtls-cache"
+		                                  (sha1 (project-root project)))))
+                                      (list  "jdtls" "-data" cache))))
                                 ((dart-mode dart-ts-mode)
                                  . ("dart" "language-server"
                                     "--client-id" "emacs.eglot-dart"))
-- 
2.39.2

Oh and this patch uses functions that are not available in Emacs 26.3,
so it would make sense to apply my patch that adds Compat support to
Eglot first.

[0] https://github.com/joaotavora/eglot/issues/1008

-- 
	Philip Kaludercic on icterid