-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixes bug with textDocument/definition on LanguageClient-neovim #13
Changes from all commits
26fe6d8
ae0d778
de49fd4
41b3428
2a649f8
358f4be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,15 +57,15 @@ template textDocumentRequest(message, kind, name, body) {.dirty.} = | |
filestash = storage / (hash(fileuri).toHex & ".nim" ) | ||
debugEcho "Got request for URI: ", fileuri, " copied to " & filestash | ||
let | ||
rawLine = name["position"]["line"].getInt | ||
rawChar = name["position"]["character"].getInt | ||
rawLine = max(name["position"]["line"].getInt, 0) | ||
rawChar = max(name["position"]["character"].getInt, 0) | ||
body | ||
|
||
template textDocumentNotification(message, kind, name, body) {.dirty.} = | ||
if message["params"].isSome: | ||
let name = message["params"].unsafeGet | ||
whenValid(name, kind): | ||
if name["textDocument"]["languageId"].getStr == "nim": | ||
if not name["textDocument"].hasKey("languageId") or name["textDocument"]["languageId"].getStr == "nim": | ||
let | ||
fileuri = name["textDocument"]["uri"].getStr | ||
filestash = storage / (hash(fileuri).toHex & ".nim" ) | ||
|
@@ -77,6 +77,9 @@ proc respond(request: RequestMessage, data: JsonNode) = | |
proc error(request: RequestMessage, errorCode: int, message: string, data: JsonNode) = | ||
outs.sendJson create(ResponseMessage, "2.0", request["id"].getInt, none(JsonNode), some(create(ResponseError, errorCode, message, data))).JsonNode | ||
|
||
proc notify(notification: string, data: JsonNode) = | ||
outs.sendJson create(NotificationMessage, "2.0", notification, some(data)).JsonNode | ||
|
||
type Certainty = enum | ||
None, | ||
Folder, | ||
|
@@ -106,10 +109,14 @@ proc getProjectFile(file: string): string = | |
discard | ||
path = dir | ||
|
||
template getNimsuggest(fileuri: string): Nimsuggest = | ||
projectFiles[openFiles[fileuri].projectFile].nimsuggest | ||
|
||
while true: | ||
try: | ||
debugEcho "Trying to read frame" | ||
let frame = ins.readFrame | ||
debugEcho frame | ||
debugEcho "Got frame:\n" & frame | ||
let message = frame.parseJson | ||
whenValid(message, RequestMessage): | ||
debugEcho "Got valid Request message of type " & message["method"].getStr | ||
|
@@ -130,7 +137,7 @@ while true: | |
change = some(TextDocumentSyncKind.Full.int), | ||
willSave = some(false), | ||
willSaveWaitUntil = some(false), | ||
save = none(SaveOptions) | ||
save = some(create(SaveOptions, some(true))) | ||
)), # ?: TextDocumentSyncOptions or int or float | ||
hoverProvider = some(true), # ?: bool | ||
completionProvider = some(create(CompletionOptions, | ||
|
@@ -162,7 +169,7 @@ while true: | |
)).JsonNode) | ||
of "textDocument/completion": | ||
message.textDocumentRequest(CompletionParams, compRequest): | ||
let suggestions = projectFiles[openFiles[fileuri].projectFile].nimsuggest.sug(fileuri[7..^1], dirtyfile = filestash, | ||
let suggestions = getNimsuggest(fileuri).sug(fileuri[7..^1], dirtyfile = filestash, | ||
rawLine + 1, | ||
openFiles[fileuri].fingerTable[rawLine].utf16to8(rawChar) | ||
) | ||
|
@@ -191,7 +198,7 @@ while true: | |
message.respond completionItems | ||
of "textDocument/hover": | ||
message.textDocumentRequest(TextDocumentPositionParams, hoverRequest): | ||
let suggestions = projectFiles[openFiles[fileuri].projectFile].nimsuggest.def(fileuri[7..^1], dirtyfile = filestash, | ||
let suggestions = getNimsuggest(fileuri).def(fileuri[7..^1], dirtyfile = filestash, | ||
rawLine + 1, | ||
openFiles[fileuri].fingerTable[rawLine].utf16to8(rawChar) | ||
) | ||
|
@@ -223,13 +230,13 @@ while true: | |
message.respond create(Hover, markedString, rangeopt).JsonNode | ||
of "textDocument/references": | ||
message.textDocumentRequest(ReferenceParams, referenceRequest): | ||
let suggestions = projectFiles[openFiles[fileuri].projectFile].nimsuggest.use(fileuri[7..^1], dirtyfile = filestash, | ||
let suggestions = getNimsuggest(fileuri).use(fileuri[7..^1], dirtyfile = filestash, | ||
rawLine + 1, | ||
openFiles[fileuri].fingerTable[rawLine].utf16to8(rawChar) | ||
) | ||
let declarations: seq[Suggestion] = | ||
if referenceRequest["context"]["includeDeclaration"].getBool: | ||
projectFiles[openFiles[fileuri].projectFile].nimsuggest.def(fileuri[7..^1], dirtyfile = filestash, | ||
getNimsuggest(fileuri).def(fileuri[7..^1], dirtyfile = filestash, | ||
rawLine + 1, | ||
openFiles[fileuri].fingerTable[rawLine].utf16to8(rawChar) | ||
) | ||
|
@@ -260,11 +267,11 @@ while true: | |
message.respond response | ||
of "textDocument/definition": | ||
message.textDocumentRequest(TextDocumentPositionParams, definitionRequest): | ||
let suggestions = projectFiles[openFiles[fileuri].projectFile].nimsuggest.def(fileuri[7..^1], dirtyfile = filestash, | ||
let suggestions = getNimsuggest(fileuri).def(fileuri[7..^1], dirtyfile = filestash, | ||
rawLine + 1, | ||
openFiles[fileuri].fingerTable[rawLine].utf16to8(rawChar) | ||
) | ||
let declarations = projectFiles[openFiles[fileuri].projectFile].nimsuggest.def(fileuri[7..^1], dirtyfile = filestash, | ||
let declarations = getNimsuggest(fileuri).def(fileuri[7..^1], dirtyfile = filestash, | ||
rawLine + 1, | ||
openFiles[fileuri].fingerTable[rawLine].utf16to8(rawChar) | ||
) | ||
|
@@ -296,7 +303,7 @@ while true: | |
# let | ||
# rawLine = signRequest["position"]["line"].getInt | ||
# rawChar = signRequest["position"]["character"].getInt | ||
# suggestions = projectFiles[openFiles[fileuri].projectFile].nimsuggest.con(fileuri[7..^1], dirtyfile = filestash, rawLine + 1, rawChar) | ||
# suggestions = getNimsuggest(fileuri).con(fileuri[7..^1], dirtyfile = filestash, rawLine + 1, rawChar) | ||
|
||
else: | ||
debugEcho "Unknown request" | ||
|
@@ -350,11 +357,60 @@ while true: | |
projectFiles[projectFile].openFiles -= 1 | ||
if projectFiles[projectFile].openFiles == 0: | ||
debugEcho "Trying to stop nimsuggest" | ||
debugEcho "Stopped nimsuggest with code: " & $projectFiles[openFiles[fileuri].projectFile].nimsuggest.stopNimsuggest() | ||
debugEcho "Stopped nimsuggest with code: " & $getNimsuggest(fileuri).stopNimsuggest() | ||
openFiles.del(fileuri) | ||
of "textDocument/didSave": | ||
message.textDocumentNotification(DidSaveTextDocumentParams, textDoc): | ||
if textDoc["text"].isSome: | ||
let file = open(filestash, fmWrite) | ||
debugEcho "Got document change for URI: ", fileuri, " saving to ", filestash | ||
openFiles[fileuri].fingerTable = @[] | ||
for line in textDoc["text"].unsafeGet.getStr.splitLines: | ||
openFiles[fileuri].fingerTable.add line.createUTFMapping() | ||
file.writeLine line | ||
file.close() | ||
debugEcho "fileuri: ", fileuri, ", project file: ", openFiles[fileuri].projectFile, ", dirtyfile: ", filestash | ||
let diagnostics = getNimsuggest(fileuri).chk(fileuri[7..^1], dirtyfile = filestash) | ||
debugEcho "Found suggestions: ", | ||
diagnostics[0..(if diagnostics.len > 10: 10 else: diagnostics.high)], | ||
(if diagnostics.len > 10: " and " & $(diagnostics.len-10) & " more" else: "") | ||
if diagnostics.len == 0: | ||
notify("textDocument/publishDiagnostics", create(PublishDiagnosticsParams, | ||
fileuri, | ||
@[]).JsonNode | ||
) | ||
else: | ||
var response: seq[Diagnostic] | ||
for diagnostic in diagnostics: | ||
if diagnostic.line == 0: | ||
continue | ||
# Try to guess the size of the identifier | ||
let | ||
message = diagnostic.nimDocstring | ||
endcolumn = max(diagnostic.column + message.rfind('\'') - message.find('\'') - 1, 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't really do much as it got max'ed with |
||
diagnosticLine = max(diagnostic.line-1, 0) | ||
diagnosticColumn = max(diagnostic.column, endcolumn) | ||
response.add create(Diagnostic, | ||
create(Range, | ||
create(Position, diagnosticLine, diagnosticColumn), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should always be |
||
create(Position, diagnosticLine, diagnosticColumn) | ||
), | ||
some(case diagnostic.qualifiedPath: | ||
of "Error": DiagnosticSeverity.Error.int | ||
of "Hint": DiagnosticSeverity.Hint.int | ||
of "Warning": DiagnosticSeverity.Warning.int | ||
else: DiagnosticSeverity.Error.int), | ||
none(int), | ||
some("nimsuggest chk"), | ||
message, | ||
none(seq[DiagnosticRelatedInformation]) | ||
) | ||
notify("textDocument/publishDiagnostics", create(PublishDiagnosticsParams, | ||
fileuri, | ||
response).JsonNode | ||
) | ||
else: | ||
debugEcho "Got unknown notification message" | ||
continue | ||
except IOError: | ||
debugEcho "Got IOError: " & getCurrentExceptionMsg() | ||
break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change means that
rawLine
andrawChar
aren't reallyraw
any longer so it should be renamed to something else.