emacs-lsp / lsp-java

lsp-mode :heart: java
https://emacs-lsp.github.io/lsp-java
GNU General Public License v3.0
641 stars 89 forks source link

Unable make Lsp / Lsp-Java work with JDT #370

Open asmodeus812 opened 2 years ago

asmodeus812 commented 2 years ago

I am facing an issue with lsp and lsp-java in particular, being unable to properly list and suggest symbols for a given type. A few issues i noticed When i start typing the name of a local variable nothing happens, no suggestions are available, when i try to type a function which i know belongs to a given type, on a local variable of that type, same problem. The Same issue with enums and so on, basically everything is crippled and the server reports errors. For example when i type down a local variable (of type List) and expect to get suggestions for the available methods of that type I am receiving the following error - error "Cannot find types for \"MANDATORY_ENVIRONMENT_TYPES\"". I am quite new in the lsp world, i was not able to find a solution to the problem browsing through the issues here.

`(use-package yasnippet :ensure t :pin melpa :config (yas-global-mode))

(use-package which-key :ensure t :pin melpa :config (which-key-mode))

(use-package flycheck :ensure t :pin melpa :init (global-flycheck-mode))

(use-package company :ensure t :pin melpa :config (add-hook 'after-init-hook 'global-company-mode))

(use-package lsp-mode :ensure t :pin melpa :hook ((lsp-mode . lsp-enable-which-key-integration)) :config (setq lsp-completion-enable-additional-text-edit nil))

(use-package lsp-java :ensure t :pin melpa)

(use-package lsp-ui :ensure t :pin melpa)

(use-package lsp-treemacs :ensure t :pin melpa)

(use-package helm-lsp :ensure t :after (lsp-mode) :commands (helm-lsp-workspace-symbol) :init (define-key lsp-mode-map [remap xref-find-apropos] #'helm-lsp-workspace-symbol))

(require 'lsp) (require 'lsp-mode)

(add-hook 'c-mode-hook 'lsp) (add-hook 'c++-mode-hook 'lsp) (add-hook 'java-mode-hook 'lsp)

(add-hook 'company-completion-started-hook (lambda (&rest _) (setq-local lsp-inhibit-lsp-hooks t) (lsp--capf-clear-cache)) nil t)

(setq gc-cons-threshold ( 100 1024 1024) read-process-output-max ( 1024 1024) treemacs-space-between-root-nodes nil company-selection-wrap-around t company-minimum-prefix-length 1 company-idle-delay 0.0 lsp-prefer-capf t lsp-use-lsp-ui t lsp-idle-delay 0.1 lsp-lens-enable t lsp-headerline-breadcrumb-enable t lsp-ui-doc-delay 0.1 lsp-ui-doc-show-with-mouse t lsp-ui-doc-show-with-cursor nil lsp-ui-sideline-enable t lsp-java-format-enabled t lsp-java-format-comments-enabled t lsp-java-save-action-organize-imports t lsp-java-save-action-organize-imports t lsp-java-import-gradle-enabled t lsp-java-import-maven-enabled t lsp-java-auto-build t lsp-print-io t lsp-java-vmargs '("-XX:+UseParallelGC" "-XX:GCTimeRatio=4" "-XX:AdaptiveSizePolicyWeight=90" "-Xmx2048m" "-Xms256m"))`

image

image

image

