Skip to content

Commit 44a2831

Browse files
committed
fix diagnostics when visiting JDT provided files
Change the URI used to refer to a JDT provided file to the one that was used to generate it in the first place.
1 parent b42b519 commit 44a2831

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

eglot-java.el

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,23 @@ DESTINATION-DIR is the directory where the LSP server will be installed."
14591459

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

1462+
(defun eglot-java--wrap-eglot--path-to-uri (original-fn &rest args)
1463+
"Advice to set up proper diagnostics in server-generated source files.
1464+
ARGS is a list with one element, a file path. If that path specifies a java
1465+
file inside a `.eglot-java' directory, recover its `jdt://' URI from
1466+
within the matching `.metadata' file. Otherwise, call ORIGINAL-FN."
1467+
(let* ((path (car args))
1468+
(directory-path (file-name-directory path)))
1469+
(if (not (and (string= "java" (file-name-extension path))
1470+
(string= ".eglot-java" (file-name-nondirectory (directory-file-name directory-path)))))
1471+
(apply original-fn args)
1472+
(let* ((metadata-file-path (format "%s.%s.metadata" directory-path (file-name-base path))))
1473+
(if (not (file-readable-p metadata-file-path))
1474+
(apply original-fn args)
1475+
(with-temp-buffer
1476+
(insert-file-contents metadata-file-path)
1477+
(buffer-string)))))))
1478+
14621479
(defun eglot-java--wrap-legacy-eglot--path-to-uri (original-fn &rest args)
14631480
"Hack until eglot is updated.
14641481
ARGS is a list with one element, a file path or potentially a URI.
@@ -1480,7 +1497,9 @@ handle it. If it is not a jar call ORIGINAL-FN."
14801497
(apply original-fn args))))
14811498

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

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

0 commit comments

Comments
 (0)