Skip to content

Commit

Permalink
Nimsuggest version detection (#60)
Browse files Browse the repository at this point in the history
* Added nimsuggest version detection via the new --info:protocolVer command line option

* Check protocol version and only issue the inlayHints command to nimsuggest if it uses protocol version 4 or later.
  • Loading branch information
nickysn authored Nov 19, 2023
1 parent 611c656 commit c28a4b5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions nimlangserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,9 @@ proc inlayHint(ls: LanguageServer, params: InlayHintParams, id: int): Future[seq
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),
Expand Down
25 changes: 24 additions & 1 deletion suggestapi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import osproc,
chronicles

const REQUEST_TIMEOUT* = 120000
const HighestSupportedNimSuggestProtocolVersion = 4

# coppied from Nim repo
type
Expand Down Expand Up @@ -87,6 +88,7 @@ type
processing: bool
timeout: int
timeoutCallback: NimsuggestCallback
protocolVersion*: int

template benchmark(benchmarkName: string, code: untyped) =
block:
Expand Down Expand Up @@ -255,6 +257,24 @@ proc doWithTimeout*[T](fut: Future[T], timeout: int, s: string): owned(Future[bo

return retFuture

proc detectNimsuggestVersion(root: string,
nimsuggestPath: string,
workingDir: string): int {.gcsafe.} =
var process = startProcess(command = nimsuggestPath,
workingDir = workingDir,
args = @[root, "--info:protocolVer"],
options = {poUsePath})
var l: string
if not process.outputStream.readLine(l):
l = ""
var exitCode = process.waitForExit()
if exitCode != 0 or l == "":
# older versions of NimSuggest don't support the --info:protocolVer option
# use protocol version 3 with them
return 3
else:
return parseInt(l)

proc createNimsuggest*(root: string,
nimsuggestPath: string,
timeout: int,
Expand All @@ -279,9 +299,12 @@ proc createNimsuggest*(root: string,
result.errorCallback = errorCallback

if fullPath != "":
result.protocolVersion = detectNimsuggestVersion(root, nimsuggestPath, workingDir)
if result.protocolVersion > HighestSupportedNimSuggestProtocolVersion:
result.protocolVersion = HighestSupportedNimSuggestProtocolVersion
result.process = startProcess(command = nimsuggestPath,
workingDir = workingDir,
args = @[root, "--v3", "--autobind"],
args = @[root, "--v" & $result.protocolVersion, "--autobind"],
options = {poUsePath})

# all this is needed to avoid the need to block on the main thread.
Expand Down

0 comments on commit c28a4b5

Please sign in to comment.