Skip to content

Conversation

@A4-Tacks
Copy link
Member

@A4-Tacks A4-Tacks commented Dec 23, 2025

Editor adds the current indentation to the content of the code snippet

This PR dedentation is used to offset the editor snippet indentation

Fixes #13370

Example

fn foo(x: Option<i32>, y: Option<i32>) {
    let _f = || {
        x
            .and(y)
            .map(|it| it+2)
            .$0
    };
}

Before this PR

fn foo(x: Option<i32>, y: Option<i32>) {
    let _f = || {
        let $0 = x
                        .and(y)
                        .map(|it| it+2);
    };
}

After this PR

fn foo(x: Option<i32>, y: Option<i32>) {
    let _f = || {
        let $0 = x
            .and(y)
            .map(|it| it+2);
    };
}

Editor adds the current indentation to the content of the code snippet

This PR dedentation is used to offset the editor snippet indentation

Example
---
```rust
fn foo(x: Option<i32>, y: Option<i32>) {
    let _f = || {
        x
            .and(y)
            .map(|it| it+2)
            .$0
    };
}
```

**Before this PR**

```rust
fn foo(x: Option<i32>, y: Option<i32>) {
    let _f = || {
        let $0 = x
                        .and(y)
                        .map(|it| it+2);
    };
}
```

**After this PR**

```rust
fn foo(x: Option<i32>, y: Option<i32>) {
    let _f = || {
        let $0 = x
            .and(y)
            .map(|it| it+2);
    };
}
```
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 23, 2025
Copy link
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels a bit wrong to change the indentation of existing code, and it's not a severe issue since formatting will fix that anyway... Are all editors indenting like that?

@A4-Tacks
Copy link
Member Author

Are all editors indenting like that?

The language server protocol can adjust this behavior

  • InsertTextMode::AS_IS
  • InsertTextMode::ADJUST_INDENTATION

It feels a bit wrong to change the indentation of existing code

A dedentation is the least disruptive implementation

it's not a severe issue since formatting will fix that anyway...

Frequent formatting greatly affects the user experience,
And the range formatting does not work well when partial filling in the syntax tree

@DropDemBits
Copy link
Contributor

Small note that this would fixup #13370, thanks!

The language server protocol can adjust this behavior

  • InsertTextMode::AS_IS
  • InsertTextMode::ADJUST_INDENTATION

I'd rather go for the route for using InsertTextMode::AS_IS where possible instead of the default InsertTextMode::ADJUST_INDENTATION, that way we don't have to rely on the client editor's implementation of how it inserts indentation and we can likely just inspect the receiver text to the indentation needed to fixup multi-line snippets instead of looking at the entire file.

@A4-Tacks
Copy link
Member Author

we can likely just inspect the receiver text to the indentation needed to fixup multi-line snippets instead of looking at the entire file.

I didn't look at the entire file, I only extracted the indentation of the current line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Snippets don't dedent the reciever text

4 participants