[英]How to open a local (inside the extension package) file in a vscode extension
I am creating a vscode extension.我正在创建一个 vscode 扩展。 I want to provide a
help
command which, when invoked, opens a help.md
file that is provided by the extension and therefore, I assume, shipped as part of the extension package.我想提供一个
help
命令,当被调用时,它会打开一个由扩展提供的help.md
文件,因此我假设它是作为扩展 package 的一部分提供的。
myext/
├── .vscode/
├── test/
├── package.json
├── extension.js
├── help.md
How can I get a reference to the extension package content and open file help.md
in the editor where the extension is running?如何获取对扩展 package 内容的引用并在运行扩展的编辑器中打开文件
help.md
?
You can get the path to any file in your extension using the extension context.您可以使用扩展上下文获取扩展中任何文件的路径。 Here's a function I use in my extension to determine the absoute path to files I want to load:
这是我在扩展中使用的 function 来确定要加载的文件的绝对路径:
/**
* Returns the absolute path to a file located in our misc folder.
*
* @param file The base file name.
* @param context The context of this extension to get its path regardless where it is installed.
* @param webview When given format the path for use in this webview.
*/
public static getMiscPath(file: string, context: ExtensionContext, webView?: Webview): string {
if (webView) {
let uri = Uri.file(context.asAbsolutePath(path.join('misc', file)));
return webView.asWebviewUri(uri).toString();
}
return context.asAbsolutePath(path.join('misc', file));
}
This function determines 2 different variants of the path:这个 function 确定了 2 个不同的路径变体:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.