rust-lang / rust-lang/rust-analyzer

Didn't `SyntaxEditor::add_annotation` consider change offset?

Open
#20,524 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

I am working on an assist, but it seems that the SyntaxEditor::add_annotation #15710 has not taken into account any editing offset, which makes it difficult to use.

Is this normal?

|builder: &mut SourceChangeBuilder| {
    let mut edit = builder.make_editor(fn_.syntax());

    // Insert 2 byte whitespace
    edit.insert(Position::before(fn_.syntax()), make::tokens::whitespace("  "));

    if let Some(cap) = ctx.config.snippet_cap {
        let annotation = builder.make_tabstop_after(cap);
        edit.add_annotation(fn_.syntax(), annotation);
    }

    builder.add_file_edits(ctx.vfs_file_id(), edit);
},

Input:

impl Foo {
    fn foo(&self) -> &i32 {
        &self.0
    }
}

Current output:

impl Foo {
      fn foo(&self) -> &i32 {
        &self.0
   $0 }
}

Expect output:

impl Foo {
      fn foo(&self) -> &i32 {
        &self.0
    }$0
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at SyntaxEditor::add_annotation and reproduce the SourceChangeBuilder example from the issue, including the two-byte whitespace insertion. Trace how the editing offset is handled for the annotation, then verify that the tabstop appears after the closing brace as shown in the expected output.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.