Code Monkey home page Code Monkey logo

Comments (7)

lukas-reineke avatar lukas-reineke commented on June 14, 2024

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?

from lsp-format.nvim.

gegoune avatar gegoune commented on June 14, 2024

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.

from lsp-format.nvim.

gegoune avatar gegoune commented on June 14, 2024

Seems like I can now reproduce it with:

Minimal init.lua file
-- 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:

  • sumneko_lua LSP installed
  • stylua installed

Steps to reproduce

Prerequisites

Create test file (duplicated content is on purpose:

Test file test.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

from lsp-format.nvim.

lukas-reineke avatar lukas-reineke commented on June 14, 2024

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

from lsp-format.nvim.

gegoune avatar gegoune commented on June 14, 2024

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

from lsp-format.nvim.

jose-elias-alvarez avatar jose-elias-alvarez commented on June 14, 2024

@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@d871b41 takes care of the issue in your reproduction, so let me know if that works for you, too.

from lsp-format.nvim.

gegoune avatar gegoune commented on June 14, 2024

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

from lsp-format.nvim.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.