Skip to content

Formatting documents with svelte-language-server always marks them as modified in nvim #1142

@MonaMayrhofer

Description

@MonaMayrhofer

Describe the bug
When using svelte-language-server in conjunction with Neovim (using neovim's builtin lsp-support) running the format command marks the open buffer as modified, even when nothing hat to be changed. This is particularly annoying when running format as an autocommand on save, as vim prevents you from gracefully closing modified buffers. Aka: Before closing a file, the file has to be written, but after a write, it is automatically marked as unsaved again.

This behaviour does not occur in VS-Code, therefore I'm not quite sure if this language server is actually the bad guy here, but I haven't noticed such a behaviour with other language servers - so maybe you guys can hint me into the right direction if svelte-lsp is not the villain here.

To Reproduce
Steps to reproduce the behavior:

  • Using neovim v0.6.0 or higher
  • Install neovim/nvim-lspconfig using paq or a similar package manager.
  • Setup svelte-lsp using require('lspconfig')["svelte"].setup{}
  • Open a (empty) .svelte file.
  • :lua vim.lsp.buf.formatting()
  • Notice that even though the file contents have not changed, nvim marked it as modified (:q will yield a warning)

Expected behavior
A call to the LSP's formatting logic via :lua vim.lsp.buf.formatting() should either modify the buffer with the results from the formatter OR if no modification is necessary leave the buffer untouched and not mark it as modified.

System (please complete the following information):

  • OS: Archlinux
  • IDE: NVIM v0.6.0-dev+61-g6f48c018b
  • Plugin/Package: nvim-lspconfig (which calls svelteserver --stdio)

Additional context
I have not dug too deep inside the code, but maybe this part

const formattedCode = prettier.format(document.getText(), {
            ...config,
            plugins: getSveltePlugin(),
            parser: 'svelte' as any
});

return [
            TextEdit.replace(
                Range.create(document.positionAt(0), document.positionAt(document.getTextLength())),
                formattedCode
            )
];

is the problem, as it yields a TextEdit.replace without regard to whether anything has actually changed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedFixed in master branch. Pending production release.bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions