typescript#CancellationToken TypeScript Examples

The following examples show how to use typescript#CancellationToken. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: yakCompletionSchema.ts    From yakit with GNU Affero General Public License v3.0 5 votes vote down vote up
yaklangCompletionHandlerProvider = (model: editor.ITextModel, position: Position, context: languages.CompletionContext, token: CancellationToken) => {
    const beforeCursor = position.column - 1;
    const line = model.getLineContent(position.lineNumber).substr(0, beforeCursor <= 0 ? 0 : beforeCursor);
    const words = Array.from(line.matchAll(/\w+/g), m => m[0]);
    const lastWord = words.length > 0 ? words[words.length - 1] : "";
    // 设置补全
    let items: languages.CompletionItem[] = [];

    const libCompletions = (completions.libCompletions || []);
    for (let i = 0; i < libCompletions.length; i++) {
        const currentLib = libCompletions[i];
        if (line.endsWith(currentLib.libName + ".")) {
            currentLib.functions.forEach(f => {
                items.push({
                    insertText: f.functionName,
                    detail: f.definitionStr,
                    label: f.functionName,
                    kind: languages.CompletionItemKind.Snippet,
                    insertTextRules: languages.CompletionItemInsertTextRule.InsertAsSnippet,
                    range: {
                        startLineNumber: position.lineNumber,
                        endLineNumber: position.lineNumber,
                        startColumn: position.column || 0,
                        endColumn: position.column || 0,
                    }
                })
            })
            return {
                suggestions: items,
            }
        }
    }

    if (items.length <= 0) {
        return {
            suggestions: completions.libNames.filter(i => {
                return i.includes(lastWord)
            }).map(i => {
                const startColumn = position.column - lastWord.length;
                return {
                    insertText: i,
                    detail: i,
                    label: i,
                    kind: languages.CompletionItemKind.Struct,
                    range: {
                        startLineNumber: position.lineNumber,
                        endLineNumber: position.lineNumber,
                        startColumn: startColumn > 0 ? startColumn : 0,
                        endColumn: position.column || 0,
                    }
                }
            })
        }
    }

    // 如果补全函数失败,则会认为
    return {
        suggestions: []
    }
}