standardrb/standard

LSP: Autoformat on save in neovim

mna opened this issue · 9 comments

mna commented

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

searls commented

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

mna commented

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 after testing this unless, right, it formats, but it does not show the diagnostics anymore

mna commented

@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.

will commented

@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 💥 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 👍

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

I also have the error with SublimeText 4169.

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.