简体   繁体   English

从 Hover 在 VSCode 扩展中打开另一个文档

[英]Open another document in VSCode extension from Hover

I try to open a document from a Hover in a VSCode Extension.我尝试从 VSCode 扩展中的悬停打开文档。

The Hover appears, the link is shown and also the URI, but when I click, nothing happens.出现悬停,显示链接和 URI,但是当我单击时,没有任何反应。 There is an output in the Debug Console, that the command is unknown in the Developer Tools Console.调试控制台中有一个输出,该命令在开发人员工具控制台中未知。

What I am doing wrong?我做错了什么? Here is the code, a little bit simplified这是代码,稍微简化了一点

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;
            }
        }));

Here is how the Hover renders:这是 Hover 的渲染方式:

在此处输入图片说明

Here is the output of the dev console:这是开发控制台的输出:

在此处输入图片说明

You should use a true command like vscode.open as documented in this article , or your own command.你应该使用一个真正的命令一样vscode.open在记录这篇文章,或您自己的命令。

window.showTextDocument alone is an extension API. window.showTextDocument本身就是一个扩展 API。

Lex Li pointed me into the right direction, thank you. Lex Li 为我指明了正确的方向,谢谢。

Wrapping the openTextDocument task into my own command and adressing this command from the Hover solves the problem:将 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);
        }));

Than composing the Hover use比编写 Hover 使用

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

did it.做到了。

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

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