我尝试在VSCode扩展中从Hover打开一个文档。
此时将出现悬停,显示链接和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;
}
}));
下面是悬停的渲染方式:
以下是dev控制台的输出:
发布于 2021-07-09 15:42:33
您应该使用一个真正的命令,如this article中记录的vscode.open
,或者使用您自己的命令。
window.showTextDocument
本身就是一个扩展应用程序接口。
发布于 2021-07-10 04:47:12
李雷克斯为我指明了正确的方向,谢谢。
将openTextDocument任务封装到我自己的命令中,并从悬停中寻址此命令解决了这个问题:
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);
}));
比编写Hover的使用
const stageCommandUri = vscode.Uri.parse(
`command:estudio.internal.open?${encodeURIComponent(JSON.stringify(args))}`
做到了。
https://stackoverflow.com/questions/68318892
复制