From d30f7018d2ba0b4fe35816989363e6f5b84f7361 Mon Sep 17 00:00:00 2001 From: Anthony Kim <62267334+anthonykim1@users.noreply.github.com> Date: Thu, 15 Feb 2024 11:39:05 -0800 Subject: [PATCH] Add Python Shell Type and .python_history (#204680) * Add Python Shell type and history * remove one comment * remove unncessary * Why can't I manually override setShellType * Detect python shell type on Windows * fire shellType in _sendProcessTitle * Detect python shell type on Windows * delete comment * remove more comments * remove comment * remove dup * clean up * more comprehensive regex for windows python * follow previous style for map key name * remove unused variable * remove comment * last cleanup * More cleanup * more clean up * re-arrange * remove unused --------- Co-authored-by: Daniel Imms <2193314+Tyriar@users.noreply.github.com> --- src/vs/platform/terminal/common/terminal.ts | 4 ++- .../platform/terminal/node/terminalProcess.ts | 8 +++++- .../terminal/node/windowsShellHelper.ts | 3 +++ .../terminal/browser/terminalInstance.ts | 1 + .../contrib/terminal/common/history.ts | 27 +++++++++++++++++++ 5 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/vs/platform/terminal/common/terminal.ts b/src/vs/platform/terminal/common/terminal.ts index ffe0e56a189..a0b94d2a7f7 100644 --- a/src/vs/platform/terminal/common/terminal.ts +++ b/src/vs/platform/terminal/common/terminal.ts @@ -140,12 +140,14 @@ export const enum PosixShellType { Csh = 'csh', Ksh = 'ksh', Zsh = 'zsh', + Python = 'python' } export const enum WindowsShellType { CommandPrompt = 'cmd', PowerShell = 'pwsh', Wsl = 'wsl', - GitBash = 'gitbash' + GitBash = 'gitbash', + Python = 'python' } export type TerminalShellType = PosixShellType | WindowsShellType; diff --git a/src/vs/platform/terminal/node/terminalProcess.ts b/src/vs/platform/terminal/node/terminalProcess.ts index 023b6dc66e0..79be9013c81 100644 --- a/src/vs/platform/terminal/node/terminalProcess.ts +++ b/src/vs/platform/terminal/node/terminalProcess.ts @@ -73,6 +73,7 @@ const posixShellTypeMap = new Map([ ['ksh', PosixShellType.Ksh], ['sh', PosixShellType.Sh], ['pwsh', PosixShellType.PowerShell], + ['python', PosixShellType.Python], ['zsh', PosixShellType.Zsh] ]); @@ -404,7 +405,12 @@ export class TerminalProcess extends Disposable implements ITerminalChildProcess this._onDidChangeProperty.fire({ type: ProcessPropertyType.Title, value: this._currentTitle }); // If fig is installed it may change the title of the process const sanitizedTitle = this.currentTitle.replace(/ \(figterm\)$/g, ''); - this._onDidChangeProperty.fire({ type: ProcessPropertyType.ShellType, value: posixShellTypeMap.get(sanitizedTitle) }); + + if (sanitizedTitle.toLowerCase().startsWith('python')) { + this._onDidChangeProperty.fire({ type: ProcessPropertyType.ShellType, value: PosixShellType.Python }); + } else { + this._onDidChangeProperty.fire({ type: ProcessPropertyType.ShellType, value: posixShellTypeMap.get(sanitizedTitle) }); + } } shutdown(immediate: boolean): void { diff --git a/src/vs/platform/terminal/node/windowsShellHelper.ts b/src/vs/platform/terminal/node/windowsShellHelper.ts index 1def6545d69..e9fcb6f8130 100644 --- a/src/vs/platform/terminal/node/windowsShellHelper.ts +++ b/src/vs/platform/terminal/node/windowsShellHelper.ts @@ -152,6 +152,9 @@ export class WindowsShellHelper extends Disposable implements IWindowsShellHelpe case 'sles-12.exe': return WindowsShellType.Wsl; default: + if (executable.match(/python(\d(\.\d{0,2})?)?\.exe/)) { + return WindowsShellType.Python; + } return undefined; } } diff --git a/src/vs/workbench/contrib/terminal/browser/terminalInstance.ts b/src/vs/workbench/contrib/terminal/browser/terminalInstance.ts index e46e771f2a8..8b11cc8d9e8 100644 --- a/src/vs/workbench/contrib/terminal/browser/terminalInstance.ts +++ b/src/vs/workbench/contrib/terminal/browser/terminalInstance.ts @@ -118,6 +118,7 @@ const shellIntegrationSupportedShellTypes = [ PosixShellType.Bash, PosixShellType.Zsh, PosixShellType.PowerShell, + PosixShellType.Python, WindowsShellType.PowerShell ]; diff --git a/src/vs/workbench/contrib/terminal/common/history.ts b/src/vs/workbench/contrib/terminal/common/history.ts index cf0d940e716..ca2588453d2 100644 --- a/src/vs/workbench/contrib/terminal/common/history.ts +++ b/src/vs/workbench/contrib/terminal/common/history.ts @@ -92,6 +92,9 @@ export async function getShellFileHistory(accessor: ServicesAccessor, shellType: case PosixShellType.Fish: result = await fetchFishHistory(accessor); break; + case PosixShellType.Python: + result = await fetchPythonHistory(accessor); + break; default: return []; } if (result === undefined) { @@ -295,6 +298,30 @@ export async function fetchZshHistory(accessor: ServicesAccessor) { return result.values(); } + +export async function fetchPythonHistory(accessor: ServicesAccessor): Promise | undefined> { + const fileService = accessor.get(IFileService); + const remoteAgentService = accessor.get(IRemoteAgentService); + + const content = await fetchFileContents(env['HOME'], '.python_history', false, fileService, remoteAgentService); + + if (content === undefined) { + return undefined; + } + + // Python history file is a simple text file with one command per line + const fileLines = content.split('\n'); + const result: Set = new Set(); + + fileLines.forEach(line => { + if (line.trim().length > 0) { + result.add(line.trim()); + } + }); + + return result.values(); +} + export async function fetchPwshHistory(accessor: ServicesAccessor) { const fileService: Pick = accessor.get(IFileService); const remoteAgentService: Pick = accessor.get(IRemoteAgentService);