All files / src/frontend CodeLensProvider.ts

0% Statements 0/27
0% Branches 0/10
0% Functions 0/7
0% Lines 0/27

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 80 81 82 83 84 85 86                                                                                                                                                                           
/*
 * This file is released under the MIT license.
 * Copyright (c) 2016, 2022 Mike Lischke
 *
 * See LICENSE file for more info.
 */
 
/* eslint-disable max-classes-per-file */
 
import {
    workspace, CodeLensProvider, TextDocument, CancellationToken, CodeLens, Range, EventEmitter, Event, ProviderResult,
} from "vscode";
import { AntlrFacade } from "../backend/facade";
import { ISymbolInfo, SymbolKind } from "../backend/types";
 
class SymbolCodeLens extends CodeLens {
    public constructor(public symbol: ISymbolInfo, range: Range) {
        super(range);
    }
}
 
export class AntlrCodeLensProvider implements CodeLensProvider {
    private changeEvent = new EventEmitter<void>();
    private documentName: string;
 
    public constructor(private backend: AntlrFacade) { }
 
    public get onDidChangeCodeLenses(): Event<void> {
        return this.changeEvent.event;
    }
 
    public refresh(): void {
        this.changeEvent.fire();
    }
 
    public provideCodeLenses(document: TextDocument, _token: CancellationToken): ProviderResult<CodeLens[]> {
        return new Promise((resolve) => {
            if (workspace.getConfiguration("antlr4.referencesCodeLens").enabled !== true) {
                resolve(null);
            } else {
                this.documentName = document.fileName;
                const symbols = this.backend.listTopLevelSymbols(document.fileName, false);
                const lenses = [];
                for (const symbol of symbols) {
                    Iif (!symbol.definition) {
                        continue;
                    }
 
                    switch (symbol.kind) {
                        case SymbolKind.FragmentLexerToken:
                        case SymbolKind.LexerRule:
                        case SymbolKind.LexerMode:
                        case SymbolKind.ParserRule: {
                            const range = new Range(
                                symbol.definition.range.start.row - 1,
                                symbol.definition.range.start.column,
                                symbol.definition.range.end.row - 1,
                                symbol.definition.range.end.column,
                            );
                            const lens = new SymbolCodeLens(symbol, range);
                            lenses.push(lens);
 
                            break;
                        }
 
                        default:
                    }
                }
 
                resolve(lenses);
            }
        });
    }
 
    public resolveCodeLens(codeLens: CodeLens, _token: CancellationToken): ProviderResult<CodeLens> {
        const refs = this.backend.countReferences(this.documentName, (codeLens as SymbolCodeLens).symbol.name);
        codeLens.command = {
            title: (refs === 1) ? "1 reference" : `${refs} references`,
            command: "",
            arguments: undefined,
        };
 
        return codeLens;
    }
}