1
- import macros, strformat,
1
+ import macros, strformat,
2
2
faststreams/ async_backend,
3
3
json_rpc/ streamconnection, json_rpc/ server, os, sugar, sequtils,
4
4
suggestapi, protocol/ enums, protocol/ types, with, tables, strutils,
@@ -73,7 +73,7 @@ proc initialize*(p: tuple[ls: LanguageServer, onExit: OnExitCallback], params: I
73
73
documentSymbolProvider: some(true ),
74
74
codeActionProvider: some(true )
75
75
)
76
- )
76
+ )
77
77
# Support rename by default, but check if we can also support prepare
78
78
result .capabilities.renameProvider = % true
79
79
if params.capabilities.textDocument.isSome:
@@ -86,7 +86,7 @@ proc initialize*(p: tuple[ls: LanguageServer, onExit: OnExitCallback], params: I
86
86
}
87
87
debug " Initialize completed. Trying to start nimsuggest instances"
88
88
# If we are in a nimble project here, we try to start the entry points
89
-
89
+
90
90
proc toCompletionItem(suggest: Suggest): CompletionItem =
91
91
with suggest:
92
92
return CompletionItem %* {
@@ -115,7 +115,7 @@ proc completion*(ls: LanguageServer, params: CompletionParams, id: int):
115
115
for completion in result :
116
116
if completion.label notin unique:
117
117
unique[completion.label] = completion
118
- result = unique.values.toSeq
118
+ result = unique.values.toSeq
119
119
120
120
proc toLocation*(suggest: Suggest): Location =
121
121
return Location %* {
@@ -192,7 +192,7 @@ proc expand*(ls: LanguageServer, params: ExpandTextDocumentPositionParams):
192
192
result = ExpandResult(content: expand[ 0].doc.fixIdentation(character),
193
193
range : expand[0 ].createRangeFromSuggest())
194
194
195
- proc status* (ls: LanguageServer, params: NimLangServerStatusParams): Future[NimLangServerStatus] {.async.} =
195
+ proc status* (ls: LanguageServer, params: NimLangServerStatusParams): Future[NimLangServerStatus] {.async.} =
196
196
debug " Received status request"
197
197
ls.getLspStatus()
198
198
@@ -468,9 +468,9 @@ proc executeCommand*(ls: LanguageServer, params: ExecuteCommandParams):
468
468
ls.progress(token, " end" )
469
469
ls.checkProject(projectFile.pathToUri).traceAsyncErrors
470
470
471
- result = newJNull()
471
+ result = newJNull()
472
472
473
- proc toSignatureInformation(suggest: Suggest): SignatureInformation =
473
+ proc toSignatureInformation(suggest: Suggest): SignatureInformation =
474
474
var fnKind, strParams: string
475
475
var params = newSeq[ParameterInformation]()
476
476
#TODO handle params. Ideally they are handled in the compiler but as fallback we could handle them as follows
@@ -490,16 +490,16 @@ proc toSignatureInformation(suggest: Suggest): SignatureInformation =
490
490
" parameters" : newSeq[ParameterInformation](), #notice params is not used
491
491
}
492
492
493
- proc signatureHelp*(ls: LanguageServer, params: SignatureHelpParams, id: int ):
494
- Future[Option[SignatureHelp]] {.async.} =
493
+ proc signatureHelp*(ls: LanguageServer, params: SignatureHelpParams, id: int ):
494
+ Future[Option[SignatureHelp]] {.async.} =
495
495
#TODO handle prev signature
496
496
# if params.context.activeSignatureHelp.isSome:
497
497
# let prevSignature = params.context.activeSignatureHelp.get.signatures.get[params.context.activeSignatureHelp.get.activeSignature.get]
498
498
# debug "prevSignature ", prevSignature = $prevSignature.label
499
499
# else:
500
500
# debug "no prevSignature"
501
501
#only support signatureHelp if the client supports it
502
- # if docCaps.signatureHelp.isSome and docCaps.signatureHelp.get.contextSupport.get(false):
502
+ # if docCaps.signatureHelp.isSome and docCaps.signatureHelp.get.contextSupport.get(false):
503
503
# result .capabilities.signatureHelpProvider = SignatureHelpOptions(
504
504
# triggerCharacters: some(@["(", ", " ])
505
505
# )
@@ -509,11 +509,11 @@ proc signatureHelp*(ls: LanguageServer, params: SignatureHelpParams, id: int):
509
509
with (params.position, params.textDocument):
510
510
let nimsuggest = await ls.getNimsuggest(uri)
511
511
if nsCon notin nimSuggest.capabilities:
512
- #support signatureHelp only if the current version of NimSuggest supports it.
512
+ #support signatureHelp only if the current version of NimSuggest supports it.
513
513
return none[SignatureHelp]()
514
514
515
515
let completions = await nimsuggest
516
- .con(uriToPath(uri),
516
+ .con(uriToPath(uri),
517
517
ls.uriToStash(uri),
518
518
line + 1,
519
519
ls.getCharacter(uri, line, character))
@@ -525,7 +525,7 @@ proc signatureHelp*(ls: LanguageServer, params: SignatureHelpParams, id: int):
525
525
activeSignature: some(0),
526
526
activeParameter: some(0)
527
527
)
528
- else:
528
+ else:
529
529
return none[SignatureHelp]()
530
530
531
531
proc workspaceSymbol*(ls: LanguageServer, params: WorkspaceSymbolParams, id: int ):
@@ -603,18 +603,21 @@ proc setTrace*(ls: LanguageServer, params: SetTraceParams) {.async.} =
603
603
proc didChange*(ls: LanguageServer, params: DidChangeTextDocumentParams):
604
604
Future[void ] {.async, gcsafe.} =
605
605
with params:
606
- let
607
- uri = textDocument.uri
608
- file = open(ls.uriStorageLocation(uri), fmWrite)
606
+ let
607
+ uri = textDocument.uri
608
+ file = open(ls.uriStorageLocation(uri), fmWrite)
609
609
610
- ls.openFiles[uri].fingerTable = @[]
611
- ls.openFiles[uri].changed = true
612
- for line in contentChanges[ 0].text.splitLines:
613
- ls.openFiles[uri].fingerTable.add line.createUTFMapping()
614
- file.writeLine line
615
- file.close()
610
+ ls.openFiles[uri].fingerTable = @[]
611
+ ls.openFiles[uri].changed = true
612
+ if contentChanges.len <= 0:
613
+ file.close()
614
+ return
615
+ for line in contentChanges[ 0].text.splitLines:
616
+ ls.openFiles[uri].fingerTable.add line.createUTFMapping()
617
+ file.writeLine line
618
+ file.close()
616
619
617
- ls.scheduleFileCheck(uri)
620
+ ls.scheduleFileCheck(uri)
618
621
619
622
proc didSave* (ls: LanguageServer, params: DidSaveTextDocumentParams):
620
623
Future[void ] {.async, gcsafe.} =
@@ -627,7 +630,7 @@ proc didSave*(ls: LanguageServer, params: DidSaveTextDocumentParams):
627
630
if ls.getWorkspaceConfiguration().await().checkOnSave.get(true ):
628
631
debug " Checking project" , uri = uri
629
632
traceAsyncErrors ls.checkProject(uri)
630
-
633
+
631
634
var toStop = newTable[string , Nimsuggest]()
632
635
#We first get the project file for the current file so we can test if this file recently imported another project
633
636
let thisProjectFile = await getProjectFile(uri.uriToPath, ls)
@@ -636,9 +639,9 @@ proc didSave*(ls: LanguageServer, params: DidSaveTextDocumentParams):
636
639
for projectFile in ls.projectFiles.keys:
637
640
if projectFile in ls.entryPoints: continue
638
641
let isKnown = await ns.isKnown(projectFile)
639
- if isKnown:
642
+ if isKnown:
640
643
toStop[projectFile] = await ls.projectFiles[projectFile]
641
-
644
+
642
645
for projectFile, ns in toStop:
643
646
ns.stop()
644
647
ls.projectFiles.del projectFile
@@ -683,13 +686,13 @@ proc didOpen*(ls: LanguageServer, params: DidOpenTextDocumentParams):
683
686
ls.getNimsuggest(uri).addCallback() do (fut: Future[Nimsuggest]) -> void :
684
687
if not fut.failed:
685
688
discard ls.warnIfUnknown(fut.read, uri, projectFile)
686
-
689
+
687
690
let projectFileUri = projectFile.pathToUri
688
691
if projectFileUri notin ls.openFiles:
689
692
var params = params
690
693
params.textDocument.uri = projectFileUri
691
694
await didOpen(ls, params)
692
-
695
+
693
696
debug "Opening project file", uri = projectFile, file = uri
694
697
695
698
proc didChangeConfiguration*(ls: LanguageServer, conf: JsonNode):
0 commit comments