All files / src/frontend DefinitionProvider.ts

0% Statements 0/11
0% Branches 0/4
0% Functions 0/3
0% Lines 0/11

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                                                                                           
/*
 * This file is released under the MIT license.
 * Copyright (c) 2016, 2022, Mike Lischke
 *
 * See LICENSE file for more info.
 */
 
import {
    TextDocument, Position, CancellationToken, Range, Location, Uri, ProviderResult, DefinitionProvider,
} from "vscode";
import { AntlrFacade } from "../backend/facade";
 
export class AntlrDefinitionProvider implements DefinitionProvider {
    public constructor(private backend: AntlrFacade) { }
 
    public provideDefinition(document: TextDocument, position: Position,
        _token: CancellationToken): ProviderResult<Location> {
        return new Promise((resolve) => {
            const info = this.backend.symbolInfoAtPosition(document.fileName, position.character, position.line + 1,
                true);
 
            if (!info) {
                resolve(null);
            } else {
                // VS code shows the text for the range given here on holding ctrl/cmd, which is rather
                // useless given that we show this info already in the hover provider. So, in order
                // to limit the amount of text we only pass on the smallest range which is possible.
                // Yet we need the correct start position to not break the goto-definition feature.
                if (info.definition) {
                    const range = new Range(
                        info.definition.range.start.row - 1,
                        info.definition.range.start.column,
                        info.definition.range.end.row - 1,
                        info.definition.range.end.column,
                    );
 
                    resolve(new Location(Uri.file(info.source), range));
                } else {
                    // Empty for built-in entities.
                    resolve(new Location(Uri.parse(""), new Position(0, 0)));
                }
            }
        });
    }
}