Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: formatting with lua-language-server rewrites the whole buffer #119

Closed
1 task done
AyushRawal opened this issue Oct 6, 2023 · 5 comments
Closed
1 task done
Labels
bug Something isn't working

Comments

@AyushRawal
Copy link

Neovim version (nvim -v)

0.9.2

Operating system/version

arch linux

Add the debug logs

  • I have set log_level = vim.log.levels.DEBUG and pasted the log contents below.

Log file

17:45:28[DEBUG] Running LSP formatter on /home/rawal/.config/nvim/lua/user/plugins.lua
17:45:32[DEBUG] Running LSP formatter on /home/rawal/.config/nvim/lua/user/plugins.lua

Describe the bug

same

Steps To Reproduce

  1. install lua language server
  2. format with lsp_fallback = true
  3. the whole buffer is written, even if there's no change

Expected Behavior

only formatted lines should have been written

Minimal example file

No response

Minimal init.lua

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "--single-branch",
    "https://github.com/folke/lazy.nvim.git",
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  {
    "stevearc/conform.nvim",
    config = function()
      require("conform").setup({
        log_level = vim.log.levels.DEBUG,
        -- add your config here
      })
    end,
  },
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

Additional context

this issue does not happen with clangd

@AyushRawal AyushRawal added the bug Something isn't working label Oct 6, 2023
@stevearc
Copy link
Owner

stevearc commented Oct 6, 2023

You say "the whole buffer is written, even if there's no change" and the expected behavior is "only formatted lines should have been written"

So...were there formatting changes or not? Is the issue that the buffer gets set as modified after every run, even when nothing visibly changed? Does the same thing happen if you use vim.lsp.buf.format()?

@AyushRawal
Copy link
Author

I am sorry for not stating it clearly. Yes, the buffer gets set as modified, even when nothing visibly changed. And yes, the same thing happens with vim.lsp.buf.format().

@stevearc
Copy link
Owner

stevearc commented Oct 8, 2023

If you see the same behavior from vim.lsp.buf.format() then there's probably nothing we can do. It's the behavior of the language server itself.

@stevearc stevearc closed this as completed Oct 8, 2023
@AyushRawal
Copy link
Author

I am sorry if I misunderstood, but in README, one of the features mentioned is:

Fixes bad-behaving LSP formatters - Some LSP servers are lazy and simply replace the entire buffer, leading to the problems mentioned above. Conform hooks into the LSP handler and turns these responses into proper piecewise changes.

That's why I filed this issue. Can you please clarify ?

@stevearc
Copy link
Owner

stevearc commented Oct 9, 2023

Ah, you are correct. I thought this would be happening because of the LSP sending some small no-op TextEdit on save, but yes it was replacing the entire buffer. The TextEdit was in a slightly different format from the other bad-behaving language server that I tested against, so we didn't catch it. I've updated the logic and it should work as expected now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants