All files / src/frontend SymbolProvider.ts

0% Statements 0/24
0% Branches 0/8
0% Functions 0/3
0% Lines 0/24

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                                                                                                           
/*
 * This file is released under the MIT license.
 * Copyright (c) 2016, 2022, Mike Lischke
 *
 * See LICENSE file for more info.
 */
 
import {
    TextDocument, CancellationToken, Range, Location, Uri, SymbolInformation, DocumentSymbolProvider, ProviderResult,
} from "vscode";
 
import { AntlrFacade } from "../backend/facade";
import { SymbolKind } from "../backend/types";
import { symbolDescriptionFromEnum, translateSymbolKind } from "./Symbol";
 
export class AntlrSymbolProvider implements DocumentSymbolProvider {
    public constructor(private backend: AntlrFacade) { }
 
    public provideDocumentSymbols(document: TextDocument,
        _cancel: CancellationToken): ProviderResult<SymbolInformation[]> {
 
        return new Promise((resolve) => {
            const symbols = this.backend.listTopLevelSymbols(document.fileName, false);
            const symbolsList = [];
            for (const symbol of symbols) {
                Iif (!symbol.definition) {
                    continue;
                }
 
                const startRow = symbol.definition.range.start.row > 0 ? symbol.definition.range.start.row - 1 : 0;
                const endRow = symbol.definition.range.end.row > 0 ? symbol.definition.range.end.row - 1 : 0;
                const range = new Range(startRow, symbol.definition.range.start.column, endRow,
                    symbol.definition.range.end.column);
                const location = new Location(Uri.file(symbol.source), range);
 
                let description = symbolDescriptionFromEnum(symbol.kind);
                const kind = translateSymbolKind(symbol.kind);
                const totalTextLength = symbol.name.length + description.length + 1;
                Iif (symbol.kind === SymbolKind.LexerMode && totalTextLength < 80) {
                    // Add a marker to show parts which belong to a particular lexer mode.
                    // Not 100% perfect (i.e. right aligned, as symbol and description use different fonts),
                    // but good enough.
                    const markerWidth = 80 - totalTextLength;
                    description += " " + "-".repeat(markerWidth);
                }
                const info = new SymbolInformation(symbol.name, kind, description, location);
                symbolsList.push(info);
            }
 
            resolve(symbolsList);
        });
    }
}