Implement Go to Definition feature in my VS Code extension

31 views Asked by At

I am trying to implement Go to Definition feature in my VS Code extension. I was able to retrieve the location coordinates (start, end). But my cursor location is not getting updated I created a custom request from my client and this will return the coordinates

`connection.onRequest('custom/goToDefinition', (params): Location | null => {
        if (symbolAtPosition) {
            const location = convertSymbolToLocation(symbolAtPosition, docUri);
            let uri: string | undefined = location?.uri.toString();
            let range: Range = location?.range;
            const loc: Location = { uri: uri!, range };
            if (loc) {
                return loc;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    return null;
});
`

I am sending and receiving the request from the client as below. But they are not getting updated.

export class GoDefinitionProvider implements vscode.DefinitionProvider {
    constructor() {
        console.log('GoDefinitionProvider constructor called.');
    }
    public provideDefinition(
        document: TextDocument, position: Position, token: vscode.CancellationToken):
        Thenable<vscode.Location | null> {
        const locationPromise: Thenable<Location | null> = client.sendRequest('custom/goToDefinition', {
            textDocument: {
                uri: document.uri.toString(),
            },
            position,
        });

        return locationPromise.then((location: Location | null) => {
            if (location) {
                const vscodeLocation: vscode.Location = {
                    uri: vscode.Uri.parse(location.uri),
                    range: new vscode.Range(
                        location.range.start.line, location.range.start.character,
                        location.range.end.line, location.range.end.character
                    )
                };
                return vscodeLocation;
            } else {
                return null;
            }
        });
    }
export async function activate(context: ExtensionContext) {
    context.subscriptions.push(vscode.languages.registerDefinitionProvider(TMDL_MODE, new       GoDefinitionProvider()));
}`
0

There are 0 answers