neoclide / coc.nvim

Nodejs extension host for vim & neovim, load extensions like VSCode and host language servers.
Other
24.48k stars 959 forks source link

Text edits with the same start position are applied in reverse #5087

Closed kyoh86 closed 3 months ago

kyoh86 commented 4 months ago

Result from CocInfo

## versions

vim version: NVIM v0.11.0-dev-bcb1768
node version: v22.2.0
coc.nvim version: 0.0.82-33d0a523 2024-07-12 09:31:17 +0800
coc.nvim directory: /home/kyoh86/Projects/github.com/kyoh86/dotfiles/nvim-app/i29392/rtp/coc.nvim
term: WezTerm
platform: linux

## Log of coc.nvim

2024-07-15T14:23:28.200 INFO (pid:30949) [plugin] - coc.nvim initialized with node: v22.2.0 after 90
2024-07-15T14:23:28.203 INFO (pid:30949) [services] - LanguageClient efm state change: stopped => starting
2024-07-15T14:23:28.232 INFO (pid:30949) [language-client-index] - Language server "languageserver.efm" started with 30960
2024-07-15T14:23:28.257 INFO (pid:30949) [services] - LanguageClient efm state change: starting => running
2024-07-15T14:23:28.260 INFO (pid:30949) [services] - service languageserver.efm started

Describe the bug

Text edits returned from the LSP that have the same start position are being applied in reverse. According to the spec, text edits with the same start position should be applied in the order received.

Recently, the language server client included with Neovim has fixed this bug (https://github.com/neovim/neovim/issues/29202)

Accordingly, using some servers (e.g. efm-langserver) with the client included with Neovim, the formatted lines of the result may be reversed. So I tried to ask the server to fix it (https://github.com/mattn/efm-langserver/issues/281), but the fix causes problems with other (non-specified) clients such as coc.nvim. Therefore, I would like you to implement a fix in coc.nvim that follows the spec, as well as Neovim.

Reproduce the bug

  1. Install efm-langserver with patch to follow the LSP spec.
$ git clone --branch fix-281 https://github.com/kyoh86/efm-langserver.git
$ cd efm-langserver
$ go install .
  1. Install the coc.nvim
$ git clone https://github.com/neoclide/coc.nvim.git --branch release ~/.config/nvim-app/i29392/rtp/coc.nvim
  1. Configure the Neovim

~/.config/nvim-app/i29392/init.vim

set rtp+=~/.config/nvim-app/i29392/rtp/coc.nvim
  1. Configure the coc.nvim

~/.config/nvim-app/i29392/coc-settings.json

{
  "coc.preferences.formatOnSave": true,
  "html.filetypes": ["html"],
  "html.format.templating": true,
  "languageserver": {
    "efm": {
      "command": "efm-langserver",
      "args": [],
      "filetypes": ["html"]
    }
  }
}
  1. Configure the efm-langserver

~/.config/efm-langserver/config.toml

version: 2

tools:
  echo-fix: &echo-fix
    format-command: zsh -c 'print "foo\nbar\nbaz"'
    format-stdin: true

languages:
  html:
    - <<: *echo-fix
  1. Set the envar
$ export PATH="$(go env GOPATH)/bin:$PATH"
$ export NVIM_APPNAME=nvim-app/i29392
  1. Open Neovim
$ nvim foo.html
  1. Call :w

Expected result:

foo
bar
baz

But actual result:

baz
bar
foo

If you install the efm-langserver without the patch, this doesn't happen.

kyoh86 commented 3 months ago

I'm having quite a bit of trouble, can anyone at least give me the maintainer's opinion?

fannheyward commented 3 months ago

coc.nvim follows the spec, but the applyEdits function follows how VSCode does. VSCode becomes the de-facto implementation and the servers responded in order, the client shouldn't do re-ordering. This change will break all servers.

kyoh86 commented 3 months ago

Now the coc.nvim client may do re-ordering.

kyoh86 commented 3 months ago

It has fixed in Neovim, thanks.