Skip to content

Odd filtering behavior when using CompletionItem range  #41208

@mjbvz

Description

@mjbvz

Problem
microsoft/TypeScript#20547 will allows js completions for:

const o = { 'k e y': 1 }
o.|

to be completed to:

const o = { 'k e y': 1 }
o['k e y']

This is accomplished by using a completion item that looks like:

{
    label: 'k e y',
    insertText: "['k e y']",
    range: range(1, 1, 1, 2)
}

The idea being that we replace the . when accepting the completion. However such a completion is currently filtered out by vs code

Repo

Create an extension:

import * as vscode from 'vscode';

export function activate(context: vscode.ExtensionContext) {
    vscode.languages.registerCompletionItemProvider('markdown', new Provider())
}

class Provider implements vscode.CompletionItemProvider {
    provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, context: vscode.CompletionContext): vscode.ProviderResult<vscode.CompletionItem[] | vscode.CompletionList> {
        const pre = document.lineAt(position.line).text.slice(0, position.character);
        const m = pre.match(/\.\w*$/);
        if (!m) {
            return;
        }
        
        const item: vscode.CompletionItem = {
            label: 's p a c e',
            insertText: "[s p a c e]",
            range: new vscode.Range(position.line, position.character - m[0].length, position.line, position.character)
        };
        return [item];
    }
}
  1. In an empty markdown file, type a. and then manually trigger completion.
  2. Note that completion for s p a c e is computed but not shown in the UI

Root cause seems to be that CompletionModel computes . as the word to use when filtering/sorting. As . does not match the entry, it is filtered out.

We can work around this by setting the filterText, but the current behavior was unexpected. Not sure if it is a bug or not

Metadata

Metadata

Assignees

Labels

*questionIssue represents a question, should be posted to StackOverflow (VS Code)suggestIntelliSense, Auto Complete

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions