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

Formatting replaces the entire file #383

Open
WhyNotHugo opened this issue Feb 11, 2024 · 5 comments
Open

Formatting replaces the entire file #383

WhyNotHugo opened this issue Feb 11, 2024 · 5 comments
Labels
enhancement New feature or request vim Related to the Neo(Vim) editor

Comments

@WhyNotHugo
Copy link
Contributor

When formatting a file, ruff will replace the entire file with a formatted copy. It should only replace lines that have changed (as far as I can tell, this is what most LSPs do).

The issue with replacing the whole file, is that (at least on neovim) the cursor position is reset to the beginning of the file on background windows/splits, because the entire file was deleted/replaced.

Reproduction steps

  • Open a well formatted file in neovim
  • Open a split window (:vsp)
  • Scroll on window to the middle of the file
  • Switch to the unscrolled window
  • Add an empty line at the beginning of the file
  • Format the file using ruff-lsp

The cursor for the second split will now jump to the top of the file.

@WhyNotHugo
Copy link
Contributor Author

I think that imara-diff can be used to actually calculate the regions that have changed:

https://docs.rs/imara-diff/0.1.5/imara_diff/sink/trait.Sink.html

@WhyNotHugo
Copy link
Contributor Author

The format returned by imara-diff is also very close to the TextEdit format used in the LSP response:

https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textEdit

Basically it is a matter of getting the ranges from the diff, and then serialising those as a TextEdit. Mostly a bit of glue code is needed here.

@WhyNotHugo
Copy link
Contributor Author

Huh, looking at the source in _fixed_source_to_edits, is seems that the change is already narrowed down to a single TextEdit, which should avoid replacing the whole file in most cases.

I wonder if the root cause of the issue that I'm seeing is something else then.

@zanieb zanieb added the enhancement New feature or request label Feb 12, 2024
@dhruvmanila
Copy link
Member

Thanks for providing the reproduction steps!

I might be doing something wrong here but I'm unable to reproduce this in the way you've described in the "Reproduction steps" section. I've attached a screen recording of the same. Can you verify if that's what you intended on doing? I'm using the vim.lsp.buf.format() function to run the formatter through the ruff-lsp.

Screen.Recording.2024-02-12.at.11.55.52.mov

@MichaReiser
Copy link
Member

@WhyNotHugo what LSP version are you using? I'm asking because the narrow edits feature has only been released with v0.0.52

@dhruvmanila dhruvmanila added the vim Related to the Neo(Vim) editor label Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request vim Related to the Neo(Vim) editor
Projects
None yet
Development

No branches or pull requests

4 participants