简体   繁体   English

开发 VSCode 扩展时获取当前活动文件的目录

[英]Get directory of Current active file when developing a VSCode extension

I am developing a VSCode extension to generate templates.我正在开发一个 VSCode 扩展来生成模板。

However, I cannot figure out how to get the location of the VSCode file that I am currently editing.但是,我无法弄清楚如何获取我当前正在编辑的 VSCode 文件的位置。

When I search for this question, most answers say to do something like vscode.window.activeTextEditor.document.uri.fsPath - however - this gets you the directory for the target VS code window (the Extension Development Host window) - which is not what I want当我搜索这个问题时,大多数答案都说要执行vscode.window.activeTextEditor.document.uri.fsPath类的操作 - 但是 - 这会为您提供目标 VS 代码 window (扩展开发主机窗口)的目录 - 这不是我想要的是

I want to get the directory of the VS code extension file I am editing (such as the extension.ts file).我想获取我正在编辑的VS代码扩展文件的目录(比如extension.ts文件)。

Any help greatly appreciated.非常感谢任何帮助。

If you mean the extension's path:如果您指的是扩展程序的路径:

context.extensionPath

For extension.ts try something like:对于extension.ts尝试类似:

context.asAbsolutePath("extension.ts") // if at top level context.asAbsolutePath("extension.ts") // 如果在顶层

If you don't know the directory structure of the file you are interested in, like src/otherDirectory/otherSubDirectory , I think you will have to search/walk for it with fs commands starting from the extensionPath .如果您不知道您感兴趣的文件的目录结构,例如src/otherDirectory/otherSubDirectory ,我认为您必须使用从extensionPath开始的fs命令搜索/遍历它。 See https://dustinpfister.github.io/2018/07/20/nodejs-ways-to-walk-a-file-system/ for example.例如,参见https://dustinpfister.github.io/2018/07/20/nodejs-ways-to-walk-a-file-system/

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

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