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

Cursor jumps to bottom of file when auto-formatting on save #12128

Closed
DetachHead opened this issue Jul 1, 2024 · 5 comments · Fixed by #12171
Closed

Cursor jumps to bottom of file when auto-formatting on save #12128

DetachHead opened this issue Jul 1, 2024 · 5 comments · Fixed by #12171
Assignees
Labels
bug Something isn't working server Related to the LSP server

Comments

@DetachHead
Copy link

this is a very strange bug that i can't seem to minify at all.

to reproduce:

  1. set ruff.nativeServer to true
  2. set editor.formatOnSave to true (it seems to only happen reliably when formatting on save)
  3. insert a newline near the top of a large file (in my case i copied the contents of builtins.pyi into a random .py file in my project)
  4. save the file with ctrl+s
Recording.2024-07-01.115658.mp4
@charliermarsh
Copy link
Member

Oh wow, thanks for the video.

@charliermarsh charliermarsh added bug Something isn't working server Related to the LSP server labels Jul 1, 2024
@dhruvmanila dhruvmanila changed the title new language server jumps to the bottom of the file when inserting newline and formatting a file with thousands of lines of code Cursor jumps to bottom of file when auto-formatting on save Jul 2, 2024
@dhruvmanila
Copy link
Member

Unfortunately, I'm not able to reproduce this. Can you provide some additional details like:

  1. VS Code version
  2. Ruff version
  3. Ruff VS Code extension version
  4. Any relevant VS Code settings related to Python and Ruff
  5. If relevant, pyproject.toml / ruff.toml settings

I've copied the builtins.pyi file and following the exact steps as you are:

Screen.Recording.2024-07-02.at.12.58.27.mov

Can you also provide the output of "Ruff: Print Debug Information" command. Here's mine:

2024-07-02 12:57:10.422 [info] [Info  - 12:57:10 PM] executable = /Users/dhruv/.local/bin/ruff
version = 0.5.0
encoding = UTF16
open_document_count = 1
active_workspace_count = 1
configuration_files = ["/Users/dhruv/playground/ruff/pyproject.toml"]
capabilities.code_action_deferred_edit_resolution = true
capabilities.apply_edit = true
capabilities.document_changes = true
capabilities.workspace_refresh = true
capabilities.pull_diagnostics = true

@dhruvmanila dhruvmanila added this to the Ruff Server: Stable milestone Jul 2, 2024
@dhruvmanila dhruvmanila added the needs-info More information is needed from the issue author label Jul 3, 2024
@DetachHead
Copy link
Author

DetachHead commented Jul 3, 2024

sorry, i completely forgot that i'm using a different version of the builtin stubs with docstrings in them, and the issue doesn't seem to occur without them.

here's the version of the file i was running it on (sorry it's a .txt instead of .py because github doesn't let you upload python files for some reason)

in case you still aren't able to reproduce it, heres the additional info you requested:

vscode: v1.90.2
ruff: v0.4.10 (also tested on v0.5.0)
ruff vscode extension: v2024.30.0
pyproject.toml/ruff.toml: N/A, i deleted them and the issue still occurs
vscode settings related to ruff: {"ruff.nativeServer": true, "[python]": {"editor.formatOnSave": true, "editor.defaultFormatter": "charliermarsh.ruff"}}

ruff debug info:

executable = c:\Users\user\project\.venv\Scripts\ruff.exe
version = 0.4.10
encoding = UTF16
open_document_count = 28
active_workspace_count = 1
configuration_files = ["c:\\Users\\user\\project\\ruff.toml"]
capabilities.code_action_deferred_edit_resolution = true
capabilities.apply_edit = true
capabilities.document_changes = true
capabilities.workspace_refresh = true
capabilities.pull_diagnostics = true

@dhruvmanila
Copy link
Member

Thank you for providing the details! I can reproduce it now.

@dhruvmanila dhruvmanila removed the needs-info More information is needed from the issue author label Jul 3, 2024
@dhruvmanila dhruvmanila self-assigned this Jul 3, 2024
@dhruvmanila
Copy link
Member

I think this is happening because the replacement algorithm (https://github.com/astral-sh/ruff/blob/88a4cc41f76cbb5d1318d5e689b118ec624b8d69/crates/ruff_server/src/edit/replacement.rs) is different than the one in ruff-lsp (https://github.com/astral-sh/ruff-lsp/blob/c51c77c269c3f13be2534d118b81e1c3b9bbfaff/ruff_lsp/server.py#L1430-L1470).

The returned TextEdit replacement range is different for the same formatting between ruff server and ruff-lsp:

// ruff server
2024-07-03 16:26:59.123 [info] Result: [
    {
        "newText": "...",
        "range": {
            "end": {
                "character": 0,
                "line": 5089
            },
            "start": {
                "character": 0,
                "line": 1146
            }
        }
    }
]


// ruff-lsp
2024-07-03 16:26:20.420 [info] Result: [
    {
        "range": {
            "start": {
                "line": 1146,
                "character": 0
            },
            "end": {
                "line": 1149,
                "character": 0
            }
        },
        "newText": ""
    }
]

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

Successfully merging a pull request may close this issue.

3 participants