Executing
vscode.executeDocumentSymbolProvider
gives me all symbols in the file.
Can I somehow get name of the function that the cursor is currently in?
Executing
vscode.executeDocumentSymbolProvider
gives me all symbols in the file.
Can I somehow get name of the function that the cursor is currently in?
If you only want the top-most outer function that the cursor is in, then the answer is fairly straightforward. If you wanted to consider multiple nested functions then it get much trickier.
Scan through the symbols. finding the symbol range that contains the cursor:
const symbols = await vscode.commands.executeCommand('vscode.executeDocumentSymbolProvider', document.uri);
const targetSymbol = Object.values(symbols).find(childSymbol => {
return childSymbol.kind === vscode.SymbolKind.Function) && childSymbol.range.contains(selection.active);
});
The childSymbol
's will be all the top-level symbols in your document.