Skip to content

Commit 768477e

Browse files
authored
Consolidate ps1 shell integration vars into __VSCodeState (microsoft#251114)
* Consolidate vscode_ps1 var into __VSCodeState * Remove comments
1 parent 5a79cf4 commit 768477e

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

src/vs/workbench/contrib/terminal/common/scripts/shellIntegration.ps1

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# ---------------------------------------------------------------------------------------------
55

66
# Prevent installing more than once per session
7-
if (Test-Path variable:global:__VSCodeOriginalPrompt) {
7+
if (Test-Path variable:global:__VSCodeState.OriginalPrompt) {
88
return;
99
}
1010

@@ -13,27 +13,33 @@ if ($ExecutionContext.SessionState.LanguageMode -ne "FullLanguage") {
1313
return;
1414
}
1515

16-
$Global:__VSCodeOriginalPrompt = $function:Prompt
17-
18-
$Global:__LastHistoryId = -1
19-
$Global:__VSCodeIsInExecution = $false
16+
$Global:__VSCodeState = @{
17+
OriginalPrompt = $function:Prompt
18+
LastHistoryId = -1
19+
IsInExecution = $false
20+
EnvVarsToReport = @()
21+
Nonce = $null
22+
IsStable = $null
23+
IsWindows10 = $false
24+
}
2025

2126
# Store the nonce in script scope and unset the global
22-
$Nonce = $env:VSCODE_NONCE
27+
$Global:__VSCodeState.Nonce = $env:VSCODE_NONCE
2328
$env:VSCODE_NONCE = $null
2429

25-
$isStable = $env:VSCODE_STABLE
30+
$Global:__VSCodeState.IsStable = $env:VSCODE_STABLE
2631
$env:VSCODE_STABLE = $null
2732

2833
$__vscode_shell_env_reporting = $env:VSCODE_SHELL_ENV_REPORTING
2934
$env:VSCODE_SHELL_ENV_REPORTING = $null
30-
$Global:envVarsToReport = @()
3135
if ($__vscode_shell_env_reporting) {
32-
$Global:envVarsToReport = $__vscode_shell_env_reporting.Split(',')
36+
$Global:__VSCodeState.EnvVarsToReport = $__vscode_shell_env_reporting.Split(',')
3337
}
38+
Remove-Variable -Name __vscode_shell_env_reporting -ErrorAction SilentlyContinue
3439

3540
$osVersion = [System.Environment]::OSVersion.Version
36-
$isWindows10 = $IsWindows -and $osVersion.Major -eq 10 -and $osVersion.Minor -eq 0 -and $osVersion.Build -lt 22000
41+
$Global:__VSCodeState.IsWindows10 = $IsWindows -and $osVersion.Major -eq 10 -and $osVersion.Minor -eq 0 -and $osVersion.Build -lt 22000
42+
Remove-Variable -Name osVersion -ErrorAction SilentlyContinue
3743

3844
if ($env:VSCODE_ENV_REPLACE) {
3945
$Split = $env:VSCODE_ENV_REPLACE.Split(":")
@@ -80,9 +86,9 @@ function Global:Prompt() {
8086
$Result = ""
8187
# Skip finishing the command if the first command has not yet started or an execution has not
8288
# yet begun
83-
if ($Global:__LastHistoryId -ne -1 -and $Global:__VSCodeIsInExecution -eq $true) {
84-
$Global:__VSCodeIsInExecution = $false
85-
if ($LastHistoryEntry.Id -eq $Global:__LastHistoryId) {
89+
if ($Global:__VSCodeState.LastHistoryId -ne -1 -and $Global:__VSCodeState.IsInExecution -eq $true) {
90+
$Global:__VSCodeState.IsInExecution = $false
91+
if ($LastHistoryEntry.Id -eq $Global:__VSCodeState.LastHistoryId) {
8692
# Don't provide a command line or exit code if there was no history entry (eg. ctrl+c, enter on no command)
8793
$Result += "$([char]0x1b)]633;D`a"
8894
}
@@ -101,34 +107,34 @@ function Global:Prompt() {
101107

102108
# Send current environment variables as JSON
103109
# OSC 633 ; EnvJson ; <Environment> ; <Nonce>
104-
if ($Global:envVarsToReport.Count -gt 0) {
110+
if ($Global:__VSCodeState.EnvVarsToReport.Count -gt 0) {
105111
$envMap = @{}
106-
foreach ($varName in $envVarsToReport) {
112+
foreach ($varName in $Global:__VSCodeState.EnvVarsToReport) {
107113
if (Test-Path "env:$varName") {
108114
$envMap[$varName] = (Get-Item "env:$varName").Value
109115
}
110116
}
111117
$envJson = $envMap | ConvertTo-Json -Compress
112-
$Result += "$([char]0x1b)]633;EnvJson;$(__VSCode-Escape-Value $envJson);$Nonce`a"
118+
$Result += "$([char]0x1b)]633;EnvJson;$(__VSCode-Escape-Value $envJson);$($Global:__VSCodeState.Nonce)`a"
113119
}
114120

115121
# Before running the original prompt, put $? back to what it was:
116122
if ($FakeCode -ne 0) {
117123
Write-Error "failure" -ea ignore
118124
}
119125
# Run the original prompt
120-
$OriginalPrompt += $Global:__VSCodeOriginalPrompt.Invoke()
126+
$OriginalPrompt += $Global:__VSCodeState.OriginalPrompt.Invoke()
121127
$Result += $OriginalPrompt
122128

123129
# Prompt
124130
# OSC 633 ; <Property>=<Value> ST
125-
if ($isStable -eq "0") {
131+
if ($Global:__VSCodeState.IsStable -eq "0") {
126132
$Result += "$([char]0x1b)]633;P;Prompt=$(__VSCode-Escape-Value $OriginalPrompt)`a"
127133
}
128134

129135
# Write command started
130136
$Result += "$([char]0x1b)]633;B`a"
131-
$Global:__LastHistoryId = $LastHistoryEntry.Id
137+
$Global:__VSCodeState.LastHistoryId = $LastHistoryEntry.Id
132138
return $Result
133139
}
134140

@@ -148,19 +154,19 @@ elseif ((Test-Path variable:global:GitPromptSettings) -and $Global:GitPromptSett
148154
if (Get-Module -Name PSReadLine) {
149155
[Console]::Write("$([char]0x1b)]633;P;HasRichCommandDetection=True`a")
150156

151-
$__VSCodeOriginalPSConsoleHostReadLine = $function:PSConsoleHostReadLine
157+
$Global:__VSCodeState.OriginalPSConsoleHostReadLine = $function:PSConsoleHostReadLine
152158
function Global:PSConsoleHostReadLine {
153-
$CommandLine = $__VSCodeOriginalPSConsoleHostReadLine.Invoke()
154-
$Global:__VSCodeIsInExecution = $true
159+
$CommandLine = $Global:__VSCodeState.OriginalPSConsoleHostReadLine.Invoke()
160+
$Global:__VSCodeState.IsInExecution = $true
155161

156162
# Command line
157163
# OSC 633 ; E [; <CommandLine> [; <Nonce>]] ST
158164
$Result = "$([char]0x1b)]633;E;"
159165
$Result += $(__VSCode-Escape-Value $CommandLine)
160166
# Only send the nonce if the OS is not Windows 10 as it seems to echo to the terminal
161167
# sometimes
162-
if ($IsWindows10 -eq $false) {
163-
$Result += ";$Nonce"
168+
if ($Global:__VSCodeState.IsWindows10 -eq $false) {
169+
$Result += ";$($Global:__VSCodeState.Nonce)"
164170
}
165171
$Result += "`a"
166172

@@ -175,9 +181,9 @@ if (Get-Module -Name PSReadLine) {
175181
}
176182

177183
# Set ContinuationPrompt property
178-
$ContinuationPrompt = (Get-PSReadLineOption).ContinuationPrompt
179-
if ($ContinuationPrompt) {
180-
[Console]::Write("$([char]0x1b)]633;P;ContinuationPrompt=$(__VSCode-Escape-Value $ContinuationPrompt)`a")
184+
$Global:__VSCodeState.ContinuationPrompt = (Get-PSReadLineOption).ContinuationPrompt
185+
if ($Global:__VSCodeState.ContinuationPrompt) {
186+
[Console]::Write("$([char]0x1b)]633;P;ContinuationPrompt=$(__VSCode-Escape-Value $Global:__VSCodeState.ContinuationPrompt)`a")
181187
}
182188
}
183189

0 commit comments

Comments
 (0)