From b8af1055a609d2fbebc563a730583300235fc8dd Mon Sep 17 00:00:00 2001 From: Tomas Nobrega Date: Thu, 11 Apr 2024 10:48:47 -0400 Subject: [PATCH 1/2] handling for terminals created by vscode-python --- src/rTerminal.ts | 65 +++++++++++++++++++----------------------------- 1 file changed, 25 insertions(+), 40 deletions(-) diff --git a/src/rTerminal.ts b/src/rTerminal.ts index 22329a73..2d1ddaa4 100644 --- a/src/rTerminal.ts +++ b/src/rTerminal.ts @@ -189,51 +189,36 @@ export async function chooseTerminal(): Promise { for (let i = 0; i < vscode.window.terminals.length; i++){ msg += `Terminal ${i}: ${vscode.window.terminals[i].name} `; } - if (vscode.window.terminals.length > 0) { - const rTermNameOptions = ['R', 'R Interactive']; - if (vscode.window.activeTerminal !== undefined) { - const activeTerminalName = vscode.window.activeTerminal.name; - if (rTermNameOptions.includes(activeTerminalName)) { - return vscode.window.activeTerminal; - } - for (let i = vscode.window.terminals.length - 1; i >= 0; i--){ - const terminal = vscode.window.terminals[i]; - const terminalName = terminal.name; - if (rTermNameOptions.includes(terminalName)) { - terminal.show(true); - return terminal; - } - } - } else { - msg += `B. There are ${vscode.window.terminals.length} terminals: `; - for (let i = 0; i < vscode.window.terminals.length; i++){ - msg += `Terminal ${i}: ${vscode.window.terminals[i].name} `; - } - // Creating a terminal when there aren't any already does not seem to set activeTerminal - if (vscode.window.terminals.length === 1) { - const activeTerminalName = vscode.window.terminals[0].name; - if (rTermNameOptions.includes(activeTerminalName)) { - return vscode.window.terminals[0]; - } - } else { - msg += `C. There are ${vscode.window.terminals.length} terminals: `; - for (let i = 0; i < vscode.window.terminals.length; i++){ - msg += `Terminal ${i}: ${vscode.window.terminals[i].name} `; - } - console.info(msg); - void vscode.window.showErrorMessage('Error identifying terminal! Please run command "Developer: Toggle Developer Tools", find the message starting with "[chooseTerminal]", and copy the message to https://github.com/REditorSupport/vscode-R/issues'); - return undefined; - } - } - } + const rTermNameOptions = ['R', 'R Interactive']; + + // General identifier for terminals to ignore + const ignoreTermIdentifier = 'Deactivate'; + + const validRTerminals = vscode.window.terminals.filter(terminal => { + // Check if the terminal name matches the R terminal options + const isRTerminal = rTermNameOptions.includes(terminal.name); + // Check if the terminal should be ignored based on a general identifier + const shouldIgnore = terminal.name.toLowerCase().includes(ignoreTermIdentifier); + return isRTerminal && !shouldIgnore; + }); - if (rTerm === undefined) { + if (validRTerminals.length > 0) { + // If there is an active terminal that is an R terminal, use it + if (vscode.window.activeTerminal && rTermNameOptions.includes(vscode.window.activeTerminal.name)) { + return vscode.window.activeTerminal; + } + // Otherwise, find the first valid R terminal + const rTerminal = validRTerminals[0]; + rTerminal.show(true); + return rTerminal; + } else { + // If no valid R terminals are found, create a new one + console.info(msg); await createRTerm(true); await delay(200); // Let RTerm warm up + return rTerm; } - - return rTerm; } export async function runSelectionInTerm(moveCursor: boolean, useRepl = true): Promise { From 750a108904c3b61f22ae274a70e57b3f8217a21a Mon Sep 17 00:00:00 2001 From: Tomas Nobrega Date: Sun, 5 May 2024 12:35:14 -0400 Subject: [PATCH 2/2] included fixes asked by andy on pull request --- src/rTerminal.ts | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/rTerminal.ts b/src/rTerminal.ts index 2d1ddaa4..e527066a 100644 --- a/src/rTerminal.ts +++ b/src/rTerminal.ts @@ -174,10 +174,18 @@ export function deleteTerminal(term: vscode.Terminal): void { } export async function chooseTerminal(): Promise { + // VSCode Python's extension creates hidden terminal with string 'Deactivate' + // For now ignore terminals with this string + const ignoreTermIdentifier = 'Deactivate'; + + // Filter out terminals to be ignored + const visibleTerminals = vscode.window.terminals.filter(terminal => { + return !terminal.name.toLowerCase().includes(ignoreTermIdentifier); + }); + if (config().get('alwaysUseActiveTerminal')) { - if (vscode.window.terminals.length < 1) { + if (visibleTerminals.length < 1) { void vscode.window.showInformationMessage('There are no open terminals.'); - return undefined; } @@ -192,15 +200,8 @@ export async function chooseTerminal(): Promise { const rTermNameOptions = ['R', 'R Interactive']; - // General identifier for terminals to ignore - const ignoreTermIdentifier = 'Deactivate'; - - const validRTerminals = vscode.window.terminals.filter(terminal => { - // Check if the terminal name matches the R terminal options - const isRTerminal = rTermNameOptions.includes(terminal.name); - // Check if the terminal should be ignored based on a general identifier - const shouldIgnore = terminal.name.toLowerCase().includes(ignoreTermIdentifier); - return isRTerminal && !shouldIgnore; + const validRTerminals = visibleTerminals.filter(terminal => { + return rTermNameOptions.includes(terminal.name); }); if (validRTerminals.length > 0) { @@ -208,8 +209,8 @@ export async function chooseTerminal(): Promise { if (vscode.window.activeTerminal && rTermNameOptions.includes(vscode.window.activeTerminal.name)) { return vscode.window.activeTerminal; } - // Otherwise, find the first valid R terminal - const rTerminal = validRTerminals[0]; + // Otherwise, use last valid R terminal + const rTerminal = validRTerminals[validRTerminals.length - 1]; rTerminal.show(true); return rTerminal; } else { @@ -221,6 +222,7 @@ export async function chooseTerminal(): Promise { } } + export async function runSelectionInTerm(moveCursor: boolean, useRepl = true): Promise { const selection = getSelection(); if (!selection) {