rust-lang / rust-lang/rust-analyzer

`additionalTextEdits` used on the same line as the main `textEdit`

Open
#9,799 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

This issue was prompted by https://gitlab.gnome.org/GNOME/gnome-builder/-/issues/1406.

RA currently uses additionalTextEdits to remove certain things when completing snippets.

Example 1
fn main() {
    10.dbg|
    // Expands to dbg!(10)
}

The dbg completion looks like this:

Click to expand
{
    "label": "dbg",
    "kind": 15,
    "detail": "dbg!(expr)",
    "deprecated": false,
    "filterText": "dbg",
    "insertTextFormat": 2,
    "textEdit": {
        "range": {
            "start": {
                "line": 1,
                "character": 7
            },
            "end": {
                "line": 1,
                "character": 10
            }
        },
        "newText": "dbg!(10)"
    },
    "additionalTextEdits": [
        {
            "range": {
                "start": {
                    "line": 1,
                    "character": 4
                },
                "end": {
                    "line": 1,
                    "character": 7
                }
            },
            "newText": ""
        }
    ]
}

where the additional text edit removes the 10.

Example 2
trait T {
    fn hello();
}

Struct S;

impl T for S {
    fn| 
    // Expands to:
    // fn hello() {
    //
    // }
}

The completion looks similar to example 1, here the initial fn is removed.

Click to expand
{
    "label": "fn hello()",
    "kind": 3,
    "deprecated": false,
    "sortText": "ffffffff",
    "filterText": "hello",
    "insertTextFormat": 2,
    "textEdit": {
        "newText": "fn hello() {\n    $0\n}",
        "insert": {
            "start": {
                "line": 12,
                "character": 7
            },
            "end": {
                "line": 12,
                "character": 7
            }
        },
        "replace": {
            "start": {
                "line": 12,
                "character": 7
            },
            "end": {
                "line": 12,
                "character": 7
            }
        }
    },
    "additionalTextEdits": [
        {
            "range": {
                "start": {
                    "line": 12,
                    "character": 4
                },
                "end": {
                    "line": 12,
                    "character": 7
                }
            },
            "newText": ""
        }
    ]
}

The LSP Spec currently discourages using additionalTextEdits on the cursor line. (See documentation of additionalTextEdits.

Relevant issues:

Would it be possible to make those edits using only the main textEdit somehow?

Thanks!

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

Review the completion implementation that produces the shown textEdit and additionalTextEdits, then compare its behavior with the LSP completion-item specification and the two examples in this issue. Done means the same-line removals can be represented without additionalTextEdits on the cursor line while preserving the displayed completion results.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.