Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions eglot-java.el
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,23 @@ DESTINATION-DIR is the directory where the LSP server will be installed."

(defvar eglot-java-jdt-uri-handling-patch-applied nil "Whether or not JDT uri handling is already patched.")

(defun eglot-java--wrap-eglot--path-to-uri (original-fn &rest args)
"Advice to set up proper diagnostics in server-generated source files.
ARGS is a list with one element, a file path. If that path specifies a java
file inside a `.eglot-java' directory, recover its `jdt://' URI from
within the matching `.metadata' file. Otherwise, call ORIGINAL-FN."
(let* ((path (car args))
(directory-path (file-name-directory path)))
(if (not (and (string= "java" (file-name-extension path))
(string= ".eglot-java" (file-name-nondirectory (directory-file-name directory-path)))))
(apply original-fn args)
(let* ((metadata-file-path (format "%s.%s.metadata" directory-path (file-name-base path))))
(if (not (file-readable-p metadata-file-path))
(apply original-fn args)
(with-temp-buffer
(insert-file-contents metadata-file-path)
(buffer-string)))))))

(defun eglot-java--wrap-legacy-eglot--path-to-uri (original-fn &rest args)
"Hack until eglot is updated.
ARGS is a list with one element, a file path or potentially a URI.
Expand All @@ -1480,7 +1497,9 @@ handle it. If it is not a jar call ORIGINAL-FN."
(apply original-fn args))))

(defun eglot-java--jdthandler-patch-eglot ()
"Patch old versions of Eglot to work with Jdthandler."
"Patch Eglot path-uri conversion functions.
Patch old versions of Eglot to work with Jdthandler.
Patch all versions to work with files from `java/classFileContents'."
(interactive) ;; TODO Remove when eglot is updated in melpa
;; See also https://debbugs.gnu.org/cgi/bugreport.cgi?bug=58790
;; See also https://git.savannah.gnu.org/gitweb/?p=emacs.git;a=blob;f=lisp/progmodes/eglot.el#l1558
Expand All @@ -1489,7 +1508,9 @@ handle it. If it is not a jar call ORIGINAL-FN."
(<= 29 emacs-major-version))
(advice-add 'eglot--path-to-uri :around #'eglot-java--wrap-legacy-eglot--path-to-uri)
(advice-add 'eglot--uri-to-path :around #'eglot-java--wrap-legacy-eglot--uri-to-path)
(message "[eglot-java--jdthandler-patch-eglot] Eglot successfully patched.")))
(message "[eglot-java--jdthandler-patch-eglot] Eglot successfully patched."))
(unless (advice-member-p #'eglot-java--wrap-eglot--path-to-uri 'eglot-path-to-uri)
(advice-add 'eglot-path-to-uri :around #'eglot-java--wrap-eglot--path-to-uri)))

(defun eglot-java--find-server ()
"Find the LSP server of type eglot-java-eclipse-jdt for the
Expand Down