【问题标题】:Open another document in VSCode extension from Hover从悬停在 VSCode 扩展中打开另一个文档
【发布时间】:2021-07-09 15:00:38
【问题描述】:

我尝试从 VSCode 扩展中的悬停打开文档。

出现悬停,显示链接以及 URI,但是当我单击时,什么也没有发生。调试控制台中有一个输出,开发者工具控制台中的命令是未知的。

我做错了什么?这是代码,稍微简化了一点

context.subscriptions.push(
        vscode.languages.registerHoverProvider({pattern: '**/*.{ttp,tts}'}, {
            provideHover(document, position, token) {
                
                const linkPosition = new vscode.Position(10, 1);
                const range = new vscode.Range(position, position);
                
                const opts: vscode.TextDocumentShowOptions = {
                    selection: range,
                    viewColumn: vscode.ViewColumn.Beside
                };
                
                const workspace = vscode.workspace.workspaceFolders?.find(e => e.uri.fsPath.endsWith("workspace"));
                const uri = vscode.Uri.file(`${workspace?.uri.path}/_global.tt/ercdata/ttc.properties`);

                const args = [{ uri: uri , options: opts}];  

                const stageCommandUri = vscode.Uri.parse(
                    `command:window.showTextDocument?${encodeURIComponent(JSON.stringify(args))}`
                );
                let link = new vscode.MarkdownString(`[Open...](${stageCommandUri})`);
                link.isTrusted = true;

                let hover: vscode.Hover = {
                    contents: [link]
                };
                return hover;

                let x = properties.getHoverFor(document, position, path.basename(document.uri.fsPath).replace(".tts","").replace(".ttp","").toLowerCase()); 
                return  x;
            }
        }));

Hover 的渲染方式如下:

这是开发控制台的输出:

【问题讨论】:

    标签: visual-studio-code vscode-extensions


    【解决方案1】:

    您应该使用像this article 中所述的vscode.open 之类的真正命令,或者您自己的命令。

    window.showTextDocument 本身就是一个扩展 API。

    【讨论】:

    • 来自文档:vscode.open - 在编辑器中打开提供的资源...如果您需要更多地控制打开文本文件的选项,请改用 vscode.window.showTextDocument。我试试看,谢谢!
    【解决方案2】:

    Lex Li 为我指明了正确的方向,谢谢。

    将 openTextDocument 任务包装到我自己的命令中并从 Hover 处理此命令可以解决问题:

    context.subscriptions.push(vscode.commands.registerCommand('estudio.internal.open', (uri: vscode.Uri, options: vscode.TextDocumentShowOptions) => {
                logger.info("Opening a document");
                vscode.window.showTextDocument(uri, options);
            }));
    

    比编写悬停使用

    const stageCommandUri = vscode.Uri.parse(
                        `command:estudio.internal.open?${encodeURIComponent(JSON.stringify(args))}`
    

    做到了。

    【讨论】:

      猜你喜欢
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      • 2021-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多