【问题标题】:async call result provides ranges and command for codelens异步调用结果为 codelens 提供范围和命令
【发布时间】:2021-12-31 15:20:12
【问题描述】:

ProvideCodeLens 声明:

此调用应尽快返回,如果计算命令成本高昂,则实施者应仅返回具有范围集的代码镜头对象并实施解析。

我对 API 的异步网络调用返回范围和我放入 CodeLens 的信息,例如:

[{
      "stenographyResult": {
        "data": "...",
      },
      "startPosition": {
        "row": 9,
        "column": 3
      },
      "endPosition": {
        "row": 11,
        "column": 4
      }
 }]

其中startPositionendPosition 分别是代码块的开始行和结束行,data 用于命令。

这似乎成为一个阻塞调用,如果此调用需要时间返回,VSC 将停止,并导致 VSC 上的其他功能(如文件创建)停止。

如果进行异步调用,返回 arrayranges 和类似上面的命令,我们怎样才能使这个调用不停止整个编辑器,最好使用 resolveCodeLens(但我们不知道范围直到网络调用完成)

相关代码:

public provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): vscode.CodeLens[] | Thenable<vscode.CodeLens[]> {        
        if (vscode.workspace.getConfiguration().get('stenography.codeLensMode')) {
            ...
                return vscode.window.withProgress({
                    location: vscode.ProgressLocation.Window,
                    cancellable: false,
                    title: 'Fetching from Stenography Autopilot'
                }, async (progress) => {
                    try {
                        progress.report({ increment: 0 });
                        return fetchStenographyAutopilot(STENOGRAPHY_API_KEY!, document.getText(), language, false).then((data: any) => {
                            try {
                                 ...
                                data.code_blocks.forEach((block: any) => {
                                    var firstLine = document.lineAt(block.startPosition.row - 1);
                                    var textRange = new vscode.Range(firstLine.range.start, firstLine.range.end);
                                    let command = {
                                        title: "<stenography autopilot />",
                                        tooltip: block.stenographyResult.pm,
                                        command: "stenography.codelensAction",
                                        arguments: [block.stenographyResult.pm]
                                    };
                                    this.codeLenses.push(new vscode.CodeLens(textRange, command));
                                    this.cache.codeLensCache[filename]!.push({
                                        boundTo: block.stenographyResult.code,
                                        command: command
                                    });
                                });
            
                                return this.codeLenses;
                            } 
                           ...
    }

完整文件:https://github.com/StenographyDev/autopilot-vsc/blob/main/src/CodeLensProvider.ts

【问题讨论】:

    标签: typescript visual-studio-code async-await


    【解决方案1】:

    我不确定为什么这会导致 VS Code 停止,但这听起来可能是一个错误。首先尝试确认您的扩展中没有任何长时间运行的同步操作阻塞事物。如果仍然无法正常工作,请整理一个演示问题的最小示例,并针对 vscode 提交问题。

    但是文档也是正确的:ProvideCodeLenses 需要尽快返回位置数据,而这不是您可以真正解决的问题。尝试尽可能加快 api 调用,或者如果不能,请尝试不同的方法。例如,也许您可​​以快速估计代码镜头会出现在哪里?

    【讨论】:

    • 感谢您的回答,马特! provideCodeLens 本身是一个阻塞函数吗?比如,它在调用时会消耗资源吗?
    • 如果您的provideCodeLens 实现触发了长时间运行的同步操作,它可能会阻止扩展主机。但是,这通常不应阻塞 UI 或冻结编辑器
    猜你喜欢
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多