Skip to content

Commit e717a6f

Browse files
committed
Move shell integration ps1 into module
Part of microsoft#250000
1 parent ba80bf2 commit e717a6f

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

src/vs/platform/terminal/node/terminalEnvironment.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,10 @@ enum ShellIntegrationExecutable {
338338

339339
const shellIntegrationArgs: Map<ShellIntegrationExecutable, string[]> = new Map();
340340
// The try catch swallows execution policy errors in the case of the archive distributable
341-
shellIntegrationArgs.set(ShellIntegrationExecutable.WindowsPwsh, ['-noexit', '-command', 'try { . \"{0}\\out\\vs\\workbench\\contrib\\terminal\\common\\scripts\\shellIntegration.ps1\" } catch {}{1}']);
342-
shellIntegrationArgs.set(ShellIntegrationExecutable.WindowsPwshLogin, ['-l', '-noexit', '-command', 'try { . \"{0}\\out\\vs\\workbench\\contrib\\terminal\\common\\scripts\\shellIntegration.ps1\" } catch {}{1}']);
343-
shellIntegrationArgs.set(ShellIntegrationExecutable.Pwsh, ['-noexit', '-command', '. "{0}/out/vs/workbench/contrib/terminal/common/scripts/shellIntegration.ps1"{1}']);
344-
shellIntegrationArgs.set(ShellIntegrationExecutable.PwshLogin, ['-l', '-noexit', '-command', '. "{0}/out/vs/workbench/contrib/terminal/common/scripts/shellIntegration.ps1"']);
341+
shellIntegrationArgs.set(ShellIntegrationExecutable.WindowsPwsh, ['-noexit', '-command', 'try { Import-Module \"{0}\\out\\vs\\workbench\\contrib\\terminal\\common\\scripts\\shellIntegration.psm1\" } catch {}{1}']);
342+
shellIntegrationArgs.set(ShellIntegrationExecutable.WindowsPwshLogin, ['-l', '-noexit', '-command', 'try { Import-Module \"{0}\\out\\vs\\workbench\\contrib\\terminal\\common\\scripts\\shellIntegration.psm1\" } catch {}{1}']);
343+
shellIntegrationArgs.set(ShellIntegrationExecutable.Pwsh, ['-noexit', '-command', 'Import-Module "{0}/out/vs/workbench/contrib/terminal/common/scripts/shellIntegration.psm1"{1}']);
344+
shellIntegrationArgs.set(ShellIntegrationExecutable.PwshLogin, ['-l', '-noexit', '-command', 'Import-Module "{0}/out/vs/workbench/contrib/terminal/common/scripts/shellIntegration.psm1"']);
345345
shellIntegrationArgs.set(ShellIntegrationExecutable.Zsh, ['-i']);
346346
shellIntegrationArgs.set(ShellIntegrationExecutable.ZshLogin, ['-il']);
347347
shellIntegrationArgs.set(ShellIntegrationExecutable.Bash, ['--init-file', '{0}/out/vs/workbench/contrib/terminal/common/scripts/shellIntegration-bash.sh']);

src/vs/workbench/contrib/terminal/common/scripts/shellIntegration.ps1 renamed to src/vs/workbench/contrib/terminal/common/scripts/shellIntegration.psm1

Lines changed: 25 additions & 23 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:__VSCodeState.OriginalPrompt) {
7+
if (Test-Path variable:global:Script:__VSCodeState.OriginalPrompt) {
88
return;
99
}
1010

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

16-
$Global:__VSCodeState = @{
16+
$abc = "def"
17+
18+
$Script:__VSCodeState = @{
1719
OriginalPrompt = $function:Prompt
1820
LastHistoryId = -1
1921
IsInExecution = $false
@@ -24,21 +26,21 @@ $Global:__VSCodeState = @{
2426
}
2527

2628
# Store the nonce in script scope and unset the global
27-
$Global:__VSCodeState.Nonce = $env:VSCODE_NONCE
29+
$Script:__VSCodeState.Nonce = $env:VSCODE_NONCE
2830
$env:VSCODE_NONCE = $null
2931

30-
$Global:__VSCodeState.IsStable = $env:VSCODE_STABLE
32+
$Script:__VSCodeState.IsStable = $env:VSCODE_STABLE
3133
$env:VSCODE_STABLE = $null
3234

3335
$__vscode_shell_env_reporting = $env:VSCODE_SHELL_ENV_REPORTING
3436
$env:VSCODE_SHELL_ENV_REPORTING = $null
3537
if ($__vscode_shell_env_reporting) {
36-
$Global:__VSCodeState.EnvVarsToReport = $__vscode_shell_env_reporting.Split(',')
38+
$Script:__VSCodeState.EnvVarsToReport = $__vscode_shell_env_reporting.Split(',')
3739
}
3840
Remove-Variable -Name __vscode_shell_env_reporting -ErrorAction SilentlyContinue
3941

4042
$osVersion = [System.Environment]::OSVersion.Version
41-
$Global:__VSCodeState.IsWindows10 = $IsWindows -and $osVersion.Major -eq 10 -and $osVersion.Minor -eq 0 -and $osVersion.Build -lt 22000
43+
$Script:__VSCodeState.IsWindows10 = $IsWindows -and $osVersion.Major -eq 10 -and $osVersion.Minor -eq 0 -and $osVersion.Build -lt 22000
4244
Remove-Variable -Name osVersion -ErrorAction SilentlyContinue
4345

4446
if ($env:VSCODE_ENV_REPLACE) {
@@ -86,9 +88,9 @@ function Global:Prompt() {
8688
$Result = ""
8789
# Skip finishing the command if the first command has not yet started or an execution has not
8890
# yet begun
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) {
91+
if ($Script:__VSCodeState.LastHistoryId -ne -1 -and $Script:__VSCodeState.IsInExecution -eq $true) {
92+
$Script:__VSCodeState.IsInExecution = $false
93+
if ($LastHistoryEntry.Id -eq $Script:__VSCodeState.LastHistoryId) {
9294
# Don't provide a command line or exit code if there was no history entry (eg. ctrl+c, enter on no command)
9395
$Result += "$([char]0x1b)]633;D`a"
9496
}
@@ -107,34 +109,34 @@ function Global:Prompt() {
107109

108110
# Send current environment variables as JSON
109111
# OSC 633 ; EnvJson ; <Environment> ; <Nonce>
110-
if ($Global:__VSCodeState.EnvVarsToReport.Count -gt 0) {
112+
if ($Script:__VSCodeState.EnvVarsToReport.Count -gt 0) {
111113
$envMap = @{}
112-
foreach ($varName in $Global:__VSCodeState.EnvVarsToReport) {
114+
foreach ($varName in $Script:__VSCodeState.EnvVarsToReport) {
113115
if (Test-Path "env:$varName") {
114116
$envMap[$varName] = (Get-Item "env:$varName").Value
115117
}
116118
}
117119
$envJson = $envMap | ConvertTo-Json -Compress
118-
$Result += "$([char]0x1b)]633;EnvJson;$(__VSCode-Escape-Value $envJson);$($Global:__VSCodeState.Nonce)`a"
120+
$Result += "$([char]0x1b)]633;EnvJson;$(__VSCode-Escape-Value $envJson);$($Script:__VSCodeState.Nonce)`a"
119121
}
120122

121123
# Before running the original prompt, put $? back to what it was:
122124
if ($FakeCode -ne 0) {
123125
Write-Error "failure" -ea ignore
124126
}
125127
# Run the original prompt
126-
$OriginalPrompt += $Global:__VSCodeState.OriginalPrompt.Invoke()
128+
$OriginalPrompt += $Script:__VSCodeState.OriginalPrompt.Invoke()
127129
$Result += $OriginalPrompt
128130

129131
# Prompt
130132
# OSC 633 ; <Property>=<Value> ST
131-
if ($Global:__VSCodeState.IsStable -eq "0") {
133+
if ($Script:__VSCodeState.IsStable -eq "0") {
132134
$Result += "$([char]0x1b)]633;P;Prompt=$(__VSCode-Escape-Value $OriginalPrompt)`a"
133135
}
134136

135137
# Write command started
136138
$Result += "$([char]0x1b)]633;B`a"
137-
$Global:__VSCodeState.LastHistoryId = $LastHistoryEntry.Id
139+
$Script:__VSCodeState.LastHistoryId = $LastHistoryEntry.Id
138140
return $Result
139141
}
140142

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

157-
$Global:__VSCodeState.OriginalPSConsoleHostReadLine = $function:PSConsoleHostReadLine
159+
$Script:__VSCodeState.OriginalPSConsoleHostReadLine = $function:PSConsoleHostReadLine
158160
function Global:PSConsoleHostReadLine {
159-
$CommandLine = $Global:__VSCodeState.OriginalPSConsoleHostReadLine.Invoke()
160-
$Global:__VSCodeState.IsInExecution = $true
161+
$CommandLine = $Script:__VSCodeState.OriginalPSConsoleHostReadLine.Invoke()
162+
$Script:__VSCodeState.IsInExecution = $true
161163

162164
# Command line
163165
# OSC 633 ; E [; <CommandLine> [; <Nonce>]] ST
164166
$Result = "$([char]0x1b)]633;E;"
165167
$Result += $(__VSCode-Escape-Value $CommandLine)
166168
# Only send the nonce if the OS is not Windows 10 as it seems to echo to the terminal
167169
# sometimes
168-
if ($Global:__VSCodeState.IsWindows10 -eq $false) {
169-
$Result += ";$($Global:__VSCodeState.Nonce)"
170+
if ($Script:__VSCodeState.IsWindows10 -eq $false) {
171+
$Result += ";$($Script:__VSCodeState.Nonce)"
170172
}
171173
$Result += "`a"
172174

@@ -181,9 +183,9 @@ if (Get-Module -Name PSReadLine) {
181183
}
182184

183185
# Set ContinuationPrompt property
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")
186+
$Script:__VSCodeState.ContinuationPrompt = (Get-PSReadLineOption).ContinuationPrompt
187+
if ($Script:__VSCodeState.ContinuationPrompt) {
188+
[Console]::Write("$([char]0x1b)]633;P;ContinuationPrompt=$(__VSCode-Escape-Value $Script:__VSCodeState.ContinuationPrompt)`a")
187189
}
188190
}
189191

0 commit comments

Comments
 (0)