简体   繁体   中英

Open another document in VSCode extension from Hover

I try to open a document from a Hover in a VSCode Extension.

The Hover appears, the link is shown and also the URI, but when I click, nothing happens. 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:

在此处输入图片说明

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.

window.showTextDocument alone is an extension API.

Lex Li pointed me into the right direction, thank you.

Wrapping the openTextDocument task into my own command and adressing this command from the Hover solves the problem:

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

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

did it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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