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

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

Closed
kyoh86 opened this issue Jul 15, 2024 · 4 comments · Fixed by #11357
Closed

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

kyoh86 opened this issue Jul 15, 2024 · 4 comments · Fixed by #11357
Labels
A-language-server Area: Language server client C-bug Category: This is a bug E-easy Call for participation: Experience needed to fix: Easy / not much E-has-instructions Call for participation: Has instructions for fixing the issue and opening a PR

Comments

@kyoh86
Copy link

kyoh86 commented Jul 15, 2024

Summary

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 (neovim/neovim#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 (mattn/efm-langserver#281), but the fix causes problems with other (non-specified) clients such as Helix.
Therefore, I would like you to implement a fix in Helix that follows the spec, as well as Neovim.

Reproduction Steps

  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. Set the path
$ export PATH="$(go env GOPATH)/bin:$PATH"
  1. Configure the Helix

helix/languages.toml

[language-server.efm]
command = "efm-langserver"

[[language]]
name = "go"
language-servers = [
  { name = "efm", only-features = [ "diagnostics", "format" ] },
]
  1. Configure the efm-langserver
version: 2

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

languages:
  json:
    - <<: *echo-fix
  1. Open helix
$ hx foo.json
  1. Call :format

Expected result:

foo
bar
baz

But actual result:

baz
bar
foo

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

Helix log

~/.cache/helix/helix.log
2024-07-15T14:46:45.102 helix_lsp::transport [ERROR] efm err <- "2024/07/15 14:46:45 jsonrpc2 handler: sending response 2: jsonrpc2: connection is closed\n"
2024-07-15T14:46:45.104 helix_lsp::transport [ERROR] efm err <- "2024/07/15 14:46:45 efm-langserver: connections closed\n"
2024-07-15T14:46:45.102 helix_lsp::client [WARN] language server failed to terminate gracefully - server closed the stream
2024-07-15T14:46:45.104 helix_lsp::transport [ERROR] efm err: <- StreamClosed
2024-07-15T14:46:45.104 helix_lsp::transport [ERROR] efm err: <- IO(Os { code: 32, kind: BrokenPipe, message: "Broken pipe" })

Platform

Linux

Terminal Emulator

WezTerm

Installation Method

releases page

Helix Version

helix 24.7 (079f544)

@pascalkuthe
Copy link
Member

does this match what vscode does? You will have a hard time convincing servers to follow the spec if vscode doesn't follow it unfortunately so that is the the behaviour I would aim to emulate

@the-mikedavis the-mikedavis added the A-language-server Area: Language server client label Jul 15, 2024
@kyoh86
Copy link
Author

kyoh86 commented Jul 16, 2024

@pascalkuthe Thank you for your reply.

In the Neovim issue that triggered this modification, it is stated that it follows the VSCode implementation as follows.
Therefore, it is assumed that VSCode also processes in the same order.

Also, in the vscode implementation, similar start position text edits aren't sorted in reverse order

I do not know how to apply formatting using VSCode and a language server, and have not been able to confirm how "changes across multiple consecutive lines" like efm-langserver are handled.

@kyoh86
Copy link
Author

kyoh86 commented Jul 28, 2024

It has fixed in Neovim, thanks.

@kyoh86 kyoh86 closed this as completed Jul 28, 2024
@pascalkuthe
Copy link
Member

This still appear in helix because we accidentally perform an unstable sort (instead of a stable sort).

Just need to replace this here

@pascalkuthe pascalkuthe reopened this Jul 28, 2024
@pascalkuthe pascalkuthe added E-easy Call for participation: Experience needed to fix: Easy / not much E-has-instructions Call for participation: Has instructions for fixing the issue and opening a PR labels Jul 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-language-server Area: Language server client C-bug Category: This is a bug E-easy Call for participation: Experience needed to fix: Easy / not much E-has-instructions Call for participation: Has instructions for fixing the issue and opening a PR
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants