|
1 | 1 | import { PythonCommandRunConfiguration, PythonEnvironment } from '../../../../api'; |
2 | 2 | import { traceInfo } from '../../../../common/logging'; |
| 3 | +import { getGlobalPersistentState } from '../../../../common/persistentState'; |
| 4 | +import { sleep } from '../../../../common/utils/asyncUtils'; |
3 | 5 | import { isWindows } from '../../../../common/utils/platformUtils'; |
4 | 6 | import { activeTerminalShellIntegration } from '../../../../common/window.apis'; |
| 7 | +import { getConfiguration } from '../../../../common/workspace.apis'; |
5 | 8 | import { ShellConstants } from '../../../common/shellConstants'; |
6 | 9 | import { quoteArgs } from '../../../execution/execUtils'; |
| 10 | +import { SHELL_INTEGRATION_POLL_INTERVAL, SHELL_INTEGRATION_TIMEOUT } from '../../utils'; |
| 11 | + |
| 12 | +export const SHELL_INTEGRATION_STATE_KEY = 'shellIntegration.enabled'; |
7 | 13 |
|
8 | 14 | function getCommandAsString(command: PythonCommandRunConfiguration[], shell: string, delimiter: string): string { |
9 | 15 | const parts = []; |
@@ -98,22 +104,60 @@ export function extractProfilePath(content: string): string | undefined { |
98 | 104 | return undefined; |
99 | 105 | } |
100 | 106 |
|
101 | | -export function shellIntegrationForActiveTerminal(name: string, profile?: string): boolean { |
102 | | - const hasShellIntegration = activeTerminalShellIntegration(); |
| 107 | +export async function shellIntegrationForActiveTerminal(name: string, profile?: string): Promise<boolean> { |
| 108 | + let hasShellIntegration = activeTerminalShellIntegration(); |
| 109 | + let timeout = 0; |
| 110 | + |
| 111 | + while (!hasShellIntegration && timeout < SHELL_INTEGRATION_TIMEOUT) { |
| 112 | + await sleep(SHELL_INTEGRATION_POLL_INTERVAL); |
| 113 | + timeout += SHELL_INTEGRATION_POLL_INTERVAL; |
| 114 | + hasShellIntegration = activeTerminalShellIntegration(); |
| 115 | + } |
103 | 116 |
|
104 | 117 | if (hasShellIntegration) { |
105 | 118 | traceInfo( |
106 | | - `SHELL: Shell integration is available on your active terminal, with name ${name} and profile ${profile}. Python activate scripts will be evaluated at shell integration level, except in WSL.` |
| 119 | + `SHELL: Shell integration is available on your active terminal, with name ${name} and profile ${profile}. Python activate scripts will be evaluated at shell integration level, except in WSL.`, |
107 | 120 | ); |
| 121 | + |
| 122 | + // Update persistent storage to reflect that shell integration is available |
| 123 | + const persistentState = await getGlobalPersistentState(); |
| 124 | + await persistentState.set(SHELL_INTEGRATION_STATE_KEY, true); |
| 125 | + |
108 | 126 | return true; |
109 | 127 | } |
110 | 128 | return false; |
111 | 129 | } |
112 | 130 |
|
113 | 131 | export function isWsl(): boolean { |
114 | 132 | // WSL sets these environment variables |
115 | | - return !!(process.env.WSL_DISTRO_NAME || |
116 | | - process.env.WSL_INTEROP || |
117 | | - process.env.WSLENV); |
| 133 | + return !!(process.env.WSL_DISTRO_NAME || process.env.WSL_INTEROP || process.env.WSLENV); |
118 | 134 | } |
119 | 135 |
|
| 136 | +export async function getShellIntegrationEnabledCache(): Promise<boolean> { |
| 137 | + const persistentState = await getGlobalPersistentState(); |
| 138 | + const shellIntegrationInspect = |
| 139 | + getConfiguration('terminal.integrated').inspect<boolean>('shellIntegration.enabled'); |
| 140 | + |
| 141 | + let shellIntegrationEnabled = true; |
| 142 | + if (shellIntegrationInspect) { |
| 143 | + // Priority: workspaceFolder > workspace > globalRemoteValue > globalLocalValue > global > default |
| 144 | + const inspectValue = shellIntegrationInspect as Record<string, unknown>; |
| 145 | + |
| 146 | + if (shellIntegrationInspect.workspaceFolderValue !== undefined) { |
| 147 | + shellIntegrationEnabled = shellIntegrationInspect.workspaceFolderValue; |
| 148 | + } else if (shellIntegrationInspect.workspaceValue !== undefined) { |
| 149 | + shellIntegrationEnabled = shellIntegrationInspect.workspaceValue; |
| 150 | + } else if ('globalRemoteValue' in shellIntegrationInspect && inspectValue.globalRemoteValue !== undefined) { |
| 151 | + shellIntegrationEnabled = inspectValue.globalRemoteValue as boolean; |
| 152 | + } else if ('globalLocalValue' in shellIntegrationInspect && inspectValue.globalLocalValue !== undefined) { |
| 153 | + shellIntegrationEnabled = inspectValue.globalLocalValue as boolean; |
| 154 | + } else if (shellIntegrationInspect.globalValue !== undefined) { |
| 155 | + shellIntegrationEnabled = shellIntegrationInspect.globalValue; |
| 156 | + } else if (shellIntegrationInspect.defaultValue !== undefined) { |
| 157 | + shellIntegrationEnabled = shellIntegrationInspect.defaultValue; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + await persistentState.set(SHELL_INTEGRATION_STATE_KEY, shellIntegrationEnabled); |
| 162 | + return shellIntegrationEnabled; |
| 163 | +} |
0 commit comments