简体   繁体   English

VSCode 扩展使用 executeDocumentSymbolProvider API

[英]VSCode Extension Using executeDocumentSymbolProvider API

I am writing a VSCode extension and want to use the executeDocumentSymbolProvider API to get the list of functions of a file.我正在编写一个 VSCode 扩展,并想使用executeDocumentSymbolProvider API 来获取文件的函数列表。
I mainly have 2 problems with how to use executeDocumentSymbolProvider :我主要有两个关于如何使用executeDocumentSymbolProvider的问题:

  1. How to correctly use executeDocumentSymbolProvider如何正确使用executeDocumentSymbolProvider
  2. How to resolve the returned Promise如何解决返回的Promise

How to correctly use executeDocumentSymbolProvider如何正确使用executeDocumentSymbolProvider
Below are the links I referenced to call executeDocumentSymbolProvider :下面是我引用来调用executeDocumentSymbolProvider的链接:

And this is how I call the API:这就是我如何称呼 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) .尽管这确实返回了Promise ,但我不禁想知道这是否是调用它的正确方法,因为从 VSCode API 文档看来我应该像let symbols = await vscode.commands.executeCommand<vscode.DocumentSymbol[]>('vscode.executeDocumentSymbolProvider',active.document.uri)一样调用它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如何解决返回的Promise
The following picture is the returned Promise .下图是返回的Promise 在此处输入图像描述 I have tried the following methods to resolve it but I still get Promise instead of symbols array as the return value.我尝试了以下方法来解决它,但我仍然得到Promise而不是符号数组作为返回值。

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.如果您以前使用executeDocumentSymbolProvider ,请告诉我。 Thank you!谢谢!

Be sure to install the C/C++ Extension Pack by Microsoft on VSCode for getting symbols from a c file.请务必在 VSCode 上安装 Microsoft 的 C/C++ 扩展包,以便从 c 文件中获取符号。 Otherwise, VSCode won't be able to find the symbols.否则,VSCode 将无法找到符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM