1

Executing

vscode.executeDocumentSymbolProvider

gives me all symbols in the file.

Can I somehow get name of the function that the cursor is currently in?

2
  • Are you concerned about nested functions? Which function do you want if you are in a nested function?
    – Mark
    Commented Apr 14 at 0:14
  • most outer function
    – mike27
    Commented Apr 15 at 8:47

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.