`Nov 26, 2021, 7:04:13 PM >> document/didChange Nov 26, 2021, 7:04:13 PM BadLocationException null org.eclipse.jface.text.BadLocationException

at org.eclipse.jface.text.TreeLineTracker.fail(TreeLineTracker.java:1054)

at org.eclipse.jface.text.TreeLineTracker.lineByOffset(TreeLineTracker.java:268)

at org.eclipse.jface.text.TreeLineTracker.getLineNumberOfOffset(TreeLineTracker.java:1122)

at org.eclipse.jface.text.AbstractLineTracker.getLineNumberOfOffset(AbstractLineTracker.java:155)

at org.eclipse.jface.text.AbstractDocument.getLineOfOffset(AbstractDocument.java:872)

at org.eclipse.core.internal.filebuffers.SynchronizableDocument.getLineOfOffset(SynchronizableDocument.java:334)

at org.eclipse.jdt.ls.core.internal.handlers.JsonRpcHelpers.toLine(JsonRpcHelpers.java:156)

at org.eclipse.jdt.ls.core.internal.handlers.JsonRpcHelpers.toLine(JsonRpcHelpers.java:101)

at org.eclipse.jdt.ls.core.internal.JDTUtils.toRange(JDTUtils.java:857)

at org.eclipse.jdt.ls.core.internal.JDTUtils.toLocation(JDTUtils.java:756)

at org.eclipse.jdt.ls.core.internal.JDTUtils.toLocation(JDTUtils.java:702)

at org.eclipse.jdt.ls.core.internal.handlers.DocumentSymbolHandler.getRange(DocumentSymbolHandler.java:205)

at org.eclipse.jdt.ls.core.internal.handlers.DocumentSymbolHandler.toDocumentSymbol(DocumentSymbolHandler.java:172)

at org.eclipse.jdt.ls.core.internal.handlers.DocumentSymbolHandler.lambda$2(DocumentSymbolHandler.java:147)

at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)

at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)

at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)

at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)

at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)

at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)

at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)

at org.eclipse.jdt.ls.core.internal.handlers.DocumentSymbolHandler.getHierarchicalOutline(DocumentSymbolHandler.java:147)

at org.eclipse.jdt.ls.core.internal.handlers.DocumentSymbolHandler.documentSymbol(DocumentSymbolHandler.java:80)

at org.eclipse.jdt.ls.core.internal.handlers.JDTLanguageServer.lambda$14(JDTLanguageServer.java:636)

at org.eclipse.jdt.ls.core.internal.BaseJDTLanguageServer.lambda$0(BaseJDTLanguageServer.java:75)

at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:642)

at java.base/java.util.concurrent.CompletableFuture$Completion.exec(CompletableFuture.java:479)

at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)

at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)

at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)

at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)

at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183)

`

yyoncho commented 2 years ago

