简体   繁体   English

终端窗口和 Vs 代码变量

[英]Terminal Window and Vs Code Variables

I'm trying to retrieve vs code variable values from terminal window in VS Code, but all of them return an empty string.我正在尝试从 VS Code 的终端窗口中检索 VS Code 变量值,但它们都返回一个空字符串。 I need for example, get the name of the file opened in the vscode editor (from a var)例如,我需要获取在 vscode 编辑器中打开的文件的名称(来自 var)

"Ex: ${fileBasename} - the current opened file's basename" “例如:${fileBasename} - 当前打开文件的基本名称”

This is the article I found for these vars, but all return empty:这是我为这些变量找到的文章,但都返回空:

https://code.visualstudio.com/docs/editor/variables-reference https://code.visualstudio.com/docs/editor/variables-reference

Thank you and regards谢谢和问候

Xabier沙比尔

The VSCode-native variables are only resolved in configs and settings files, but they are not automatically available in any hosted terminal. VSCode 原生变量仅在配置和设置文件中解析,但它们不会在任何托管终端中自动可用。

That being said, the integrated terminal that comes with the official PowerShell language extension hooks into VSCode's editor API and exposes it via the $psEditor automatic variable:话虽如此,官方PowerShell 语言扩展附带的集成终端挂钩到 VSCode 的编辑器 API,并通过$psEditor自动变量公开它:

# obtain editor context handle
$editorCtx = $psEditor.GetEditorContext()

# get current open file's path, infer name from path
$fileBasename = Split-Path $editorCtx.CurrentFile.Path -Child

# This now works
"Ex: ${fileBasename} - the current opened file's basename"

Beware that the editor context object is a snapshot - if you want to know the current file open in the main editor after some time has passed you have to call GetEditorContext() again.请注意,编辑器上下文对象是一个快照- 如果您想知道经过一段时间后在主编辑器中打开的当前文件,您必须再次调用GetEditorContext()

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

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