forked from nim-lang/langserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nimlangserver.nim
1024 lines (904 loc) · 35.9 KB
/
nimlangserver.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
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
import macros, strformat, faststreams/async_backend,
faststreams/asynctools_adapters, faststreams/inputs, faststreams/outputs,
json_rpc/streamconnection, os, sugar, sequtils, hashes, osproc,
suggestapi, protocol/enums, protocol/types, with, tables, strutils, sets,
./utils, ./pipes, chronicles, std/re, uri, "$nim/compiler/pathutils",
procmonitor
const
RESTART_COMMAND = "nimlangserver.restart"
RECOMPILE_COMMAND = "nimlangserver.recompile"
CHECK_PROJECT_COMMAND = "nimlangserver.checkProject"
FILE_CHECK_DELAY = 1000
type
NlsNimsuggestConfig = ref object of RootObj
projectFile: string
fileRegex: string
NlsWorkingDirectoryMaping = ref object of RootObj
projectFile: string
directory: string
NlsConfig = ref object of RootObj
projectMapping*: OptionalSeq[NlsNimsuggestConfig]
workingDirectoryMapping*: OptionalSeq[NlsWorkingDirectoryMaping]
checkOnSave*: Option[bool]
nimsuggestPath*: Option[string]
timeout*: Option[int]
autoRestart*: Option[bool]
autoCheckFile*: Option[bool]
autoCheckProject*: Option[bool]
FileInfo = ref object of RootObj
projectFile: Future[string]
changed: bool
fingerTable: seq[seq[tuple[u16pos, offset: int]]]
cancelFileCheck: Future[void]
checkInProgress: bool
needsChecking: bool
LanguageServer* = ref object
clientCapabilities*: ClientCapabilities
initializeParams*: InitializeParams
connection: StreamConnection
projectFiles: Table[string, Future[Nimsuggest]]
openFiles: Table[string, FileInfo]
cancelFutures: Table[int, Future[void]]
workspaceConfiguration: Future[JsonNode]
filesWithDiags: HashSet[string]
lastNimsuggest: Future[Nimsuggest]
isShutdown*: bool
storageDir*: string
Certainty = enum
None,
Folder,
Cfg,
Nimble
macro `%*`*(t: untyped, inputStream: untyped): untyped =
result = newCall(bindSym("to", brOpen),
newCall(bindSym("%*", brOpen), inputStream), t)
proc partial*[A, B, C] (fn: proc(a: A, b: B): C {.gcsafe.}, a: A):
proc (b: B) : C {.gcsafe, raises: [Defect, CatchableError, Exception].} =
return
proc(b: B): C {.gcsafe, raises: [Defect, CatchableError, Exception].} =
return fn(a, b)
proc partial*[A, B, C] (fn: proc(a: A, b: B, id: int): C {.gcsafe.}, a: A):
proc (b: B, id: int) : C {.gcsafe, raises: [Defect, CatchableError, Exception].} =
return
proc(b: B, id: int): C {.gcsafe, raises: [Defect, CatchableError, Exception].} =
return fn(a, b, id)
proc getProjectFileAutoGuess(fileUri: string): string =
let file = fileUri.decodeUrl
result = file
let (dir, _, _) = result.splitFile()
var
path = dir
certainty = Certainty.None
while path.len > 0 and path != "/":
let
(dir, fname, ext) = path.splitFile()
current = fname & ext
if fileExists(path / current.addFileExt(".nim")) and certainty <= Folder:
result = path / current.addFileExt(".nim")
certainty = Folder
if fileExists(path / current.addFileExt(".nim")) and
(fileExists(path / current.addFileExt(".nim.cfg")) or
fileExists(path / current.addFileExt(".nims"))) and certainty <= Cfg:
result = path / current.addFileExt(".nim")
certainty = Cfg
if certainty <= Nimble:
for nimble in walkFiles(path / "*.nimble"):
let info = execProcess("nimble dump " & nimble)
var sourceDir, name: string
for line in info.splitLines:
if line.startsWith("srcDir"):
sourceDir = path / line[(1 + line.find '"')..^2]
if line.startsWith("name"):
name = line[(1 + line.find '"')..^2]
let projectFile = sourceDir / (name & ".nim")
if sourceDir.len != 0 and name.len != 0 and
file.isRelativeTo(sourceDir) and fileExists(projectFile):
debug "Found nimble project", projectFile = projectFile
result = projectFile
certainty = Nimble
path = dir
proc getWorkspaceConfiguration(ls: LanguageServer): Future[NlsConfig] {.async.} =
try:
let nlsConfig: seq[NlsConfig] =
(%ls.workspaceConfiguration.await).to(seq[NlsConfig])
result = if nlsConfig.len > 0 and nlsConfig[0] != nil: nlsConfig[0] else: NlsConfig()
except CatchableError:
debug "Failed to parse the configuration."
result = NlsConfig()
proc getProjectFile(fileUri: string, ls: LanguageServer): Future[string] {.async.} =
let
rootPath = AbsoluteDir(ls.initializeParams.rootUri.uriToPath)
pathRelativeToRoot = string(AbsoluteFile(fileUri).relativeTo(rootPath))
mappings = ls.getWorkspaceConfiguration.await().projectMapping.get(@[])
for mapping in mappings:
if find(cstring(pathRelativeToRoot), re(mapping.fileRegex), 0, pathRelativeToRoot.len) != -1:
result = string(rootPath) / mapping.projectFile
trace "getProjectFile", project = result, uri = fileUri, matchedRegex = mapping.fileRegex
return result
else:
trace "getProjectFile does not match", uri = fileUri, matchedRegex = mapping.fileRegex
result = getProjectFileAutoGuess(fileUri)
debug "getProjectFile", project = result
proc showMessage(ls: LanguageServer, message: string, typ: MessageType) =
ls.connection.notify(
"window/showMessage",
%* {
"type": typ.int,
"message": message
})
# Fixes callback clobbering in core implementation
proc `or`*[T, Y](fut1: Future[T], fut2: Future[Y]): Future[void] =
var retFuture = newFuture[void]("asyncdispatch.`or`")
proc cb[X](fut: Future[X]) =
if not retFuture.finished:
if fut.failed: retFuture.fail(fut.error)
else: retFuture.complete()
fut1.addCallback(cb[T])
fut2.addCallback(cb[Y])
return retFuture
proc getCharacter(ls: LanguageServer, uri: string, line: int, character: int): int =
return ls.openFiles[uri].fingerTable[line].utf16to8(character)
proc initialize(ls: LanguageServer, params: InitializeParams):
Future[InitializeResult] {.async.} =
debug "Initialize received..."
if params.processId.isSome:
let pid = params.processId.get
if pid.kind == JInt:
hookProcMonitor(int(pid.num))
ls.initializeParams = params
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)
),
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)
)
)
# 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
if docCaps.rename.isSome and docCaps.rename.get().prepareSupport.get(false):
result.capabilities.renameProvider = %* {
"prepareProvider": true
}
proc initialized(ls: LanguageServer, _: JsonNode):
Future[void] {.async.} =
debug "Client initialized."
let workspaceCap = ls.initializeParams.capabilities.workspace
if workspaceCap.isSome and workspaceCap.get.configuration.get(false):
debug "Requesting configuration from the client"
let configurationParams = ConfigurationParams %* {"items": [{"section": "nim"}]}
ls.workspaceConfiguration =
ls.connection.call("workspace/configuration",
%configurationParams)
ls.workspaceConfiguration.addCallback() do (futConfiguration: Future[JsonNode]):
if futConfiguration.error.isNil:
debug "Received the following configuration", configuration = futConfiguration.read()
else:
debug "Client does not support workspace/configuration"
ls.workspaceConfiguration.complete(newJArray())
proc orCancelled[T](fut: Future[T], ls: LanguageServer, id: int): Future[T] {.async.} =
ls.cancelFutures[id] = newFuture[void]()
await fut or ls.cancelFutures[id]
ls.cancelFutures.del id
if fut.finished:
if fut.error.isNil:
return fut.read
else:
raise fut.error
else:
debug "Future cancelled.", id = id
let ex = newException(Cancelled, fmt "Cancelled {id}")
fut.fail(ex)
debug "Future cancelled, throwing...", id = id
raise ex
proc cancelRequest(ls: LanguageServer, params: CancelParams):
Future[void] {.async.} =
if params.id.isSome:
let
id = params.id.get.getInt
cancelFuture = ls.cancelFutures.getOrDefault id
debug "Cancelling: ", id = id
if not cancelFuture.isNil:
cancelFuture.complete()
proc uriStorageLocation(ls: LanguageServer, uri: string): string =
ls.storageDir / (hash(uri).toHex & ".nim")
proc uriToStash(ls: LanguageServer, uri: string): string =
if ls.openFiles.hasKey(uri) and ls.openFiles[uri].changed:
uriStorageLocation(ls, uri)
else:
""
proc getNimsuggest(ls: LanguageServer, uri: string): Future[Nimsuggest] {.async.} =
let projectFile = await ls.openFiles[uri].projectFile
ls.lastNimsuggest = ls.projectFiles[projectFile]
return await ls.projectFiles[projectFile]
proc range*(startLine, startCharacter, endLine, endCharacter: int): Range =
return Range %* {
"start": {
"line": startLine,
"character": startCharacter
},
"end": {
"line": endLine,
"character": endCharacter
}
}
proc toLabelRange(suggest: Suggest): Range =
with suggest:
let endColumn = column + qualifiedPath[^1].strip(chars = {'`'}).len
return range(line - 1, column, line - 1, endColumn)
proc toDiagnostic(suggest: Suggest): Diagnostic =
with suggest:
let
endColumn = column + doc.rfind('\'') - doc.find('\'') - 2
node = %* {
"uri": pathToUri(filepath) ,
"range": range(line - 1, column, line - 1, column + endColumn),
"severity": case forth:
of "Error": DiagnosticSeverity.Error.int
of "Hint": DiagnosticSeverity.Hint.int
of "Warning": DiagnosticSeverity.Warning.int
else: DiagnosticSeverity.Error.int,
"message": doc,
"source": "nim",
"code": "nimsuggest chk"
}
return node.to(Diagnostic)
proc progressSupported(ls: LanguageServer): bool =
result = ls.initializeParams
.capabilities
.window
.get(ClientCapabilities_window())
.workDoneProgress
.get(false)
proc progress(ls: LanguageServer; token, kind: string, title = "") =
if ls.progressSupported:
ls.connection.notify(
"$/progress",
%* {
"token": token,
"value": {
"kind": kind,
"title": title
}
})
proc workDoneProgressCreate(ls: LanguageServer, token: string) =
if ls.progressSupported:
discard ls.connection.call("window/workDoneProgress/create",
%ProgressParams(token: token))
proc sendDiagnostics(ls: LanguageServer, diagnostics: seq[Suggest], path: string) =
debug "Sending diagnostics", count = diagnostics.len, path = path
let params = PublishDiagnosticsParams %* {
"uri": pathToUri(path),
"diagnostics": diagnostics.map(toDiagnostic)
}
ls.connection.notify("textDocument/publishDiagnostics", %params)
if diagnostics.len != 0:
ls.filesWithDiags.incl path
else:
ls.filesWithDiags.excl path
proc checkFile(ls: LanguageServer, uri: string): Future[void] {.async.} =
debug "Checking", uri = uri
let token = fmt "Checking file {uri}"
ls.workDoneProgressCreate(token)
ls.progress(token, "begin", fmt "Checking {uri.uriToPath}")
let
path = uriToPath(uri)
diagnostics = ls.getNimsuggest(uri)
.await()
.chkFile(path, ls.uriToStash(uri))
.await()
ls.progress(token, "end")
ls.sendDiagnostics(diagnostics, path)
proc cancelPendingFileChecks(ls: LanguageServer, nimsuggest: Nimsuggest) =
# stop all checks on file level if we are going to run checks on project
# level.
for uri in nimsuggest.openFiles:
let fileData = ls.openFiles[uri]
if fileData != nil:
let cancelFileCheck = fileData.cancelFileCheck
if cancelFileCheck != nil and not cancelFileCheck.finished:
cancelFileCheck.complete()
fileData.needsChecking = false
proc checkProject(ls: LanguageServer, uri: string): Future[void] {.async, gcsafe.} =
if not ls.getWorkspaceConfiguration.await().autoCheckProject.get(true):
return
debug "Running diagnostics", uri = uri
let nimsuggest = ls.getNimsuggest(uri).await
if nimsuggest.checkProjectInProgress:
debug "Check project is already running", uri = uri
nimsuggest.needsCheckProject = true
return
ls.cancelPendingFileChecks(nimsuggest)
let token = fmt "Checking {uri}"
ls.workDoneProgressCreate(token)
ls.progress(token, "begin", fmt "Checking project {uri.uriToPath}")
nimsuggest.checkProjectInProgress = true
proc getFilepath(s: Suggest): string = s.filepath
let
diagnostics = nimsuggest.chk(uriToPath(uri), ls.uriToStash(uri))
.await()
.filter(sug => sug.filepath != "???")
filesWithDiags = diagnostics.map(s => s.filepath).toHashSet
ls.progress(token, "end")
debug "Found diagnostics", file = filesWithDiags
for (path, diags) in groupBy(diagnostics, getFilepath):
ls.sendDiagnostics(diags, path)
# clean files with no diags
for path in ls.filesWithDiags:
if not filesWithDiags.contains path:
debug "Sending zero diags", path = path
let params = PublishDiagnosticsParams %* {
"uri": pathToUri(path),
"diagnostics": @[]
}
ls.connection.notify("textDocument/publishDiagnostics", %params)
ls.filesWithDiags = filesWithDiags
nimsuggest.checkProjectInProgress = false
if nimsuggest.needsCheckProject:
nimsuggest.needsCheckProject = false
callSoon() do () {.gcsafe.}:
debug "Running delayed check project...", uri = uri
traceAsyncErrors ls.checkProject(uri)
proc getWorkingDir(ls: LanguageServer, path: string): Future[string] {.async.} =
let
rootPath = AbsoluteDir(ls.initializeParams.rootUri.uriToPath)
pathRelativeToRoot = string(AbsoluteFile(path).relativeTo(rootPath))
mapping = ls.getWorkspaceConfiguration.await().workingDirectoryMapping.get(@[])
result = getCurrentDir()
for m in mapping:
if m.projectFile == pathRelativeToRoot:
result = rootPath.string / m.directory
break;
proc createOrRestartNimsuggest(ls: LanguageServer, projectFile: string, uri = ""): void {.gcsafe.} =
let
configuration = ls.getWorkspaceConfiguration().waitFor()
nimsuggestPath = configuration.nimsuggestPath.get("nimsuggest")
workingDir = ls.getWorkingDir(projectFile).waitFor()
timeout = configuration.timeout.get(REQUEST_TIMEOUT)
restartCallback = proc (ns: Nimsuggest) {.gcsafe.} =
warn "Restarting the server due to requests being to slow", projectFile = projectFile
ls.showMessage(fmt "Restarting nimsuggest for file {projectFile} due to timeout.",
MessageType.Warning)
ls.createOrRestartNimsuggest(projectFile, uri)
errorCallback = proc (ns: Nimsuggest) {.gcsafe.} =
warn "Server stopped.", projectFile = projectFile
if configuration.autoRestart.get(true) and ns.successfullCall:
ls.createOrRestartNimsuggest(projectFile, uri)
else:
ls.showMessage(fmt "Server failed with {ns.errorMessage}.",
MessageType.Error)
nimsuggestFut = createNimsuggest(projectFile, nimsuggestPath,
timeout, restartCallback, errorCallback, workingDir)
token = fmt "Creating nimsuggest for {projectFile}"
if ls.projectFiles.hasKey(projectFile):
var nimsuggestData = ls.projectFiles[projectFile]
nimSuggestData.addCallback() do (fut: Future[Nimsuggest]) -> void:
fut.read.stop()
ls.projectFiles[projectFile] = nimsuggestFut
ls.progress(token, "begin", fmt "Restarting nimsuggest for {projectFile}")
else:
ls.progress(token, "begin", fmt "Creating nimsuggest for {projectFile}")
ls.projectFiles[projectFile] = nimsuggestFut
ls.workDoneProgressCreate(token)
nimsuggestFut.addCallback do (fut: Future[Nimsuggest]):
if fut.read.failed:
let msg = fut.read.errorMessage
ls.showMessage(fmt "Nimsuggest initialization for {projectFile} failed with: {msg}",
MessageType.Error)
else:
ls.showMessage(fmt "Nimsuggest initialized for {projectFile}",
MessageType.Info)
traceAsyncErrors ls.checkProject(uri)
fut.read().openFiles.incl uri
ls.progress(token, "end")
proc warnIfUnknown(ls: LanguageServer, ns: Nimsuggest, uri: string, projectFile: string):
Future[void] {.async, gcsafe.} =
let path = uri.uriToPath
let sug = await ns.known(path)
if sug[0].forth == "false":
ls.showMessage(fmt """{path} is not compiled as part of project {projectFile}.
In orde to get the IDE features working you must either configure nim.projectMapping or import the module.""",
MessageType.Warning)
proc didOpen(ls: LanguageServer, params: DidOpenTextDocumentParams):
Future[void] {.async, gcsafe.} =
with params.textDocument:
debug "New document opened for URI:", uri = uri
let
file = open(ls.uriStorageLocation(uri), fmWrite)
projectFileFuture = getProjectFile(uriToPath(uri), ls)
ls.openFiles[uri] = FileInfo(
projectFile: projectFileFuture,
changed: false,
fingerTable: @[])
let projectFile = await projectFileFuture
debug "Document associated with the following projectFile", uri = uri, projectFile = projectFile
if not ls.projectFiles.hasKey(projectFile):
ls.createOrRestartNimsuggest(projectFile, uri)
for line in text.splitLines:
ls.openFiles[uri].fingerTable.add line.createUTFMapping()
file.writeLine line
file.close()
ls.getNimsuggest(uri).addCallback() do (fut: Future[Nimsuggest]) -> void:
if not fut.failed:
discard ls.warnIfUnknown(fut.read, uri, projectFile)
proc scheduleFileCheck(ls: LanguageServer, uri: string) {.gcsafe.} =
if not ls.getWorkspaceConfiguration().waitFor().autoCheckFile.get(true):
return
# schedule file check after the file is modified
let fileData = ls.openFiles[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.}:
ls.openFiles[uri].checkInProgress = false
if fileData.needsChecking:
fileData.needsChecking = false
ls.scheduleFileCheck(uri)
proc didChange(ls: LanguageServer, params: DidChangeTextDocumentParams):
Future[void] {.async, gcsafe.} =
with params:
let
uri = textDocument.uri
file = open(ls.uriStorageLocation(uri), fmWrite)
ls.openFiles[uri].fingerTable = @[]
ls.openFiles[uri].changed = true
for line in contentChanges[0].text.splitLines:
ls.openFiles[uri].fingerTable.add line.createUTFMapping()
file.writeLine line
file.close()
ls.scheduleFileCheck(uri)
proc didSave(ls: LanguageServer, params: DidSaveTextDocumentParams):
Future[void] {.async, gcsafe.} =
let
uri = params.textDocument.uri
nimsuggest = ls.getNimsuggest(uri).await()
ls.openFiles[uri].changed = false
traceAsyncErrors nimsuggest.changed(uriToPath(uri))
if ls.getWorkspaceConfiguration().await().checkOnSave.get(true):
debug "Checking project", uri = uri
traceAsyncErrors ls.checkProject(uri)
proc didClose(ls: LanguageServer, params: DidCloseTextDocumentParams):
Future[void] {.async, gcsafe.} =
let uri = params.textDocument.uri
debug "Closed the following document:", uri = uri
if ls.openFiles[uri].changed:
# check the file if it is closed but not saved.
traceAsyncErrors ls.checkFile(uri)
ls.openFiles.del uri
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
nimsuggest = await ls.getNimsuggest(uri)
suggestions = await nimsuggest
.def(uriToPath(uri),
ls.uriToStash(uri),
line + 1,
ls.getCharacter(uri, line, character))
.orCancelled(ls, id)
if suggestions.len == 0:
return none[Hover]();
else:
return some(Hover(contents: some(%toMarkedStrings(suggestions[0]))))
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):
result = ls.getNimsuggest(uri)
.await()
.def(uriToPath(uri),
ls.uriToStash(uri),
line + 1,
ls.getCharacter(uri, line, character))
.orCancelled(ls, id)
.await()
.map(toLocation)
proc declaration(ls: LanguageServer, params: TextDocumentPositionParams, id: int):
Future[seq[Location]] {.async.} =
with (params.position, params.textDocument):
result = ls.getNimsuggest(uri)
.await()
.declaration(uriToPath(uri),
ls.uriToStash(uri),
line + 1,
ls.getCharacter(uri, line, character))
.orCancelled(ls, id)
.await()
.map(toLocation)
proc expandAll(ls: LanguageServer, params: TextDocumentPositionParams):
Future[ExpandResult] {.async.} =
with (params.position, params.textDocument):
let expand = ls.getNimsuggest(uri)
.await()
.expand(uriToPath(uri),
ls.uriToStash(uri),
line + 1,
ls.getCharacter(uri, line, character))
.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
expand = ls.getNimsuggest(uri)
.await()
.expand(uriToPath(uri),
ls.uriToStash(uri),
line + 1,
ls.getCharacter(uri, line, character),
fmt " {tag}")
.await()
if expand.len != 0:
result = ExpandResult(content: expand[0].doc.fixIdentation(character),
range: expand[0].createRangeFromSuggest())
proc typeDefinition(ls: LanguageServer, params: TextDocumentPositionParams, id: int):
Future[seq[Location]] {.async.} =
with (params.position, params.textDocument):
result = ls.getNimsuggest(uri)
.await()
.`type`(uriToPath(uri),
ls.uriToStash(uri),
line + 1,
ls.getCharacter(uri, line, character))
.orCancelled(ls, id)
.await()
.map(toLocation)
proc references(ls: LanguageServer, params: ReferenceParams):
Future[seq[Location]] {.async.} =
with (params.position, params.textDocument, params.context):
let
nimsuggest = await ls.getNimsuggest(uri)
refs = await nimsuggest
.use(uriToPath(uri),
ls.uriToStash(uri),
line + 1,
ls.getCharacter(uri, line, character))
result = refs
.filter(suggest => suggest.section != ideDef or includeDeclaration)
.map(toLocation);
proc prepareRename(ls: LanguageServer, params: PrepareRenameParams,
id: int): Future[JsonNode] {.async.} =
with (params.position, params.textDocument):
let
nimsuggest = await ls.getNimsuggest(uri)
def = await nimsuggest.def(
uriToPath(uri),
ls.uriToStash(uri),
line + 1,
ls.getCharacter(uri, line, character)
)
if def.len == 0:
return newJNull()
# Check if the symbol belongs to the project
let projectDir = ls.initializeParams.rootUri.uriToPath
if def[0].filePath.isRelativeTo(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.rootUri.uriToPath
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().isRelativeTo(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
proc toInlayHint(suggest: SuggestInlayHint): 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.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):
let
nimsuggest = await ls.getNimsuggest(uri)
if nimsuggest.protocolVersion < 4:
return @[]
let
suggestions = await nimsuggest
.inlayHints(uriToPath(uri),
ls.uriToStash(uri),
start.line + 1,
ls.getCharacter(uri, start.line, start.character),
`end`.line + 1,
ls.getCharacter(uri, `end`.line, `end`.character))
.orCancelled(ls, id)
result = suggestions
.map(x => x.inlayHintInfo.toInlayHint());
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)
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 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):
let
nimsuggest = await ls.getNimsuggest(uri)
completions = await nimsuggest
.sug(uriToPath(uri),
ls.uriToStash(uri),
line + 1,
ls.getCharacter(uri, line, character))
.orCancelled(ls, id)
return completions.map(toCompletionItem);
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
result = ls.getNimsuggest(uri)
.await()
.outline(uriToPath(uri), ls.uriToStash(uri))
.orCancelled(ls, id)
.await()
.map(toSymbolInformation)
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, "-")
.orCancelled(ls, id)
return symbols.map(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):
let
nimsuggest = await ls.getNimsuggest(uri)
suggestLocations = await nimsuggest.highlight(uriToPath(uri),
ls.uriToStash(uri),
line + 1,
ls.getCharacter(uri, line, character))
.orCancelled(ls, id)
result = suggestLocations.map(toDocumentHighlight);
proc shutdown(ls: LanguageServer, params: JsonNode):
Future[JsonNode] {.async.} =
debug "Shutting down"
for ns in ls.projectFiles.values:
let ns = await ns
ns.stop()
ls.isShutdown = true
result = newJNull()
trace "Shutdown complete"
proc exit(pipeInput: AsyncInputStream, _: JsonNode):
Future[void] {.async.} =
debug "Quitting process"
pipeInput.close()
proc didChangeConfiguration(ls: LanguageServer, conf: JsonNode):
Future[void] {.async, gcsafe.} =
debug "Changed configuration: ", conf = conf
ls.workspaceConfiguration = newFuture[JsonNode]()
ls.workspaceConfiguration.complete(conf)
proc registerHandlers*(connection: StreamConnection,
pipeInput: AsyncInputStream,
storageDir: string): LanguageServer =
let ls = LanguageServer(
connection: connection,
workspaceConfiguration: Future[JsonNode](),
projectFiles: initTable[string, Future[Nimsuggest]](),
cancelFutures: initTable[int, Future[void]](),
filesWithDiags: initHashSet[string](),
openFiles: initTable[string, FileInfo](),
storageDir: storageDir)
result = ls
connection.register("initialize", partial(initialize, ls))
connection.register("textDocument/completion", partial(completion, ls))
connection.register("textDocument/definition", partial(definition, ls))
connection.register("textDocument/declaration", partial(declaration, ls))
connection.register("textDocument/typeDefinition", partial(typeDefinition, ls))
connection.register("textDocument/documentSymbol", partial(documentSymbols, ls))
connection.register("textDocument/hover", partial(hover, ls))
connection.register("textDocument/references", partial(references, ls))
connection.register("textDocument/codeAction", partial(codeAction, ls))
connection.register("textDocument/prepareRename", partial(prepareRename, ls))
connection.register("textDocument/rename", partial(rename, ls))
connection.register("textDocument/inlayHint", partial(inlayHint, ls))
connection.register("workspace/executeCommand", partial(executeCommand, ls))
connection.register("workspace/symbol", partial(workspaceSymbol, ls))
connection.register("textDocument/documentHighlight", partial(documentHighlight, ls))
connection.register("extension/macroExpand", partial(expand, ls))
connection.register("shutdown", partial(shutdown, ls))
connection.registerNotification("$/cancelRequest", partial(cancelRequest, ls))
connection.registerNotification("exit", partial(exit, pipeInput))
connection.registerNotification("initialized", partial(initialized, ls))
connection.registerNotification("textDocument/didChange", partial(didChange, ls))
connection.registerNotification("textDocument/didOpen", partial(didOpen, ls))
connection.registerNotification("textDocument/didSave", partial(didSave, ls))
connection.registerNotification("textDocument/didClose", partial(didClose, ls))
connection.registerNotification("workspace/didChangeConfiguration", partial(didChangeConfiguration, ls))
proc ensureStorageDir*: string =
result = getTempDir() / "nimlangserver"
discard existsOrCreateDir(result)