Skip to content

Fix formatting triggering on swapping lines#2852

Merged
rchl merged 1 commit intomainfrom
fix/format-on-swap
Apr 8, 2026
Merged

Fix formatting triggering on swapping lines#2852
rchl merged 1 commit intomainfrom
fix/format-on-swap

Conversation

@rchl
Copy link
Copy Markdown
Member

@rchl rchl commented Apr 7, 2026

With a yaml file like:

services:
  foo:
    ports:
      - 80:80
      - 81:81
    container_name: bar

Place cursor on the container_name line and move the line up (cmd+ctrl+up on Mac).
Yaml server ends up adding dash where the cursor is.

Screen.Recording.2026-04-07.at.07.15.21.mov

Fix by identifying swap command as change action so the edit is not treated as type action.

Instead of explicit command name just look for swap_line to cater for different variants of the command including from third party packages (I'm using SmarterLineMoves which overrides swap commands with smart_* variants.

@rchl
Copy link
Copy Markdown
Member Author

rchl commented Apr 7, 2026

While this is obviously our bug that we trigger OnTypeFormatting in this case, I wonder whether the position we are sending to the server is actually not correct. Should we instead figure out position based on where the edit ends? Spec says:

/**
	 * The position around which the on type formatting should happen.
	 * This is not necessarily the exact position where the character denoted
	 * by the property `ch` got typed.
	 */
	position: [Position](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position);

which leaves quite a bit for interpretation.

@jwortmann
Copy link
Copy Markdown
Member

Should we instead figure out position based on where the edit ends?

I think using the caret position should be appropriate. So I would maybe have used selection.b here

**text_document_position_params(view, selection.a),

although it shouldn't make any difference because the selection region should always be empty while typing.

I guess the formulation with "around" from the docstring is because the edit could change text that is not directly at the caret position (like inserting f at the front of an f-string).

@rchl rchl merged commit 67f79e5 into main Apr 8, 2026
8 checks passed
@rchl rchl deleted the fix/format-on-swap branch April 8, 2026 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants