@@ -8,15 +8,49 @@ open Microsoft.FSharp.Compiler.Interactive.Shell
88[<AutoOpen>]
99module Evaluation =
1010
11- let internal fsiout = ref false
12- let internal sbOut = new StringBuilder()
13- let internal sbErr = new StringBuilder()
14- let internal inStream = new StringReader( " " )
15- let internal outStream = new StringWriter( sbOut)
16- let internal errStream = new StringWriter( sbErr)
17- let internal fsiConfig = FsiEvaluationSession.GetDefaultConfiguration()
18- let internal fsiEval = FsiEvaluationSession.Create( fsiConfig, [| " --noninteractive" |], inStream, outStream, errStream)
11+ /// Extend the ` fsi ` object with ` fsi.AddHtmlPrinter `
12+ let addHtmlPrinter = """
13+ module FsInteractiveService =
14+ let mutable htmlPrinters = new ResizeArray<System.Type * (obj -> seq<string * string> * string)>()
15+ let htmlPrinterParams = System.Collections.Generic.Dictionary<string, obj>()
16+ do htmlPrinterParams.["html-standalone-output"] <- false
17+
18+ type Microsoft.FSharp.Compiler.Interactive.InteractiveSession with
19+ member x.HtmlPrinterParameters = FsInteractiveService.htmlPrinterParams
20+ member x.AddHtmlPrinter<'T>(f:'T -> seq<string * string> * string) =
21+ FsInteractiveService.htmlPrinters.Add(typeof<'T>, fun (value:obj) ->
22+ f (value :?> 'T))"""
23+
24+ /// Start the F# interactive session with HAS_FSI_ADDHTMLPRINTER symbol defined
25+ let internal startSession () =
26+ let sbOut = new StringBuilder()
27+ let sbErr = new StringBuilder()
28+ let inStream = new StringReader( " " )
29+ let outStream = new StringWriter( sbOut)
30+ let errStream = new StringWriter( sbErr)
1931
32+ let fsiObj = Microsoft.FSharp.Compiler.Interactive.Settings.fsi
33+ let fsiConfig = FsiEvaluationSession.GetDefaultConfiguration( fsiObj, false )
34+ let args = [| " --noninteractive" ; " --define:HAS_FSI_ADDHTMLPRINTER" |]
35+ let fsiSession = FsiEvaluationSession.Create( fsiConfig, args, inStream, outStream, errStream)
36+
37+ // Load the `fsi` object from the right location of the `FSharp.Compiler.Interactive.Settings.dll`
38+ // assembly and add the `fsi.AddHtmlPrinter` extension method; then clean it from FSI output
39+ let origLength = sbOut.Length
40+ let fsiLocation = typeof< Microsoft.FSharp.Compiler.Interactive.InteractiveSession>. Assembly.Location
41+ fsiSession.EvalInteraction( " #r @\" " + fsiLocation + " \" " )
42+ fsiSession.EvalInteraction( addHtmlPrinter)
43+ sbOut.Remove( origLength, sbOut.Length- origLength) |> ignore
44+
45+ // Get reference to the extra HTML printers registered inside the FSI session
46+ let extraPrinters =
47+ unbox< ResizeArray< System.Type * ( obj -> seq< string * string> * string)>>
48+ ( fsiSession.EvalExpression( " FsInteractiveService.htmlPrinters" ) .Value.ReflectionValue)
49+ sbErr, sbOut, extraPrinters, fsiSession
50+
51+ let internal fsiout = ref false
52+ let internal sbErr , sbOut , extraPrinters , fsiEval = startSession ()
53+
2054 /// Gets ` it ` only if ` it ` was printed to the console
2155 let GetLastExpression () =
2256
0 commit comments