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
:
- How to correctly use
executeDocumentSymbolProvider
- How to resolve the returned
Promise
How to correctly use executeDocumentSymbolProvider
Below are the links I referenced to call executeDocumentSymbolProvider
:
- https://code.visualstudio.com/api/references/commands
- https://code.visualstudio.com/api/extension-guides/command
- https://www.reddit.com/r/vscode/comments/oxnhna/get_all_symbols_of_the_current_document_in_vscode/
- How can I implement my own code outline layout in vscode?
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
.
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!