All files / src/frontend CompletionItemProvider.ts

0% Statements 0/16
0% Branches 0/2
0% Functions 0/6
0% Lines 0/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79                                                                                                                                                             
/*
 * This file is released under the MIT license.
 * Copyright (c) 2017, 2022, Mike Lischke
 *
 * See LICENSE file for more info.
 */
 
import {
    TextDocument, Position, CancellationToken, CompletionItem, ProviderResult, CompletionList,
} from "vscode";
import { AntlrFacade } from "../backend/facade";
import { translateCompletionKind } from "./Symbol";
 
// Determines the sort order in the completion list. One value for each SymbolKind.
const sortKeys = [
    "01", // Keyword
    "06", // TokenVocab
    "07", // Import
    "03", // BuiltInLexerToken
    "03", // VirtualLexerToken
    "03", // FragmentLexerToken
    "03", // LexerToken
    "05", // BuiltInMode
    "05", // LexerMode
    "02", // BuiltInChannel
    "02", // TokenChannel
    "04", // ParserRule
    "08", // Action
    "09", // Predicate
    "00", // Operator
    "10", // Option
];
 
// Descriptions for each symbol kind.
const details = [
    "Keyword",                // Keyword
    undefined,                // TokenVocab
    undefined,                // Import
    "Built-in lexer token",   // BuiltInLexerToken
    "Virtual lexer token",    // VirtualLexerToken
    "Fragment lexer token",   // FragmentLexerToken
    "Lexer token",            // LexerToken
    "Built-in lexer mode",    // BuiltInMode
    "Lexer mode",             // LexerMode
    "Built-in token channel", // BuiltInChannel
    "Token channel",          // TokenChannel
    "Parser rule",            // ParserRule
    "Action",                 // Action
    "Predicate",              // Predicate
    "Operator",               // Operators usually come with an own description from the backend.
    "Grammar option",
];
 
export class AntlrCompletionItemProvider {
    public constructor(private backend: AntlrFacade) { }
 
    public provideCompletionItems(document: TextDocument, position: Position,
        _token: CancellationToken): ProviderResult<CompletionList> {
 
        return new Promise((resolve, reject) => {
            this.backend.getCodeCompletionCandidates(document.fileName, position.character, position.line + 1)
                .then((candidates) => {
                    const completionList: CompletionItem[] = [];
                    candidates.forEach((info) => {
                        const item = new CompletionItem(info.name, translateCompletionKind(info.kind));
                        item.sortText = sortKeys[info.kind] + info.name;
                        item.detail = (info.description !== undefined) ? info.description : details[info.kind];
 
                        completionList.push(item);
                    });
 
                    resolve(new CompletionList(completionList, false));
                }).catch((reason) => {
                    reject(reason);
                });
        });
    }
}