Execute “go to Symbol in File” programmatically in vscode?

微笑、不失礼 提交于 2019-12-20 05:39:09

问题


Is it possible to jump to an existing symbol in file from Extension?

Something of the sort:

goToSymbol(symbol: string)

P.S. I don't want to provide a DocumentSymbolProvider. How to use existing ones?


回答1:


async function getSymbols(document: TextDocument): Promise<DocumentSymbol[]> {
    return await commands.executeCommand<DocumentSymbol[]>('vscode.executeDocumentSymbolProvider', document.uri) || [];
}

async function goToSymbol(document: TextDocument, symbolName: string) {
    const symbols = await getSymbols(document);
    const findSymbol = symbols.find(symbol => symbol.name === symbolName);
    const activeTextEditor = window.activeTextEditor;
    if (findSymbol && activeTextEditor) {
        activeTextEditor.revealRange(findSymbol.range, vscode.TextEditorRevealType.AtTop);
        activeTextEditor.selection = new Selection(findSymbol.range.start, findSymbol.range.start);
    }
}

Note: the code above should be enough to go to symbol of 1 lvl.

Nested symbols accessible as .children (on every element of symbols)



来源:https://stackoverflow.com/questions/54053373/execute-go-to-symbol-in-file-programmatically-in-vscode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!