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
52 changes: 52 additions & 0 deletions lsp-java.el
Original file line number Diff line number Diff line change
Expand Up @@ -2182,6 +2182,58 @@ With prefix 2 show both."
(user-error "No class under point."))
(setq lsp--buffer-workspaces workspaces)))

(defun lsp-java--load-vscode-workspace (file)
"Prepare workspace loaded from vscode workspace file
if Java projects and settings were configured. This adds all the folders
to the JDTLS session. Because of the way JDTLS works, dependent projects
would not be *open* from the PoV of the JDTLS server otherwise and thus
typechecking against them and building multi-project workspaces would
not work properly.

Additionally, this also takes a few configuration settings into account
to setup Java runtimes and debug templates if possible."

(when-let* ((json (json-read-file file)))
(--> json
(alist-get 'settings it)
(alist-get 'java.configuration.runtimes it)
(-each it (-lambda ((&alist 'name 'path 'default))
(setq lsp-java-configuration-runtimes
(vconcat lsp-java-configuration-runtimes
`[(:name ,name :path ,path :default ,default)])))))
(--> json
(alist-get 'launch it)
(alist-get 'configurations it)
(-each it (-lambda ((&alist 'type 'name 'projectName 'request 'hostName 'port))
(when (and name (string-equal type "java"))
(eval-after-load 'dap-java
(lambda ()
(dap-register-debug-template name
(list :type type
:request request
:projectName projectName
:hostName hostName
:port port)))))))))

(seq-do (lambda (folder)
(if-let* ((project-file (f-join folder ".project"))
(xml (condition-case nil
(with-temp-buffer
(insert-file-contents project-file)
(xml-parse-region (point-min) (point-max)))
(error nil)))
(project-description (xml-get-children (car xml) 'projectDescription))
(natures (xml-get-children (xml-get-children (car project-description) 'natures) 'nature)))
(if (and (= 1 (seq-length natures))
(member "org.eclipse.jdt.core.javanature" (xml-node-children (car natures))))
(puthash 'jdtls
(append (gethash 'jdtls (lsp-session-server-id->folders (lsp-session)))
(list folder))
(lsp-session-server-id->folders (lsp-session))))))
(lsp-session-folders (lsp-session))))

(advice-add #'lsp-load-vscode-workspace :after #'lsp-java--load-vscode-workspace)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit concerned that using advice-add might be too aggressive. 🤔 It could potentially introduce side effects for other LSP clients.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, true. alternatively we just make it a new lsp-java-load-vscode-workspace function that calls the lsp-load-vscode-workspace itself, and then I just add that to the documentation?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think that's probably the best approach for now. 🤔


(provide 'lsp-java)

;;; lsp-java.el ends here
Expand Down