-
-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathLanguageService.fs
More file actions
1089 lines (865 loc) · 43.7 KB
/
Copy pathLanguageService.fs
File metadata and controls
1089 lines (865 loc) · 43.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
namespace Ionide.VSCode.FSharp
open System
open Fable.Core
open Fable.Core.JsInterop
open Fable.Import
open Fable.Import.VSCode
open Fable.Import.VSCode.Vscode
open Ionide.VSCode.Helpers
open Semver
open DTO
open LanguageServer
module node = Node.Api
module Notifications =
type DocumentParsedEvent =
{
uri: string
version: float
/// BEWARE: Live object, might have changed since the parsing
document: TextDocument
}
let onDocumentParsedEmitter = vscode.EventEmitter.Create<DocumentParsedEvent>()
let onDocumentParsed = onDocumentParsedEmitter.event
let private tooltipRequestedEmitter = vscode.EventEmitter.Create<Position>()
let tooltipRequested = tooltipRequestedEmitter.event
let mutable notifyWorkspaceHandler
: Option<Choice<ProjectResult, ProjectLoadingResult, (string * ErrorData), string> -> unit> =
None
let testDetectedEmitter = vscode.EventEmitter.Create<TestForFile>()
let testDetected = testDetectedEmitter.event
module LanguageService =
open Fable.Import.LanguageServer.Client
let private logger =
ConsoleAndOutputChannelLogger(Some "LanguageService", Level.DEBUG, Some defaultOutputChannel, Some Level.DEBUG)
module Types =
open Fable.Import.VSCode.Vscode
type PlainNotification = { content: string }
/// Position in a text document expressed as zero-based line and zero-based character offset.
/// A position is between two characters like an ‘insert’ cursor in a editor.
type Position =
{
/// Line position in a document (zero-based).
Line: int
/// Character offset on a line in a document (zero-based). Assuming that the line is
/// represented as a string, the `character` value represents the gap between the
/// `character` and `character + 1`.
///
/// If the character value is greater than the line length it defaults back to the
/// line length.
Character: int
}
type DocumentUri = string
type TextDocumentIdentifier = { Uri: DocumentUri }
type VersionedTextDocumentIdentifier = { Uri: DocumentUri; Version: float }
type TextDocumentPositionParams =
{ TextDocument: TextDocumentIdentifier
Position: Position }
type VersionedTextDocumentPositionParams =
{ TextDocument: VersionedTextDocumentIdentifier
Position: Position }
type FileParams = { Project: TextDocumentIdentifier }
type WorkspaceLoadParms =
{ TextDocuments: TextDocumentIdentifier[] }
type HighlightingRequest =
{ TextDocument: TextDocumentIdentifier }
type FSharpLiterateRequest =
{ TextDocument: TextDocumentIdentifier }
type FSharpPipelineHintsRequest =
{ TextDocument: TextDocumentIdentifier }
type LspRange =
{ start: Fable.Import.VSCode.Vscode.Position
``end``: Fable.Import.VSCode.Vscode.Position }
type TestRunRequest =
{ LimitToProjects: string array option
TestCaseFilter: string option
AttachDebugger: bool }
type Uri with
member uri.ToDocumentUri = uri.ToString()
let mutable client: LanguageClient option = None
let inline checkNotificationAndCast<'t> (response: Types.PlainNotification) : 't option =
if isNullOrUndefined response then
None
else
Some(ofJson<'t> response.content)
let selector: DocumentSelector =
let fileSchemeFilter: DocumentFilter =
jsOptions<TextDocumentFilter> (fun f ->
f.language <- Some "fsharp"
f.scheme <- Some "file")
|> U2.Case1
let untitledSchemeFilter: DocumentFilter =
jsOptions<TextDocumentFilter> (fun f ->
f.language <- Some "fsharp"
f.scheme <- Some "untitled")
|> U2.Case1
[| U2.Case2 fileSchemeFilter; U2.Case2 untitledSchemeFilter |]
//TODO: remove (-> use URI instead)
let private handleUntitled (fn: string) =
if fn.EndsWith ".fs" || fn.EndsWith ".fsi" || fn.EndsWith ".fsx" then
fn
else
(fn + ".fsx")
let tryFindDotnet () =
let reportError (msg: string) =
promise {
let! selection = window.showErrorMessage ("Could not find 'dotnet'", "More details")
match selection with
| Some "More details" ->
// Prepare a temporary file to store the error message
let fileName =
"ionide_dotnet_not_found_details_"
+ node.crypto.randomBytes(4).readUInt32LE(0).ToString()
+ ".md"
let tempPath = node.path.join (node.os.tmpdir (), fileName)
// Write the error message to the file, this will allow us to close the text editor
// without a confirmation dialog as the file content will not be modified.
node.fs.writeFileSync (tempPath, msg)
// Open the file in VSCode
let newFile = vscode.Uri.parse (tempPath)
let! document = workspace.openTextDocument newFile
let! _ = window.showTextDocument (document, ?options = None)
// Show the error message in the markdown preview (better display)
do! commands.executeCommand ("markdown.showPreview", Some(box document))
// Close the text editor (this does not close the markdown preview)
do! commands.executeCommand ("workbench.action.closeActiveEditor", Some(box document))
// Delete the temporary file as it is not needed anymore.
node.fs.unlinkSync (U2.Case1 tempPath)
| Some _
| None -> ()
}
promise {
// User has specified a custom dotnet location
match Configuration.tryGet "FSharp.dotnetRoot" with
| Some dotnetRoot ->
// Choose the right program name depending on the OS
let program = if Environment.isWin then "dotnet.exe" else "dotnet"
// Compute the full path
let dotnetFullPath = node.path.join (dotnetRoot, program)
// Check if the program exists at the computed location
if node.fs.existsSync (U2.Case1 dotnetFullPath) then
return Ok dotnetFullPath
else
let msg =
// The special syntax: %s{"\n" + dotnetFullPath}
// Force a new line to be added, if I put %s{dotnetFullPath}
// at the beginning of the next line, there is a compiler error...
$"""
Could not find `dotnet` in the `dotnetRoot` directory: %s{"\n" + dotnetFullPath}
Consider:
- updating the `FSharp.dotnetRoot` settings key to a directory with a `dotnet` binary
- removing the `FSharp.dotnetRoot` settings key and :
- including `dotnet` in your PATH
- installing .NET Core"""
do! reportError msg
return Core.Error "Could not find 'dotnet'"
// No custom location was specified, try to find it from the PATH
| None ->
match! Environment.tryGetTool "dotnet" with
| Some dotnetFullPath -> return Ok dotnetFullPath
| None ->
let msg =
"""Could not find `dotnet` in the path.
Consider:
- setting the `FSharp.dotnetRoot` settings key to a directory with a `dotnet` binary
- including `dotnet` in your PATH
- installing .NET Core
"""
do! reportError msg
return Core.Error "Could not find 'dotnet'"
}
/// runs `dotnet --version` in the current rootPath to determine the resolved sdk version from the global.json file.
let sdkVersion () =
promise {
let! dotnet = tryFindDotnet ()
match dotnet with
| Error msg -> return Core.Error msg
| Ok dotnet ->
let! (error, stdout, stderr) = Process.exec dotnet (ResizeArray [ "--version" ])
match error with
| Some e -> return Core.Error $"error while invoking: {e.message}"
| None ->
let stdoutTrimmed = stdout.TrimEnd()
let semver = semver.parse (Some(U2.Case1 stdoutTrimmed))
match semver with
| None -> return Core.Error $"unable to parse version string '{stdoutTrimmed}'"
| Some semver -> return Core.Ok semver
}
let f1Help (uri: Uri) line col =
match client with
| None -> Promise.empty
| Some cl ->
let req: Types.TextDocumentPositionParams =
{ TextDocument = { Uri = uri.ToDocumentUri }
Position = { Line = line; Character = col } }
cl.sendRequest ("fsharp/f1Help", req)
|> Promise.map checkNotificationAndCast<Result<string>>
let documentation (uri: Uri) line col =
match client with
| None -> Promise.empty
| Some cl ->
let req: Types.TextDocumentPositionParams =
{ TextDocument = { Uri = uri.ToDocumentUri }
Position = { Line = line; Character = col } }
cl.sendRequest ("fsharp/documentation", req)
|> Promise.map checkNotificationAndCast<Result<DocumentationDescription>>
let documentationForSymbol (request: DocumentationForSymbolRequest) =
match client with
| None -> Promise.empty
| Some cl ->
cl.sendRequest ("fsharp/documentationSymbol", request)
|> Promise.map checkNotificationAndCast<Result<DocumentationDescription>>
let signature (uri: Uri) line col =
match client with
| None -> Promise.empty
| Some cl ->
let req: Types.TextDocumentPositionParams =
{ TextDocument = { Uri = uri.ToDocumentUri }
Position = { Line = line; Character = col } }
cl.sendRequest ("fsharp/signature", req)
|> Promise.map checkNotificationAndCast<Result<string>>
let signatureData (uri: Uri) line col =
match client with
| None -> Promise.empty
| Some cl ->
let req: Types.TextDocumentPositionParams =
{ TextDocument = { Uri = uri.ToDocumentUri }
Position = { Line = line; Character = col } }
cl.sendRequest ("fsharp/signatureData", req)
|> Promise.map checkNotificationAndCast<SignatureDataResult>
let generateDocumentation (fileUri: Uri, version) (line, col) =
match client with
| None -> Promise.empty
| Some cl ->
let req: Types.VersionedTextDocumentPositionParams =
{ TextDocument =
{ Uri = fileUri.ToDocumentUri
Version = version }
Position = { Line = line; Character = col } }
cl.sendRequest ("fsharp/documentationGenerator", req)
|> Promise.map (fun _ -> ())
let lineLenses (uri: Uri) =
match client with
| None -> Promise.empty
| Some cl ->
let req: Types.FileParams = { Project = { Uri = uri.ToDocumentUri } }
cl.sendRequest ("fsharp/lineLens", req)
|> Promise.map checkNotificationAndCast<Result<Symbols[]>>
let compile s =
match client with
| None -> Promise.empty
| Some cl ->
let req: Types.FileParams = { Project = { Uri = handleUntitled s } }
cl.sendRequest ("fsharp/compile", req)
|> Promise.map (fun (res: Types.PlainNotification) -> res.content |> ofJson<CompileResult>)
let fsdn (signature: string) =
let parse (ws: obj) =
{ FsdnResponse.Functions = ws?Functions |> unbox }
match client with
| None -> Promise.empty
| Some cl ->
let req: Types.FileParams = { Project = { Uri = signature } }
cl.sendRequest ("fsharp/fsdn", req)
|> Promise.map (fun (res: Types.PlainNotification) ->
let res = res.content |> ofJson<obj>
parse (res?Data |> unbox))
let dotnetNewList () =
match client with
| None -> Promise.empty
| Some cl ->
let req: DotnetNew.DotnetNewListRequest = { Query = "" }
cl.sendRequest ("fsharp/dotnetnewlist", req)
|> Promise.map checkNotificationAndCast<DotnetNew.DotnetNewListResponse>
let dotnetNewRun (template: string) (name: string option) (output: string option) =
match client with
| None -> Promise.empty
| Some cl ->
let req: DotnetNew.DotnetNewRunRequest =
{ Template = template
Output = output
Name = name }
cl.sendRequest ("fsharp/dotnetnewrun", req) |> Promise.map ignore
let dotnetAddProject (target: string) (reference: string) =
match client with
| None -> Promise.empty
| Some cl ->
let req: FsProj.DotnetProjectRequest =
{ Target = target
Reference = reference }
cl.sendRequest ("fsharp/dotnetaddproject", req) |> Promise.map ignore
let dotnetRemoveProject (target: string) (reference: string) =
match client with
| None -> Promise.empty
| Some cl ->
let req: FsProj.DotnetProjectRequest =
{ Target = target
Reference = reference }
cl.sendRequest ("fsharp/dotnetremoveproject", req) |> Promise.map ignore
let dotnetAddSln (target: string) (reference: string) =
match client with
| None -> Promise.empty
| Some cl ->
let req: FsProj.DotnetProjectRequest =
{ Target = target
Reference = reference }
cl.sendRequest ("fsharp/dotnetaddsln", req) |> Promise.map ignore
let fsprojMoveFileUp (fsproj: string) (file: string) =
match client with
| None -> Promise.empty
| Some cl ->
let req: FsProj.DotnetFileRequest =
{ FsProj = fsproj
FileVirtualPath = file }
cl.sendRequest ("fsproj/moveFileUp", req) |> Promise.map ignore
let fsprojMoveFileDown (fsproj: string) (file: string) =
match client with
| None -> Promise.empty
| Some cl ->
let req: FsProj.DotnetFileRequest =
{ FsProj = fsproj
FileVirtualPath = file }
cl.sendRequest ("fsproj/moveFileDown", req) |> Promise.map ignore
let fsprojAddFileAbove (fsproj: string) (file: string) (newFile: string) =
match client with
| None -> Promise.empty
| Some cl ->
let req: FsProj.DotnetFile2Request =
{ FsProj = fsproj
FileVirtualPath = file
NewFile = newFile }
cl.sendRequest ("fsproj/addFileAbove", req) |> Promise.map ignore
let fsprojAddFileBelow (fsproj: string) (file: string) (newFile: string) =
match client with
| None -> Promise.empty
| Some cl ->
let req: FsProj.DotnetFile2Request =
{ FsProj = fsproj
FileVirtualPath = file
NewFile = newFile }
cl.sendRequest ("fsproj/addFileBelow", req) |> Promise.map ignore
let fsprojAddFile (fsproj: string) (file: string) =
match client with
| None -> Promise.empty
| Some cl ->
let req: FsProj.DotnetFileRequest =
{ FsProj = fsproj
FileVirtualPath = file }
cl.sendRequest ("fsproj/addFile", req) |> Promise.map ignore
let fsprojAddExistingFile (fsproj: string) (file: string) =
match client with
| None -> Promise.empty
| Some cl ->
let req: FsProj.DotnetFileRequest =
{ FsProj = fsproj
FileVirtualPath = file }
cl.sendRequest ("fsproj/addExistingFile", req) |> Promise.map ignore
let fsprojRenameFile (fsproj: string) (file: string) (newFile: string) =
match client with
| None -> Promise.empty
| Some cl ->
let req: FsProj.DotnetRenameFileRequest =
{ FsProj = fsproj
OldFileVirtualPath = file
NewFileName = newFile }
cl.sendRequest ("fsproj/renameFile", req) |> Promise.map ignore
let fsprojRemoveFile (fsproj: string) (file: string) =
match client with
| None -> Promise.empty
| Some cl ->
let req: FsProj.DotnetFileRequest =
{ FsProj = fsproj
FileVirtualPath = file }
cl.sendRequest ("fsproj/removeFile", req) |> Promise.map ignore
let parseError (err: obj) =
let data =
match err?Code |> unbox with
| ErrorCodes.GenericError -> ErrorData.GenericError
| ErrorCodes.ProjectNotRestored -> ErrorData.ProjectNotRestored(err?AdditionalData |> unbox)
| ErrorCodes.ProjectParsingFailed -> ErrorData.ProjectParsingFailed(err?AdditionalData |> unbox)
| ErrorCodes.LanguageNotSupported -> ErrorData.LangugageNotSupported(err?AdditionalData |> unbox)
| unknown -> ErrorData.GenericError
(err?Message |> unbox<string>), data
let project s =
match client with
| None -> Promise.empty
| Some cl ->
let req: Types.FileParams = { Project = { Uri = s } }
cl.sendRequest ("fsharp/project", req)
|> Promise.map (fun (res: Types.PlainNotification) ->
let res = res.content |> ofJson<obj>
match res?Kind |> unbox with
| "error" -> res?Data |> parseError |> Core.Result.Error
| _ -> res |> unbox<ProjectResult> |> Ok)
let workspacePeek dir deep excludedDirs =
let rec mapItem (f: WorkspacePeekFoundSolutionItem) : WorkspacePeekFoundSolutionItem option =
let mapItemKind (i: obj) : WorkspacePeekFoundSolutionItemKind option =
let data = i?Data
match i?Kind |> unbox with
| "folder" ->
let folderData: WorkspacePeekFoundSolutionItemKindFolder = data |> unbox
let folder: WorkspacePeekFoundSolutionItemKindFolder =
{ Files = folderData.Files
Items = folderData.Items |> Array.choose mapItem }
Some(WorkspacePeekFoundSolutionItemKind.Folder folder)
| "msbuildFormat" -> Some(WorkspacePeekFoundSolutionItemKind.MsbuildFormat(data |> unbox))
| _ -> None
match mapItemKind f.Kind with
| Some kind ->
Some
{ WorkspacePeekFoundSolutionItem.Guid = f.Guid
Name = f.Name
Kind = kind }
| None -> None
let mapFound (f: obj) : WorkspacePeekFound option =
let data = f?Data
match f?Type |> unbox with
| "directory" -> Some(WorkspacePeekFound.Directory(data |> unbox))
| "solution" ->
let sln =
{ WorkspacePeekFoundSolution.Path = data?Path |> unbox
Configurations = data?Configurations |> unbox
Items = data?Items |> unbox |> Array.choose mapItem }
Some(WorkspacePeekFound.Solution sln)
| _ -> None
let parse (ws: obj) =
{ WorkspacePeek.Found = ws?Found |> unbox |> Array.choose mapFound }
match client with
| None -> Promise.empty
| Some cl ->
let req =
{ WorkspacePeekRequest.Directory = dir
Deep = deep
ExcludedDirs = excludedDirs |> Array.ofList }
cl.sendRequest ("fsharp/workspacePeek", req)
|> Promise.map (fun (res: Types.PlainNotification) ->
let res = res.content |> ofJson<obj>
parse (res?Data |> unbox))
let workspaceLoad (projects: string list) =
match client with
| None -> Promise.empty
| Some cl ->
let req: Types.WorkspaceLoadParms =
{ TextDocuments = projects |> List.map (fun s -> { Types.Uri = s }) |> List.toArray }
cl.sendRequest ("fsharp/workspaceLoad", req) |> Promise.map ignore
let loadAnalyzers () =
match client with
| None -> Promise.empty
| Some cl ->
let req: Types.FileParams = { Project = { Uri = "" } }
cl.sendRequest ("fsharp/loadAnalyzers", req) |> Promise.map ignore
let getHighlighting (uri: Uri) : JS.Promise<HighlightingResponse> =
match client with
| None -> Promise.empty
| Some cl ->
let req: Types.HighlightingRequest = { TextDocument = { Uri = uri.ToDocumentUri } }
cl.sendRequest ("fsharp/highlighting", req)
|> Promise.map (fun (res: obj) -> res?data)
let fsharpLiterate (uri: Uri) =
match client with
| None -> Promise.empty
| Some cl ->
let req: Types.FSharpLiterateRequest =
{ TextDocument = { Uri = uri.ToDocumentUri } }
cl.sendRequest ("fsharp/fsharpLiterate", req)
|> Promise.map (fun (res: Types.PlainNotification) -> res.content |> ofJson<FSharpLiterateResult>)
let pipelineHints (uri: Uri) =
match client with
| None -> Promise.empty
| Some cl ->
let req: Types.FSharpPipelineHintsRequest =
{ TextDocument = { Uri = uri.ToDocumentUri } }
cl.sendRequest ("fsharp/pipelineHint", req)
|> Promise.map checkNotificationAndCast<PipelineHintsResult>
let fsiSdk () =
promise { return Environment.configFsiSdkFilePath () }
let discoverTests onDiscoveryProgress () =
match client with
| None -> Promise.empty
| Some cl ->
cl.onNotification (
"test/testDiscoveryUpdate",
(fun (notification: Types.PlainNotification) ->
let parsed = ofJson<TestDiscoveryUpdate> notification.content
onDiscoveryProgress parsed)
)
cl.sendRequest ("test/discoverTests", ())
|> Promise.map (fun (res: Types.PlainNotification) -> res.content |> ofJson<DiscoverTestsResult>)
type ProcessId = int
type DidDebuggerAttach = bool
let runTests
(onTestRunProgress: TestRunProgress -> unit)
(onAttachDebugger: ProcessId -> JS.Promise<bool>)
(projectSubset: string array option)
(testCaseFilter: string option)
(attachDebugger: bool)
=
match client with
| None -> Promise.empty
| Some cl ->
cl.onNotification (
"test/testRunProgressUpdate",
(fun (notification: Types.PlainNotification) ->
let parsed = ofJson<TestRunProgress> notification.content
onTestRunProgress parsed)
)
cl.onRequest (
"test/processWaitingForDebugger",
(fun (notification: Types.PlainNotification) ->
promise {
let parsed = ofJson<int> notification.content
return! onAttachDebugger parsed
})
)
let request: Types.TestRunRequest =
{ LimitToProjects = projectSubset
TestCaseFilter = testCaseFilter
AttachDebugger = attachDebugger }
cl.sendRequest ("test/runTests", request)
|> Promise.map (fun (res: Types.PlainNotification) -> res.content |> ofJson<RunTestsResult>)
let private createClient (opts: Executable) =
let options: ServerOptions = U5.Case2 {| run = opts; debug = opts |}
let fileDeletedWatcher =
workspace.createFileSystemWatcher (U2.Case1 "**/*.{fs,fsx}", true, true, false)
let clientOpts =
let opts = createEmpty<Client.LanguageClientOptions>
let mutable initFails = 0
let initializationFailureHandler (error: U3<ResponseError, Exception, obj option>) =
if initFails < 5 then
logger.Error($"Initialization failed: %%", error)
initFails <- initFails + 1
true
else
false
let restarts = new ResizeArray<_>()
let errorHandling =
{
new ErrorHandler with
member this.closed() : CloseAction =
restarts.Add(1)
if restarts |> Seq.length < 5 then
CloseAction.Restart
else
logger.Error("Server closed")
CloseAction.DoNotRestart
member this.error(error: Exception, message: Message, count: float) : ErrorAction =
logger.Error($"Error from server: {error} {message} {count}")
if count < 3.0 then
ErrorAction.Continue
else
ErrorAction.Shutdown
}
let initOpts = createObj [ "AutomaticWorkspaceInit" ==> false ]
let synch = createEmpty<Client.SynchronizeOptions>
synch.configurationSection <- Some !^ "FSharp"
synch.fileEvents <- Some(!^ ResizeArray([ fileDeletedWatcher ]))
// this type needs to be updated on the bindings - DocumentSelector is a (string|DocumentFilter) [] now only.
// that's why we need to coerce it here.
opts.documentSelector <- Some selector
opts.synchronize <- Some synch
opts.errorHandler <- Some errorHandling
opts.revealOutputChannelOn <- Some Client.RevealOutputChannelOn.Never
// Worth keeping around for debug purposes
// opts.traceOutputChannel <- Some defaultOutputChannel
// opts.outputChannel <- Some defaultOutputChannel
opts.initializationFailedHandler <- Some(!!initializationFailureHandler)
opts.initializationOptions <- Some !^(Some initOpts)
opts?markdown <- createObj [ "isTrusted" ==> true; "supportHtml" ==> true ]
opts
let cl = LanguageClient("FSharp", "F#", options, clientOpts, false)
client <- Some cl
cl
let getOptions (c: ExtensionContext) : JS.Promise<Executable> =
promise {
let openTelemetryEnabled = "FSharp.openTelemetry.enabled" |> Configuration.get false
let enableProjectGraph =
"FSharp.enableMSBuildProjectGraph" |> Configuration.get false
let useTransparentCompiler =
"FSharp.fcs.transparentCompiler.enabled" |> Configuration.get false
let tryBool x =
// Boolean.TryParse generates: TypeError: e.match is not a function if we don't call toString first
match Boolean.TryParse(x.ToString()) with
| (true, v) -> Some v
| _ -> None
let tryInt x =
match Int32.TryParse(x.ToString()) with
| (true, v) -> Some v
| _ -> None
let oldgcConserveMemory =
"FSharp.fsac.conserveMemory"
|> Configuration.tryGet
|> Option.map string
|> Option.bind tryBool
let gcConserveMemory =
"FSharp.fsac.gc.conserveMemory" |> Configuration.tryGet |> Option.bind tryInt
let gcConserveMemory =
// prefer new setting, fallback to old, default is 0
match gcConserveMemory, oldgcConserveMemory with
| Some x, _ -> x
| None, Some true -> 9
| None, _ -> 0
let gcServer = "FSharp.fsac.gc.server" |> Configuration.get true
let gcServerUseDatas: bool option =
"FSharp.fsac.gc.useDatas" |> Configuration.tryGet |> Option.bind tryBool
let gcNoAffinitize =
"Fsharp.fsac.gc.noAffinitize" |> Configuration.tryGet |> Option.bind tryBool
let gcHeapCount =
"FSharp.fsac.gc.heapCount" |> Configuration.tryGet |> Option.bind tryInt
let parallelReferenceResolution =
"FSharp.fsac.parallelReferenceResolution" |> Configuration.get false
let fsacAttachDebugger = "FSharp.fsac.attachDebugger" |> Configuration.get false
let fsacNetcorePath = "FSharp.fsac.netCoreDllPath" |> Configuration.get ""
let fsacSilencedLogs = "FSharp.fsac.silencedLogs" |> Configuration.get [||]
let verbose = "FSharp.verboseLogging" |> Configuration.get false
/// given a set of tfms and a target tfm, find the first of the set that satisfies the target.
/// if no target is found, use the 'latest' tfm
/// e.g. [net6.0, net7.0] + net8.0 -> net7.0
/// e.g. [net6.0, net7.0] + net7.0 -> net7.0
let findBestTFM (availableTFMs: string seq) (tfm: string) =
let tfmToSemVer (t: string) =
t.Replace("netcoreapp", "").Replace("net", "").Split([| '.' |], 2)
|> fun ver -> semver.parse (!! $"{ver[0]}.{ver[1]}.0")
let tfmMap =
availableTFMs
|> Seq.choose (fun tfm ->
match tfmToSemVer tfm with
| Some v -> Some(tfm, v)
| None -> None)
|> Seq.sortBy (fun (_, v) -> v.major, v.minor)
printfn $"choosing from among %A{tfmMap}"
match tfmToSemVer tfm with
| None ->
printfn "unable to parse target tfm, using latest"
Seq.last availableTFMs
| Some ver ->
tfmMap
|> Seq.skipWhile (fun (_, v) -> (semver.compare (!!v, !!ver)) = enum -1) // skip while fsac tfm is less than target tfm
|> Seq.tryHead // get first fsac tfm that is greater than or equal to target tfm
|> Option.map fst
|> Option.defaultWith (fun () -> Seq.last availableTFMs)
let probePathForTFMs (basePath: string) (tfm: string) =
let availableTFMs =
node.fs.readdirSync (!!basePath) |> Seq.filter (fun p -> p.StartsWith "net") // there are loose files in the basePath, ignore those
printfn $"Available FSAC TFMs: %A{availableTFMs}"
if availableTFMs |> Seq.contains tfm then
printfn "TFM match found"
tfm, node.path.join (basePath, tfm, "fsautocomplete.dll")
else
// find best-matching
let tfm = findBestTFM availableTFMs tfm
tfm, node.path.join (basePath, tfm, "fsautocomplete.dll")
let isNetFolder (folder: string) =
printfn $"checking folder %s{folder}"
let baseName = node.path.basename folder
baseName.StartsWith("net")
&& let stat = node.fs.statSync (!!folder) in
stat.isDirectory ()
/// locates the FSAC dll and TFM for that dll given a host TFM
let fsacPathForTfm (tfm: string) : string * string =
match fsacNetcorePath with
| null
| "" ->
// user didn't specify a path, so use FSAC from our extension
let binPath = node.path.join (VSCodeExtension.ionidePluginPath (), "bin")
probePathForTFMs binPath tfm
| userSpecified ->
if userSpecified.EndsWith ".dll" then
let tfm = node.path.basename (node.path.dirname userSpecified)
tfm, userSpecified
else
// if dir has tfm folders, probe
let filesAndFolders =
node.fs.readdirSync (!!userSpecified)
|> Seq.map (fun child -> node.path.join ([| userSpecified; child |]))
printfn $"candidates: %A{filesAndFolders}"
if filesAndFolders |> Seq.exists isNetFolder then
// tfm directories found, probe this directory like we would our own bin path
probePathForTFMs userSpecified tfm
else
// no tfm paths, try to use `fsautocomplete.dll` from this directory
let tfm = node.path.basename (node.path.dirname userSpecified)
tfm, node.path.join (userSpecified, "fsautocomplete.dll")
let tfmForSdkVersion (v: SemVer) =
match int v.major, int v.minor with
| 3, 1 -> "netcoreapp3.1"
| 3, 0 -> "netcoreapp3.0"
| 2, 1 -> "netcoreapp2.1"
| 2, 0 -> "netcoreapp2.0"
| n, _ -> $"net{n}.0"
let discoverDotnetArgs () =
promise {
let! sdkVersionAtRootPath = sdkVersion ()
match sdkVersionAtRootPath with
| Error e ->
printfn $"Error finding dotnet version: {e}"
return failwith "Error finding dotnet version, do you have dotnet installed and on the PATH?"
| Ok sdkVersion ->
printfn "Parsed SDK version at root path: %s" sdkVersion.raw
let sdkTfm = tfmForSdkVersion sdkVersion
printfn "Parsed SDK version to tfm: %s" sdkTfm
let fsacTfm, fsacPath = fsacPathForTfm sdkTfm
printfn "Parsed TFM to fsac path: %s" fsacPath
let userDotnetArgs = "FSharp.fsac.dotnetArgs" |> Configuration.get [||]
let hasUserRollForward =
userDotnetArgs
|> Array.tryFindIndex (fun a -> a = "--roll-forward")
|> Option.map (fun _ -> true)
|> Option.defaultValue false
let hasUserFxVersion =
userDotnetArgs
|> Array.tryFindIndex (fun a -> a = "--fx-version")
|> Option.map (fun _ -> true)
|> Option.defaultValue false
let shouldApplyImplicitRollForward =
not (hasUserFxVersion || hasUserRollForward) && sdkTfm <> fsacTfm // if the SDK doesn't match one of our FSAC TFMs, then we're in compat mode
let args = userDotnetArgs
let envVariables =
[ if shouldApplyImplicitRollForward then
"DOTNET_ROLL_FORWARD", box "LatestMajor"
match sdkVersion.prerelease with
| null -> ()
| pres when Seq.length pres > 0 -> "DOTNET_ROLL_FORWARD_TO_PRERELEASE", box 1
| _ -> () ]
return args, envVariables, fsacPath, sdkVersion
}
/// Converts true to 1 and false to 0
/// Useful for environment variables that require this semantic
let inline boolToInt b = if b then 1 else 0
let spawnNetCore dotnet : JS.Promise<Executable> =
promise {
let! (fsacDotnetArgs, fsacEnvVars, fsacPath, sdkVersion) = discoverDotnetArgs ()
// Only set DOTNET_GCHeapCount if we're on .NET 7 or higher
// .NET 6 has some issues with this env var on linux
// https://github.com/ionide/ionide-vscode-fsharp/issues/1899
let versionSupportingDATASGCMode = (semver.parse (Some(U2.Case1 "9.0.0"))).Value
let isdotnet8 = sdkVersion.major = 8
let isNet9orHigher =
semver.cmp (U2.Case2 sdkVersion, Operator.GTE, U2.Case2 versionSupportingDATASGCMode)
// datas is on by 9
let useDatas =
match gcServerUseDatas with
| Some b -> b
| None -> isNet9orHigher
let gcNoAffinitize =
match gcNoAffinitize with
| Some b -> b
| None -> isdotnet8 || not useDatas // no need to affinitize on 9 because Datas
let gcHeapCount =
match gcHeapCount with
| Some i -> Some i
| None -> if isdotnet8 then Some 2 else None
let fsacEnvVars =
[ yield! fsacEnvVars
if useDatas then
// DATAS and affinitization/heap management seem to be mutually exclusive, so we enforce that here.
yield "DOTNET_GCDynamicAdaptationMode", box (boolToInt useDatas) // https://learn.microsoft.com/en-us/dotnet/core/runtime-config/garbage-collector#dynamic-adaptation-to-application-sizes-datas
else
// it doesn't really make sense to set GCNoAffinitize without setting GCHeapCount
yield "DOTNET_GCNoAffinitize", box (boolToInt gcNoAffinitize) // https://learn.microsoft.com/en-us/dotnet/core/runtime-config/garbage-collector#affinitize
yield!
gcHeapCount
|> Option.map (fun hc -> "DOTNET_GCHeapCount", box (hc.ToString("X")))
|> Option.toList // Requires hexadecimal value https://learn.microsoft.com/en-us/dotnet/core/runtime-config/garbage-collector#heap-count
yield "DOTNET_GCConserveMemory", box gcConserveMemory //https://learn.microsoft.com/en-us/dotnet/core/runtime-config/garbage-collector#conserve-memory
yield "DOTNET_GCServer", box (boolToInt gcServer) // https://learn.microsoft.com/en-us/dotnet/core/runtime-config/garbage-collector#workstation-vs-server
if parallelReferenceResolution then
yield "FCS_ParallelReferenceResolution", box "true" ]
logger.Debug $"""FSAC (NETCORE): '%s{fsacPath}'"""
let exeOpts = createEmpty<ExecutableOptions>
let exeEnv =
match fsacEnvVars with
| [] -> None
| fsacEnvVars ->
// only need to set the process env if FSAC needs rollfoward env vars.
let keys = Node.Util.Object.keys node.``process``.env
let baseEnv = keys |> Seq.toList |> List.map (fun k -> k, node.``process``.env?(k))
let combinedEnv = baseEnv @ fsacEnvVars |> ResizeArray
let envObj = createObj combinedEnv
Some envObj
exeOpts.env <- exeEnv
let additionalFSACArgs = "FSharp.fsac.fsacArgs" |> Configuration.get [||]
let args =
[ yield! fsacDotnetArgs
yield fsacPath
if fsacAttachDebugger then
yield "--attachdebugger"
yield "--wait-for-debugger"
if enableProjectGraph then
yield "--project-graph-enabled"
if openTelemetryEnabled then