Skip to content

Commit 11e284d

Browse files
committed
lsp-java to work against lsp.el
1 parent fe39577 commit 11e284d

File tree

2 files changed

+156
-106
lines changed

2 files changed

+156
-106
lines changed

lsp-java-treemacs.el

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
(require 'lsp-mode)
3030
(require 'lsp-java)
3131
(require 'dash-functional)
32+
(require 'treemacs)
3233
(require 'treemacs-extensions)
3334

3435
(defface lsp-java-treemacs-directory-face
@@ -46,6 +47,15 @@
4647
"Face used by treemacs for files."
4748
:group 'lsp-java-treemacs)
4849

50+
(defun lsp-java-treemacs--is-root (dir-or-project)
51+
"Return whether DIR-OR-PROJECT is root of a project."
52+
(let ((dir (if (stringp dir-or-project)
53+
dir-or-project
54+
(treemacs-project->path dir-or-project))))
55+
56+
(when-lsp-workspace (lsp-java--find-workspace dir)
57+
(-contains? (lsp-java--get-project-uris lsp--cur-workspace)
58+
(lsp--path-to-uri dir)))))
4959

5060
(defun lsp-java-treemacs--get-libraries (project-uri)
5161
"Get the list of buffers, grouped by their major mode.
@@ -101,26 +111,26 @@ Insert VAR into `treemacs-icon-hash' for each of the given file EXTENSIONS."
101111
,var)))
102112

103113
(if (treemacs--is-image-creation-impossible?)
104-
(progn
105-
(lsp-java-treemacs--setup-icon lsp-java-treemacs-jar "icons/vscode/file_type_jar.png")
106-
(lsp-java-treemacs--setup-icon lsp-java-treemacs-jar-opened "icons/vscode/file_type_jar.png")
107-
(lsp-java-treemacs--setup-icon lsp-java-treemacs-package "icons/vscode/folder_type_package.png")
108-
(lsp-java-treemacs--setup-icon lsp-java-treemacs-package-opened "icons/vscode/folder_type_package_opened.png")
109-
(lsp-java-treemacs--setup-icon lsp-java-treemacs-jar-folder "icons/vscode/folder_type_component.png")
110-
(lsp-java-treemacs--setup-icon lsp-java-treemacs-jar-folder-opened "icons/vscode/folder_type_component_opened.png")
111-
(lsp-java-treemacs--setup-icon lsp-java-treemacs-library-folder "icons/vscode/folder_type_library.png")
112-
(lsp-java-treemacs--setup-icon lsp-java-treemacs-library-folder-opened "icons/vscode/folder_type_library_opened.png")
113-
(lsp-java-treemacs--setup-icon lsp-java-treemacs-class "icons/vscode/file_type_class.png" "class"))
114-
(treemacs--set-icon-save-default
115-
lsp-java-treemacs-jar treemacs-icon-closed-text
116-
lsp-java-treemacs-jar-opened treemacs-icon-open-text
117-
lsp-java-treemacs-package treemacs-icon-closed-text
118-
lsp-java-treemacs-package-opened treemacs-icon-open-text
119-
lsp-java-treemacs-jar-folder treemacs-icon-closed-text
120-
lsp-java-treemacs-jar-folder-opened treemacs-icon-open-text
121-
lsp-java-treemacs-library-folder treemacs-icon-closed-text
122-
lsp-java-treemacs-library-folder-opened treemacs-icon-open-text
123-
lsp-java-treemacs-class treemacs-icon-tag-leaf-text))
114+
(treemacs--set-icon-save-default
115+
lsp-java-treemacs-jar treemacs-icon-closed-text
116+
lsp-java-treemacs-jar-opened treemacs-icon-open-text
117+
lsp-java-treemacs-package treemacs-icon-closed-text
118+
lsp-java-treemacs-package-opened treemacs-icon-open-text
119+
lsp-java-treemacs-jar-folder treemacs-icon-closed-text
120+
lsp-java-treemacs-jar-folder-opened treemacs-icon-open-text
121+
lsp-java-treemacs-library-folder treemacs-icon-closed-text
122+
lsp-java-treemacs-library-folder-opened treemacs-icon-open-text
123+
lsp-java-treemacs-class treemacs-icon-tag-leaf-text)
124+
(progn
125+
(lsp-java-treemacs--setup-icon lsp-java-treemacs-jar "icons/vscode/file_type_jar.png")
126+
(lsp-java-treemacs--setup-icon lsp-java-treemacs-jar-opened "icons/vscode/file_type_jar.png")
127+
(lsp-java-treemacs--setup-icon lsp-java-treemacs-package "icons/vscode/folder_type_package.png")
128+
(lsp-java-treemacs--setup-icon lsp-java-treemacs-package-opened "icons/vscode/folder_type_package_opened.png")
129+
(lsp-java-treemacs--setup-icon lsp-java-treemacs-jar-folder "icons/vscode/folder_type_component.png")
130+
(lsp-java-treemacs--setup-icon lsp-java-treemacs-jar-folder-opened "icons/vscode/folder_type_component_opened.png")
131+
(lsp-java-treemacs--setup-icon lsp-java-treemacs-library-folder "icons/vscode/folder_type_library.png")
132+
(lsp-java-treemacs--setup-icon lsp-java-treemacs-library-folder-opened "icons/vscode/folder_type_library_opened.png")
133+
(lsp-java-treemacs--setup-icon lsp-java-treemacs-class "icons/vscode/file_type_class.png" "class")))
124134

125135
(defmacro treemacs--lsp-node-or-folder ()
126136
"Extract common code from nodes."
@@ -215,27 +225,33 @@ ADDED and REMOVED are pointing which are the changed folders."
215225
(-first (lambda (project) (f-equal? it (treemacs-project->path project))))
216226
treemacs-do-remove-project-from-workspace)))
217227

228+
(defun lsp-java-treemacs--get-projects ()
229+
"Get projects."
230+
(or (when (functionp 'lsp-session)
231+
(lsp-session-folders (lsp-session)))
232+
(hash-table-keys lsp--workspaces)))
233+
218234
(defun lsp-java-treemacs-register ()
219235
"Register `lsp-java' extension."
220236
(interactive)
221237
(treemacs-define-directory-extension
222238
:extension 'treemacs-EXTERNAL-LIBRARY-extension
223-
:position 'directory-start
224-
:predicate 'lsp-java--is-root)
239+
:position 'top
240+
:predicate 'lsp-java-treemacs--is-root)
225241
(treemacs-define-project-extension
226242
:extension 'treemacs-EXTERNAL-LIBRARY-extension
227-
:position 'project-start
228-
:predicate 'lsp-java--is-root)
243+
:position 'top
244+
:predicate 'lsp-java-treemacs--is-root)
229245

230246
(require 'treemacs)
231247
(unless (eq 'visible (treemacs-current-visibility))
232248
(treemacs))
233249

234-
(maphash (lambda (root-path _workspace)
235-
(unless (or (s-equals? (f-canonical root-path) (f-canonical lsp-java-workspace-dir))
236-
(s-equals? (f-canonical root-path) (f-canonical lsp-java-workspace-cache-dir)))
237-
(treemacs-do-add-project-to-workspace root-path (f-filename root-path))))
238-
lsp--workspaces)
250+
(mapc (lambda (root-path)
251+
(unless (or (s-equals? (f-canonical root-path) (f-canonical lsp-java-workspace-dir))
252+
(s-equals? (f-canonical root-path) (f-canonical lsp-java-workspace-cache-dir)))
253+
(treemacs-do-add-project-to-workspace root-path (f-filename root-path))))
254+
(lsp-java-treemacs--get-projects))
239255
(add-hook 'lsp-workspace-folders-change 'lsp-java-treemacs--folders-change))
240256

241257
(defun lsp-java-treemacs-unregister ()

lsp-java.el

Lines changed: 111 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,13 @@ The entry point of the language server is in `lsp-java-server-install-dir'/plugi
316316
"java.edit.organizeImports"
317317
(list (lsp--path-to-uri buffer-file-name))))
318318

319+
(defun lsp-java--current-workspace-or-lose ()
320+
"Look for the jdt-ls workspace."
321+
(or lsp--cur-workspace
322+
(when (boundp 'lsp--workspaces) (gethash (lsp-java--get-root) lsp--workspaces))
323+
(when (functionp 'lsp-find-workspace) (lsp-find-workspace 'jdtls (buffer-file-name)))
324+
(error "Unable to find workspace")))
325+
319326
(defun lsp-java-build-project (&optional full)
320327
"Perform project build action.
321328
@@ -328,14 +335,12 @@ FULL specify whether full or incremental build will be performed."
328335
"Update project configuration."
329336
(interactive)
330337
(let ((file-name (file-name-nondirectory (buffer-file-name))))
331-
(if-let ((lsp--cur-workspace (or lsp--cur-workspace
332-
(gethash (lsp-java--get-root) lsp--workspaces))))
333-
(if (or (string= file-name "pom.xml") (string-match "\\.gradle\\'" file-name))
334-
(lsp-send-notification
335-
(lsp-make-request "java/projectConfigurationUpdate"
336-
(list :uri (lsp--buffer-uri))))
337-
(error "Update configuration could be called only from build file(pom.xml or gradle build file)"))
338-
(error "Unable to find workspace"))))
338+
(if (or (string= file-name "pom.xml") (string-match "\\.gradle\\'" file-name))
339+
(with-lsp-workspace (lsp-java--current-workspace-or-lose)
340+
(lsp-send-notification
341+
(lsp-make-request "java/projectConfigurationUpdate"
342+
(list :uri (lsp--buffer-uri)))))
343+
(error "Update configuration could be called only from build file(pom.xml or gradle build file)"))))
339344

340345
(defun lsp-java--ensure-dir (path)
341346
"Ensure that directory PATH exists."
@@ -375,15 +380,12 @@ FULL specify whether full or incremental build will be performed."
375380
,lsp-java-workspace-dir
376381
,@java-9-args)))
377382

378-
(defun lsp-java--is-root (dir-or-project)
379-
"Return whether DIR-OR-PROJECT is root of a project."
380-
(let ((dir (if (stringp dir-or-project)
381-
dir-or-project
382-
(treemacs-project->path dir-or-project))))
383-
(-some-> dir
384-
lsp-java--find-workspace
385-
lsp-java--get-project-uris
386-
(-contains? (lsp--path-to-uri dir)))))
383+
(defun lsp-java--is-root (dir)
384+
"Return whether DIR is root of a project."
385+
(-some-> dir
386+
lsp-java--find-workspace
387+
lsp-java--get-project-uris
388+
(-contains? (lsp--path-to-uri dir))))
387389

388390
(defun lsp-java--get-root ()
389391
"Retrieves the root directory of the java project root if available.
@@ -552,11 +554,11 @@ PARAMS progress report notification data."
552554

553555
(defun lsp-java--folders-change (&rest _)
554556
"Handler for folder's change."
555-
(lsp-java-update-project-uris lsp--cur-workspace))
557+
(lsp-java-update-project-uris))
556558

557559
(defun lsp-java--workspace-notify (&rest _args)
558560
"Workspace notify handler."
559-
(lsp-java-update-project-uris lsp--cur-workspace))
561+
(lsp-java-update-project-uris))
560562

561563
(defun lsp-java--client-initialized (client)
562564
"Callback for CLIENT initialized."
@@ -728,21 +730,26 @@ server."
728730
(lsp-java-update-user-settings)
729731
(puthash "initialized" t (lsp--workspace-metadata lsp--cur-workspace)))
730732

731-
(lsp-java-update-project-uris lsp--cur-workspace))
733+
(lsp-java-update-project-uris))
734+
735+
(defun lsp-java--workspace-folders (workspace)
736+
"Return WORKSPACE folders."
737+
(or (when (functionp 'lsp-session)
738+
(lsp-session-folders (lsp-session)))
739+
(lsp--workspace-workspace-folders workspace)))
732740

733-
(defun lsp-java-update-project-uris (&optional workspace)
741+
(defun lsp-java-update-project-uris ()
734742
"Update WORKSPACE project uris."
735743
(interactive)
736-
(setq workspace (or workspace lsp--cur-workspace))
737-
(with-lsp-workspace workspace
738-
(->> workspace
739-
lsp--workspace-workspace-folders
740-
(--map (or (lsp-send-execute-command "che.jdt.ls.extension.mavenProjects"
741-
(lsp--path-to-uri it))
742-
(lsp--path-to-uri (file-name-as-directory it))))
743-
-flatten
744-
-uniq
745-
(lsp-java--set-project-uris workspace))))
744+
(let ((workspace (lsp-java--current-workspace-or-lose)))
745+
(with-lsp-workspace workspace
746+
(->> (lsp-java--workspace-folders workspace)
747+
(--map (or (lsp-send-execute-command "che.jdt.ls.extension.mavenProjects"
748+
(lsp--path-to-uri it))
749+
(lsp--path-to-uri (file-name-as-directory it))))
750+
-flatten
751+
-uniq
752+
(lsp-java--set-project-uris workspace)))))
746753

747754
(defun lsp-java--set-project-uris (workspace project-uris)
748755
"Set WORKSPACE project uri list to PROJECT-URIS."
@@ -757,28 +764,28 @@ server."
757764
;; TODO investigate a better way to do that.
758765
(if (eq current-workspace-folders (gethash "last-workspace-folders" workspace-metadata))
759766
(gethash "project-uris" workspace-metadata)
760-
(lsp-java-update-project-uris workspace)
767+
(lsp-java-update-project-uris)
761768
(puthash "last-workspace-folders" current-workspace-folders workspace-metadata))))
762769

763770
(defun lsp-java--find-workspace (file-uri)
764771
"Return the workspace corresponding FILE-URI."
765-
(->> lsp--workspaces
766-
ht-values
767-
-uniq
768-
(--first (-some? (lambda (project-uri)
769-
(s-starts-with? (lsp--uri-to-path project-uri)
770-
(lsp--uri-to-path file-uri)))
771-
(lsp-java--get-project-uris it)))))
772+
(or (when (boundp 'lsp--workspaces)
773+
(->> lsp--workspaces
774+
ht-values
775+
-uniq
776+
(--first (-some? (lambda (project-uri)
777+
(s-starts-with? (lsp--uri-to-path project-uri)
778+
(lsp--uri-to-path file-uri)))
779+
(lsp-java--get-project-uris it)))))
780+
(when (functionp 'lsp-find-workspace)
781+
(lsp-find-workspace 'jdtls (lsp--uri-to-path file-uri)))))
772782

773783
(defun lsp-java--find-project-uri (file-uri)
774784
"Return the java project corresponding FILE-URI."
775-
(->> lsp--workspaces
776-
ht-values
777-
-uniq
778-
(-map 'lsp-java--get-project-uris)
779-
-flatten
780-
(--filter (s-starts-with? (lsp--uri-to-path it)
781-
(lsp--uri-to-path file-uri)))
785+
(->> (lsp--uri-to-path file-uri)
786+
(lsp-find-workspace 'jdtls)
787+
lsp-java--get-project-uris
788+
(--filter (s-starts-with? (lsp--uri-to-path it) (lsp--uri-to-path file-uri)))
782789
(-max-by (lambda (project-a project-b)
783790
(> (length project-a)
784791
(length project-b))))))
@@ -934,37 +941,64 @@ PROJECT-URI uri of the item."
934941
(defun lsp-java-classpath-browse ()
935942
"Show currently active sessions."
936943
(interactive)
937-
(let ((uri (lsp--path-to-uri (or buffer-file-name (f-canonical default-directory)))))
938-
(--if-let (or lsp--cur-workspace (lsp-java--find-workspace uri))
939-
(with-lsp-workspace it
940-
(if-let (project-uri (lsp-java--find-project-uri buffer-file-name))
941-
(let ((inhibit-read-only t)
942-
(buf (get-buffer-create "*classpath*")))
943-
(with-current-buffer buf
944-
(erase-buffer)
945-
(lsp-java-classpath-mode)
946-
(setq-local lsp--cur-workspace it)
947-
948-
(when lsp-java-themes-directory
949-
(setq-local tree-widget-themes-directory lsp-java-themes-directory))
950-
951-
(when lsp-java-theme
952-
(tree-widget-set-theme lsp-java-theme))
953-
954-
(widget-create
955-
`(tree-widget
956-
:node (push-button :format "%[%t%]\n"
957-
:tag ,(f-filename (lsp--uri-to-path project-uri)))
958-
:open t
959-
:file ,(lsp--uri-to-path project-uri)
960-
,@lsp-java-icons-default-root-folder
961-
,@(--map (lsp-java--classpath-render-classpath it project-uri)
962-
(lsp-send-execute-command "che.jdt.ls.extension.classpathTree" project-uri))))
963-
(run-hooks 'lsp-java-classpath-mode-hook))
964-
(funcall lsp-java-pop-buffer-function buf)
965-
(goto-char (point-min)))
966-
(user-error "Failed to calculate project for buffer %s" (buffer-name))))
967-
(user-error "Unable to find workspace for buffer %s" (buffer-name)))))
944+
(let ((workspace (lsp-java--current-workspace-or-lose)))
945+
(with-lsp-workspace workspace
946+
(if-let (project-uri (lsp-java--find-project-uri buffer-file-name))
947+
(let ((inhibit-read-only t)
948+
(buf (get-buffer-create "*classpath*")))
949+
(with-current-buffer buf
950+
(erase-buffer)
951+
(lsp-java-classpath-mode)
952+
(setq-local lsp--cur-workspace workspace)
953+
(when lsp-java-themes-directory
954+
(setq-local tree-widget-themes-directory lsp-java-themes-directory))
955+
956+
(when lsp-java-theme
957+
(tree-widget-set-theme lsp-java-theme))
958+
959+
(widget-create
960+
`(tree-widget
961+
:node (push-button :format "%[%t%]\n"
962+
:tag ,(f-filename (lsp--uri-to-path project-uri)))
963+
:open t
964+
:file ,(lsp--uri-to-path project-uri)
965+
,@lsp-java-icons-default-root-folder
966+
,@(--map (lsp-java--classpath-render-classpath it project-uri)
967+
(lsp-send-execute-command "che.jdt.ls.extension.classpathTree" project-uri))))
968+
(run-hooks 'lsp-java-classpath-mode-hook))
969+
(funcall lsp-java-pop-buffer-function buf)
970+
(goto-char (point-min)))
971+
(user-error "Failed to calculate project for buffer %s" (buffer-name))))))
972+
973+
(eval-after-load 'lsp
974+
'(lsp-register-client
975+
(make-lsp--client
976+
:new-connection (lsp-stdio-connection 'lsp-java--ls-command)
977+
:major-modes '(java-mode)
978+
:server-id 'jdtls
979+
:multi-root t
980+
:notification-handlers (lsp-ht ("language/status" 'lsp-java--language-status-callback)
981+
("language/actionableNotification" 'lsp-java--actionable-notification-callback)
982+
("language/progressReport" 'lsp-java--progress-report)
983+
("workspace/notify" 'lsp-java--workspace-notify))
984+
:action-handlers (lsp-ht ("java.apply.workspaceEdit" 'lsp-java--apply-workspace-edit))
985+
:uri-handlers (lsp-ht ("jdt" 'lsp-java--resolve-uri)
986+
("chelib" 'lsp-java--resolve-uri))
987+
:initialization-options (lambda ()
988+
(list :settings (lsp-java--settings)
989+
:extendedClientCapabilities (list :progressReportProvider t
990+
:classFileContentsSupport t)
991+
:bundles (lsp-java--bundles)))
992+
:library-folders-fn (lambda (_workspace) (list lsp-java-workspace-cache-dir))
993+
:before-file-open-fn (lambda (workspace)
994+
(let ((metadata-file-name (lsp-java--get-metadata-location buffer-file-name)))
995+
(setq-local lsp-buffer-uri
996+
(when (file-exists-p metadata-file-name)
997+
(with-temp-buffer (insert-file-contents metadata-file-name)
998+
(buffer-string))))))
999+
:initialized-fn (lambda (workspace)
1000+
(with-lsp-workspace workspace
1001+
(lsp-java-update-project-uris))))))
9681002

9691003
(provide 'lsp-java)
9701004
;;; lsp-java.el ends here

0 commit comments

Comments
 (0)