@@ -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