/
Program.fs
1268 lines (1209 loc) · 74.3 KB
/
Program.fs
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
module Main.Program
open LSP
open LSP.Types
open System
open System.IO
open CWTools.Parser
open CWTools.Parser.UtilityParser
open CWTools.Parser.Types
open CWTools.Common
open CWTools.Common.STLConstants
open CWTools.Games
open FParsec
open System.Threading.Tasks
open System.Text
open System.Reflection
open System.IO
open System.Runtime.InteropServices
open FSharp.Data
open LSP
open CWTools.Validation.ValidationCore
open System
open CWTools.Rules
open System.Xml.Schema
open CWTools.Games.Files
open LSP.Json.Ser
open System.ComponentModel
open CWTools.Games.Stellaris
open CWTools.Games.Stellaris.STLLookup
open MBrace.FsPickler
open CWTools.Process
open CWTools.Utilities.Position
open CWTools.Games.EU4
open Main.Serialize
open Main.Git
open FSharp.Data
open System.Diagnostics
open Main.Lang
open Main.Lang.GameLoader
open Main.Lang.LanguageServerFeatures
open Main.Completion
open CWTools.Utilities.Utils
let private TODO() = raise (Exception "TODO")
[<assembly: AssemblyDescription("CWTools language server for PDXScript")>]
do()
// client.LogMessage { ``type`` = MessageType.Error; message = "error"}
// client.LogMessage { ``type`` = MessageType.Warning; message = "warning"}
// client.LogMessage { ``type`` = MessageType.Info; message = "info"}
// client.LogMessage { ``type`` = MessageType.Log; message = "log"}
let mutable diagnosticLogging = false
let setupLogger(client :ILanguageClient) =
let logInfo = (fun m -> client.LogMessage { ``type`` = MessageType.Info; message = m} )
let logWarning = (fun m -> client.LogMessage { ``type`` = MessageType.Warning; message = m} )
let logError = (fun m -> client.LogMessage { ``type`` = MessageType.Error; message = m} )
let logDiag = (fun m -> if diagnosticLogging then client.LogMessage { ``type`` = MessageType.Log; message = sprintf "[Diag - %s] %s" (System.DateTime.Now.ToString("HH:mm:ss")) m})
CWTools.Utilities.Utils.logInfo <- logInfo
CWTools.Utilities.Utils.logWarning <- logWarning
CWTools.Utilities.Utils.logError <- logError
CWTools.Utilities.Utils.logDiag <- logDiag
type LintRequestMsg =
| UpdateRequest of VersionedTextDocumentIdentifier * bool
| WorkComplete of DateTime
type Server(client: ILanguageClient) =
do setupLogger(client)
let docs = DocumentStore()
let projects = ProjectManager()
let notFound (doc: Uri) (): 'Any =
raise (Exception (sprintf "%s does not exist" (doc.ToString())))
let mutable docErrors : DocumentHighlight list = []
let mutable activeGame = STL
let mutable isVanillaFolder = false
let mutable gameObj : option<IGame> = None
let mutable stlGameObj : option<IGame<STLComputedData>> = None
let mutable hoi4GameObj : option<IGame<HOI4ComputedData>> = None
let mutable eu4GameObj : option<IGame<EU4ComputedData>> = None
let mutable ck2GameObj : option<IGame<CK2ComputedData>> = None
let mutable irGameObj : option<IGame<IRComputedData>> = None
let mutable vic2GameObj : option<IGame<VIC2ComputedData>> = None
let mutable ck3GameObj : option<IGame<CK3ComputedData>> = None
let mutable vic3GameObj : option<IGame<VIC3ComputedData>> = None
let mutable customGameObj : option<IGame<JominiComputedData>> = None
let mutable languages : Lang list = []
let mutable rootUri : Uri option = None
let mutable workspaceFolders : WorkspaceFolder list = []
let mutable cachePath : string option = None
let mutable stlVanillaPath : string option = None
let mutable hoi4VanillaPath : string option = None
let mutable eu4VanillaPath : string option = None
let mutable ck2VanillaPath : string option = None
let mutable irVanillaPath : string option = None
let mutable vic2VanillaPath : string option = None
let mutable ck3VanillaPath : string option = None
let mutable vic3VanillaPath : string option = None
let mutable stellarisCacheVersion : string option = None
let mutable eu4CacheVersion : string option = None
let mutable hoi4CacheVersion : string option = None
let mutable ck2CacheVersion : string option = None
let mutable irCacheVersion : string option = None
let mutable vic2CacheVersion : string option = None
let mutable ck3CacheVersion : string option = None
let mutable vic3CacheVersion : string option = None
let mutable remoteRepoPath : string option = None
let mutable rulesChannel : string = "stable"
let mutable manualRulesFolder : string option = None
let mutable useManualRules : bool = false
let mutable validateVanilla : bool = false
let mutable experimental : bool = false
let mutable debugMode : bool = false
let mutable maxFileSize : int = 2
let mutable generatedStrings : string = ":0 \"REPLACE_ME\""
let mutable ignoreCodes : string list = []
let mutable ignoreFiles : string list = []
let mutable dontLoadPatterns : string list = []
let mutable locCache : Map<string, CWError list> = Map.empty
let mutable lastFocusedFile : string option = None
let mutable currentlyRefreshingFiles : bool = false
let (|TrySuccess|TryFailure|) tryResult =
match tryResult with
| true, value -> TrySuccess value
| _ -> TryFailure
let sevToDiagSev =
function
| Severity.Error -> DiagnosticSeverity.Error
| Severity.Warning -> DiagnosticSeverity.Warning
| Severity.Information -> DiagnosticSeverity.Information
| Severity.Hint -> DiagnosticSeverity.Hint
let parserErrorToDiagnostics e =
let code, sev, file, error, (position : range), length, related = e
let startC, endC =
match length with
| 0 -> 0,( int position.StartColumn)
| x ->(int position.StartColumn),(int position.StartColumn) + length
let startLine = (int position.StartLine) - 1
let startLine = max startLine 0
let createUri (f : string) = (match Uri.TryCreate(f, UriKind.Absolute) with |TrySuccess value -> value |TryFailure -> logWarning f; Uri "/")
let result =
{
range = {
start = {
line = startLine
character = startC
}
``end`` = {
line = startLine
character = endC
}
}
severity = Some (sevToDiagSev sev)
code = Some code
source = Some code
message = error
relatedInformation = related |> Option.map (List.map (fun rel -> { DiagnosticRelatedInformation.location = { uri = createUri rel.location.FileName; range = convRangeToLSPRange rel.location}; message = rel.message})) |> Option.defaultValue []
}
(file, result)
let sendDiagnostics s =
let diagnosticFilter ((f : string), d) =
match (f, d) with
| _, {Diagnostic.code = Some code} when List.contains code ignoreCodes -> false
| f, _ when List.contains (Path.GetFileName f) ignoreFiles -> false
| _, _ -> true
// logDiag (sprintf "sd %A" s)
s |> List.groupBy fst
|> List.map ((fun (f, rs) -> f, rs |> List.filter (diagnosticFilter)) >>
(fun (f, rs) ->
try {uri = (match Uri.TryCreate(f, UriKind.Absolute) with |TrySuccess value -> value |TryFailure -> logWarning f; Uri "/") ; diagnostics = List.map snd rs} with |e -> failwith (sprintf "%A %A" e rs)))
|> List.iter (client.PublishDiagnostics)
// let updateDebugBar (message : string) =
// client.CustomNotification ("debugBar", JsonValue.Record [| "value", JsonValue.String(message); "enable", JsonValue.Boolean(true) |])
// let hideDebugBar() =
// client.CustomNotification ("debugBar", JsonValue.Record [| "enable", JsonValue.Boolean(false) |])
let mutable delayedLocUpdate = false
let lint (doc: Uri) (shallowAnalyze : bool) (forceDisk : bool) : Async<unit> =
async {
let name =
if System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && doc.LocalPath.StartsWith "/"
then doc.LocalPath.Substring(1)
else doc.LocalPath
if name.EndsWith(".yml") then delayedLocUpdate <- true else ()
let filetext = if forceDisk then None else docs.GetText (FileInfo(doc.LocalPath))
let getRange (start: FParsec.Position) (endp : FParsec.Position) = mkRange start.StreamName (mkPos (int start.Line) (int start.Column)) (mkPos (int endp.Line) (int endp.Column))
let parserErrors =
match docs.GetText (FileInfo(doc.LocalPath)) with
|None -> []
|Some t ->
let parsed = CKParser.parseString t name
match name, parsed with
| x, _ when x.EndsWith(".yml") -> []
| _, Success(_,_,_) -> []
| _, Failure(msg,p,s) -> [("CW001", Severity.Error, name, msg, (getRange p.Position p.Position), 0, None)]
let locErrors =
locCache.TryFind (doc.LocalPath) |> Option.defaultValue [] |> List.map (fun e -> (e.code, e.severity, e.range.FileName, e.message, e.range, e.keyLength, e.relatedErrors))
// logDiag (sprintf "lint le %A" (locCache.TryFind (doc.LocalPath) |> Option.defaultValue []))
let errors =
parserErrors @
locErrors @
match gameObj with
|None -> []
|Some game ->
let results = game.UpdateFile shallowAnalyze name filetext
// logDiag (sprintf "lint uf %A" results)
results |> List.map (fun e -> (e.code, e.severity, e.range.FileName, e.message, e.range, e.keyLength, e.relatedErrors) )
match errors with
| [] -> client.PublishDiagnostics {uri = doc; diagnostics = []}
| x -> x
|> List.map parserErrorToDiagnostics
|> sendDiagnostics
//let compilerOptions = projects.FindProjectOptions doc |> Option.defaultValue emptyCompilerOptions
// let! parseResults, checkAnswer = checker.ParseAndCheckFileInProject(name, version, source, projectOptions)
// for error in parseResults.Errors do
// eprintfn "%s %d:%d %s" error.FileName error.StartLineAlternate error.StartColumn error.Message
// match checkAnswer with
// | FSharpCheckFileAnswer.Aborted -> eprintfn "Aborted checking %s" name
// | FSharpCheckFileAnswer.Succeeded checkResults ->
// for error in checkResults.Errors do
// eprintfn "%s %d:%d %s" error.FileName error.StartLineAlternate error.StartColumn error.Message
}
let mutable delayTime = TimeSpan(0,0,30)
let delayedAnalyze() =
match gameObj with
|Some game ->
let stopwatch = Stopwatch()
stopwatch.Start()
game.RefreshCaches();
if delayedLocUpdate
then
logDiag "delayedLocUpdate true"
game.RefreshLocalisationCaches();
delayedLocUpdate <- false
locCache <- game.LocalisationErrors(true, true) |> List.groupBy (fun e -> e.range.FileName) |> Map.ofList
// eprintfn "lc update %A" locCache
else
logDiag "delayedLocUpdate false"
locCache <- game.LocalisationErrors(false, true) |> List.groupBy (fun e -> e.range.FileName) |> Map.ofList
// eprintfn "lc update light %A" locCache
stopwatch.Stop()
let time = stopwatch.Elapsed
delayTime <- TimeSpan(Math.Min(TimeSpan(0,0,60).Ticks, Math.Max(TimeSpan(0,0, 5).Ticks, 3L * time.Ticks)))
GC.Collect(2, System.GCCollectionMode.Optimized, false, false)
|None -> ()
let lintAgent =
MailboxProcessor.Start(
(fun agent ->
let mutable nextAnalyseTime = DateTime.Now
let analyzeTask uri force =
new Task(
fun () ->
let mutable nextTime = nextAnalyseTime
try
try
let shallowAnalyse = DateTime.Now < nextTime
logDiag (sprintf "lint force: %b, shallow: %b" force shallowAnalyse)
lint uri (shallowAnalyse && (not(force))) false |> Async.RunSynchronously
if not(shallowAnalyse)
then
delayedAnalyze();
logDiag "lint after delayed"
/// Somehow get updated localisation errors after loccache is updated
lint uri true false |> Async.RunSynchronously
nextTime <- DateTime.Now.Add(delayTime);
else ()
with
| e -> logError (sprintf "uri %A \n exception %A" uri.LocalPath e)
finally
agent.Post (WorkComplete (nextTime)))
let analyze (file : VersionedTextDocumentIdentifier) force =
//eprintfn "Analyze %s" (file.uri.ToString())
let task = analyzeTask file.uri force
//let task = new Task((fun () -> lint (file.uri) false false |> Async.RunSynchronously; agent.Post (WorkComplete ())))
task.Start()
let rec loop (inprogress : bool) (state : Map<string, VersionedTextDocumentIdentifier * bool>) =
async{
// hideDebugBar()
let! msg = agent.Receive()
if state.Count > 0 then
logDiag (sprintf "queue length: %i" state.Count)
// updateDebugBar (sprintf "queue length: %i" state.Count)
match msg, inprogress with
| UpdateRequest (ur, force), false ->
analyze ur force
return! loop true state
| UpdateRequest (ur, force), true ->
if Map.containsKey ur.uri.LocalPath state
then
if (Map.find ur.uri.LocalPath state) |> (fun ({VersionedTextDocumentIdentifier.version = v}, _) -> v < ur.version)
then
return! loop inprogress (state |> Map.add ur.uri.LocalPath (ur, force))
else
return! loop inprogress state
else
return! loop inprogress (state |> Map.add ur.uri.LocalPath (ur, force))
| WorkComplete time, _ ->
nextAnalyseTime <- time
if Map.isEmpty state
then
return! loop false state
else
let key, (next, force) = state |> Map.pick (fun k v -> (k, v) |> function | (k, v) -> Some (k, v))
let newstate = state |> Map.remove key
analyze next force
return! loop true newstate
}
loop false Map.empty
)
)
let setupRulesCaches() =
match cachePath, remoteRepoPath, useManualRules with
| Some cp, Some rp, false ->
let stable = rulesChannel <> "latest"
match initOrUpdateRules rp cp stable true with
| true, Some date ->
let text = sprintf "Validation rules for %O have been updated to %O." activeGame date
client.CustomNotification ("forceReload", JsonValue.String(text))
| _ -> ()
| _ -> ()
let checkOrSetGameCache(forceCreate : bool) =
match (cachePath, isVanillaFolder, activeGame) with
| _, _, Custom -> ()
| Some cp, false, _ ->
let gameCachePath = cp+"/../"
let doesCacheExist =
match activeGame with
| STL -> File.Exists (gameCachePath + "stl.cwb")
| HOI4 -> File.Exists (gameCachePath + "hoi4.cwb")
| EU4 -> File.Exists (gameCachePath + "eu4.cwb")
| CK2 -> File.Exists (gameCachePath + "ck2.cwb")
| IR -> File.Exists (gameCachePath + "ir.cwb")
| VIC2 -> File.Exists (gameCachePath + "vic2.cwb")
| CK3 -> File.Exists (gameCachePath + "ck3.cwb")
| VIC3 -> File.Exists (gameCachePath + "vic3.cwb")
if doesCacheExist && not forceCreate
then logInfo (sprintf "Cache exists at %s" (gameCachePath + ".cwb"))
else
match (activeGame, stlVanillaPath, eu4VanillaPath, hoi4VanillaPath, ck2VanillaPath, irVanillaPath, vic2VanillaPath, ck3VanillaPath, vic3VanillaPath) with
| STL, Some vp, _, _ ,_, _, _, _,_ ->
client.CustomNotification ("loadingBar", JsonValue.Record [| "value", JsonValue.String("Generating vanilla cache..."); "enable", JsonValue.Boolean(true) |])
serializeSTL (vp) (gameCachePath)
let text = sprintf "Vanilla cache for %O has been updated." activeGame
client.CustomNotification ("forceReload", JsonValue.String(text))
| STL, None, _, _, _, _, _, _,_ ->
client.CustomNotification ("promptVanillaPath", JsonValue.String("stellaris"))
| EU4, _, Some vp, _, _, _, _, _,_ ->
client.CustomNotification ("loadingBar", JsonValue.Record [| "value", JsonValue.String("Generating vanilla cache..."); "enable", JsonValue.Boolean(true) |])
serializeEU4 (vp) (gameCachePath)
let text = sprintf "Vanilla cache for %O has been updated." activeGame
client.CustomNotification ("forceReload", JsonValue.String(text))
| EU4, _, None, _, _, _, _, _,_ ->
client.CustomNotification ("promptVanillaPath", JsonValue.String("eu4"))
| HOI4, _, _, Some vp, _, _, _, _,_ ->
client.CustomNotification ("loadingBar", JsonValue.Record [| "value", JsonValue.String("Generating vanilla cache..."); "enable", JsonValue.Boolean(true) |])
serializeHOI4 (vp) (gameCachePath)
let text = sprintf "Vanilla cache for %O has been updated." activeGame
client.CustomNotification ("forceReload", JsonValue.String(text))
| HOI4, _, _, None, _, _, _, _,_->
client.CustomNotification ("promptVanillaPath", JsonValue.String("hoi4"))
| CK2, _, _, _, Some vp, _, _, _,_ ->
client.CustomNotification ("loadingBar", JsonValue.Record [| "value", JsonValue.String("Generating vanilla cache..."); "enable", JsonValue.Boolean(true) |])
serializeCK2 (vp) (gameCachePath)
let text = sprintf "Vanilla cache for %O has been updated." activeGame
client.CustomNotification ("forceReload", JsonValue.String(text))
| CK2, _, _, _, None, _, _, _,_ ->
client.CustomNotification ("promptVanillaPath", JsonValue.String("ck2"))
| IR, _, _, _, _, Some vp, _, _,_ ->
client.CustomNotification ("loadingBar", JsonValue.Record [| "value", JsonValue.String("Generating vanilla cache..."); "enable", JsonValue.Boolean(true) |])
serializeIR (vp) (gameCachePath)
let text = sprintf "Vanilla cache for %O has been updated." activeGame
client.CustomNotification ("forceReload", JsonValue.String(text))
| IR, _, _, _, _, None, _, _,_ ->
client.CustomNotification ("promptVanillaPath", JsonValue.String("imperator"))
| VIC2, _, _, _, _, _, Some vp, _,_ ->
client.CustomNotification ("loadingBar", JsonValue.Record [| "value", JsonValue.String("Generating vanilla cache..."); "enable", JsonValue.Boolean(true) |])
serializeVIC2 (vp) (gameCachePath)
let text = sprintf "Vanilla cache for %O has been updated." activeGame
client.CustomNotification ("forceReload", JsonValue.String(text))
| VIC2, _, _, _, _, _, None, _,_ ->
client.CustomNotification ("promptVanillaPath", JsonValue.String("vic2"))
| CK3, _, _, _, _, _, _, Some vp,_ ->
client.CustomNotification ("loadingBar", JsonValue.Record [| "value", JsonValue.String("Generating vanilla cache..."); "enable", JsonValue.Boolean(true) |])
serializeCK3 (vp) (gameCachePath)
let text = sprintf "Vanilla cache for %O has been updated." activeGame
client.CustomNotification ("forceReload", JsonValue.String(text))
| CK3, _, _, _, _, _, _, None,_ ->
client.CustomNotification ("promptVanillaPath", JsonValue.String("ck3"))
| VIC3, _, _, _, _, _, _, _,Some vp ->
client.CustomNotification ("loadingBar", JsonValue.Record [| "value", JsonValue.String("Generating vanilla cache..."); "enable", JsonValue.Boolean(true) |])
serializeVIC3 (vp) (gameCachePath)
let text = sprintf "Vanilla cache for %O has been updated." activeGame
client.CustomNotification ("forceReload", JsonValue.String(text))
| VIC3, _, _, _, _, _, _,_, None ->
client.CustomNotification ("promptVanillaPath", JsonValue.String("vic3"))
| _ -> logInfo ("No cache path")
// client.CustomNotification ("promptReload", JsonValue.String("Cached generated, reload to use"))
let processWorkspace (uri : option<Uri>) =
client.CustomNotification ("loadingBar", JsonValue.Record [| "value", JsonValue.String("Loading project..."); "enable", JsonValue.Boolean(true) |])
match uri with
| Some u ->
let path =
if System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && u.LocalPath.StartsWith "/"
then u.LocalPath.Substring(1)
else u.LocalPath
try
let timer = new System.Diagnostics.Stopwatch()
timer.Start()
logInfo path
logInfo (sprintf "Parse docs time: %i" timer.ElapsedMilliseconds;)
timer.Restart()
// let docs = DocsParser.parseDocsFile @"G:\Projects\CK2 Events\CWTools\files\game_effects_triggers_1.9.1.txt"
let serverSettings =
{
cachePath = cachePath
useManualRules = useManualRules
manualRulesFolder = manualRulesFolder
isVanillaFolder = isVanillaFolder
path = path
workspaceFolders = workspaceFolders
dontLoadPatterns = dontLoadPatterns
validateVanilla = validateVanilla
languages = languages
experimental = experimental
debug_mode = debugMode
maxFileSize = maxFileSize
}
let game =
match activeGame with
|STL ->
let game = loadSTL serverSettings
stlGameObj <- Some (game :> IGame<STLComputedData>)
game :> IGame
|HOI4 ->
let game = loadHOI4 serverSettings
hoi4GameObj <- Some (game :> IGame<HOI4ComputedData>)
game :> IGame
|EU4 ->
let game = loadEU4 serverSettings
eu4GameObj <- Some (game :> IGame<EU4ComputedData>)
game :> IGame
|CK2 ->
let game = loadCK2 serverSettings
ck2GameObj <- Some (game :> IGame<CK2ComputedData>)
game :> IGame
|IR ->
let game = loadIR serverSettings
irGameObj <- Some (game :> IGame<IRComputedData>)
game :> IGame
|VIC2 ->
let game = loadVIC2 serverSettings
vic2GameObj <- Some (game :> IGame<VIC2ComputedData>)
game :> IGame
|CK3 ->
let game = loadCK3 serverSettings
ck3GameObj <- Some (game :> IGame<CK3ComputedData>)
game :> IGame
|VIC3 ->
let game = loadVIC3 serverSettings
vic3GameObj <- Some (game :> IGame<VIC3ComputedData>)
game :> IGame
|Custom ->
let game = loadCustom serverSettings
customGameObj <- Some (game :> IGame<JominiComputedData>)
game :> IGame
gameObj <- Some game
let getRange (start: FParsec.Position) (endp : FParsec.Position) = mkRange start.StreamName (mkPos (int start.Line) (int start.Column)) (mkPos (int endp.Line) (int endp.Column))
let parserErrors = game.ParserErrors() |> List.map (fun ( n, e, p) -> "CW001", Severity.Error, n, e, (getRange p p), 0, None)
parserErrors
|> List.map parserErrorToDiagnostics
|> sendDiagnostics
let mapResourceToFilePath =
function
| EntityResource (f, r) -> r.scope, f, r.logicalpath
| FileResource (f, r) -> r.scope, f, r.logicalpath
| FileWithContentResource (f, r) -> r.scope, f, r.logicalpath
let fileList =
game.AllFiles() |> List.map (mapResourceToFilePath)
|> List.choose (fun (s, f, l) -> match Uri.TryCreate(f, UriKind.Absolute) with |TrySuccess value -> Some (s, value, l) |TryFailure -> None)
|> List.map (fun (s, uri, l) -> JsonValue.Record [| "scope", JsonValue.String s; "uri", uri.AbsoluteUri |> JsonValue.String; "logicalpath", JsonValue.String l |])
|> Array.ofList
client.CustomNotification ("updateFileList", JsonValue.Record [| "fileList", JsonValue.Array fileList|])
client.CustomNotification ("loadingBar", JsonValue.Record [| "value", JsonValue.String("Validating files..."); "enable", JsonValue.Boolean(true) |])
//eprintfn "%A" game.AllFiles
let valErrors = game.ValidationErrors() |> List.map (fun e -> (e.code, e.severity, e.range.FileName, e.message, e.range, e.keyLength, e.relatedErrors))
let locRaw = game.LocalisationErrors(true, true)
locCache <- locRaw |> List.groupBy (fun e -> e.range.FileName) |> Map.ofList
// eprintfn "lc p %A" locCache
let locErrors = locRaw |> List.map (fun e -> (e.code, e.severity, e.range.FileName, e.message, e.range, e.keyLength, e.relatedErrors))
valErrors @ locErrors
|> List.map parserErrorToDiagnostics
|> sendDiagnostics
GC.Collect()
//eprintfn "%A" game.ValidationErrors
// |> List.groupBy fst
// |> List.map ((fun (f, rs) -> f, rs |> List.filter (fun (_, d) -> match d.code with |Some s -> not (List.contains s ignoreCodes) |None -> true)) >>
// (fun (f, rs) -> PublishDiagnostics {uri = (match Uri.TryCreate(f, UriKind.Absolute) with |TrySuccess value -> value |TryFailure -> eprintfn "%s" f; Uri "/") ; diagnostics = List.map snd rs}))
// |> List.iter (fun f -> LanguageServer.sendNotification send f)
with
| :? System.Exception as e -> eprintfn "%A" e
|None -> ()
client.CustomNotification ("loadingBar", JsonValue.Record [| "value", JsonValue.String(""); "enable", JsonValue.Boolean(false) |])
let createRange startLine startCol endLine endCol =
{
``start`` = {
line = startLine
character = startCol
}
``end`` = {
line = endLine
character = endCol
}
}
let isRangeInError (range : LSP.Types.Range) (start : range) (length : int) =
range.start.line = (int start.StartLine - 1) && range.``end``.line = (int start.StartLine - 1)
&& range.start.character >= int start.StartColumn && range.``end``.character <= (int start.StartColumn + length)
let isRangeInRange (range : LSP.Types.Range) (inner : LSP.Types.Range) =
(range.start.line < inner.start.line || (range.start.line = inner.start.line && range.start.character <= inner.start.character))
&& (range.``end``.line > inner.``end``.line || (range.``end``.line = inner.``end``.line && range.``end``.character >= inner.``end``.character))
let catchError (defaultValue) (a : Async<_>) =
async {
try
return! a
with
| ex ->
client.LogMessage { ``type`` = MessageType.Error; message = (sprintf "%A" ex)}
return defaultValue
}
let parseUri path =
let inner p =
match Uri.TryCreate(p, UriKind.Absolute) with
| TrySuccess uri -> Some (uri.AbsoluteUri |> JsonValue.String)
| _ -> None
memoize id inner path
interface ILanguageServer with
member this.Initialize(p: InitializeParams) =
async {
rootUri <- p.rootUri
workspaceFolders <- p.workspaceFolders
match p.initializationOptions with
| Some opt ->
match opt.Item("language") with
| JsonValue.String "stellaris" ->
activeGame <- STL
| JsonValue.String "hoi4" ->
activeGame <- HOI4
| JsonValue.String "eu4" ->
activeGame <- EU4
| JsonValue.String "ck2" ->
activeGame <- CK2
| JsonValue.String "imperator" ->
activeGame <- IR
| JsonValue.String "vic2" ->
activeGame <- VIC2
| JsonValue.String "ck3" ->
activeGame <- CK3
| JsonValue.String "vic3" ->
activeGame <- VIC3
| JsonValue.String "paradox" ->
activeGame <- Custom
| _ -> ()
match opt.Item("rulesCache") with
| JsonValue.String x ->
match activeGame with
| STL -> cachePath <- Some (x + "/stellaris")
| HOI4 -> cachePath <- Some (x + "/hoi4")
| EU4 -> cachePath <- Some (x + "/eu4")
| CK2 -> cachePath <- Some (x + "/ck2")
| IR -> cachePath <- Some (x + "/imperator")
| VIC2 -> cachePath <- Some (x + "/vic2")
| VIC3 -> cachePath <- Some (x + "/vic3")
| CK3 -> cachePath <- Some (x + "/ck3")
| _ -> ()
| _ -> ()
match opt.Item("repoPath") with
| JsonValue.String x ->
logInfo (sprintf "repo path %A" x)
remoteRepoPath <- Some x
| _ -> ()
match opt.Item("isVanillaFolder") with
| JsonValue.Boolean b ->
if b then logInfo "Client thinks this is a vanilla directory" else ()
isVanillaFolder <- b
| _ -> ()
// match opt.Item("rulesVersion") with
// | JsonValue.Array x ->
// match x with
// |[|JsonValue.String s; JsonValue.String e|] ->
// stellarisCacheVersion <- Some s
// eu4CacheVersion <- Some e
// | _ -> ()
// | _ -> ()
match opt.Item("diagnosticLogging") with
| JsonValue.Boolean b -> diagnosticLogging <- b
| _ -> ()
match opt.Item("rules_version") with
| JsonValue.String x ->
match x with
|"manual" ->
useManualRules <- true
rulesChannel <- "manual"
|x -> rulesChannel <- x
| _ -> ()
| _ -> ()
|None -> ()
logInfo (sprintf "New init %s" (p.ToString()))
return { capabilities =
{ defaultServerCapabilities with
hoverProvider = true
definitionProvider = true
referencesProvider = true
documentFormattingProvider = true
textDocumentSync =
{ defaultTextDocumentSyncOptions with
openClose = true
willSave = true
save = Some { includeText = true }
change = TextDocumentSyncKind.Full }
completionProvider = Some {resolveProvider = true; triggerCharacters = ['.']}
codeActionProvider = true
documentSymbolProvider = true
executeCommandProvider =
Some {commands = ["pretriggerThisFile"; "pretriggerAllFiles"; "genlocfile"; "genlocall"; "debugrules"; "outputerrors"; "reloadrulesconfig"; "cacheVanilla"; "listAllFiles";"listAllLocFiles"; "gettech"; "getGraphData"; "showEventGraph"; "exportTypes"]} } }
}
member this.Initialized() =
async { () }
member this.Shutdown() =
async { return None }
member this.DidChangeConfiguration(p: DidChangeConfigurationParams) =
async {
let newLanguages =
match p.settings.Item("cwtools").Item("localisation").Item("languages"), activeGame with
| JsonValue.Array o, STL ->
o |> Array.choose (function |JsonValue.String s -> (match STLLang.TryParse<STLLang> s with |TrySuccess s -> Some s |TryFailure -> None) |_ -> None)
|> List.ofArray
|> (fun l -> if List.isEmpty l then [STLLang.English] else l)
|> List.map Lang.STL
| _, STL -> [Lang.STL STLLang.English]
| JsonValue.Array o, EU4 ->
o |> Array.choose (function |JsonValue.String s -> (match EU4Lang.TryParse<EU4Lang> s with |TrySuccess s -> Some s |TryFailure -> None) |_ -> None)
|> List.ofArray
|> (fun l -> if List.isEmpty l then [EU4Lang.English] else l)
|> List.map Lang.EU4
| _, EU4 -> [Lang.EU4 EU4Lang.English]
| JsonValue.Array o, HOI4 ->
o |> Array.choose (function |JsonValue.String s -> (match HOI4Lang.TryParse<HOI4Lang> s with |TrySuccess s -> Some s |TryFailure -> None) |_ -> None)
|> List.ofArray
|> (fun l -> if List.isEmpty l then [HOI4Lang.English] else l)
|> List.map Lang.HOI4
| _, HOI4 -> [Lang.HOI4 HOI4Lang.English]
| JsonValue.Array o, CK2 ->
o |> Array.choose (function |JsonValue.String s -> (match CK2Lang.TryParse<CK2Lang> s with |TrySuccess s -> Some s |TryFailure -> None) |_ -> None)
|> List.ofArray
|> (fun l -> if List.isEmpty l then [CK2Lang.English] else l)
|> List.map Lang.CK2
| _, CK2 -> [Lang.CK2 CK2Lang.English]
| JsonValue.Array o, IR ->
o |> Array.choose (function |JsonValue.String s -> (match IRLang.TryParse<IRLang> s with |TrySuccess s -> Some s |TryFailure -> None) |_ -> None)
|> List.ofArray
|> (fun l -> if List.isEmpty l then [IRLang.English] else l)
|> List.map Lang.IR
| _, IR -> [Lang.IR IRLang.English]
| JsonValue.Array o, VIC2 ->
o |> Array.choose (function |JsonValue.String s -> (match VIC2Lang.TryParse<VIC2Lang> s with |TrySuccess s -> Some s |TryFailure -> None) |_ -> None)
|> List.ofArray
|> (fun l -> if List.isEmpty l then [VIC2Lang.English] else l)
|> List.map Lang.VIC2
| _, VIC2 -> [Lang.VIC2 VIC2Lang.English]
| JsonValue.Array o, CK3 ->
o |> Array.choose (function |JsonValue.String s -> (match CK3Lang.TryParse<CK3Lang> s with |TrySuccess s -> Some s |TryFailure -> None) |_ -> None)
|> List.ofArray
|> (fun l -> if List.isEmpty l then [CK3Lang.English] else l)
|> List.map Lang.CK3
| _, CK3 -> [Lang.CK3 CK3Lang.English]
| JsonValue.Array o, VIC3 ->
o |> Array.choose (function |JsonValue.String s -> (match VIC3Lang.TryParse<VIC3Lang> s with |TrySuccess s -> Some s |TryFailure -> None) |_ -> None)
|> List.ofArray
|> (fun l -> if List.isEmpty l then [VIC3Lang.English] else l)
|> List.map Lang.VIC3
| _, VIC3 -> [Lang.VIC3 VIC3Lang.English]
| _, Custom -> [Lang.Custom CustomLang.English]
languages <- newLanguages
match p.settings.Item("cwtools").Item("localisation").Item("generated_strings") with
| JsonValue.String newString -> generatedStrings <- newString
| _ -> ()
let newVanillaOnly =
match p.settings.Item("cwtools").Item("errors").Item("vanilla") with
| JsonValue.Boolean b -> b
| _ -> false
validateVanilla <- newVanillaOnly
let newExperimental =
match p.settings.Item("cwtools").Item("experimental") with
| JsonValue.Boolean b -> b
| _ -> false
experimental <- newExperimental
let newDebugMode =
match p.settings.Item("cwtools").Item("debug_mode") with
| JsonValue.Boolean b -> b
| _ -> false
debugMode <- newDebugMode
let newIgnoreCodes =
match p.settings.Item("cwtools").Item("errors").Item("ignore") with
| JsonValue.Array o ->
o |> Array.choose (function |JsonValue.String s -> Some s |_ -> None)
|> List.ofArray
| _ -> []
ignoreCodes <- newIgnoreCodes
let newIgnoreFiles =
match p.settings.Item("cwtools").Item("errors").Item("ignorefiles") with
| JsonValue.Array o ->
o |> Array.choose (function |JsonValue.String s -> Some s |_ -> None)
|> List.ofArray
| _ -> []
ignoreFiles <- newIgnoreFiles
let excludePatterns =
match p.settings.Item("cwtools").Item("ignore_patterns") with
| JsonValue.Array o ->
o |> Array.choose (function |JsonValue.String s -> Some s |_ -> None)
|> List.ofArray
| _ -> []
dontLoadPatterns <- excludePatterns
match p.settings.Item("cwtools").Item("trace").Item("server") with
| JsonValue.String "messages"
| JsonValue.String "verbose" -> CWTools.Utilities.Utils.loglevel <- CWTools.Utilities.Utils.LogLevel.Verbose
|_ -> ()
match p.settings.Item("cwtools").Item("cache").Item("eu4") with
| JsonValue.String "" -> ()
| JsonValue.String s ->
eu4VanillaPath <- Some s
|_ -> ()
match p.settings.Item("cwtools").Item("cache").Item("stellaris") with
| JsonValue.String "" -> ()
| JsonValue.String s ->
stlVanillaPath <- Some s
|_ -> ()
match p.settings.Item("cwtools").Item("cache").Item("hoi4") with
| JsonValue.String "" -> ()
| JsonValue.String s ->
hoi4VanillaPath <- Some s
|_ -> ()
match p.settings.Item("cwtools").Item("cache").Item("ck2") with
| JsonValue.String "" -> ()
| JsonValue.String s ->
ck2VanillaPath <- Some s
|_ -> ()
match p.settings.Item("cwtools").Item("cache").Item("imperator") with
| JsonValue.String "" -> ()
| JsonValue.String s ->
irVanillaPath <- Some s
|_ -> ()
match p.settings.Item("cwtools").Item("cache").Item("vic2") with
| JsonValue.String "" -> ()
| JsonValue.String s ->
vic2VanillaPath <- Some s
|_ -> ()
match p.settings.Item("cwtools").Item("cache").Item("ck3") with
| JsonValue.String "" -> ()
| JsonValue.String s ->
ck3VanillaPath <- Some s
|_ -> ()
match p.settings.Item("cwtools").Item("cache").Item("vic3") with
| JsonValue.String "" -> ()
| JsonValue.String s ->
vic3VanillaPath <- Some s
|_ -> ()
match p.settings.Item("cwtools").Item("rules_folder") with
| JsonValue.String x ->
manualRulesFolder <- Some x
|_ -> ()
match p.settings.Item("cwtools").Item("maxFileSize") with
| JsonValue.Number x ->
maxFileSize <- int x
|_ -> ()
logInfo (sprintf "New configuration %s" (p.ToString()))
match cachePath with
|Some dir ->
if Directory.Exists dir then () else Directory.CreateDirectory dir |> ignore
|_ -> ()
let task = new Task((fun () -> checkOrSetGameCache(false); processWorkspace(rootUri)))
task.Start()
let task = new Task((fun () -> setupRulesCaches()))
task.Start()
}
member this.DidOpenTextDocument(p: DidOpenTextDocumentParams) =
async {
docs.Open p
lintAgent.Post (UpdateRequest ({uri = p.textDocument.uri; version = p.textDocument.version}, true))
let mapResourceToFilePath =
function
| EntityResource (f, r) -> r.scope, f, r.logicalpath
| FileResource (f, r) -> r.scope, f, r.logicalpath
| FileWithContentResource (f, r) -> r.scope, f, r.logicalpath
match gameObj, currentlyRefreshingFiles with
| Some game, false ->
currentlyRefreshingFiles <- true
let task =
new Task(
(fun () ->
let fileList =
game.AllFiles() |> List.map (mapResourceToFilePath)
|> List.choose (fun (s, f, l) -> parseUri f |> Option.map (fun u -> (s, u, l)))
|> List.map (fun (s, uri, l) -> JsonValue.Record [| "scope", JsonValue.String s; "uri", uri; "logicalpath", JsonValue.String l |])
|> Array.ofList
client.CustomNotification ("updateFileList", JsonValue.Record [| "fileList", JsonValue.Array fileList|])
currentlyRefreshingFiles <- false
))
task.Start()
| _ -> ()
// let task =
// new Task((fun () ->
// match gameObj with
// |Some game ->
// locCache <- game.LocalisationErrors(true, true) |> List.groupBy (fun (_, _, r, _, _, _) -> r.FileName) |> Map.ofList
// eprintfn "lc %A" locCache
// |None -> ()
// ))
// task.Start()
}
member this.DidFocusFile(p : DidFocusFileParams) =
async {
let path =
if System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && p.uri.LocalPath.StartsWith "/"
then p.uri.LocalPath.Substring(1)
else p.uri.LocalPath
lastFocusedFile <- Some path
lintAgent.Post (UpdateRequest ({uri = p.uri; version = 0}, true))
}
member this.DidChangeTextDocument(p: DidChangeTextDocumentParams) =
async {
docs.Change p
lintAgent.Post (UpdateRequest ({uri = p.textDocument.uri; version = p.textDocument.version}, false))
}
member this.WillSaveTextDocument(p: WillSaveTextDocumentParams) =
async {
lintAgent.Post (UpdateRequest ({uri = p.textDocument.uri; version = 0}, true))
}
member this.WillSaveWaitUntilTextDocument(p: WillSaveTextDocumentParams) = TODO()
member this.DidSaveTextDocument(p: DidSaveTextDocumentParams) =
async {
lintAgent.Post (UpdateRequest ({uri = p.textDocument.uri; version = 0}, false))
}
member this.DidCloseTextDocument(p: DidCloseTextDocumentParams) =
async {
docs.Close p
}
member this.DidChangeWatchedFiles(p: DidChangeWatchedFilesParams) =
async {
for change in p.changes do
match change.``type`` with
|FileChangeType.Created ->
lintAgent.Post (UpdateRequest ({uri = change.uri; version = 0}, true))
//eprintfn "Watched file %s %s" (change.uri.ToString()) (change._type.ToString())
|FileChangeType.Deleted ->
client.PublishDiagnostics {uri = change.uri; diagnostics = []}
|_ -> ()
// if change.uri.AbsolutePath.EndsWith ".fsproj" then
// projects.UpdateProjectFile change.uri
//lintAgent.Post (UpdateRequest {uri = p.textDocument.uri; version = p.textDocument.version})
}
member this.Completion(p: CompletionParams) =
async {
return completion gameObj p docs debugMode
} |> catchError None
member this.Hover(p: TextDocumentPositionParams) =
async {
return (LanguageServerFeatures.hoverDocument eu4GameObj hoi4GameObj stlGameObj ck2GameObj irGameObj vic2GameObj ck3GameObj vic3GameObj customGameObj client docs p.textDocument.uri p.position) |> Async.RunSynchronously |> Some
} |> catchError None
member this.ResolveCompletionItem(p: CompletionItem) =
async {
return completionResolveItem gameObj p |> Async.RunSynchronously
} |> catchError p
member this.SignatureHelp(p: TextDocumentPositionParams) = TODO()
member this.GotoDefinition(p: TextDocumentPositionParams) =
async {
return
match gameObj with
|Some game ->
let position = Pos.fromZ p.position.line p.position.character// |> (fun p -> Pos.fromZ)
logInfo (sprintf "goto fn %A" p.textDocument.uri)
let path =
let u = p.textDocument.uri
if System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && u.LocalPath.StartsWith "/"
then u.LocalPath.Substring(1)
else u.LocalPath
let gototype = game.GoToType position (path) (docs.GetText (FileInfo(p.textDocument.uri.LocalPath)) |> Option.defaultValue "")
match gototype with
|Some goto ->
logInfo (sprintf "goto %s" goto.FileName)
[{ uri = Uri(goto.FileName); range = (convRangeToLSPRange goto)}]
|None -> []
|None -> []
} |> catchError []
member this.FindReferences(p: ReferenceParams) =
async {
return
match gameObj with
|Some game ->
let position = Pos.fromZ p.position.line p.position.character// |> (fun p -> Pos.fromZ)
let path =
let u = p.textDocument.uri
if System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && u.LocalPath.StartsWith "/"
then u.LocalPath.Substring(1)
else u.LocalPath
let gototype = game.FindAllRefs position (path) (docs.GetText (FileInfo(p.textDocument.uri.LocalPath)) |> Option.defaultValue "")
match gototype with
|Some gotos ->
gotos |> List.map (fun goto -> { uri = Uri(goto.FileName); range = (convRangeToLSPRange goto)})
|None -> []
|None -> []
} |> catchError []
member this.DocumentHighlight(p: TextDocumentPositionParams) = TODO()
member this.DocumentSymbols(p: DocumentSymbolParams) =
let createDocumentSymbol name detail range =
let range = convRangeToLSPRange range
{
name = name
detail = detail
kind = SymbolKind.Class
deprecated = false
range = range
selectionRange = range
children = []
}
async {
return
match gameObj with
|Some game ->
let types = game.Types()
let (all : DocumentSymbol list) =
types |> Map.toList
|> List.collect (fun (k, vs) -> vs |> List.filter (fun (tdi) -> tdi.range.FileName = p.textDocument.uri.LocalPath) |> List.map (fun (tdi) -> createDocumentSymbol tdi.id k tdi.range))
|> List.rev
|> List.filter (fun ds -> not (ds.detail.Contains(".")))
all |> List.fold (fun (acc : DocumentSymbol list) (next : DocumentSymbol) ->
if acc |> List.exists (fun a -> isRangeInRange a.range next.range && a.name <> next.name)
then
acc |> List.map (fun (a : DocumentSymbol) -> if isRangeInRange a.range next.range && a.name <> next.name then { a with children = (next::(a.children))} else a )
else next::acc) []
// all |> List.fold (fun acc next -> acc |> List.tryFind (fun a -> isRangeInRange a.range next.range) |> function |None -> next::acc |Some ) []
// |> List.map (fun (k, vs) -> createDocumentSymbol k )
|None -> []
} |> catchError []
member this.WorkspaceSymbols(p: WorkspaceSymbolParams) = TODO()
member this.CodeActions(p: CodeActionParams) =
async {
return
match gameObj with
|Some game ->
let es = locCache
let path =
if System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && p.textDocument.uri.LocalPath.StartsWith "/"
then p.textDocument.uri.LocalPath.Substring(1)