LSP icon indicating copy to clipboard operation
LSP copied to clipboard

`lsp_format_document` breaks formatting when executed right after editing code

Open MrFoxPro opened this issue 11 months ago • 3 comments

https://github.com/user-attachments/assets/9b12a0ed-bdb2-4f17-91ec-9816d1774257

Seems like server is not fast enough to parse document after change and applies formatting to old version?

Environment:

  • OS: Arch Linux 6.12.1
  • Sublime Text version: 4180
  • LSP version: 2.2.0, 2.3.0
  • Language servers used: vstls, also tried LSP-typescript package

MrFoxPro avatar Dec 11 '24 14:12 MrFoxPro

Seems like server is not fast enough to parse document after change and applies formatting to old version?

I'd say this looks more like textDocument/formatting is accidentally sent before textDocument/didChange (the didChange notification is debounced by 300 ms), so I think the server is doing the right thing. We should probably call purge_changes for the SessionBuffer before running the formatting request in https://github.com/sublimelsp/LSP/blob/c64ac00a1295fdc8461811033e0974443fd9ea55/plugin/formatting.py#L116 and in https://github.com/sublimelsp/LSP/blob/c64ac00a1295fdc8461811033e0974443fd9ea55/plugin/formatting.py#L177

Next time ideally also post a copy of the logs directly, that would be easier than trying to read the logs from the video :)

jwortmann avatar Dec 11 '24 16:12 jwortmann

Yes. I came to the same conclusion on discord where it was reported first

rchl avatar Dec 11 '24 17:12 rchl

I ran into a bug(?): with lsp_ruff and lsp_pyright installed,

    "lsp_format_on_save": true,
    "lsp_code_actions_on_save": {
        "source.fixAll": true,
        "source.organizeImports": true,
    },

try saving this py file

import csv
import tempfile
import shutil
import os
C = 1
class CsvExporter:
    D = os.DirEntry
    X = tempfile

becomes

import tempfile
import os
import shutil
import tempfile

    D = os.DirEntry
    X = tempfile

I was suggested on Discord that this might the same bug, any plan on fixing this?

du-song avatar Feb 21 '25 12:02 du-song

@du-song your issue is https://github.com/sublimelsp/LSP/issues/2510 and will be fixed in the next release.

rchl avatar Nov 09 '25 13:11 rchl