Skip to content

Commit 842911b

Browse files
authored
clear out old terminal state when a new one is created (ionide#1556)
* clear out old terminal state when a new one is created * only clean up on close
1 parent dd59e60 commit 842911b

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

src/Components/Fsi.fs

+27-20
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,12 @@ module Fsi =
311311
Some (U2.Case2 work)
312312
}
313313

314+
let private setupTerminalState (terminal: Terminal) =
315+
terminal.processId |> Promise.onSuccess (fun pId -> fsiOutputPID <- Some pId) |> ignore
316+
lastCd <- None
317+
lastCurrentFile <- None
318+
fsiOutput <- Some terminal
319+
314320
let private start () =
315321
fsiOutput |> Option.iter (fun n -> n.dispose())
316322
promise {
@@ -326,12 +332,7 @@ module Fsi =
326332
window.showErrorMessage("Unable to spawn FSI", null) |> ignore
327333
failwith "unable to spawn FSI"
328334
let w = Fable.Core.JsInterop.import "window" "vscode"
329-
let terminal: Terminal = w?createTerminal(profile.options)
330-
terminal.processId |> Promise.onSuccess (fun pId -> fsiOutputPID <- Some pId) |> ignore
331-
lastCd <- None
332-
lastCurrentFile <- None
333-
fsiOutput <- Some terminal
334-
sendCd window.activeTextEditor
335+
let terminal: Terminal = w?createTerminal(profile.options) // setting up terminal state will happen on the terminal listener
335336
terminal.show(true)
336337
return terminal
337338
}
@@ -424,19 +425,25 @@ module Fsi =
424425
|> Promise.suppress
425426
|> ignore)
426427

428+
429+
let private clearOldTerminalState () =
430+
fsiOutput |> Option.iter (fun t -> t.dispose())
431+
427432
let private handleCloseTerminal (terminal : Terminal) =
428-
fsiOutputPID
429-
|> Option.iter (fun currentTerminalPID ->
430-
terminal.processId
431-
|> Promise.onSuccess (fun closedTerminalPID ->
432-
if closedTerminalPID = currentTerminalPID then
433-
fsiOutput <- None
434-
fsiOutputPID <- None
435-
lastCd <- None
436-
lastCurrentFile <- None)
437-
|> Promise.suppress // prevent unhandled promise exception
438-
|> ignore)
439-
|> ignore
433+
fsiOutput <- None
434+
fsiOutputPID <- None
435+
lastCd <- None
436+
lastCurrentFile <- None
437+
()
438+
439+
// when a new terminal is created, if it's FSI and if we don't already have a terminal then setup the state for tracking FSI
440+
let private handleOpenTerminal (terminal: Terminal): unit =
441+
if terminal.name = fsiNetCoreName || terminal.name = fsiNetFrameworkName
442+
then
443+
clearOldTerminalState ()
444+
setupTerminalState terminal
445+
// initially have to set up the terminal to be in the correct start directory
446+
sendCd window.activeTextEditor
440447

441448
let private generateProjectReferences () =
442449
let ctn =
@@ -488,8 +495,8 @@ module Fsi =
488495
SdkScriptsNotify.activate context
489496
let w = Fable.Core.JsInterop.import "window" "vscode"
490497
w?registerTerminalProfileProvider("ionide-fsharp.fsi", provider) |> context.subscriptions.Add
491-
window.onDidCloseTerminal $ (handleCloseTerminal, (), context.subscriptions) |> ignore
492-
498+
window.onDidCloseTerminal.Invoke(handleCloseTerminal >> box) |> context.subscriptions.Add
499+
(w?onDidOpenTerminal : Event<Terminal>).Invoke(handleOpenTerminal >> box) |> context.subscriptions.Add
493500
commands.registerCommand("fsi.Start", start |> objfy2) |> context.subscriptions.Add
494501
commands.registerCommand("fsi.SendLine", sendLine |> objfy2) |> context.subscriptions.Add
495502
commands.registerCommand("fsi.SendSelection", sendSelection |> objfy2) |> context.subscriptions.Add

0 commit comments

Comments
 (0)