registerCompletionItemProvider API来自VS Code扩展,不会触发provideCompletionItems。

huangapple go评论55阅读模式
英文:

registerCompletionItemProvider API from VS Code extension does not to trigger provideCompletionItems

问题

Here is the translation of the code snippet you provided:

嘿,我正在尝试动态注册一些 VS Code 片段。因此,我正在遍历多个文件并收集所有找到的片段。通过收集的数据,我为每个文件调用以下函数(我猜这可能效率不高,但是首先我想解决为什么根本没有显示片段的问题):

const registerCompletionItemProvider = (
    snippetLanguage: string | string[],
    snippetObject: any,
    name: string
) => {
    console.log(snippetLanguage);
    console.log(snippetObject);
    console.log(name); // this executed
    vscode.languages.registerCompletionItemProvider(
        // snippetLanguage,
        [{ scheme: 'file', language: 'javascript' }], // put this here for debugging
        {
            provideCompletionItems(document, position) {
                // this never runs
                const start = new vscode.Position(position.line, 0);
                const range = new vscode.Range(start, position);
                const completionItems = Object.keys(snippetObject).map((key) => {
                    const snippet = new vscode.SnippetString(snippetObject[key]);
                    const completionItem = new vscode.CompletionItem(
                        key,
                        vscode.CompletionItemKind.Snippet
                    );
                    completionItem.range = range;
                    completionItem.insertText = snippet;
                    return completionItem;
                });
                return completionItems;
            },
        },
        name
    );
};

不过,不知何故,在.js文件中似乎从未执行provideCompletionItems。我预期它应该在我输入片段的nameprefix时触发?

{
    "First": { // First would be one name
        "prefix": [
            "test1" // this would be the prefix 
        ],
        "body": [
            "function ${1:myFunction}($2) {",
            "\t$0",
            "}"
        ],
        "description": "Any Function"
    },
    "Secondary": {
        "prefix": [
            "test"
        ],
        "body": [
            "function ${1:myFunction}($2) {",
            "\t$0",
            "}"
        ],
        "description": "Any Function"
    }
}
英文:

Hey I am trying to register some snippets in VS Code dynamically. Therefore I am going over multiple files and collecting all snippets I find. With the Collected data I call then for every File this function (I guess this is pretty ineffective, however, I first want to solve the issue of why no snippets at all show up):

const registerCompletionItemProvider = (
    snippetLanguage: string | string[],
    snippetObject: any,
    name: string
) => {
    console.log(snippetLanguage);
    console.log(snippetObject);
    console.log(name); // this executed
    vscode.languages.registerCompletionItemProvider(
        // snippetLanguage,
        [{ scheme: 'file', language: 'javascript' }], // put this here for debugging
        {
            provideCompletionItems(document, position) {
                // this never runs
                const start = new vscode.Position(position.line, 0);
                const range = new vscode.Range(start, position);
                const completionItems = Object.keys(snippetObject).map((key) => {
                    const snippet = new vscode.SnippetString(snippetObject[key]);
                    const completionItem = new vscode.CompletionItem(
                        key,
                        vscode.CompletionItemKind.Snippet
                    );
                    completionItem.range = range;
                    completionItem.insertText = snippet;
                    return completionItem;
                });
                return completionItems;
            },
        },
        name
    );
};

However somehow, it seems that the provideCompletionItems is never executed in a .js. I expected that it should trigger when I enter the name of the Snippet or the prefix?

{
    "First": { // First would be one name
        "prefix": [
            "test1" // this would be the prefix 
        ],
        "body": [
            "function ${1:myFunction}($2) {",
            "\t$0",
            "}"
        ],
        "description": "Any Function"
    },
    "Secondary": {
        "prefix": [
            "test"
        ],
        "body": [
            "function ${1:myFunction}($2) {",
            "\t$0",
            "}"
        ],
        "description": "Any Function"
    }
}

答案1

得分: 1

这似乎是我对“triggerCharacter”是什么的误解,即“registerCompletionProvider”方法的第三个参数。

这必须是一组单字符字符串,通常是"."":"。当用户键入其中之一时,您的完成提供程序将被调用,就好像用户键入Ctrl+Space以显示建议的完成列表一样。然后,您的提供程序需要分析或解析当前位置周围的文本 - 在您的情况下,可以发现单词边界,然后提供匹配的完成项。例如,如果位置在“te”之间或紧接在其后,返回“test1”和“test”的代码片段。

英文:

This looks to me like a misunderstanding of what a triggerCharacter is, i.e. the third argument of the registerCompletionProvider method.

This has to be a list of single-character strings, usually "." or ":". When the user types one of these, your completion provider would be called, just as if the user typed Ctrl+Space to bring up a list of suggested completions. Your provider then needs to analyze or parse the text around the current position -- In your case something that discovers word boundarys and then provide matching completion items. For example if the position is somewhere on or right after "te", return the snippets for "test1" and "test".

huangapple
  • 本文由 发表于 2023年5月7日 01:24:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190204.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定