1

I am writing a VSCode extension and want to use the executeDocumentSymbolProvider API to get the list of functions of a file.
I mainly have 2 problems with how to use executeDocumentSymbolProvider:

  1. How to correctly use executeDocumentSymbolProvider
  2. How to resolve the returned Promise

How to correctly use executeDocumentSymbolProvider
Below are the links I referenced to call executeDocumentSymbolProvider:

And this is how I call the API:

var active = vscode.window.activeTextEditor;
let symbols = await vscode.commands.executeCommand('vscode.executeDocumentSymbolProvider',active.document.uri);

Although this does return a Promise, I can't help but wonder if this is the correct way to invoke it because from the VSCode API doc it looks like I should invoke it like let symbols = await vscode.commands.executeCommand<vscode.DocumentSymbol[]>('vscode.executeDocumentSymbolProvider',active.document.uri). However I keep getting an error that says there should be an argument in the [].

How to resolve the returned Promise
The following picture is the returned Promise. enter image description here I have tried the following methods to resolve it but I still get Promise instead of symbols array as the return value.

async function(){
    var active = vscode.window.activeTextEditor;
    let symbols = await vscode.commands.executeCommand('vscode.executeDocumentSymbolProvider',active.document.uri);
}
var active = vscode.window.activeTextEditor;
vscode.commands.executeCommand('vscode.executeDocumentSymbolProvider',active.document.uri).then((symbols) => {
    if(symbols !== undefined){
        console.log(symbols);
    }
});
async function getFunctionList(textEditor){
    var active = vscode.window.activeTextEditor;
    return await new Promise((resolve, reject) => {
        setTimeout(function(){
            resolve(vscode.commands.executeCommand('vscode.executeDocumentSymbolProvider',active.document.uri));
        }, 1000);
    });
    
}

Please let me know if you have used executeDocumentSymbolProvider before. Thank you!

WhaleShark
  • 75
  • 6

1 Answers1

0

Be sure to install the C/C++ Extension Pack by Microsoft on VSCode for getting symbols from a c file. Otherwise, VSCode won't be able to find the symbols.

WhaleShark
  • 75
  • 6