-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathHelpers.fs
More file actions
818 lines (668 loc) · 28.1 KB
/
Helpers.fs
File metadata and controls
818 lines (668 loc) · 28.1 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
module Helpers
open System
open Expecto
open System.IO
open FsAutoComplete.Lsp
open Ionide.LanguageServerProtocol
open Ionide.LanguageServerProtocol.Types
open Ionide.LanguageServerProtocol.JsonRpc
open FsAutoComplete
open FsAutoComplete.LspHelpers
open FSharp.Control.Reactive
open System.Threading
open FSharp.UMX
module Expecto =
open System.Threading.Tasks
let inline testBuilderWithTimeout (ts: TimeSpan) name testCase focus =
TestLabel(name, TestCase(Test.timeout (int ts.TotalMilliseconds) (testCase), focus), focus)
let inline testCaseWithTimeout (ts: TimeSpan) name test = testBuilderWithTimeout ts name (Sync test) Normal
let inline ftestCaseWithTimeout (ts: TimeSpan) name test = testBuilderWithTimeout ts name (Sync test) Focused
let inline ptestCaseWithTimeout (ts: TimeSpan) name test = testBuilderWithTimeout ts name (Sync test) Pending
let inline testCaseAsyncWithTimeout (ts: TimeSpan) name test = testBuilderWithTimeout ts name (Async test) Normal
let inline ftestCaseAsyncWithTimeout (ts: TimeSpan) name test = testBuilderWithTimeout ts name (Async test) Focused
let inline ptestCaseAsyncWithTimeout (ts: TimeSpan) name test = testBuilderWithTimeout ts name (Async test) Pending
let inline testCaseTaskWithTimeout (ts: TimeSpan) name (test: unit -> Task<unit>) =
testCaseAsyncWithTimeout ts name (async.Delay(fun () -> Async.AwaitTask(test ())))
let inline ftestCaseTaskWithTimeout (ts: TimeSpan) name (test: unit -> Task<unit>) =
ftestCaseAsyncWithTimeout ts name (async.Delay(fun () -> Async.AwaitTask(test ())))
let inline ptestCaseTaskWithTimeout (ts: TimeSpan) name (test: unit -> Task<unit>) =
ptestCaseAsyncWithTimeout ts name (async.Delay(fun () -> Async.AwaitTask(test ())))
// millisecond
let DEFAULT_TIMEOUT =
Environment.GetEnvironmentVariable "FSAC_TEST_DEFAULT_TIMEOUT"
|> Option.ofObj
|> Option.bind (fun x ->
match Int32.TryParse x with
| (true, v) -> Some v
| _ -> None)
|> Option.defaultValue (60000)
|> float
|> TimeSpan.FromMilliseconds
/// Contains testCase functions that have a `DEFAULT_TIMEOUT` set to them
module ShadowedTimeouts =
let testCase = testCaseWithTimeout DEFAULT_TIMEOUT
let ptestCase = ptestCaseWithTimeout DEFAULT_TIMEOUT
let ftestCase = ftestCaseWithTimeout DEFAULT_TIMEOUT
let testCaseAsync = testCaseAsyncWithTimeout DEFAULT_TIMEOUT
let ptestCaseAsync = ptestCaseAsyncWithTimeout DEFAULT_TIMEOUT
let ftestCaseAsync = ftestCaseAsyncWithTimeout DEFAULT_TIMEOUT
let rec private copyDirectory (sourceDir: DirectoryInfo) destDir =
// Get the subdirectories for the specified directory.
// let dir = DirectoryInfo(sourceDir)
if not sourceDir.Exists then
raise (DirectoryNotFoundException("Source directory does not exist or could not be found: " + sourceDir.FullName))
let dirs = sourceDir.GetDirectories()
// If the destination directory doesn't exist, create it.
Directory.CreateDirectory(destDir) |> ignore
// Get the files in the directory and copy them to the new location.
sourceDir.GetFiles()
|> Seq.iter (fun file ->
let tempPath = Path.Combine(destDir, file.Name)
file.CopyTo(tempPath, false) |> ignore)
// If copying subdirectories, copy them and their contents to new location.
dirs
|> Seq.iter (fun dir ->
let tempPath = Path.Combine(destDir, dir.Name)
copyDirectory dir tempPath)
type DisposableDirectory(directory: string, deleteParentDir) =
static member Create(?name: string) =
let tempPath, deleteParentDir =
match name with
| Some name -> IO.Path.GetTempPath() </> Guid.NewGuid().ToString("n") </> name, true
| None -> IO.Path.Combine(IO.Path.GetTempPath(), Guid.NewGuid().ToString("n")), false
// printfn "Creating directory %s" tempPath
IO.Directory.CreateDirectory tempPath |> ignore
new DisposableDirectory(tempPath, deleteParentDir)
static member From(sourceDir: DirectoryInfo) =
let self = DisposableDirectory.Create(sourceDir.Name)
copyDirectory sourceDir self.DirectoryInfo.FullName
self
member x.DirectoryInfo: DirectoryInfo = IO.DirectoryInfo(directory)
interface IDisposable with
member x.Dispose() =
let dirToDelete =
if deleteParentDir then
x.DirectoryInfo.Parent
else
x.DirectoryInfo
let mutable attempts = 25
// Handle odd cases with windows file locking
while attempts > 0 do
try
IO.Directory.Delete(dirToDelete.FullName, true)
attempts <- 0
with _ ->
attempts <- attempts - 1
if attempts = 0 then
reraise ()
Thread.Sleep(15)
type Async =
/// Behaves like AwaitObservable, but calls the specified guarding function
/// after a subscriber is registered with the observable.
static member GuardedAwaitObservable (ev1: IObservable<'T1>) guardFunction =
async {
let! token = Async.CancellationToken // capture the current cancellation token
return!
Async.FromContinuations(fun (cont, econt, ccont) ->
// start a new mailbox processor which will await the result
MailboxProcessor.Start(
(fun (mailbox: MailboxProcessor<Choice<'T1, exn, OperationCanceledException>>) ->
async {
// register a callback with the cancellation token which posts a cancellation message
use __ =
token.Register(
(fun _ -> mailbox.Post(Choice3Of3(OperationCanceledException("The operation was cancelled.")))),
null
)
// subscribe to the observable: if an error occurs post an error message and post the result otherwise
use __ =
ev1.Subscribe(
{ new IObserver<'T1> with
member __.OnNext result = mailbox.Post(Choice1Of3 result)
member __.OnError exn = mailbox.Post(Choice2Of3 exn)
member __.OnCompleted() =
let msg =
"Cancelling the workflow, because the Observable awaited using AwaitObservable has completed."
mailbox.Post(Choice3Of3(OperationCanceledException(msg))) }
)
guardFunction () // call the guard function
// wait for the first of these messages and call the appropriate continuation function
let! message = mailbox.Receive()
match message with
| Choice1Of3 reply -> cont reply
| Choice2Of3 exn -> econt exn
| Choice3Of3 exn -> ccont exn
})
)
|> ignore)
}
/// Creates an asynchronous workflow that will be resumed when the
/// specified observables produces a value. The workflow will return
/// the value produced by the observable.
static member AwaitObservable(ev1: IObservable<'T1>) = Async.GuardedAwaitObservable ev1 ignore
/// Creates an asynchronous workflow that runs the asynchronous workflow
/// given as an argument at most once. When the returned workflow is
/// started for the second time, it reuses the result of the
/// previous execution.
static member Cache(input: Async<'T>) =
let agent =
MailboxProcessor<AsyncReplyChannel<_>>.Start(fun agent ->
async {
let! repl = agent.Receive()
let! res = input |> Async.Catch
repl.Reply(res)
while true do
let! repl = agent.Receive()
repl.Reply(res)
})
async {
let! result = agent.PostAndAsyncReply(id)
return
match result with
| Choice1Of2 v -> v
| Choice2Of2 exn -> raise exn
}
let logger = lazy Serilog.Log.Logger.ForContext("SourceContext", "LSPTests")
type Cacher<'t> = System.Reactive.Subjects.ReplaySubject<'t>
type ClientEvents = IObservable<string * obj>
module Range =
let rangeContainsPos (range: Range) (pos: Position) = range.Start <= pos && pos <= range.End
let record (cacher: Cacher<_>) =
fun name payload ->
cacher.OnNext(name, payload)
AsyncLspResult.success Unchecked.defaultof<_>
let createAdaptiveServer workspaceLoader sourceTextFactory useTransparentCompiler =
let serverInteractions = new Cacher<_>()
let recordNotifications = record serverInteractions
let recordRequests =
{ new Server.ClientRequestSender with
member __.Send name payload = record serverInteractions name payload }
let loader = workspaceLoader ()
let client = new FSharpLspClient(recordNotifications, recordRequests)
let server =
new AdaptiveFSharpLspServer(loader, client, sourceTextFactory, useTransparentCompiler)
server :> IFSharpLspServer, serverInteractions :> ClientEvents
let defaultConfigDto: FSharpConfigDto =
{ WorkspaceModePeekDeepLevel = None
ExcludeProjectDirectories = None
KeywordsAutocomplete = None
ExternalAutocomplete = None
FullNameExternalAutocomplete = None
Linter = None
LinterConfig = None
IndentationSize = None
UnionCaseStubGeneration = None
UnionCaseStubGenerationBody = None
RecordStubGeneration = None
RecordStubGenerationBody = None
AddPrivateAccessModifier = None
UnusedOpensAnalyzer = None
UnusedOpensAnalyzerExclusions = None
UnusedDeclarationsAnalyzer = None
UnusedDeclarationsAnalyzerExclusions = None
SimplifyNameAnalyzer = None
SimplifyNameAnalyzerExclusions = None
UnnecessaryParenthesesAnalyzer = None
UnnecessaryParenthesesAnalyzerExclusions = None
ResolveNamespaces = None
EnableReferenceCodeLens = None
EnableAnalyzers = None
AnalyzersPath = None
ExcludeAnalyzers = None
IncludeAnalyzers = None
DisableInMemoryProjectReferences = None
AutomaticWorkspaceInit = Some true
InterfaceStubGeneration = None
InterfaceStubGenerationObjectIdentifier = None
InterfaceStubGenerationMethodBody = None
LineLens = None
UseSdkScripts = Some true
DotNetRoot = None
FSIExtraParameters = None
FSIExtraInteractiveParameters = None
FSIExtraSharedParameters = None
FSICompilerToolLocations = None
TooltipMode = None
TooltipShowDocumentationLink = None
GenerateBinlog = Some true
AbstractClassStubGeneration = None
AbstractClassStubGenerationMethodBody = None
AbstractClassStubGenerationObjectIdentifier = None
CodeLenses =
Some
{ Signature = Some {| Enabled = Some true |}
References = Some {| Enabled = Some true |} }
InlayHints =
Some
{ typeAnnotations = Some true
parameterNames = Some true
disableLongTooltip = Some true }
PipelineHints =
Some
{ Enabled = Some true
Prefix = Some "//" }
Notifications = None
Fsac = None }
let clientCaps: ClientCapabilities =
let didChangeConfigCaps: DidChangeConfigurationClientCapabilities =
{ DynamicRegistration = Some true }
let executeCommandsClientCaps: ExecuteCommandClientCapabilities =
{ DynamicRegistration = Some true }
let workspaceCaps: WorkspaceClientCapabilities =
let weCaps: WorkspaceEditClientCapabilities =
{ DocumentChanges = Some true
ResourceOperations = None
FailureHandling = None
NormalizesLineEndings = None
ChangeAnnotationSupport = None }
let symbolCaps: WorkspaceSymbolClientCapabilities =
{ DynamicRegistration = Some true
SymbolKind = None
TagSupport = None
ResolveSupport = None }
let semanticTokenCaps: SemanticTokensWorkspaceClientCapabilities =
{ RefreshSupport = Some true }
let inlayHintCaps: InlayHintWorkspaceClientCapabilities =
{ RefreshSupport = Some false }
let inlineValueCaps: InlineValueWorkspaceClientCapabilities =
{ RefreshSupport = Some false }
let codeLensCaps: CodeLensWorkspaceClientCapabilities =
{ RefreshSupport = Some true }
{ ApplyEdit = Some true
WorkspaceEdit = Some weCaps
DidChangeConfiguration = Some didChangeConfigCaps
DidChangeWatchedFiles = None
Symbol = Some symbolCaps
SemanticTokens = Some semanticTokenCaps
InlayHint = Some inlayHintCaps
InlineValue = Some inlineValueCaps
CodeLens = Some codeLensCaps
ExecuteCommand = Some executeCommandsClientCaps
WorkspaceFolders = Some false
Configuration = Some true
FileOperations = None
Diagnostics = Some { RefreshSupport = Some false } }
let textCaps: TextDocumentClientCapabilities =
let syncCaps: TextDocumentSyncClientCapabilities =
{ DynamicRegistration = Some true
WillSave = Some true
WillSaveWaitUntil = Some true
DidSave = Some true }
let publishDiagCaps: PublishDiagnosticsClientCapabilities =
{ RelatedInformation = Some true
TagSupport = Some { ValueSet = [||] }
VersionSupport = Some false
CodeDescriptionSupport = Some true
DataSupport = Some false }
let compCaps: CompletionClientCapabilities =
{ DynamicRegistration = Some true
CompletionItem =
Some
{ SnippetSupport = Some true
CommitCharactersSupport = Some true
DocumentationFormat = None
DeprecatedSupport = Some false
PreselectSupport = Some false
TagSupport = None
InsertReplaceSupport = Some false
ResolveSupport = None
InsertTextModeSupport = None
LabelDetailsSupport = Some true }
CompletionItemKind = Some { ValueSet = None }
ContextSupport = Some true
InsertTextMode = Some InsertTextMode.AsIs
CompletionList = Some { ItemDefaults = None } }
let hoverCaps: HoverClientCapabilities =
{ DynamicRegistration = Some true
ContentFormat = Some [| MarkupKind.Markdown |] }
let sigCaps: SignatureHelpClientCapabilities =
{ DynamicRegistration = Some true
SignatureInformation =
Some
{ DocumentationFormat = Some [| MarkupKind.Markdown |]
ParameterInformation = Some { LabelOffsetSupport = Some true }
ActiveParameterSupport = Some true }
ContextSupport = Some true }
let docSymCaps: DocumentSymbolClientCapabilities =
{ DynamicRegistration = Some true
SymbolKind = Some { ValueSet = None }
HierarchicalDocumentSymbolSupport = Some false
TagSupport = None
LabelSupport = Some true }
let foldingRangeCaps: FoldingRangeClientCapabilities =
{ DynamicRegistration = Some true
LineFoldingOnly = Some true
RangeLimit = Some 100u
FoldingRange = Some { CollapsedText = Some true }
FoldingRangeKind = None }
let semanticTokensCaps: SemanticTokensClientCapabilities =
{ DynamicRegistration = Some true
Requests =
{ Range = Some(U2.C1 true)
Full = Some(U2.C1 true) }
TokenTypes = [||]
TokenModifiers = [||]
Formats = [| TokenFormat.Relative |]
OverlappingTokenSupport = None
MultilineTokenSupport = None
ServerCancelSupport = Some true
AugmentsSyntaxTokens = Some true }
let codeActionCaps =
{ DynamicRegistration = Some true
CodeActionLiteralSupport = None
IsPreferredSupport = None
DisabledSupport = None
DataSupport = None
ResolveSupport = None
HonorsChangeAnnotations = None }
let inlayHintCaps: InlayHintClientCapabilities =
{ DynamicRegistration = Some true
ResolveSupport = None }
let _inlineValueCaps: InlineValueClientCapabilities =
{ DynamicRegistration = Some true }
let renameCaps: RenameClientCapabilities =
{ DynamicRegistration = Some true
HonorsChangeAnnotations = Some false
PrepareSupport = Some false
PrepareSupportDefaultBehavior = Some PrepareSupportDefaultBehavior.Identifier }
let referenceCaps: ReferenceClientCapabilities = { DynamicRegistration = Some true }
let declarationCaps: DeclarationClientCapabilities =
{ DynamicRegistration = Some true
LinkSupport = Some false }
let defCaps: DefinitionClientCapabilities =
{ DynamicRegistration = Some true
LinkSupport = Some false }
let typeDefCaps: TypeDefinitionClientCapabilities =
{ DynamicRegistration = Some true
LinkSupport = Some false }
let implCaps: ImplementationClientCapabilities =
{ DynamicRegistration = Some true
LinkSupport = Some false }
let docLinkCaps: DocumentLinkClientCapabilities =
{ DynamicRegistration = Some true
TooltipSupport = Some true }
let diagCaps: DiagnosticClientCapabilities =
{ DynamicRegistration = Some true
RelatedDocumentSupport = Some true }
let highlightCaps: DocumentHighlightClientCapabilities =
{ DynamicRegistration = Some true }
let formattingCaps: DocumentFormattingClientCapabilities =
{ DynamicRegistration = Some true }
let rangeFormattingCaps: DocumentRangeFormattingClientCapabilities =
{ DynamicRegistration = Some true }
let onTypeFormattingCaps: DocumentOnTypeFormattingClientCapabilities =
{ DynamicRegistration = Some true }
let codeLensCaps: CodeLensClientCapabilities = { DynamicRegistration = Some true }
let selectionRangeCaps: SelectionRangeClientCapabilities =
{ DynamicRegistration = Some true }
let monikerCaps: MonikerClientCapabilities = { DynamicRegistration = Some true }
let colorProviderCaps: DocumentColorClientCapabilities =
{ DynamicRegistration = Some true }
let linkedEditingRangeCaps: LinkedEditingRangeClientCapabilities =
{ DynamicRegistration = Some true }
let inlineValueCaps: InlineValueClientCapabilities =
{ DynamicRegistration = Some true }
{ Synchronization = Some syncCaps
PublishDiagnostics = Some publishDiagCaps
Completion = Some compCaps
Hover = Some hoverCaps
SignatureHelp = Some sigCaps
References = Some referenceCaps
DocumentHighlight = Some highlightCaps
DocumentSymbol = Some docSymCaps
Formatting = Some formattingCaps
RangeFormatting = Some rangeFormattingCaps
OnTypeFormatting = Some onTypeFormattingCaps
Definition = Some defCaps
CodeAction = Some codeActionCaps
CodeLens = Some codeLensCaps
DocumentLink = Some docLinkCaps
Rename = Some renameCaps
FoldingRange = Some foldingRangeCaps
SelectionRange = Some selectionRangeCaps
SemanticTokens = Some semanticTokensCaps
InlayHint = Some inlayHintCaps
CallHierarchy = None
TypeHierarchy = None
Declaration = Some declarationCaps
TypeDefinition = Some typeDefCaps
Implementation = Some implCaps
ColorProvider = Some colorProviderCaps
LinkedEditingRange = Some linkedEditingRangeCaps
Moniker = Some monikerCaps
InlineValue = Some inlineValueCaps
Diagnostic = Some diagCaps }
{ Workspace = Some workspaceCaps
TextDocument = Some textCaps
Experimental = None
Window = None
General = None
NotebookDocument = None }
open Expecto.Logging
open Expecto.Logging.Message
open System.Threading
open FsAutoComplete.CommandResponse
open CliWrap
open CliWrap.Buffered
let logEvent (name, payload) = logger.Value.Debug("{name}: {payload}", name, payload)
let logDotnetRestore section line =
if not (String.IsNullOrWhiteSpace(line)) then
logger.Value.Debug("[{section}] dotnet restore: {line}", section, line)
let dotnetCleanup baseDir =
[ "obj"; "bin" ]
|> List.map (fun f -> Path.Combine(baseDir, f))
|> List.filter Directory.Exists
|> List.iter (fun path ->
try
Directory.Delete(path, true)
with _ ->
// On some platforms (e.g., macOS with .NET 10), the recursive delete can
// transiently fail with "Directory not empty" due to background .NET SDK processes
// still holding locks. Retry once after a short pause.
try
System.Threading.Thread.Sleep(200)
Directory.Delete(path, true)
with ex ->
// If cleanup still fails, log and continue. Stale build artifacts are not critical
// and the test should still run correctly against pre-existing output.
eprintfn $"Warning: dotnetCleanup could not delete {path}: {ex.Message}")
let runProcess (workingDir: string) (exePath: string) (args: string) =
async {
let! ctok = Async.CancellationToken
let! result =
Cli
.Wrap(exePath)
.WithArguments(args)
.WithWorkingDirectory(workingDir)
.WithValidation(CommandResultValidation.None)
.ExecuteBufferedAsync(ctok)
.Task
|> Async.AwaitTask
return result
}
let inline expectExitCodeZero (r: BufferedCommandResult) =
Expect.equal
r.ExitCode
0
$"Expected exit code zero but was %i{r.ExitCode}.\nStdOut: %s{r.StandardOutput}\nStdErr: %s{r.StandardError}"
let dotnetRestore dir =
async {
let! r = runProcess (DirectoryInfo(dir).FullName) "dotnet" "restore -v d"
return expectExitCodeZero r
}
let dotnetToolRestore dir =
async {
let! r = runProcess (DirectoryInfo(dir).FullName) "dotnet" "tool restore"
return expectExitCodeZero r
}
let serverInitialize path (config: FSharpConfigDto) createServer =
async {
dotnetCleanup path
for file in System.IO.Directory.EnumerateFiles(path, "*.fsproj", SearchOption.AllDirectories) do
do! file |> Path.GetDirectoryName |> dotnetRestore
let (server: IFSharpLspServer), clientNotifications = createServer ()
clientNotifications |> Observable.add logEvent
let p: InitializeParams =
{ ProcessId = Some 1
RootPath = Some path
RootUri = Some(sprintf "file://%s" path)
InitializationOptions = Some(Server.serialize config)
Capabilities = clientCaps
ClientInfo =
Some
{ Name = "FSAC Tests"
Version = Some "0.0.0" }
WorkspaceFolders =
Some
[| { Uri = Path.FilePathToUri path
Name = "Test Folder" } |]
Trace = None
Locale = None
WorkDoneToken = None }
let! result = server.Initialize p
match result with
| Result.Ok _res ->
do! server.Initialized()
return (server, clientNotifications)
| Result.Error _e -> return failwith "Initialization failed"
}
let loadDocument path : TextDocumentItem =
{ Uri = Path.FilePathToUri path
LanguageId = "fsharp"
Version = 0
Text = File.ReadAllText path }
let parseProject projectFilePath (server: IFSharpLspServer) =
async {
let projectParams: ProjectParms =
{ Project = { Uri = Path.FilePathToUri projectFilePath } }
let projectName = Path.GetFileNameWithoutExtension projectFilePath
let! result = server.FSharpProject projectParams
do! Async.Sleep(TimeSpan.FromSeconds 3.)
logger.Value.Debug("{project} parse result: {result}", projectName, result)
}
let (|UnwrappedPlainNotification|_|) eventType (notification: PlainNotification) : 't option =
notification.Content
|> JsonSerializer.readJson<ResponseMsg<'t>>
|> fun r -> if r.Kind = eventType then Some r.Data else None
let internal defaultTimeout = TimeSpan.FromSeconds 10.0
let waitForWorkspaceFinishedParsing (events: ClientEvents) =
let chooser (name, payload) =
match name with
| "fsharp/notifyWorkspace" ->
match unbox payload with
| (UnwrappedPlainNotification "workspaceLoad" (workspaceLoadResponse:
FsAutoComplete.CommandResponse.WorkspaceLoadResponse)) ->
if workspaceLoadResponse.Status = "finished" then
Some()
else
None
| _ -> None
| _ -> None
logger.Value.Debug "waiting for workspace to finish loading"
events
|> Observable.choose chooser
|> Observable.timeoutSpan defaultTimeout
|> Async.AwaitObservable
let private typedEvents<'t> (typ: string) : IObservable<string * obj> -> IObservable<'t> =
Observable.choose (fun (typ', _o) -> if typ' = typ then Some(unbox _o) else None)
let private getDiagnosticsEvents: IObservable<string * obj> -> IObservable<_> =
typedEvents<Ionide.LanguageServerProtocol.Types.PublishDiagnosticsParams> "textDocument/publishDiagnostics"
let private fileName (u: DocumentUri) = u.Split([| '/'; '\\' |], StringSplitOptions.RemoveEmptyEntries) |> Array.last
/// note that the files here are intended to be the filename only., not the full URI.
let private matchFiles (files: string Set) =
Observable.choose (fun (p: Ionide.LanguageServerProtocol.Types.PublishDiagnosticsParams) ->
let filename = fileName p.Uri
if Set.contains filename files then
Some(filename, p)
else
None)
let workspaceEdits: IObservable<string * obj> -> IObservable<_> =
typedEvents<ApplyWorkspaceEditParams> "workspace/applyEdit"
let editsFor (file: string) =
let fileAsUri = Path.FilePathToUri file
Observable.choose (fun (p: ApplyWorkspaceEditParams) ->
let edits = p.Edit.DocumentChanges.Value
let forFile =
edits
|> Array.collect (fun e ->
match e with
| U4.C1 e -> if e.TextDocument.Uri = fileAsUri then e.Edits else [||]
| _ -> [||])
match forFile with
| [||] -> None
| edits -> Some edits)
let fileDiagnostics (file: string) =
logger.Value.Information("waiting for events on file {file}", file)
getDiagnosticsEvents
>> matchFiles (Set.ofList [ file ])
>> Observable.map snd
>> Observable.map (fun d -> d.Diagnostics)
let fileDiagnosticsForUri (uri: string) =
logger.Value.Information("waiting for events on file {file}", uri)
getDiagnosticsEvents
>> Observable.choose (fun n -> if n.Uri = uri then Some n.Diagnostics else None)
let diagnosticsFromSource (desiredSource: String) =
Observable.choose (fun (diags: Diagnostic[]) ->
match
diags
|> Array.choose (fun d ->
match d.Source with
| Some s ->
if s.StartsWith(desiredSource, StringComparison.Ordinal) then
Some d
else
None
| None -> None)
with
| [||] -> None
| diags -> Some diags)
let analyzerDiagnostics file = fileDiagnostics file >> diagnosticsFromSource "F# Analyzers"
let linterDiagnostics file = fileDiagnostics file >> diagnosticsFromSource "F# Linter"
let fsacDiagnostics file = fileDiagnostics file >> diagnosticsFromSource "FSAC"
let compilerDiagnostics file = fileDiagnostics file >> diagnosticsFromSource "F# Compiler"
let diagnosticsToResult =
Observable.map (function
| [||] -> Ok()
| diags -> Core.Error diags)
let waitForParseResultsForFile file = fileDiagnostics file >> diagnosticsToResult >> Async.AwaitObservable
let waitForDiagnosticErrorForFile file =
fileDiagnostics file
>> Observable.choose (function
| [||] -> None
| diags -> Some diags)
>> diagnosticsToResult
>> Async.AwaitObservable
let waitForFsacDiagnosticsForFile file = fsacDiagnostics file >> diagnosticsToResult >> Async.AwaitObservable
let waitForCompilerDiagnosticsForFile file = compilerDiagnostics file >> diagnosticsToResult >> Async.AwaitObservable
let waitForParsedScript (event: ClientEvents) =
event
|> typedEvents<Ionide.LanguageServerProtocol.Types.PublishDiagnosticsParams> "textDocument/publishDiagnostics"
|> Observable.choose (fun n ->
let filename = n.Uri.Replace('\\', '/').Split('/') |> Array.last
if filename = "Script.fs" then Some n else None)
|> Async.AwaitObservable
let waitForTestDetected (fileName: string) (events: ClientEvents) : Async<TestDetectedNotification> =
typedEvents<TestDetectedNotification> "fsharp/testDetected" events
|> Observable.filter (fun (tdn: TestDetectedNotification) ->
let testNotificationFileName = Path.GetFileName(tdn.File)
testNotificationFileName = fileName)
|> Async.AwaitObservable
let waitForEditsForFile file = workspaceEdits >> editsFor file >> Async.AwaitObservable
let trySerialize (t: string) : 't option =
try
JsonSerializer.readJson t |> Some
with _ ->
None
let (|As|_|) (m: PlainNotification) : 't option =
match trySerialize m.Content with
| Some(r: FsAutoComplete.CommandResponse.ResponseMsg<'t>) -> Some r.Data
| None -> None
let (|CodeActions|_|) (t: TextDocumentCodeActionResult) =
let actions =
t
|> Array.choose (function
| U2.C2 action -> Some action
| _ -> None)
match actions with
| [||] -> None
| actions -> Some actions