0

I'm pretty new to emacs, and I wanted to setup an LSP with code completion for java and other languages there. I started out with java and made a simple configuration

(use-package lsp-mode
 :hook (java-mode . lsp-deferred)
 :commands (lsp lsp-deferred)
 :config (lsp-enable-which-key-integration t))
(use-package lsp-java
   :config (add-hook 'java-mode-hook #'lsp))

but I also want LSP for any other standalone files and scripts, which I somehow cannot get to work. Whenever I open a standalone .java file I get this prompt:

test.java is not part of any project.

i ==> Import project root /home/maksatr/Documents/
I ==> Import project by selecting root directory interactively
. ==> Import project at current directory /home/maksatr/Documents/
d ==> Do not ask again for the current project by adding /home/maksatr/Documents/ to lsp-session-folders-blocklist
D ==> Do not ask again for the current project by selecting ignore path interactively
n ==> Do nothing: ask again when opening other files from the current project

Select action: 

and as you can see every option is either creating a project or bypassing it without having all of the LSP's benefits.
Googling the issue seems to miss it, perhaps because it's a pretty noob question that I just failed to solve properly.
I have also tried Eglot, but it does not seem to show good enough completions, only syntax errors.
Is there a way to set it up in a way so that it also works with standalone files?

1 Answer 1

0

I found the solution, thankfully it was rather simple:

(use-package lsp-mode
 :hook (java-mode . lsp-deferred)
 :commands (lsp lsp-deferred)
 :config 
(setq lsp-auto-guess-root t) ; This is the fix
(lsp-enable-which-key-integration t))
(use-package lsp-java
   :config (add-hook 'java-mode-hook #'lsp))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.