about summary refs log tree commit diff
path: root/.emacs.d/lang/java.el
blob: 5d41a0c26043444e4b86968fb5ac12f744e4b908 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
;; Emacs Configuration for Java

; JDT URI fix for JDTLS from https://www.reddit.com/r/emacs/comments/1ibkh2h/comment/m9slw00/
(with-eval-after-load 'eglot
  (add-to-list 'eglot-server-programs
               `((java-mode java-ts-mode) .
                 ("jdtls"
                  :initializationOptions
                  (:extendedClientCapabilities (:classFileContentsSupport t))))))

(defun jdt-file-name-handler (operation &rest args)
  "Support Eclipse jdtls `jdt://' uri scheme."
  (let* ((uri (car args))
         (cache-dir "/tmp/.eglot")
         (source-file
          (expand-file-name
           (file-name-concat
            cache-dir
            (save-match-data
              (when (string-match "jdt://contents/\\(.*?\\)/\\(.*\\)\.class\\?" uri)
                (format "%s.java" (replace-regexp-in-string "/" "." (match-string 2 uri) t t))))))))
    (unless (file-readable-p source-file)
      (let ((content (jsonrpc-request (eglot-current-server) :java/classFileContents (list :uri uri)))
            (metadata-file (format "%s.%s.metadata"
                                   (file-name-directory source-file)
                                   (file-name-base source-file))))
        (unless (file-directory-p cache-dir) (make-directory cache-dir t))
        (with-temp-file source-file (insert content))
        (with-temp-file metadata-file (insert uri))))
    source-file))
(add-to-list 'file-name-handler-alist '("\\`jdt://" . jdt-file-name-handler))

(add-hook 'java-mode-hook #'eglot-ensure t)

(load-file
   (expand-file-name
    "pkg/consult-jdt.el"
    user-emacs-directory))