anchor.diff:          commands.registerCommand ("fsi.SendFile", sendFile |> objfy2)
           |> context.Subscribe
   
  -        commands.registerCommand ("fsi.SendText", sendText |> objfy2)
  -        |> context.Subscribe
  -
           commands.registerCommand ("fsi.SendProjectReferences", sendReferences |> objfy2)
           |> context.Subscribe
   
  
positive.code:                 | U2.Case1 opts -> window.createTerminal opts
                  | U2.Case2 opts -> window.createTerminal opts
  
              terminal.show (true)
              return terminal
          }
          |> Promise.onFail (fun _ ->
              window.showErrorMessage ("Failed to spawn FSI, please ensure it's in PATH")
              |> ignore)
  
      let private chunkStringBySize (size: int) (str: string) =
          let mutable i1 = 0
  
          [ while i1 < str.Length do
                let i2 = min (i1 + size) (str.Length)
                yield str.[i1 .. i2 - 1]
                i1 <- i2 ]
  
      let private send (msg: string) =
          let msgWithNewline = msg + (if msg.Contains "//" then "\n" else "") + ";;\n"
  
          match fsiOutput with
          | None -> start ()
          | Some fo -> Promise.lift fo
          |> Promise.onSuccess (fun fp ->
              fp.show true
              fp.sendText (msgWithNewline, false)
              lastSelectionSent <- Some msg)
          |> Promise.onFail (fun _ -> window.showErrorMessage ("Failed to send text to FSI") |> ignore)
  
      let private moveCursorDownOneLine () =
          let args = createObj [ "to" ==> "down"; "by" ==> "line"; "value" ==> 1 ]
  
          commands.executeCommand ("cursorMove", Some(box args)) |> ignore
  
      let private sendLine () =
          let editor = window.activeTextEditor.Value
          let _ = editor.document.fileName
          let pos = editor.selection.start
          let line = editor.document.lineAt pos
          sendCd (Some editor)
  
          send line.text
          |> Promise.onSuccess (fun _ -> moveCursorDownOneLine ())
          |> Promise.suppress // prevent unhandled promise exception
          |> ignore
  
      let private sendSelection () =
          let editor = window.activeTextEditor.Value
          let _ = editor.document.fileName
  
          sendCd (Some editor)
  
          if editor.selection.isEmpty then
              sendLine ()
          else
              let range =
                  vscode.Range.Create(
                      editor.selection.anchor.line,
                      editor.selection.anchor.character,
                      editor.selection.active.line,
                      editor.selection.active.character
                  )
  
              let text = editor.document.getText range
  
              send text
              |> Promise.suppress // prevent unhandled promise exception
              |> ignore
  
      let private sendLastSelection () =
          match lastSelectionSent with
          | Some x ->
              if "FSharp.saveOnSendLastSelection" |> Configuration.get false then
                  window.activeTextEditor.Value.document.save () |> Promise.ofThenable
              else
                  Promise.lift true
              |> Promise.bind (fun _ -> send x)
              |> Promise.suppress // prevent unhandled promise exception
              |> ignore
          | None -> ()
  
      let private sendFile () =
          let editor = window.activeTextEditor.Value
          let text = editor.document.getText ()
          sendCd (Some editor)
  
          send text
          |> Promise.suppress // prevent unhandled promise exception
          |> ignore
  
      let private sendText (lines: string list) =
          send (String.concat ";;\n" lines)
          |> Promise.suppress // prevent unhandled promise exception
          |> ignore
  
      let private referenceAssembly (path: ResolvedReferencePath) = path |> sprintf "#r @\"%s\"" |> send
      let private referenceAssemblies = Promise.executeForAll referenceAssembly
  
      let private sendReferences () =
          window.activeTextEditor.Value.document.fileName
          |> Project.tryFindLoadedProjectByFile
          |> Option.iter (fun p ->
              sendCd window.activeTextEditor
  
              p.References
              |> Seq.filter (fun n -> n.EndsWith "FSharp.Core.dll" |> not && n.EndsWith "mscorlib.dll" |> not)
              |> Seq.toList
              |> referenceAssemblies
              |> Promise.suppress
              |> ignore)
  
  
      let private clearOldTerminalState () =
          fsiOutput |> Option.iter (fun t -> t.dispose ())
  
      let private handleCloseTerminal (terminal: Terminal) =
          fsiOutputPID
          |> Option.iter (fun currentTerminalPID ->
              terminal.processId
              |> Promise.ofThenable
              |> Promise.onSuccess (fun closedTerminalPID ->
                  if Option.map int closedTerminalPID = Some currentTerminalPID then
                      fsiOutput <- None
                      fsiOutputPID <- None
                      lastCd <- None
                      lastCurrentFile <- None)
              |> Promise.suppress // prevent unhandled promise exception
              |> ignore)
          |> ignore
  
          None
  
      // 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
      let private handleOpenTerminal (terminal: Terminal) =
          if terminal.name = fsiNetCoreName || terminal.name = fsiNetFrameworkName then
              clearOldTerminalState ()
              setupTerminalState terminal
              // initially have to set up the terminal to be in the correct start directory
              sendCd window.activeTextEditor
  
          None
  
  
      let private generateProjectReferences () =
          let ctn =
              window.activeTextEditor.Value.document.fileName
              |> Project.tryFindLoadedProjectByFile
              |> Option.map (fun p ->
                  [| yield!
                         p.References
                         |> Seq.filter (fun n -> n.EndsWith "FSharp.Core.dll" |> not && n.EndsWith "mscorlib.dll" |> not)
                         |> Seq.map (sprintf "#r @\"%s\"")
                     yield! p.Files |> Seq.map (sprintf "#load @\"%s\"") |])
  
          promise {
              match ctn with
              | Some c ->
                  let path = node.path.join (workspace.rootPath.Value, "references.fsx")
  
                  let! td = vscode.Uri.parse ("untitled:" + path) |> workspace.openTextDocument
  
                  let! te = window.showTextDocument (td, ViewColumn.Three)
  
                  let! _ =
                      te.edit (fun e ->
                          let p = vscode.Position.Create(0., 0.)
                          let ctn = c |> String.concat "\n"
                          e.insert (p, ctn))
  
  
                  return ()
              | None -> return ()
          }
  
      let sendReferencesForProject project =
          project.References
          |> Seq.filter (fun n -> n.EndsWith "FSharp.Core.dll" |> not && n.EndsWith "mscorlib.dll" |> not)
          |> Seq.toList
          |> referenceAssemblies
          |> Promise.suppress
          |> ignore
  
      let generateProjectReferencesForProject project =
          let ctn =
  
negative.code:                 node.path.join (dir, "tmp.fsx"), dir
  
          match lastCd with
          // Same dir as last time, no need to send it
          | Some (cd) when cd = dir -> ()
          | _ ->
              let msg = sprintf "# silentCd @\"%s\";;\n" dir
  
              terminal.sendText (msg, false)
  
              lastCd <- Some dir
  
          match lastCurrentFile with
          // Same file as last time, no need to send it
          | Some (currentFile) when currentFile = file -> ()
          | _ ->
              let msg = sprintf "# %d @\"%s\"\n;;\n" 1 file
  
              terminal.sendText (msg, false)
  
              lastCurrentFile <- Some file
  
  