-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathroutes.nim
956 lines (881 loc) · 33.8 KB
/
routes.nim
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
import
macros,
strformat,
chronos,
chronos/asyncproc,
json_rpc/server,
os,
sugar,
sequtils,
suggestapi,
protocol/enums,
protocol/types,
with,
tables,
strutils,
./utils,
chronicles,
asyncprocmonitor,
json_serialization,
std/[strscans, times, json, parseutils, strutils],
ls,
stew/[byteutils],
nimexpand,
nimcheck
proc getNphPath(): Option[string] =
let path = findExe "nph"
if path == "":
none(string)
else:
some path
#routes
proc initialize*(
p: tuple[ls: LanguageServer, onExit: OnExitCallback], params: InitializeParams
): Future[InitializeResult] {.async.} =
proc onClientProcessExitAsync(): Future[void] {.async.} =
debug "onClientProcessExitAsync"
await p.ls.stopNimsuggestProcesses
await p.onExit()
proc onClientProcessExit() {.closure, gcsafe.} =
try:
debug "onClientProcessExit"
waitFor onClientProcessExitAsync()
except Exception:
error "Error in onClientProcessExit ", msg = getCurrentExceptionMsg()
debug "Initialize received..."
if params.processId.isSome:
let pid = params.processId.get
if pid.kind == JInt:
debug "Registering monitor for process ", pid = pid.num
var pidInt = int(pid.num)
if p.ls.cmdLineClientProcessId.isSome:
if p.ls.cmdLineClientProcessId.get == pidInt:
debug "Process ID already specified in command line, no need to register monitor again"
else:
debug "Warning! Client Process ID in initialize request differs from the one, specified in the command line. This means the client violates the LSP spec!"
debug "Will monitor both process IDs..."
hookAsyncProcMonitor(pidInt, onClientProcessExit)
else:
hookAsyncProcMonitor(pidInt, onClientProcessExit)
p.ls.initializeParams = params
p.ls.clientCapabilities = params.capabilities
result = InitializeResult(
capabilities: ServerCapabilities(
textDocumentSync: some(
%TextDocumentSyncOptions(
openClose: some(true),
change: some(TextDocumentSyncKind.Full.int),
willSave: some(false),
willSaveWaitUntil: some(false),
save: some(SaveOptions(includeText: some(true))),
)
),
hoverProvider: some(true),
workspace: some(
ServerCapabilities_workspace(
workspaceFolders: some(WorkspaceFoldersServerCapabilities())
)
),
completionProvider:
CompletionOptions(triggerCharacters: some(@["."]), resolveProvider: some(false)),
signatureHelpProvider: SignatureHelpOptions(triggerCharacters: some(@["(", ","])),
definitionProvider: some(true),
declarationProvider: some(true),
typeDefinitionProvider: some(true),
referencesProvider: some(true),
documentHighlightProvider: some(true),
workspaceSymbolProvider: some(true),
executeCommandProvider: some(
ExecuteCommandOptions(
commands: some(@[RESTART_COMMAND, RECOMPILE_COMMAND, CHECK_PROJECT_COMMAND])
)
),
inlayHintProvider: some(InlayHintOptions(resolveProvider: some(false))),
documentSymbolProvider: some(true),
codeActionProvider: some(true),
documentFormattingProvider: some(getNphPath().isSome),
)
)
# Support rename by default, but check if we can also support prepare
result.capabilities.renameProvider = %true
if params.capabilities.textDocument.isSome:
let docCaps = params.capabilities.textDocument.unsafeGet()
# Check if the client support prepareRename
#TODO do the test on the action
if docCaps.rename.isSome and docCaps.rename.get().prepareSupport.get(false):
result.capabilities.renameProvider = %*{"prepareProvider": true}
debug "Initialize completed. Trying to start nimsuggest instances"
let ls = p.ls
ls.serverCapabilities = result.capabilities
let rootPath = ls.initializeParams.getRootPath
if rootPath != "":
let nimbleFiles = walkFiles(rootPath / "*.nimble").toSeq
if nimbleFiles.len > 0:
let nimbleFile = nimbleFiles[0]
let nimbleDumpInfo = ls.getNimbleDumpInfo(nimbleFile)
ls.entryPoints =
nimbleDumpInfo.getNimbleEntryPoints(ls.initializeParams.getRootPath)
# ls.showMessage(fmt "Found entry point {ls.entryPoints}?", MessageType.Info)
for entryPoint in ls.entryPoints:
debug "Starting nimsuggest for entry point ", entry = entryPoint
if entryPoint notin ls.projectFiles:
ls.createOrRestartNimsuggest(entryPoint)
proc toCompletionItem(suggest: Suggest): CompletionItem =
with suggest:
return
CompletionItem %* {
"label": qualifiedPath[^1].strip(chars = {'`'}),
"kind": nimSymToLSPKind(suggest).int,
"documentation": doc,
"detail": nimSymDetails(suggest),
}
proc completion*(
ls: LanguageServer, params: CompletionParams, id: int
): Future[seq[CompletionItem]] {.async.} =
with (params.position, params.textDocument):
asyncSpawn ls.addProjectFileToPendingRequest(id.uint, uri)
let nimsuggest = await ls.tryGetNimsuggest(uri)
if nimsuggest.isNone():
return @[]
let ch = ls.getCharacter(uri, line, character)
if ch.isNone:
return @[]
let completions =
await nimsuggest.get.sug(uriToPath(uri), ls.uriToStash(uri), line + 1, ch.get)
result = completions.map(toCompletionItem)
if ls.clientCapabilities.supportSignatureHelp() and
nsCon in nimSuggest.get.capabilities:
#show only unique overloads if we support signatureHelp
var unique = initTable[string, CompletionItem]()
for completion in result:
if completion.label notin unique:
unique[completion.label] = completion
result = unique.values.toSeq
proc toLocation*(suggest: Suggest): Location =
return
Location %* {"uri": pathToUri(suggest.filepath), "range": toLabelRange(suggest)}
proc definition*(
ls: LanguageServer, params: TextDocumentPositionParams, id: int
): Future[seq[Location]] {.async.} =
with (params.position, params.textDocument):
asyncSpawn ls.addProjectFileToPendingRequest(id.uint, uri)
let ns = await ls.tryGetNimsuggest(uri)
if ns.isNone:
return @[]
let ch = ls.getCharacter(uri, line, character)
if ch.isNone:
return @[]
result = ns.get
.def(uriToPath(uri), ls.uriToStash(uri), line + 1, ch.get)
.await()
.map(x => x.toUtf16Pos(ls).toLocation)
proc declaration*(
ls: LanguageServer, params: TextDocumentPositionParams, id: int
): Future[seq[Location]] {.async.} =
with (params.position, params.textDocument):
asyncSpawn ls.addProjectFileToPendingRequest(id.uint, uri)
let ns = await ls.tryGetNimsuggest(uri)
if ns.isNone:
return @[]
let ch = ls.getCharacter(uri, line, character)
if ch.isNone:
return @[]
result = ns.get
.declaration(uriToPath(uri), ls.uriToStash(uri), line + 1, ch.get)
.await()
.map(x => x.toUtf16Pos(ls).toLocation)
proc expandAll*(
ls: LanguageServer, params: TextDocumentPositionParams
): Future[ExpandResult] {.async.} =
with (params.position, params.textDocument):
let ns = await ls.tryGetNimsuggest(uri)
if ns.isNone:
return ExpandResult() #TODO make it optional
let ch = ls.getCharacter(uri, line, character)
if ch.isNone:
return ExpandResult()
let expand =
ns.get.expand(uriToPath(uri), ls.uriToStash(uri), line + 1, ch.get).await()
proc createRangeFromSuggest(suggest: Suggest): Range =
result = range(suggest.line - 1, 0, suggest.endLine - 1, suggest.endCol)
proc fixIdentation(s: string, indent: int): string =
result = s
.split("\n")
.mapIt(
if (it != ""):
repeat(" ", indent) & it
else:
it
)
.join("\n")
proc expand*(
ls: LanguageServer, params: ExpandTextDocumentPositionParams
): Future[ExpandResult] {.async.} =
with (params, params.position, params.textDocument):
let
lvl = level.get(-1)
tag =
if lvl == -1:
"all"
else:
$lvl
ns = await ls.tryGetNimsuggest(uri)
if ns.isNone:
return ExpandResult()
let ch = ls.getCharacter(uri, line, character)
if ch.isNone:
return ExpandResult()
let expand = ns.get
.expand(uriToPath(uri), ls.uriToStash(uri), line + 1, ch.get, fmt " {tag}")
.await()
if expand.len != 0:
result = ExpandResult(
content: expand[0].doc.fixIdentation(character),
range: expand[0].createRangeFromSuggest(),
)
proc status*(
ls: LanguageServer, params: NimLangServerStatusParams
): Future[NimLangServerStatus] {.async.} =
debug "Received status request"
ls.getLspStatus()
proc extensionCapabilities*(
ls: LanguageServer, _: JsonNode
): Future[seq[string]] {.async.} =
ls.extensionCapabilities.toSeq.mapIt($it)
proc extensionSuggest*(
ls: LanguageServer, params: SuggestParams
): Future[SuggestResult] {.async.} =
debug "[Extension Suggest]", params = params
var projectFile = params.projectFile
if projectFile != "" and projectFile notin ls.projectFiles:
#test if just a regular file
let uri = projectFile.pathToUri
if uri in ls.openFiles:
let openFile = ls.openFiles[uri]
projectFile = await openFile.projectFile
debug "[ExtensionSuggest] Found project file for ",
file = params.projectFile, project = projectFile
else:
error "Project file must exists ", params = params
return SuggestResult()
template restart(ls: LanguageServer, project: Project) =
ls.showMessage(fmt "Restarting nimsuggest {projectFile}", MessageType.Info)
project.errorCallback = none(ProjectCallback)
project.stop()
ls.createOrRestartNimsuggest(projectFile, projectFile.pathToUri)
ls.sendStatusChanged()
case params.action
of saRestart:
let project = ls.projectFiles[projectFile]
ls.restart(project)
SuggestResult(actionPerformed: saRestart)
of saRestartAll:
let projectFiles = ls.projectFiles.keys.toSeq()
for projectFile in projectFiles:
let project = ls.projectFiles[projectFile]
ls.restart(project)
SuggestResult(actionPerformed: saRestartAll)
of saNone:
error "An action must be specified", params = params
SuggestResult()
proc typeDefinition*(
ls: LanguageServer, params: TextDocumentPositionParams, id: int
): Future[seq[Location]] {.async.} =
with (params.position, params.textDocument):
asyncSpawn ls.addProjectFileToPendingRequest(id.uint, uri)
let ns = await ls.tryGetNimSuggest(uri)
if ns.isNone:
return @[]
let ch = ls.getCharacter(uri, line, character)
if ch.isNone:
return @[]
result = ns.get
.`type`(uriToPath(uri), ls.uriToStash(uri), line + 1, ch.get)
.await()
.map(x => x.toUtf16Pos(ls).toLocation)
proc toSymbolInformation*(suggest: Suggest): SymbolInformation =
with suggest:
return
SymbolInformation %* {
"location": toLocation(suggest),
"kind": nimSymToLSPSymbolKind(suggest.symKind).int,
"name": suggest.name,
}
proc documentSymbols*(
ls: LanguageServer, params: DocumentSymbolParams, id: int
): Future[seq[SymbolInformation]] {.async.} =
let uri = params.textDocument.uri
asyncSpawn ls.addProjectFileToPendingRequest(id.uint, uri)
let ns = await ls.tryGetNimsuggest(uri)
if ns.isSome:
ns.get().outline(uriToPath(uri), ls.uriToStash(uri)).await().map(
x => x.toUtf16Pos(ls).toSymbolInformation
)
else:
@[]
proc scheduleFileCheck(ls: LanguageServer, uri: string) {.gcsafe, raises: [].} =
if not ls.getWorkspaceConfiguration().waitFor().autoCheckFile.get(true):
return
# schedule file check after the file is modified
let fileData = ls.openFiles.getOrDefault(uri)
if fileData.cancelFileCheck != nil and not fileData.cancelFileCheck.finished:
fileData.cancelFileCheck.complete()
if fileData.checkInProgress:
fileData.needsChecking = true
return
var cancelFuture = newFuture[void]()
fileData.cancelFileCheck = cancelFuture
sleepAsync(FILE_CHECK_DELAY).addCallback do():
if not cancelFuture.finished:
fileData.checkInProgress = true
ls.checkFile(uri).addCallback do() {.gcsafe, raises: [].}:
try:
ls.openFiles[uri].checkInProgress = false
if fileData.needsChecking:
fileData.needsChecking = false
ls.scheduleFileCheck(uri)
except KeyError:
discard
# except Exception:
# discard
proc toMarkedStrings(suggest: Suggest): seq[MarkedStringOption] =
var label = suggest.qualifiedPath.join(".")
if suggest.forth != "":
label &= ": " & suggest.forth
result = @[MarkedStringOption %* {"language": "nim", "value": label}]
if suggest.doc != "":
result.add MarkedStringOption %* {"language": "markdown", "value": suggest.doc}
proc hover*(
ls: LanguageServer, params: HoverParams, id: int
): Future[Option[Hover]] {.async.} =
with (params.position, params.textDocument):
let config = await ls.getWorkspaceConfiguration()
asyncSpawn ls.addProjectFileToPendingRequest(id.uint, uri)
let nimsuggest = await ls.tryGetNimsuggest(uri)
if nimsuggest.isNone:
return none(Hover)
let ch = ls.getCharacter(uri, line, character)
if ch.isNone:
return none(Hover)
let suggestions =
await nimsuggest.get().highlight(uriToPath(uri), ls.uriToStash(uri), line + 1, ch.get)
if suggestions.len == 0:
return none(Hover)
var suggest = suggestions[0]
if suggest.symkind == "skModule": # NOTE: skMoudle always return position (1, 0)
return some(Hover(contents: some(%toMarkedStrings(suggest))))
else:
for s in suggestions:
if s.line == line + 1:
if s.column <= ch.get:
suggest = s
else:
break
var content = toMarkedStrings(suggest)
if suggest.symkind == "skMacro" and config.nimExpandMacro.get(NIM_EXPAND_MACRO_BY_DEFAULT):
let expanded = await nimsuggest.get
.expand(uriToPath(uri), ls.uriToStash(uri), suggest.line, suggest.column)
if expanded.len > 0 and expanded[0].doc != "":
# debug "Expanded macro", expanded = expanded[0].doc
content.add MarkedStringOption %* {"language": "nim", "value": expanded[0].doc}
else:
# debug "Couldnt expand the macro. Trying with nim expand", suggest = suggest[]
let nimPath = config.getNimPath()
if nimPath.isSome:
let expanded = await nimExpandMacro(nimPath.get, suggest, uriToPath(uri))
content.add MarkedStringOption %* {"language": "nim", "value": expanded}
if suggest.section == ideDef and suggest.symkind in ["skProc"] and config.nimExpandArc.get(NIM_EXPAND_ARC_BY_DEFAULT):
debug "#Expanding arc", suggest = suggest[]
let nimPath = config.getNimPath()
if nimPath.isSome:
let expanded = await nimExpandArc(nimPath.get, suggest, uriToPath(uri))
let arcContent = "#Expanded arc \n" & expanded
content.add MarkedStringOption %* {"language": "nim", "value": arcContent}
return some(Hover(
contents: some(%content),
range: some(toLabelRange(suggest.toUtf16Pos(ls))),
))
proc references*(
ls: LanguageServer, params: ReferenceParams
): Future[seq[Location]] {.async.} =
with (params.position, params.textDocument, params.context):
let nimsuggest = await ls.tryGetNimsuggest(uri)
if nimsuggest.isNone:
return @[]
let ch = ls.getCharacter(uri, line, character)
if ch.isNone:
return @[]
let refs =
await nimsuggest.get.use(uriToPath(uri), ls.uriToStash(uri), line + 1, ch.get)
result = refs.filter(suggest => suggest.section != ideDef or includeDeclaration).map(
x => x.toUtf16Pos(ls).toLocation
)
proc prepareRename*(
ls: LanguageServer, params: PrepareRenameParams, id: int
): Future[JsonNode] {.async.} =
with (params.position, params.textDocument):
asyncSpawn ls.addProjectFileToPendingRequest(id.uint, uri)
let nimsuggest = await ls.tryGetNimsuggest(uri)
if nimsuggest.isNone:
return newJNull()
let ch = ls.getCharacter(uri, line, character)
if ch.isNone:
return newJNull()
let def =
await nimsuggest.get.def(uriToPath(uri), ls.uriToStash(uri), line + 1, ch.get)
if def.len == 0:
return newJNull()
# Check if the symbol belongs to the project
let projectDir = ls.initializeParams.getRootPath
if def[0].filePath.isRelTo(projectDir):
return %def[0].toLocation().range
return newJNull()
proc rename*(
ls: LanguageServer, params: RenameParams, id: int
): Future[WorkspaceEdit] {.async.} =
# We reuse the references command as to not duplicate it
let references = await ls.references(
ReferenceParams(
context: ReferenceContext(includeDeclaration: true),
textDocument: params.textDocument,
position: params.position,
)
)
# Build up list of edits that the client needs to perform for each file
let projectDir = ls.initializeParams.getRootPath
var edits = newJObject()
for reference in references:
# Only rename symbols in the project.
# If client supports prepareRename then an error will already have been thrown
if reference.uri.uriToPath().isRelTo(projectDir):
if reference.uri notin edits:
edits[reference.uri] = newJArray()
edits[reference.uri] &= %TextEdit(range: reference.range, newText: params.newName)
result = WorkspaceEdit(changes: some edits)
proc convertInlayHintKind(kind: SuggestInlayHintKind): InlayHintKind_int =
case kind
of sihkType:
result = 1
of sihkParameter:
result = 2
of sihkException:
# LSP doesn't have an exception inlay hint type, so we pretend (i.e. lie) that it is a type hint.
result = 1
proc toInlayHint(suggest: SuggestInlayHint, configuration: NlsConfig): InlayHint =
let hint_line = suggest.line - 1
# TODO: how to convert column?
var hint_col = suggest.column
result = InlayHint(
position: Position(line: hint_line, character: hint_col),
label: suggest.label,
kind: some(convertInlayHintKind(suggest.kind)),
paddingLeft: some(suggest.paddingLeft),
paddingRight: some(suggest.paddingRight),
)
if suggest.kind == sihkException and suggest.label == "try " and
configuration.inlayHints.isSome and
configuration.inlayHints.get.exceptionHints.isSome and
configuration.inlayHints.get.exceptionHints.get.hintStringLeft.isSome:
result.label = configuration.inlayHints.get.exceptionHints.get.hintStringLeft.get
if suggest.kind == sihkException and suggest.label == "!" and
configuration.inlayHints.isSome and
configuration.inlayHints.get.exceptionHints.isSome and
configuration.inlayHints.get.exceptionHints.get.hintStringRight.isSome:
result.label = configuration.inlayHints.get.exceptionHints.get.hintStringRight.get
if suggest.tooltip != "":
result.tooltip = some(suggest.tooltip)
else:
result.tooltip = some("")
if suggest.allowInsert:
result.textEdits = some(
@[
TextEdit(
newText: suggest.label,
`range`: Range(
start: Position(line: hint_line, character: hint_col),
`end`: Position(line: hint_line, character: hint_col),
),
)
]
)
proc inlayHint*(
ls: LanguageServer, params: InlayHintParams, id: int
): Future[seq[InlayHint]] {.async.} =
debug "inlayHint received..."
with (params.range, params.textDocument):
asyncSpawn ls.addProjectFileToPendingRequest(id.uint, uri)
let
configuration = ls.getWorkspaceConfiguration.await()
nimsuggest = await ls.tryGetNimsuggest(uri)
if nimsuggest.isNone or nimsuggest.get.protocolVersion < 4 or
not configuration.inlayHintsEnabled:
return @[]
let ch = ls.getCharacter(uri, start.line, start.character)
if ch.isNone:
return @[]
let suggestions = await nimsuggest.get.inlayHints(
uriToPath(uri),
ls.uriToStash(uri),
start.line + 1,
ch.get,
`end`.line + 1,
ch.get,
" +exceptionHints +parameterHints",
)
result = suggestions
.filter(
x =>
((x.inlayHintInfo.kind == sihkType) and configuration.typeHintsEnabled) or (
(x.inlayHintInfo.kind == sihkException) and
configuration.exceptionHintsEnabled
) or (
(x.inlayHintInfo.kind == sihkParameter) and
configuration.parameterHintsEnabled
)
)
.map(x => x.inlayHintInfo.toUtf16Pos(ls, uri).toInlayHint(configuration))
.filter(x => x.label != "")
proc codeAction*(
ls: LanguageServer, params: CodeActionParams
): Future[seq[CodeAction]] {.async.} =
let projectUri = await getProjectFile(params.textDocument.uri.uriToPath, ls)
return
seq[CodeAction] %* [
{
"title": "Clean build",
"kind": "source",
"command": {
"title": "Clean build",
"command": RECOMPILE_COMMAND,
"arguments": @[projectUri],
},
},
{
"title": "Refresh project errors",
"kind": "source",
"command": {
"title": "Refresh project errors",
"command": CHECK_PROJECT_COMMAND,
"arguments": @[projectUri],
},
},
{
"title": "Restart nimsuggest",
"kind": "source",
"command": {
"title": "Restart nimsuggest",
"command": RESTART_COMMAND,
"arguments": @[projectUri],
},
},
]
proc executeCommand*(
ls: LanguageServer, params: ExecuteCommandParams
): Future[JsonNode] {.async.} =
let projectFile = params.arguments[0].getStr
case params.command
of RESTART_COMMAND:
debug "Restarting nimsuggest", projectFile = projectFile
ls.createOrRestartNimsuggest(projectFile, projectFile.pathToUri)
of CHECK_PROJECT_COMMAND:
debug "Checking project", projectFile = projectFile
ls.checkProject(projectFile.pathToUri).traceAsyncErrors
of RECOMPILE_COMMAND:
debug "Clean build", projectFile = projectFile
let
token = fmt "Compiling {projectFile}"
ns = ls.projectFiles.getOrDefault(projectFile).ns
if ns != nil:
ls.workDoneProgressCreate(token)
ls.progress(token, "begin", fmt "Compiling project {projectFile}")
ns.await().recompile().addCallback do():
ls.progress(token, "end")
ls.checkProject(projectFile.pathToUri).traceAsyncErrors
result = newJNull()
proc toSignatureInformation(suggest: Suggest): SignatureInformation =
var fnKind, strParams: string
var params = newSeq[ParameterInformation]()
#TODO handle params. Ideally they are handled in the compiler but as fallback we could handle them as follows
#notice we will need to also handle the ',' and the back and forths between the client and the server
if scanf(suggest.forth, "$*($*)", fnKind, strParams):
for param in strParams.split(","):
params.add(ParameterInformation(label: param))
let name = suggest.qualifiedPath[^1].strip(chars = {'`'})
let detail = suggest.forth.split(" ")
var label = name
if detail.len > 1:
label = &"{fnKind} {name}({strParams})"
return
SignatureInformation %* {
"label": label,
"documentation": suggest.doc,
"parameters": newSeq[ParameterInformation](), #notice params is not used
}
proc signatureHelp*(
ls: LanguageServer, params: SignatureHelpParams, id: int
): Future[Option[SignatureHelp]] {.async.} =
#TODO handle prev signature
# if params.context.activeSignatureHelp.isSome:
# let prevSignature = params.context.activeSignatureHelp.get.signatures.get[params.context.activeSignatureHelp.get.activeSignature.get]
# debug "prevSignature ", prevSignature = $prevSignature.label
# else:
# debug "no prevSignature"
#only support signatureHelp if the client supports it
# if docCaps.signatureHelp.isSome and docCaps.signatureHelp.get.contextSupport.get(false):
# result.capabilities.signatureHelpProvider = SignatureHelpOptions(
# triggerCharacters: some(@["(", ","])
# )
if not ls.clientCapabilities.supportSignatureHelp():
#Some clients doesnt support signatureHelp
return none[SignatureHelp]()
with (params.position, params.textDocument):
asyncSpawn ls.addProjectFileToPendingRequest(id.uint, uri)
let nimsuggest = await ls.tryGetNimsuggest(uri)
if nimsuggest.isNone:
return none[SignatureHelp]()
if nsCon notin nimSuggest.get.capabilities:
#support signatureHelp only if the current version of NimSuggest supports it.
return none[SignatureHelp]()
let ch = ls.getCharacter(uri, line, character)
if ch.isNone:
return none[SignatureHelp]()
let completions =
await nimsuggest.get.con(uriToPath(uri), ls.uriToStash(uri), line + 1, ch.get)
let signatures = completions.map(toSignatureInformation)
if signatures.len() > 0:
return some SignatureHelp(
signatures: some(signatures), activeSignature: some(0), activeParameter: some(0)
)
else:
return none[SignatureHelp]()
proc format*(
ls: LanguageServer, nphPath, uri: string
): Future[Option[TextEdit]] {.async.} =
let filePath = ls.uriStorageLocation(uri)
if not fileExists(filePath):
warn "File doenst exist ", filePath = filePath, uri = uri
return none(TextEdit)
debug "nph starts", nphPath = nphPath, filePath = filePath
let process = await startProcess(
nphPath,
arguments = @[filePath],
options = {UsePath},
stderrHandle = AsyncProcess.Pipe,
)
let res = await process.waitForExit(InfiniteDuration)
if res != 0:
let err = string.fromBytes(process.stderrStream.read().await)
error "There was an error trying to format the document. ", err = err
ls.showMessage(&"Error formating {uri}:{err}", MessageType.Error)
return none(TextEdit)
debug "nph completes ", res = res
let formattedText = readFile(filePath)
let fullRange = Range(
start: Position(line: 0, character: 0),
`end`: Position(line: int(uint32.high), character: int(uint32.high)),
)
some TextEdit(range: fullRange, newText: formattedText)
proc formatting*(
ls: LanguageServer, params: DocumentFormattingParams, id: int
): Future[seq[TextEdit]] {.async.} =
with (params.textDocument):
asyncSpawn ls.addProjectFileToPendingRequest(id.uint, uri)
debug "Received Formatting request "
let formatTextEdit = await ls.format(getNphPath().get(), uri)
if formatTextEdit.isSome:
return @[formatTextEdit.get]
proc workspaceSymbol*(
ls: LanguageServer, params: WorkspaceSymbolParams, id: int
): Future[seq[SymbolInformation]] {.async.} =
if ls.lastNimsuggest != nil:
let
nimsuggest = await ls.lastNimsuggest
symbols = await nimsuggest.globalSymbols(params.query, "-")
return symbols.map(x => x.toUtf16Pos(ls).toSymbolInformation)
proc toDocumentHighlight(suggest: Suggest): DocumentHighlight =
return DocumentHighlight %* {"range": toLabelRange(suggest)}
proc documentHighlight*(
ls: LanguageServer, params: TextDocumentPositionParams, id: int
): Future[seq[DocumentHighlight]] {.async.} =
with (params.position, params.textDocument):
asyncSpawn ls.addProjectFileToPendingRequest(id.uint, uri)
let nimsuggest = await ls.tryGetNimsuggest(uri)
if nimsuggest.isNone:
return @[]
let ch = ls.getCharacter(uri, line, character)
if ch.isNone:
return @[]
let suggestLocations = await nimsuggest.get.highlight(
uriToPath(uri), ls.uriToStash(uri), line + 1, ch.get
)
result = suggestLocations.map(x => x.toUtf16Pos(ls).toDocumentHighlight)
proc extractId(id: JsonNode): int =
if id.kind == JInt:
result = id.getInt
if id.kind == JString:
discard parseInt(id.getStr, result)
proc shutdown*(
ls: LanguageServer, input: JsonNode
): Future[JsonNode] {.async, gcsafe.} =
debug "Shutting down"
await ls.stopNimsuggestProcesses()
ls.isShutdown = true
# let id = input{"id"}.extractId
result = newJNull()
trace "Shutdown complete"
proc exit*(
p: tuple[ls: LanguageServer, onExit: OnExitCallback], _: JsonNode
): Future[JsonNode] {.async, gcsafe.} =
if not p.ls.isShutdown:
debug "Received an exit request without prior shutdown request"
await p.ls.stopNimsuggestProcesses()
debug "Quitting process"
result = newJNull()
await p.onExit()
proc startNimbleProcess(ls: LanguageServer, args: seq[string]): Future[AsyncProcessRef] {.async.} =
await startProcess(
"nimble",
arguments = args,
options = {UsePath},
workingDir = ls.initializeParams.getRootPath,
stdoutHandle = AsyncProcess.Pipe,
stderrHandle = AsyncProcess.Pipe,
)
proc tasks*(
ls: LanguageServer, conf: JsonNode
): Future[seq[NimbleTask]] {.async.} =
let rootPath: string = ls.initializeParams.getRootPath
debug "Received tasks ", rootPath = rootPath
delEnv "NIMBLE_DIR"
let process = await ls.startNimbleProcess(@["tasks"])
let res =
await process.waitForExit(InfiniteDuration) #TODO handle error (i.e. no nimble file)
let output = await process.stdoutStream.readLine()
var name, desc: string
for line in output.splitLines:
if scanf(line, "$+ $*", name, desc):
#first run of nimble tasks can compile nim and output the result of the compilation
if name.isWord:
result.add NimbleTask(name: name.strip(), description: desc.strip())
proc runTask*(
ls: LanguageServer, params: RunTaskParams
): Future[RunTaskResult] {.async.} =
let process = await ls.startNimbleProcess(params.command)
let res = await process.waitForExit(InfiniteDuration)
result.command = params.command
let prefix = "\""
while not process.stdoutStream.atEof():
var lines = process.stdoutStream.readLine().await.splitLines
for line in lines.mitems:
if line.startsWith(prefix):
line = line.unescape(prefix)
if line != "":
result.output.add line
debug "Ran nimble cmd/task", command = $params.command, output = $result.output
#Notifications
proc initialized*(ls: LanguageServer, _: JsonNode): Future[void] {.async.} =
debug "Client initialized."
maybeRegisterCapabilityDidChangeConfiguration(ls)
maybeRequestConfigurationFromClient(ls)
proc cancelRequest*(ls: LanguageServer, params: CancelParams): Future[void] {.async.} =
if params.id.isSome:
let id = params.id.get.getInt.uint
if id notin ls.pendingRequests:
return
let pendingRequest = ls.pendingRequests[id]
if ls.pendingRequests[id].request != nil:
debug "Cancelling: ", id = id
await ls.pendingRequests[id].request.cancelAndWait()
ls.pendingRequests[id].state = prsCancelled
ls.pendingRequests[id].endTime = now()
proc setTrace*(ls: LanguageServer, params: SetTraceParams) {.async.} =
debug "setTrace", value = params.value
proc didChange*(
ls: LanguageServer, params: DidChangeTextDocumentParams
): Future[void] {.async, gcsafe.} =
with params:
let uri = textDocument.uri
if uri notin ls.openFiles:
return
let file = open(ls.uriStorageLocation(uri), fmWrite)
ls.openFiles[uri].fingerTable = @[]
ls.openFiles[uri].changed = true
if contentChanges.len <= 0:
file.close()
return
for line in contentChanges[0].text.splitLines:
ls.openFiles[uri].fingerTable.add line.createUTFMapping()
file.writeLine line
file.close()
ls.scheduleFileCheck(uri)
proc autoFormat(ls: LanguageServer, config: NlsConfig, uri: string) {.async.} =
let nphPath = getNphPath()
let shouldAutoFormat =
nphPath.isSome and ls.serverCapabilities.documentFormattingProvider.get(false) and
ls.clientCapabilities.workspace.get(ClientCapabilities_workspace()).applyEdit.get(
false
) and config.formatOnSave.get(false)
if shouldAutoFormat:
let formatTextEdit = await ls.format(nphPath.get(), uri)
if formatTextEdit.isSome:
let workspaceEdit = WorkspaceEdit(
documentChanges: some @[
TextDocumentEdit(
textDocument: VersionedTextDocumentIdentifier(uri: uri),
edits: some @[formatTextEdit.get()],
)
]
)
discard await ls.applyEdit(ApplyWorkspaceEditParams(edit: workspaceEdit))
proc didSave*(
ls: LanguageServer, params: DidSaveTextDocumentParams
): Future[void] {.async, gcsafe.} =
let
uri = params.textDocument.uri
config = await ls.getWorkspaceConfiguration()
asyncSpawn ls.autoFormat(config, uri)
let nimsuggest = await ls.tryGetNimsuggest(uri)
if nimsuggest.isNone:
return
ls.openFiles[uri].changed = false
traceAsyncErrors nimsuggest.get.changed(uriToPath(uri))
if config.checkOnSave.get(true):
debug "Checking project", uri = uri
traceAsyncErrors ls.checkProject(uri)
# var toStop = newTable[string, Nimsuggest]()
# #We first get the project file for the current file so we can test if this file recently imported another project
# let thisProjectFile = await getProjectFile(uri.uriToPath, ls)
# let ns: NimSuggest = await ls.projectFiles[thisProjectFile]
# if ns.canHandleUnknown:
# for projectFile in ls.projectFiles.keys:
# if projectFile in ls.entryPoints: continue
# let isKnown = await ns.isKnown(projectFile)
# if isKnown:
# toStop[projectFile] = await ls.projectFiles[projectFile]
# for projectFile, ns in toStop:
# ns.stop()
# ls.projectFiles.del projectFile
# if toStop.len > 0:
# ls.sendStatusChanged()
proc didClose*(
ls: LanguageServer, params: DidCloseTextDocumentParams
): Future[void] {.async, gcsafe.} =
await ls.didCloseFile(params.textDocument.uri)
proc didOpen*(
ls: LanguageServer, params: DidOpenTextDocumentParams
): Future[void] {.async, gcsafe.} =
await ls.didOpenFile(params.textDocument)
proc didChangeConfiguration*(
ls: LanguageServer, conf: JsonNode
): Future[void] {.async, gcsafe.} =
debug "Changed configuration: ", conf = conf
if ls.usePullConfigurationModel:
ls.maybeRequestConfigurationFromClient
else:
if ls.workspaceConfiguration.finished:
let
oldConfiguration = parseWorkspaceConfiguration(ls.workspaceConfiguration.read)
newConfiguration = parseWorkspaceConfiguration(conf)
ls.workspaceConfiguration = newFuture[JsonNode]()
ls.workspaceConfiguration.complete(conf)
handleConfigurationChanges(ls, oldConfiguration, newConfiguration)