standard icon indicating copy to clipboard operation
standard copied to clipboard

LSP: Autoformat on save in neovim

Open mna opened this issue 1 year ago • 9 comments

Hello,

I'm using neovim with the standardrb LSP configured:

$ nvim --version
NVIM v0.10.0-dev+852-g54be7d6b4
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3

It does report the documentFormattingProvider capability, but it doesn't seem to work when I try to format a buffer, I get the following error in the LSP log:

[ERROR][2023-08-17 10:54:09] .../vim/lsp/rpc.lua:675	"rpc"	"<path>/standardrb"	"stderr"	"[server] Format request arrived before text synchonized; skipping: `file:///<path>/a.rb'\n"

Is there some configuration I'm missing? I'm using the default LSP configuration as documented in the nvim-lspconfig link above (require'lspconfig'.standardrb.setup{}). I noticed the stardard repo's Wiki on neovim doesn't mention formatting as part of its page, so I'm not sure if that's something that's supported or not (even though it does report the capability).

Thanks, Martin

mna avatar Aug 17 '23 15:08 mna

hey @will would you be so kind as to look at this?

searls avatar Aug 17 '23 16:08 searls

I've taken a closer look at this, it's not just autoformat on save, it's the formatting of the buffer that has this bug (that is, it doesn't have to be called in a BufWritePre to reproduce).

If that helps (I'm not a ruby expert, nor an LSP one!), when I tweak that line https://github.com/standardrb/standard/blob/main/lib/standard/lsp/routes.rb#L163 for this:

@text_cache[file_uri] = text unless text.nil?

formatting does work. It seems like for some reason, it receives a nil text before formatting (even though the vim buffer is never empty) and that clears the @text_cache thing so no formatting happens.

mna avatar Aug 17 '23 20:08 mna

@mna after testing this unless, right, it formats, but it does not show the diagnostics anymore

fernandes avatar Aug 17 '23 23:08 fernandes

@fernandes oh no, for sure, I didn't mean that this was a workaround (and even less a fix)! Just that it illustrates that for some reason, the diagnostic call receives a nil text before formatting, storing it in the @text_cache and that's why it doesn't format anything.

mna avatar Aug 17 '23 23:08 mna

@mna it formats for me on nvim 0.9.1, which is the current released version of nivm. Formatting breaking might be due to an upcoming change in 10?

I'm not in a good situation at the moment to build and try neovim nightly. Would you be able to try formatting with 0.9.1 to test this theory?

will avatar Aug 18 '23 11:08 will

@will 💥 you nailed it!

on 0.10 (nighly compiled / macos) happens this error, using nvim 0.9.1 it works perfectly, so yes, some breaking on 10 👍

fernandes avatar Aug 18 '23 11:08 fernandes

Just wanted to add that I'm seeing this too!

waferbaby avatar Nov 06 '23 04:11 waferbaby

I also have the error with SublimeText 4169.

RomainMorlevat avatar Jan 19 '24 09:01 RomainMorlevat

I think this is because currently textDocument/diagnostic is handled incorrectly. It expects the client to send the text in the request, but according to the spec, this request only includes the document uri. I've opened a PR with a fix.

muxcmux avatar Jan 30 '24 21:01 muxcmux