Skip to content

Commit 094593d

Browse files
authored
Merge pull request #501 from timfel/patch-1
Extend loading of VSCode code-workspace files for Java workspaces
2 parents 14852c7 + 1e08bb6 commit 094593d

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

README.md

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

lsp-java.el

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,6 +1924,79 @@ With prefix 2 show both."
19241924
(user-error "No class under point."))
19251925
(setq lsp--buffer-workspaces workspaces)))
19261926

1927+
;;;###autoload
1928+
(defun lsp-java-load-vscode-workspace (file &optional prefix)
1929+
"Load a Java workspace from a VSCode workspace file.
1930+
1931+
With prefix, delete the JDTLS workspace and cache dirs first.
1932+
1933+
Any Java projects are added directly into the to the JDTLS session.
1934+
Because of the way JDTLS works, dependent projects would not be *open*
1935+
from the PoV of the JDTLS server otherwise and thus typechecking against
1936+
them and building multi-project workspaces would not work properly.
1937+
1938+
Additionally, this also takes a few configuration settings into account
1939+
to setup Java runtimes and debug templates if possible."
1940+
(interactive "fSelect file to import: \nP")
1941+
1942+
(lsp-load-vscode-workspace file)
1943+
1944+
(when prefix
1945+
(f-delete lsp-java-workspace-cache-dir t)
1946+
(f-delete lsp-java-workspace-dir t))
1947+
1948+
;; lsp-load-vscode-workspace cleared the workspace folders, also clear the
1949+
;; jdtls session folders
1950+
(puthash 'jdtls '() (lsp-session-server-id->folders (lsp-session)))
1951+
1952+
(when-let* ((json (json-read-file file)))
1953+
(--> json
1954+
(alist-get 'settings it)
1955+
(alist-get 'java.configuration.runtimes it)
1956+
(if it (progn (setq lsp-java-configuration-runtimes (vector)) it) it)
1957+
(-each it (-lambda ((&alist 'name 'path 'default))
1958+
(setq lsp-java-configuration-runtimes
1959+
(vconcat lsp-java-configuration-runtimes
1960+
`[(:name ,name :path ,path :default ,default)])))))
1961+
1962+
(--> json
1963+
(alist-get 'settings it)
1964+
(alist-get 'java.completion.filteredTypes it)
1965+
(if it (progn (setq lsp-java-completion-filtered-types (vector)) it) it)
1966+
(-each it (lambda (str)
1967+
(setq lsp-java-completion-filtered-types
1968+
(vconcat lsp-java-completion-filtered-types
1969+
`[,str])))))
1970+
(--> json
1971+
(alist-get 'launch it)
1972+
(alist-get 'configurations it)
1973+
(-each it (-lambda ((&alist 'type 'name 'projectName 'request 'hostName 'port))
1974+
(when (and name (string-equal type "java"))
1975+
(eval-after-load 'dap-java
1976+
(lambda ()
1977+
(dap-register-debug-template name
1978+
(list :type type
1979+
:request request
1980+
:projectName projectName
1981+
:hostName hostName
1982+
:port port)))))))))
1983+
1984+
(seq-do (lambda (folder)
1985+
(if-let* ((project-file (f-join folder ".project"))
1986+
(xml (condition-case nil
1987+
(with-temp-buffer
1988+
(insert-file-contents project-file)
1989+
(xml-parse-region (point-min) (point-max)))
1990+
(error nil)))
1991+
(natures (xml-get-children (car (xml-get-children (car xml) 'natures)) 'nature)))
1992+
(if (and (= 1 (seq-length natures))
1993+
(member "org.eclipse.jdt.core.javanature" (xml-node-children (car natures))))
1994+
(puthash 'jdtls
1995+
(append (gethash 'jdtls (lsp-session-server-id->folders (lsp-session)))
1996+
(list folder))
1997+
(lsp-session-server-id->folders (lsp-session))))))
1998+
(lsp-session-folders (lsp-session))))
1999+
19272000
(provide 'lsp-java)
19282001

19292002
;;; lsp-java.el ends here

0 commit comments

Comments
 (0)