lsp-java icon indicating copy to clipboard operation
lsp-java copied to clipboard

lsp-format-buffer: Capability not supported by the language server

Open fgabolde opened this issue 4 years ago • 16 comments

Describe the bug

When calling lsp-format-buffer, nothing happens in the buffer and I get a message in the minibuffer:

lsp-format-buffer: Capability not supported by the language server: "documentFormattingProvider"

This is with the latest melpa packages (not melpa-stable) on Emacs 25.2.2. I have formatting enabled (on comments and on type as well). My settings are pointing at a local codestyle file and profile that work fine in Eclipse. I made sure to update the LS just in case.

Something similar happens with lsp-format-region. Format on type runs fine, oddly enough.

All of this used to work in an earlier release.

To Reproduce

Start a new LSP session, open a Java buffer, run M-x lsp-format-buffer.

Expected behavior

The file should be reformatted according to the settings in the XML file.

Logs

Attached are the contents of the IO log buffer after the LS has been initialized, edited to remove sensitive contents. I also included a textDocument/onTypeFormatting command after that; as far as I can tell no other commands are related to formatting (I assume lsp-java didn't send them since it thinks the capability is not enabled).

lsp-log-io.txt

fgabolde avatar Apr 24 '20 12:04 fgabolde

same problem

thinkiny avatar Jun 02 '20 16:06 thinkiny

Same problem.

I checked that the path to the xml format file is valid : intentionally making a small typo in the path, causes the LSP to report an error. Also, if I unset the format profile, lsp-format-buffer comes back but with the default setting, probably google.

deb75 avatar Jul 27 '20 23:07 deb75

@yyoncho?

ericdallo avatar Jul 28 '20 00:07 ericdallo

It is strange, sometimes it works, looks like a race condition at startup.

deb75 avatar Jul 28 '20 09:07 deb75

It is strange, sometimes it works, looks like a race condition at startup.

Yes, I also noticed this in fact.

lsp-format-buffer will sometimes work for a whole session without my changing any settings, then I need to restart the LS for whatever reason and poof.

I think I've had format-on-type work at least once while lsp-format-buffer didn't, too. I can try again tomorrow.

fgabolde avatar Jul 28 '20 17:07 fgabolde

I tried to import the formatting directly into .settings/org.eclipse.jdt.core.prefs. To do this, I opened the project with Eclipse and then imported the format file on a per project basis. This wrote everything in the aforementioned file.

Even with this, lsp-format-buffer seems to work every now and then, although the server seems to be correctly configured :

New configuration: {java={,
format={onType={enabled=false},
comments={enabled=true},
settings={profile=JavaAlgoConventions, url=file:///C:/Users/xxx/.emacs.d/lsp/formatter.xml}, enabled=true}, 
,}}

Here is my lsp-java configuration :

(use-package lsp-java
  :defer 3
  :init
  (setq-default lsp-java-server-install-dir "C:/Users/xxx/.emacs.d/lsp/server/eclipse.jdt.ls/")
  (setq lsp-java-server-install-dir "C:/Users/xxx/.emacs.d/lsp/server/eclipse.jdt.ls/")
  (setq lsp-java-format-settings-url "file:///C:/Users/xxx/.emacs.d/lsp/formatter.xml")
  (setq lsp-java-format-on-type-enabled nil)
  (setq lsp-java-format-settings-profile "JavaAlgoConventions")
  (setq lsp-java-java-path "D:/xxx/yyy/jdk-11.0.8/bin/java.exe")
  (progn
    (require 'lsp-ui-flycheck)
    (require 'lsp-ui-sideline)
    (add-hook 'java-mode-hook #'lsp)
    (add-hook 'java-mode-hook #'flycheck-mode)
    (add-hook 'java-mode-hook #'company-mode)
    (add-hook 'java-mode-hook #'lsp-ui-sideline-mode))
  :config
  (setq lsp-java-configuration-runtimes '[(:name "JavaSE-11" 
                                                 :path "D:/xxx/yyy/jdk-11.0.8")
				                          (:name "JavaSE-1.8"
                                                 :path "D:/xxx/yyy/jdk1.8.0_202"
                                                 :default t)])
  (setq lsp-java-format-settings-profile "JavaAlgoConventions")
  (setq lsp-java-format-settings-url "file:///C:/Users/xxx/.emacs.d/lsp/formatter.xml")
  (setq lsp-java-format-on-type-enabled nil)
  (setq lsp-java-java-path "D:/xxx/yyy/utils/jdk-11.0.8/bin/java.exe")
  (add-hook 'java-mode-hook 'lsp))

I put :defer 3, I have seen it elsewhere, not sure if it helps.

deb75 avatar Jul 31 '20 10:07 deb75

I reinstalled lsp-mode and all its dependencies with latest emacs (27.1), under mingw64 (windows 10). Then, I wiped out the workspace and reimported my projects.

This issue seems to happen less often. I noticed it happens especially when emacs auto-open java files and then auto-start the java language server. There must be some race conditions that occurs less when the language server is run more softly. This is at least how I understand this issue.

deb75 avatar Sep 14 '20 08:09 deb75

For me, the problem is that the language server formatting capability is permanently disabled if I load the language server in a buffer where the formatting is disabled. Then if I later enable formatting, I need to restart the language server or I'll get the Capability not supported error.

I disable formatting with the following, essentially:

(progn 
  (remove-hook 'before-save-hook #'lsp-format-buffer t) 
  (remove-hook 'before-save-hook #'lsp-organize-imports t) 
  (setq lsp-java-format-enabled nil) 
  (setq lsp-java-format-on-type-enabled nil))

This is pretty frustrating because sometimes I want to make surgical changes without reformatting everything, or reformat just a region in a certain file, but still have auto-format in other buffers. Doing that requires constant restarting of the language server, which is also time-consuming to sit around and wait for 30 seconds for the language server to start back up. It would be nice if formatting settings were applied per-buffer instead of across all projects in the language server's workspace.

guseggert avatar Nov 19 '20 19:11 guseggert

This is pretty frustrating because sometimes I want to make surgical changes without reformatting everything, or reformat just a region in a certain file, but still have auto-format in other buffers.

AFAIK lsp-java does not perform auto-format.

yyoncho avatar Nov 19 '20 19:11 yyoncho

This is pretty frustrating because sometimes I want to make surgical changes without reformatting everything, or reformat just a region in a certain file, but still have auto-format in other buffers.

AFAIK lsp-java does not perform auto-format.

Thanks for clarifying, I'm not sure about the relationship between lsp-java and the language server. Are you suggesting I should move this discussion over to the jdt.ls issue tracker? Perhaps this issue?

guseggert avatar Nov 19 '20 19:11 guseggert

Are you suggesting I should move this discussion over to the jdt.ls issue tracker? Perhaps this issue?

What I saying is that you should not see automatic formatting using the default lsp-java settings. Maybe you are talking about organizing imports on save?

yyoncho avatar Nov 19 '20 20:11 yyoncho

Are you suggesting I should move this discussion over to the jdt.ls issue tracker? Perhaps this issue?

What I saying is that you should not see automatic formatting using the default lsp-java settings. Maybe you are talking about organizing imports on save?

It's definitely sending textDocument/formatting requests, and I've turned off all format settings and removed lsp-format-buffer from the before-save hook. Note that I also have formatter settings in the project's org.eclipse.jdt.core.prefs file (don't know if that's relevant). Once I find the time, I can open a debugger to see why the formatting request is being sent.

guseggert avatar Nov 19 '20 20:11 guseggert

Can you try M-x debug-debug-on-entry RET lsp-format-buffer RET and paste the callstack.

yyoncho avatar Nov 19 '20 20:11 yyoncho

Thanks! I realized that my buffer-local before-save-hook had a t element so it was also running the global hook.

guseggert avatar Nov 23 '20 06:11 guseggert

Hi @guseggert - would you mind sharing your config with the before-save-hook now? I have the same issue with something like:

(defun add-clj-format-before-save () 
  (interactive)
  (add-hook 'before-save-hook 'lsp-format-buffer nil 'local))

(add-hook 'clojure-mode-hook #'add-clj-format-before-save)

nikonikoniko avatar Jan 03 '22 15:01 nikonikoniko

Sorry I don't seem to have that anymore :(

guseggert avatar Jan 07 '22 21:01 guseggert