1
1
2
- #I " ../packages/FSharp.Compiler.Service.0.0.7 -alpha/lib/net40"
2
+ #I " ../packages/FSharp.Compiler.Service.0.0.11 -alpha/lib/net40"
3
3
#I " ../packages/FSharp.Data.1.1.10/lib/net40"
4
4
#r " FSharp.Compiler.Service.dll"
5
5
#r " FSharp.Data.dll"
@@ -65,11 +65,32 @@ let fsi = lazy (
65
65
| None -> failwith " fsi path not set"
66
66
)
67
67
68
- let eval text =
69
- let fsiSession = fsi.Value
68
+ let evalExpr ( fsiSession : FsiEvaluationSession ) text =
70
69
match fsiSession.EvalExpression( text) with
71
- | Some value -> Some ( sprintf " %A " value.ReflectionValue)
72
- | None -> Some ( sprintf " Got no result!" )
70
+ | Some value -> ( sprintf " %A " value.ReflectionValue)
71
+ | None -> ( sprintf " Got no result!" )
72
+
73
+ let evalInteraction ( fsiSession : FsiEvaluationSession ) text =
74
+ fsiSession.EvalInteraction( text)
75
+
76
+ type Evaluation =
77
+ | Success
78
+ | Exception of string
79
+ | Result of string
80
+
81
+ let eval text =
82
+ let fsiSession = fsi.Value
83
+ try
84
+ let v = evalExpr fsiSession text
85
+ Evaluation.Result( v)
86
+ with e ->
87
+ try
88
+ evalInteraction fsiSession text
89
+ Evaluation.Success
90
+ with e ->
91
+ let exInfo = [ e.Message; e.StackTrace.Replace( " \r\n " , " \n " ) ] |> List.fold ( fun s x -> x + " \n " + s) " "
92
+ Evaluation.Exception( exInfo)
93
+
73
94
74
95
// LightTable
75
96
@@ -160,6 +181,13 @@ let rec handle stream request =
160
181
log " handle request:"
161
182
logf " %A " request
162
183
184
+ let responseAndLoop response =
185
+ log " Sending eval response"
186
+ logf " %A " response
187
+ send_ response stream response
188
+ log " Sent Eval response"
189
+ read_ request stream handle
190
+
163
191
match request with
164
192
| Malformed s ->
165
193
log " Received malformed request, aborting"
@@ -171,42 +199,26 @@ let rec handle stream request =
171
199
| EvalFSharpSelectedCmd ( clientId, args) ->
172
200
log " Eval cmd selected"
173
201
let cmd , res =
174
- try
175
- match eval ( args.Code) with
176
- | None ->
177
- ( " editor.eval.fsharp.success" , JsonValue.Object( Map.ofList [( " meta" , args.Meta.JsonValue)] ))
178
- | Some result ->
179
- ( " editor.eval.fsharp.result" , JsonValue.Object( Map.ofList [( " result" , JsonValue.String( result)); ( " meta" , args.Meta.JsonValue)] ))
180
- with e ->
181
- let exInfo = [ e.Message; e.StackTrace.Replace( " \r\n " , " \n " ) ] |> List.fold ( fun s x -> x + " \n " + s) " "
202
+ match eval ( args.Code) with
203
+ | Evaluation.Success ->
204
+ ( " editor.eval.fsharp.success" , JsonValue.Object( Map.ofList [( " meta" , args.Meta.JsonValue)] ))
205
+ | Evaluation.Result( result) ->
206
+ ( " editor.eval.fsharp.result" , JsonValue.Object( Map.ofList [( " result" , JsonValue.String( result)); ( " meta" , args.Meta.JsonValue)] ))
207
+ | Evaluation.Exception( exInfo) ->
182
208
( " editor.eval.fsharp.exception" , JsonValue.Object( Map.ofList [( " ex" , JsonValue.String( exInfo)); ( " meta" , args.Meta.JsonValue)] ))
183
-
184
- let response = { ClientId = clientId; Cmd = cmd; Args = res}
185
- log " Sending eval response"
186
- logf " %A " response
187
- send_ response stream response
188
- log " Sent Eval response"
189
- read_ request stream handle
209
+ responseAndLoop { ClientId = clientId; Cmd = cmd; Args = res}
190
210
191
211
| EvalFSharpNoSelectionCmd ( clientId, args) ->
192
212
log " Eval cmd not selected"
193
213
let cmd , res =
194
- try
195
- match eval ( args.Code) with
196
- | None ->
197
- ( " editor.eval.fsharp.success" , JsonValue.Object( Map.ofList [( " meta" , JsonValue.Null)] ))
198
- | Some result ->
199
- ( " editor.eval.fsharp.result" , JsonValue.Object( Map.ofList [( " result" , JsonValue.String( result)); ( " meta" , JsonValue.Null)] ))
200
- with e ->
201
- let exInfo = [ e.Message; e.StackTrace.Replace( " \r\n " , " \n " ) ] |> List.fold ( fun s x -> x + " \n " + s) " "
214
+ match eval ( args.Code) with
215
+ | Evaluation.Success ->
216
+ ( " editor.eval.fsharp.success" , JsonValue.Object( Map.ofList [( " meta" , JsonValue.Null)] ))
217
+ | Evaluation.Result( result) ->
218
+ ( " editor.eval.fsharp.result" , JsonValue.Object( Map.ofList [( " result" , JsonValue.String( result)); ( " meta" , JsonValue.Null)] ))
219
+ | Evaluation.Exception( exInfo) ->
202
220
( " editor.eval.fsharp.exception" , JsonValue.Object( Map.ofList [( " ex" , JsonValue.String( exInfo)); ( " meta" , JsonValue.Null)] ))
203
-
204
- let response = { ClientId = clientId; Cmd = cmd; Args = res}
205
- log " Sending eval response"
206
- logf " %A " response
207
- send_ response stream response
208
- log " Sent Eval response"
209
- read_ request stream handle
221
+ responseAndLoop { ClientId = clientId; Cmd = cmd; Args = res}
210
222
211
223
| Valid ( clientId, cmd, args) ->
212
224
logf " Cmd '%s ' unsupported (args '%s ') " cmd ( args.ToString()) |> ignore
0 commit comments