eclipse.jdt.ls icon indicating copy to clipboard operation
eclipse.jdt.ls copied to clipboard

org.eclipse.jdt.core.formatter.tabulation.* settings are ignored

Open rybesh opened this issue 1 year ago • 4 comments

I am using jdtls with eglot in Emacs.

I have the following lines in .settings/org.eclipse.buildship.core.prefs:

org.eclipse.jdt.core.formatter.tabulation.char=SPACE
org.eclipse.jdt.core.formatter.tabulation.size=2

But eglot-format results in a formatted buffer with 8-space indentation, whether or not those lines are there.

rybesh avatar Nov 02 '23 20:11 rybesh

I haven't tried this out yet, but I would think you need to place those JDT settings into .settings/org.eclipse.jdt.core.prefs.

rgrunber avatar Nov 03 '23 17:11 rgrunber

That doesn’t seem to have any effect either.

rybesh avatar Nov 03 '23 21:11 rybesh

I think I found the problem. Similar to https://github.com/emacs-lsp/lsp-java/issues/318#issuecomment-783953847 .

The textDocument/formatting & textDocument/rangeFormatting requests take as a parameter (aside from the document itself & the range to apply) FormattingOptions. In particular this includes :

/**
  * Size of a tab in spaces.
  */
tabSize: uinteger;

/**
  * Prefer spaces over tabs.
  */
insertSpaces: boolean;

So the spec expects that the client should send options for whether to use tabs/spaces and the number of spaces for a tab.

The language server then overrides tabulation.char & tabulation.size in the format call (because those are the options controlling that), so what you set is being ignored. I confirmed this by adding another option like org.eclipse.jdt.core.formatter.lineSplit=50, which was respected. I just had to place a .settings/org.eclipse.jdt.core.prefs where .settings/ was a sibling to the src folder.

https://github.com/eclipse-jdtls/eclipse.jdt.ls/blob/395b5da0cc588844d8b43464bc6bb001aa4ff23b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/FormatterHandler.java#L154-L163

Do you know what values eglot is sending to JDT-LS ? Is there a way made available to users to set this on the emacs side ?

rgrunber avatar Nov 06 '23 03:11 rgrunber

Unfortunately communication between eglot and jdtls is somewhat shrouded in mystery:

https://github.com/joaotavora/eglot/discussions/1222

rybesh avatar Nov 15 '23 19:11 rybesh