VS Code 中的自定义语言自动补全

Tom*_*mek 7 autocomplete visual-studio-code

所以我想为一些自定义语言自定义 VS Code。我制作了一个带有片段的 .json 文件,这些片段是从我使用这种语言获得的所有 .inc 文件中解析出来的,但我更愿意将它实现到 IntelliSense 中。所以我的问题是,当我拥有包含所有全局变量、函数等的 .inc 文件时,如何创建自定义语言 IntelliSense 支持?我已经研究了几个小时,但找不到任何可以帮助我开始的东西。

小智 5

您需要创建一个语言服务器并向其添加代码完成功能。下面的示例代码向服务器添加了代码完成。它提出了“TypeScript”和“JavaScript”这两个词

// This handler provides the initial list of the completion items.
connection.onCompletion(
    (_textDocumentPosition: TextDocumentPositionParams): CompletionItem[] => {
        // The pass parameter contains the position of the text document in
        // which code complete got requested. For the example we ignore this
        // info and always provide the same completion items.
        return [
            {
                label: 'TypeScript',
                kind: CompletionItemKind.Text,
                data: 1
            },
            {
                label: 'JavaScript',
                kind: CompletionItemKind.Text,
                data: 2
            }
        ];
    }
);

// This handler resolve additional information for the item selected in
// the completion list.
connection.onCompletionResolve(
    (item: CompletionItem): CompletionItem => {
        if (item.data === 1) {
            (item.detail = 'TypeScript details'),
                (item.documentation = 'TypeScript documentation');
        } else if (item.data === 2) {
            (item.detail = 'JavaScript details'),
                (item.documentation = 'JavaScript documentation');
        }
        return item;
    }
);
Run Code Online (Sandbox Code Playgroud)