monaco-editor icon indicating copy to clipboard operation
monaco-editor copied to clipboard

[Bug] additionalTextEdits behave in an unexpected way

Open PhillippOhlandt opened this issue 3 years ago • 0 comments

Reproducible in vscode.dev or in VS Code Desktop?

  • [X] Not reproducible in vscode.dev or VS Code Desktop

Reproducible in the monaco editor playground?

Monaco Editor Playground Code

function createDependencyProposals(range) {
    return [
        {
            label: 'Test',
            kind: monaco.languages.CompletionItemKind.Function,
            documentation: 'Test',
            insertText: 'test',
            range: range,
            additionalTextEdits: [{
                text: '!! ', range: {
                    startLineNumber: range.startLineNumber,
                    endLineNumber: range.endLineNumber,
                    startColumn: range.startColumn - 2,
                    endColumn: range.startColumn
                }
            }],
        },
    ];
}

monaco.languages.register({ id: "testlang" });

monaco.languages.registerCompletionItemProvider('testlang', {
    triggerCharacters: [' '],
    provideCompletionItems: function (model, position) {
        range = {
            startLineNumber: position.lineNumber,
            endLineNumber: position.lineNumber,
            startColumn: position.column,
            endColumn: position.column
        };

        return {
            suggestions: createDependencyProposals(range)
        };
    }
});

monaco.editor.create(document.getElementById('container'), {
    value: 'some prefix text ?',
    language: 'testlang'
});

Actual Behavior

When typing whitespace to trigger the autocompletion and selecting "Test", the following text is generated:

some prefix text !!test

Expected Behavior

I would expect that the range of 2 columns gets replaced by !! and we end up with:

some prefix text !! test

It seems like it's only picking the first two characters from the provided text because the range is just 2 columns wide. Or the insertion stops because the test word starts and has "higher priority"?

Anyway, how would one tell the additionalTextEdits to override the specified range with the specified text.

Analogy: Selecting the range (2 characters) with the mouse and inserting the text (3 characters).

Additional Context

No response

PhillippOhlandt avatar Jul 01 '22 10:07 PhillippOhlandt