Closed gegoune closed 2 years ago
There still is no formatting with :wq
, except when you set it up https://github.com/lukas-reineke/lsp-format.nvim#wq-will-not-format-when-not-using-sync
And I can't reproduce this
Can you make a minimal init.lua
file that triggers this?
Did not set it up, so pretty surprise to hear it and still see it happening. Will do bit more investigation and try to come up with reproducible minimal. Thanks for looking into it.
Seems like I can now reproduce it with:
sumneko_lua
LSP installedstylua
installed Create test file (duplicated content is on purpose:
Note: To make replication easier please save minimal configuration file to disk, run command from step 1. below and then comment out or remove line 58 (require('packer').sync()
) and restart test from step 1.
nvim --clean -u minimal.init.lua test.lua
:4
dG
:w
:wq
cat test.file
test.lua
file to be 3 lines long.
test.lua
is 3 lines long after step 4. but it's content gets duplicated after step 5 (run watch -n1 test.lua
in another terminal window).
nvim: NVIM v0.8.0-dev+1510-g440b65c33
sumneko_lua: lua-language-server: stable 3.1.0 (bottled), HEAD
(installed via homebrew)
stylua: stylua 0.13.1
Thank you for the effort to write a reproducible example.
I can reproduce it, and I also know what is going wrong. This is a bug in null-ls.
null-ls computes the diff between the current buffer state and the formatted state here. https://github.com/jose-elias-alvarez/null-ls.nvim/blob/c832a0ecb18fac8b35967111327434edf6f782aa/lua/null-ls/formatting.lua#L79-L83
It does this by getting the content of the buffer at the time the formatting is done. But in case of :wq
, that buffer is already closed. So the diff wrongfully assumes the buffer is just empty, and the change is to append the whole content to the end.
You should open an issue in null-ls. But the proper solution is probably to discard the format request if the buffer is already closed.
If you want to make :wq
work properly, use the example I have in the readme with using sync in this case.
I'll keep this issue open until there is a fix on null-ls side
@jose-elias-alvarez I can recreate it using your template and open new issue in null-ls repository, unless you are already aware of this?
@lukas-reineke Thank you very much for looking into it.
@gegoune Yep, I was able to reproduce this and it does seem to be an issue on our end. The fix in jose-elias-alvarez/null-ls.nvim@d871b418c67867a11a43eb1412a1a21aee888ae3 takes care of the issue in your reproduction, so let me know if that works for you, too.
Seems to be working well now. Thank you both, you rock!
With null-ls (stylua enabled only) and lsp-format attached to buffer through
on_attach()
with sumneko's lua LSP buffer's content will get duplicated on:wq
. Seems like it's only happening when there are changes made to buffer. I think previously there was no formatting on:wq
at all? If buffer has no changes made to it since opening of neovim content of file is not changed, but as soon as buffer is changed:wq
results in duplication.