简体   繁体   中英

vscode how to automatically jump to proper definition

//extension.ts
export function activate(context: vscode.ExtensionContext) {
    console.log('Congratulations, your extension "helloworld" is now active!');

    context.subscriptions.push(vscode.languages.registerDefinitionProvider(
        {language: "plsql"}, new GoDefinitionProvider() ));

}

class GoDefinitionProvider implements vscode.DefinitionProvider {
    public provideDefinition(
        document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken):
        Thenable<vscode.Definition>{
            return new Promise((resolve, reject) =>{
                let definitions:vscode.Definition = [];         
                for (let i = 0; i < document.lineCount; i++) {
                    let eachLine = document.lineAt(i).text.toLowerCase().trim();
                    if (eachLine.startsWith("cursor")) {                    
                        definitions.push({
                            uri: document.uri,
                            range: document.lineAt(i).range
                        });                     
                    }                   
                } 

                resolve(definitions);
            });

    }
}
// testing.txt
cursor a1 is
cursor a2 is
cursor a3 is
cursor a4 is
cursor a5 is
cursor a6 is
cursor a7 is
cursor a8 is
cursor a9 is

a1
a2
a3
a4
a5
a6
a7
a8
a9

for example, now we want to select "a4 " to jump to show definition, but the definition is auto jump to "cursor a9 is", not the proper one "cursor a4 is".

result image: https://i.imgur.com/RNAvWMN.png

how can I implement the auto jumping to the proper definition? Above is the source code of extension.ts for your reference.

Problem in your code is, that you return array of Definitions, not single Definition Location.

return new Promise((resolve, reject) =>{
    const range = document.getWordRangeAtPosition(position);
    const selectedWord = document.getText(range);
    let definitions:vscode.Definition = [];         
    for (let i = 0; i < document.lineCount; i++) {
        let eachLine = document.lineAt(i).text.toLowerCase().trim();
        if (eachLine.startsWith("cursor")) { 
            if ( eachLine.includes(selectedWord))  //only selectedWord                  
                definitions.push({
                    uri: document.uri,
                    range: document.lineAt(i).range
                });                     
            }
        }                   
    } 
    resolve(definitions);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM