Skip to content

Commit 73f89e5

Browse files
authored
Extend loading of VSCode code-workspace files for Java workspaces
I find myself often loading vscode workspaces with multiple Java projects that have dependencies between one another, and require the use of custom Java runtimes and debugging settings. Because of the way JDTLS works, the dependent project folders need to be "open" in the session at the same time, even though I don't usually have files from all projects open, so I add them into the jdtls hash. For the runtimes and debug settings, I just read from the code-workspace file's json config.
1 parent 0a1a041 commit 73f89e5

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

lsp-java.el

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,6 +2182,58 @@ With prefix 2 show both."
21822182
(user-error "No class under point."))
21832183
(setq lsp--buffer-workspaces workspaces)))
21842184

2185+
(defun lsp-java--load-vscode-workspace (file)
2186+
"Prepare workspace loaded from vscode workspace file
2187+
if Java projects and settings were configured. This adds all the folders
2188+
to the JDTLS session. Because of the way JDTLS works, dependent projects
2189+
would not be *open* from the PoV of the JDTLS server otherwise and thus
2190+
typechecking against them and building multi-project workspaces would
2191+
not work properly.
2192+
2193+
Additionally, this also takes a few configuration settings into account
2194+
to setup Java runtimes and debug templates if possible."
2195+
2196+
(when-let* ((json (json-read-file file)))
2197+
(--> json
2198+
(alist-get 'settings it)
2199+
(alist-get 'java.configuration.runtimes it)
2200+
(-each it (-lambda ((&alist 'name 'path 'default))
2201+
(setq lsp-java-configuration-runtimes
2202+
(vconcat lsp-java-configuration-runtimes
2203+
`[(:name ,name :path ,path :default ,default)])))))
2204+
(--> json
2205+
(alist-get 'launch it)
2206+
(alist-get 'configurations it)
2207+
(-each it (-lambda ((&alist 'type 'name 'projectName 'request 'hostName 'port))
2208+
(when (and name (string-equal type "java"))
2209+
(eval-after-load 'dap-java
2210+
(lambda ()
2211+
(dap-register-debug-template name
2212+
(list :type type
2213+
:request request
2214+
:projectName projectName
2215+
:hostName hostName
2216+
:port port)))))))))
2217+
2218+
(seq-do (lambda (folder)
2219+
(if-let* ((project-file (f-join folder ".project"))
2220+
(xml (condition-case nil
2221+
(with-temp-buffer
2222+
(insert-file-contents project-file)
2223+
(xml-parse-region (point-min) (point-max)))
2224+
(error nil)))
2225+
(project-description (xml-get-children (car xml) 'projectDescription))
2226+
(natures (xml-get-children (xml-get-children (car project-description) 'natures) 'nature)))
2227+
(if (and (= 1 (seq-length natures))
2228+
(member "org.eclipse.jdt.core.javanature" (xml-node-children (car natures))))
2229+
(puthash 'jdtls
2230+
(append (gethash 'jdtls (lsp-session-server-id->folders (lsp-session)))
2231+
(list folder))
2232+
(lsp-session-server-id->folders (lsp-session))))))
2233+
(lsp-session-folders (lsp-session))))
2234+
2235+
(advice-add #'lsp-load-vscode-workspace :after #'lsp-java--load-vscode-workspace)
2236+
21852237
(provide 'lsp-java)
21862238

21872239
;;; lsp-java.el ends here

0 commit comments

Comments
 (0)