Mauricio Gracia Gutierrez
Mauricio Gracia Gutierrez

Reputation: 10844

How to get current text/symbol when implementing Go To Definition

I'm working on my first vscode extension using Language Server Protocol, I need to get the text were the Right click -> Go to definition was triggered

enter image description here

My current onDefinition method receives only the textPosition

export default class DefinitionFinder extends Handler {
    constructor(
        protected connection: Connection,
        private refManager: ReferenceManager)
    {
        super();

        this.connection.onDefinition(async (textPosition) => {
            return this.handleErrors(
                this.getDefinition(textPosition), undefined) as Promise<Definition>;
        });
    }
    
private async getDefinition(textPosition: TextDocumentPositionParams): Promise<Location[]> {
    const text = "funABC";

    // instead of hardcoded value I need to get the text/symbol 
    // that is going to be use to go to definition 

    return this.refManager.getDefinitionLocations(text);
}

The TextDocumentPositionParams only contains the documentUri, line(number) and character(number)

Does that mean that on each call to onDefinition I need to open the document, go to the line and character and get the current word ?

export interface TextDocumentPositionParams {
    /**
     * The text document.
     */
    textDocument: TextDocumentIdentifier;
    /**
     * The position inside the text document.
     */
    position: Position;
}

export interface TextDocumentIdentifier {
    /**
     * The text document's uri. (string)
     */
    uri: DocumentUri;
}

export declare namespace Position {
    /**
     * Creates a new Position literal from the given line and character.
     * @param line The position's line.
     * @param character The position's character.
     */

Upvotes: 2

Views: 942

Answers (3)

Mauricio Gracia Gutierrez
Mauricio Gracia Gutierrez

Reputation: 10844

This is how I ended up implementing the logic to get the word/symbol when "Go To Definition" is requested on a Language Server

I decided to return a LocationLink which also appears as a hyperlink over the word/symbol

this.connection.onDefinition(async (textPosition) => {
    return this.handleErrors(
        this.getDefinitionLink(textPosition), undefined) as Promise<LocationLink[]>;
});

This method get the symbol from the document and then look its definition in the refManager

private async getDefinitionLink(textPosition: TextDocumentPositionParams): Promise<LocationLink[]> {
    const symbol = this.getSymbolAtPosition(textPosition.position, textPosition.textDocument.uri);

    return this.refManager.getDefinitionLink(symbol);
}

This is the actual logic to extract the current word no matter if it was click in the middle of it.


let TokenSeparatorsXML = /[\t= <>"]/;

public getSymbolAtPosition(position: Position, documentUri: string): string {
    const range = {
        start: { line: position.line, character: 0},
        end: { line: position.line, character: Number.MAX_VALUE  }
    };
    //get the whole line 
    const txtDoc = this.refManager.getDocument(documentUri)!;

    const context = txtDoc.getText(range);
    const offset = position.character ;

    let start = offset-1;

    while ((start > 0) && !context[start].match(this.TokenSeparatorsXML))
    {
        start--;
    }

    let end = offset;

    while ((end < context.length) && !context[end].match(this.TokenSeparatorsXML))
    {
        end++;
    }

    const symbol = context.substr(start + 1, end - start - 1);

    console.log(`${start}->${end}- symbol: ${symbol}`);

    return symbol;
}

In my case the token separators are very narrow but you should consider using the word pattern https://code.visualstudio.com/api/language-extensions/language-configuration-guide#word-pattern

The full code of the extension can be found here - https://github.com/mauriciogracia/petrel-xml

Upvotes: 1

In my case, I implemented this:

let word = document.getText(document.getWordRangeAtPosition(position));

This works for the extension API

Upvotes: -1

Gama11
Gama11

Reputation: 34148

Language servers usually maintain a text document cache based on the document change events they receive. If you're writing the language server in TypeScript, you can simply use the implementation from the vscode-languageserver and vscode-languageserver-textdocument packages. Relevant excerpts from the official sample:

import { TextDocuments } from 'vscode-languageserver';
import { TextDocument } from 'vscode-languageserver-textdocument';

// Create a simple text document manager.
const documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);

...

// Make the text document manager listen on the connection
// for open, change and close text document events
documents.listen(connection);

Then you can just do:

documents.get(uri).getText(range)

Upvotes: 1

Related Questions