lukas-reineke / lsp-format.nvim

A wrapper around Neovims native LSP formatting.
559 stars 27 forks source link

Buffers content duplicated on wq #40

Closed gegoune closed 2 years ago

gegoune commented 2 years ago

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.

lukas-reineke commented 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?

gegoune commented 2 years ago

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.

gegoune commented 2 years ago

Seems like I can now reproduce it with:

Minimal init.lua file ```lua -- run: nvim --clean -u minimal.init.lua vim.api.nvim_command [[set runtimepath=$VIMRUNTIME]] vim.api.nvim_command [[set packpath=/tmp/nvim/site]] local package_root = '/tmp/nvim/site/pack' local install_path = package_root .. '/packer/start/packer.nvim' vim.opt.termguicolors = true local function load_plugins() require('packer').startup { function(use) use 'wbthomason/packer.nvim' use { 'jose-elias-alvarez/null-ls.nvim', requires = { 'nvim-lua/plenary.nvim' } } use { 'neovim/nvim-lspconfig', requires = { 'lukas-reineke/lsp-format.nvim' } } end, config = { package_root = '/tmp/nvim/site/pack', compile_path = install_path .. '/plugin/packer_compiled.lua', }, } end _G.load_config = function() require('lspconfig').sumneko_lua.setup { on_attach = function(client) require('lsp-format').on_attach(client) end, settings = { Lua = { format = { enable = false, }, }, }, } require('null-ls').setup { on_attach = function(client) require('lsp-format').on_attach(client) end, sources = { require('null-ls').builtins.formatting.stylua, }, } end if vim.fn.isdirectory(install_path) == 0 then vim.fn.system { 'git', 'clone', '--depth', '1', 'https://github.com/wbthomason/packer.nvim', install_path } load_plugins() require('packer').sync() vim.api.nvim_command 'autocmd User PackerComplete ++once lua load_config()' else load_plugins() require('packer').sync() _G.load_config() end ```

Requirements:

Steps to reproduce

Prerequisites

Create test file (duplicated content is on purpose:

Test file test.lua ```lua -- Just a test print 'test' return 1 -- Just a test print 'test' return 1 ```

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.

  1. nvim --clean -u minimal.init.lua test.lua
  2. :4
  3. dG
  4. :w
  5. :wq
  6. cat test.file

Expected behaviour

test.lua file to be 3 lines long.

Actual behaviour

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

Additional info:

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

lukas-reineke commented 2 years ago

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

gegoune commented 2 years ago

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

jose-elias-alvarez commented 2 years ago

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

gegoune commented 2 years ago

Seems to be working well now. Thank you both, you rock!