Skip to content

Commit 1e08bb6

Browse files
authored
Make lsp-java-load-vscode-workspace it's own command
1 parent 73f89e5 commit 1e08bb6

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,8 @@ LSP slowness could be caused by slow JDT server, especially on large JAVA projec
325325
;; current VSCode defaults
326326
(setq lsp-java-vmargs '("-XX:+UseParallelGC" "-XX:GCTimeRatio=4" "-XX:AdaptiveSizePolicyWeight=90" "-Dsun.zip.disableMemoryMapping=true" "-Xmx2G" "-Xms100m"))
327327
```
328+
329+
* I have configured everything in VSCode and saved the code workspace there, can import it?
330+
331+
Yes, the command `lsp-java-load-vscode-workspace` allows you to load a code-workspace file created in VScode.
332+
It will also import the Java runtime and Debug launch configurations from the VSCode settings.

lsp-java.el

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,25 +2182,49 @@ 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.
2185+
;;;###autoload
2186+
(defun lsp-java-load-vscode-workspace (file &optional prefix)
2187+
"Load a Java workspace from a VSCode workspace file.
2188+
2189+
With prefix, delete the JDTLS workspace and cache dirs first.
2190+
2191+
Any Java projects are added directly into the to the JDTLS session.
2192+
Because of the way JDTLS works, dependent projects would not be *open*
2193+
from the PoV of the JDTLS server otherwise and thus typechecking against
2194+
them and building multi-project workspaces would not work properly.
21922195
21932196
Additionally, this also takes a few configuration settings into account
21942197
to setup Java runtimes and debug templates if possible."
2198+
(interactive "fSelect file to import: \nP")
2199+
2200+
(lsp-load-vscode-workspace file)
2201+
2202+
(when prefix
2203+
(f-delete lsp-java-workspace-cache-dir t)
2204+
(f-delete lsp-java-workspace-dir t))
2205+
2206+
;; lsp-load-vscode-workspace cleared the workspace folders, also clear the
2207+
;; jdtls session folders
2208+
(puthash 'jdtls '() (lsp-session-server-id->folders (lsp-session)))
21952209

21962210
(when-let* ((json (json-read-file file)))
21972211
(--> json
21982212
(alist-get 'settings it)
21992213
(alist-get 'java.configuration.runtimes it)
2214+
(if it (progn (setq lsp-java-configuration-runtimes (vector)) it) it)
22002215
(-each it (-lambda ((&alist 'name 'path 'default))
22012216
(setq lsp-java-configuration-runtimes
22022217
(vconcat lsp-java-configuration-runtimes
22032218
`[(:name ,name :path ,path :default ,default)])))))
2219+
2220+
(--> json
2221+
(alist-get 'settings it)
2222+
(alist-get 'java.completion.filteredTypes it)
2223+
(if it (progn (setq lsp-java-completion-filtered-types (vector)) it) it)
2224+
(-each it (lambda (str)
2225+
(setq lsp-java-completion-filtered-types
2226+
(vconcat lsp-java-completion-filtered-types
2227+
`[,str])))))
22042228
(--> json
22052229
(alist-get 'launch it)
22062230
(alist-get 'configurations it)
@@ -2222,8 +2246,7 @@ to setup Java runtimes and debug templates if possible."
22222246
(insert-file-contents project-file)
22232247
(xml-parse-region (point-min) (point-max)))
22242248
(error nil)))
2225-
(project-description (xml-get-children (car xml) 'projectDescription))
2226-
(natures (xml-get-children (xml-get-children (car project-description) 'natures) 'nature)))
2249+
(natures (xml-get-children (car (xml-get-children (car xml) 'natures)) 'nature)))
22272250
(if (and (= 1 (seq-length natures))
22282251
(member "org.eclipse.jdt.core.javanature" (xml-node-children (car natures))))
22292252
(puthash 'jdtls
@@ -2232,8 +2255,6 @@ to setup Java runtimes and debug templates if possible."
22322255
(lsp-session-server-id->folders (lsp-session))))))
22332256
(lsp-session-folders (lsp-session))))
22342257

2235-
(advice-add #'lsp-load-vscode-workspace :after #'lsp-java--load-vscode-workspace)
2236-
22372258
(provide 'lsp-java)
22382259

22392260
;;; lsp-java.el ends here

0 commit comments

Comments
 (0)