diff --git a/README.md b/README.md index 902fcc5..3c91aa6 100644 --- a/README.md +++ b/README.md @@ -325,3 +325,8 @@ LSP slowness could be caused by slow JDT server, especially on large JAVA projec ;; current VSCode defaults (setq lsp-java-vmargs '("-XX:+UseParallelGC" "-XX:GCTimeRatio=4" "-XX:AdaptiveSizePolicyWeight=90" "-Dsun.zip.disableMemoryMapping=true" "-Xmx2G" "-Xms100m")) ``` + +* I have configured everything in VSCode and saved the code workspace there, can import it? + +Yes, the command `lsp-java-load-vscode-workspace` allows you to load a code-workspace file created in VScode. +It will also import the Java runtime and Debug launch configurations from the VSCode settings. diff --git a/lsp-java.el b/lsp-java.el index 5350b23..9b8ef5d 100644 --- a/lsp-java.el +++ b/lsp-java.el @@ -2182,6 +2182,79 @@ With prefix 2 show both." (user-error "No class under point.")) (setq lsp--buffer-workspaces workspaces))) +;;;###autoload +(defun lsp-java-load-vscode-workspace (file &optional prefix) + "Load a Java workspace from a VSCode workspace file. + +With prefix, delete the JDTLS workspace and cache dirs first. + +Any Java projects are added directly into the 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." + (interactive "fSelect file to import: \nP") + + (lsp-load-vscode-workspace file) + + (when prefix + (f-delete lsp-java-workspace-cache-dir t) + (f-delete lsp-java-workspace-dir t)) + + ;; lsp-load-vscode-workspace cleared the workspace folders, also clear the + ;; jdtls session folders + (puthash 'jdtls '() (lsp-session-server-id->folders (lsp-session))) + + (when-let* ((json (json-read-file file))) + (--> json + (alist-get 'settings it) + (alist-get 'java.configuration.runtimes it) + (if it (progn (setq lsp-java-configuration-runtimes (vector)) it) 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 'settings it) + (alist-get 'java.completion.filteredTypes it) + (if it (progn (setq lsp-java-completion-filtered-types (vector)) it) it) + (-each it (lambda (str) + (setq lsp-java-completion-filtered-types + (vconcat lsp-java-completion-filtered-types + `[,str]))))) + (--> 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))) + (natures (xml-get-children (car (xml-get-children (car xml) '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)))) + (provide 'lsp-java) ;;; lsp-java.el ends here