@@ -174,10 +174,18 @@ export function deleteTerminal(term: vscode.Terminal): void {
174174}
175175
176176export async function chooseTerminal ( ) : Promise < vscode . Terminal | undefined > {
177+ // VSCode Python's extension creates hidden terminal with string 'Deactivate'
178+ // For now ignore terminals with this string
179+ const ignoreTermIdentifier = 'Deactivate' ;
180+
181+ // Filter out terminals to be ignored
182+ const visibleTerminals = vscode . window . terminals . filter ( terminal => {
183+ return ! terminal . name . toLowerCase ( ) . includes ( ignoreTermIdentifier ) ;
184+ } ) ;
185+
177186 if ( config ( ) . get ( 'alwaysUseActiveTerminal' ) ) {
178- if ( vscode . window . terminals . length < 1 ) {
187+ if ( visibleTerminals . length < 1 ) {
179188 void vscode . window . showInformationMessage ( 'There are no open terminals.' ) ;
180-
181189 return undefined ;
182190 }
183191
@@ -189,53 +197,32 @@ export async function chooseTerminal(): Promise<vscode.Terminal | undefined> {
189197 for ( let i = 0 ; i < vscode . window . terminals . length ; i ++ ) {
190198 msg += `Terminal ${ i } : ${ vscode . window . terminals [ i ] . name } ` ;
191199 }
192- if ( vscode . window . terminals . length > 0 ) {
193- const rTermNameOptions = [ 'R' , 'R Interactive' ] ;
194- if ( vscode . window . activeTerminal !== undefined ) {
195- const activeTerminalName = vscode . window . activeTerminal . name ;
196- if ( rTermNameOptions . includes ( activeTerminalName ) ) {
197- return vscode . window . activeTerminal ;
198- }
199- for ( let i = vscode . window . terminals . length - 1 ; i >= 0 ; i -- ) {
200- const terminal = vscode . window . terminals [ i ] ;
201- const terminalName = terminal . name ;
202- if ( rTermNameOptions . includes ( terminalName ) ) {
203- terminal . show ( true ) ;
204- return terminal ;
205- }
206- }
207- } else {
208- msg += `B. There are ${ vscode . window . terminals . length } terminals: ` ;
209- for ( let i = 0 ; i < vscode . window . terminals . length ; i ++ ) {
210- msg += `Terminal ${ i } : ${ vscode . window . terminals [ i ] . name } ` ;
211- }
212- // Creating a terminal when there aren't any already does not seem to set activeTerminal
213- if ( vscode . window . terminals . length === 1 ) {
214- const activeTerminalName = vscode . window . terminals [ 0 ] . name ;
215- if ( rTermNameOptions . includes ( activeTerminalName ) ) {
216- return vscode . window . terminals [ 0 ] ;
217- }
218- } else {
219- msg += `C. There are ${ vscode . window . terminals . length } terminals: ` ;
220- for ( let i = 0 ; i < vscode . window . terminals . length ; i ++ ) {
221- msg += `Terminal ${ i } : ${ vscode . window . terminals [ i ] . name } ` ;
222- }
223- console . info ( msg ) ;
224- 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' ) ;
225200
226- return undefined ;
227- }
228- }
229- }
201+ const rTermNameOptions = [ 'R' , 'R Interactive' ] ;
202+
203+ const validRTerminals = visibleTerminals . filter ( terminal => {
204+ return rTermNameOptions . includes ( terminal . name ) ;
205+ } ) ;
230206
231- if ( rTerm === undefined ) {
207+ if ( validRTerminals . length > 0 ) {
208+ // If there is an active terminal that is an R terminal, use it
209+ if ( vscode . window . activeTerminal && rTermNameOptions . includes ( vscode . window . activeTerminal . name ) ) {
210+ return vscode . window . activeTerminal ;
211+ }
212+ // Otherwise, use last valid R terminal
213+ const rTerminal = validRTerminals [ validRTerminals . length - 1 ] ;
214+ rTerminal . show ( true ) ;
215+ return rTerminal ;
216+ } else {
217+ // If no valid R terminals are found, create a new one
218+ console . info ( msg ) ;
232219 await createRTerm ( true ) ;
233220 await delay ( 200 ) ; // Let RTerm warm up
221+ return rTerm ;
234222 }
235-
236- return rTerm ;
237223}
238224
225+
239226export async function runSelectionInTerm ( moveCursor : boolean , useRepl = true ) : Promise < void > {
240227 const selection = getSelection ( ) ;
241228 if ( ! selection ) {
0 commit comments