I have similar issue on windows when using ^M line endings. Can you do the following: M-: (set-buffer-file-coding-system 'undecided-unix nil), save the buffer, close the buffer and then open it again.

asmodeus812 commented 2 years ago

I have similar issue on windows when using ^M line endings. Can you do the following: M-: (set-buffer-file-coding-system 'undecided-unix nil), save the buffer, close the buffer and then open it again.

Hi Just Tried that but nothing really changed. Also I should have noted that I am opening already exisisting IntelliJ based project files which have by default LF encoding and .projectfile, pom and .classpath files in the root directory. In the messages buffer i can now see that the server is throwing an internal error. Updated my OP

yyoncho commented 2 years ago

That error matches my guess. Can you do C-s C-q C-m RET? Can you test with a new file? Can you test with another project (like this one https://github.com/emacs-lsp/lsp-mode/tree/master/test/fixtures/org-mode/java-project, make sure to pick the correct project root).

asmodeus812 commented 2 years ago

That error matches my guess. Can you do C-s C-q C-m RET? Can you test with a new file? Can you test with another project (like this one https://github.com/emacs-lsp/lsp-mode/tree/master/test/fixtures/org-mode/java-project, make sure to pick the correct project root).

I tried to download the test project, and mimic the problem, same issue seems to be present. image

PS. The way I load the project is by loading the in emacs pom after which i open a file from the project in a buffer, the minibuffer log shows that the server has started successfully. And The workspace is created.

yyoncho commented 2 years ago

based on the screenshot you have selected wrong project root. Use M-x lsp-workspace-folders-add/remove and make sure that you have added the folder that contains pom.xml

asmodeus812 commented 2 years ago

@yyoncho Thanks, tried to run lsp-workspace-folders-add and typed in the full directory anyway. Seems that the workspace is created correctly, when i type lsp-describe-session I can see the workspace (the project root folder where the pom is) Also when i try to run lsp-workspace-folders-remove i can select that workspace and can remove it. I found out that dragging, dropping the pom in emacs seems to automatically create the workspace anyway. But the issue still persists, I am probably missing something.

image

yyoncho commented 2 years ago

can you try using lsp-start-plain.el as described here https://github.com/emacs-lsp/lsp-mode/issues/new?assignees=&labels=bug&template=bug_report.yml ? There is something in your enviroment that causing these issues, let's see if it will work with clean config.

asmodeus812 commented 2 years ago

@yyoncho I have made some progress regarding the configuration, made sure to test with the .el your provided above, and replicated the same config in my .emacs file. As it turns out it seems to be working okay now. Pasting the relevant configuration, let me know if you find something that is misplaced. I am not sure what this is: gc-cons-threshold (* 100 1024 1024) read-process-output-max (* 1024 1024)

Noticed a problem with sideline, while very useful i wanted to disable the hideous wall of text next to the code that was showing actions and diagnostics, and simply use lsp-ui-sideline-apply-code-actions to see them in the minibuffer. Sadly i was able to hide the text but not able to make list of available actions for a symbol show, with the config below, lsp-ui-sideline-apply-code-actions says - No code actions on the current line lsp-ui-sideline-enable t lsp-ui-sideline-show-hover nil lsp-ui-sideline-show-symbol nil lsp-ui-sideline-show-diagnostics nil lsp-ui-sideline-show-code-actions nil

LSP Configuration `(let* ((pkg-list '(

  lsp-mode lsp-ui yasnippet lsp-java
  lsp-python-ms lsp-haskell
  helm-lsp lsp-treemacs dap-mode
  lsp-origami lsp-dart lsp-pyright
  company flycheck which-key

  rust-mode php-mode scala-mode yaml-mode
  dart-mode clojure-mode typescript-mode
  json-mode js2-mode
)

) ) (mapc (lambda (pkg) (unless (package-installed-p pkg) (package-install pkg)) (require pkg)) pkg-list ) )

(which-key-mode) (yas-global-mode) (global-flycheck-mode)

(add-hook 'c-mode-hook 'lsp) (add-hook 'c++-mode-hook 'lsp) (add-hook 'java-mode-hook 'lsp)

(add-hook 'prog-mode-hook 'lsp) (add-hook 'after-init-hook 'global-company-mode) (add-hook 'lsp-mode-hook #'lsp-enable-which-key-integration)

(setq treemacs-space-between-root-nodes nil company-selection-wrap-around t company-minimum-prefix-length 1

  lsp-prefer-capf t
  lsp-use-lsp-ui t

  lsp-lens-enable t
  lsp-headerline-breadcrumb-enable t

  lsp-ui-doc-delay 0.2
  lsp-ui-doc-show-with-mouse t
  lsp-ui-doc-show-with-cursor nil

  lsp-ui-sideline-enable t

  lsp-java-format-enabled t
  lsp-java-format-comments-enabled t
  lsp-java-save-action-organize-imports t
  lsp-java-save-action-organize-imports t
  lsp-java-import-gradle-enabled t
  lsp-java-import-maven-enabled t

  lsp-java-vmargs '("-XX:+UseParallelGC" "-XX:GCTimeRatio=4" "-XX:AdaptiveSizePolicyWeight=90" "-Xmx2048m" "-Xms256m"))

` Le

yyoncho commented 2 years ago

Refer to https://emacs-lsp.github.io/lsp-mode/tutorials/how-to-turn-off/

asmodeus812 commented 2 years ago

@yyoncho Thanks,already had a look at it and followed it yesterday, but as i said above, hiding the sideline actually completely disabled the apply-actions, if that is by design, is there not a way to not show the actions constantly when your cursor is over a symbol and still have access to them, seems like a reasonable thing.

yyoncho commented 2 years ago

Use M-x lsp-execute-code-action for executing code actions.

asmodeus812 commented 2 years ago

@yyoncho That is helpful, the issue with that is that this shows all available actions, not the ones that are only valid for the symbol currently being hovered over. Which is only available with the apply fun.

yyoncho commented 2 years ago

That is the right thing to do from lsp-mode's perspective. In general, lsp-ui method should go away. If you want to have a method that shows only the refactorings and fixes without source level code actions you can create custom method. You may follow lsp-execute-code-action-by-kind as an example.

asmodeus812 commented 2 years ago

@yyoncho Thanks, will look into it. I have a few more queries but i reckon i can go over to the discord chat, I think we can close this ticket. Thanks for the Help !

Just to summarize, for anyone in the future reading this - I made sure to clear all emacs packages and cache from - C:\Users\\AppData\Roaming.emacs.d . Removed all LSP related configurations from my config and then copied the configuration as provided in the plain.el example linked above in my config file, and everything seemed to work correctly after the packages were downloaded. After that, you can try and setq vars to customize the behaviour.