Skip to content

Commit 0f4463c

Browse files
authored
Gets the nimsuggest path from nimble when nimble has nimdir (#200)
* Gets the nimsuggest path from `nimble` when `nimble` has `nimdir` This depends on nim-lang/nimble#1221 It also needs to be tested in Win. While developing this found an issue with the project discovery, it uses `nim dump` but it doesnt seem to end when Nim is a local dep, it takes too long. Will try to speed it up in the nimble side of things * Win works * Adds Nim version to the notification so the user knows what version is used * uses nimble dump for nimdir
1 parent 97ffd6d commit 0f4463c

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

nimlangserver.nim

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,12 +665,41 @@ proc getWorkingDir(ls: LanguageServer, path: string): Future[string] {.async.} =
665665
result = rootPath.string / m.directory
666666
break;
667667

668+
proc getNimVersion(nimDir: string): string =
669+
let cmd =
670+
if nimDir == "": "nim --version"
671+
else: nimDir / "nim --version"
672+
let info = execProcess(cmd)
673+
const NimCompilerVersion = "Nim Compiler Version "
674+
for line in info.splitLines:
675+
if line.startsWith(NimCompilerVersion):
676+
return line
677+
678+
proc getNimSuggestPath(ls: LanguageServer, conf: NlsConfig, workingDir: string): string =
679+
#Attempting to see if the project is using a custom Nim version, if it's the case this will be slower than usual
680+
let info: string = execProcess("nimble dump ", workingDir)
681+
var nimDir = ""
682+
const NimDirSplit = "nimDir:"
683+
for line in info.splitLines:
684+
if line.startsWith(NimDirSplit):
685+
nimDir = line.split(NimDirSplit)[1].strip.strip(chars = {'"', ' '})
686+
687+
result = expandTilde(conf.nimsuggestPath.get(""))
688+
var nimVersion = ""
689+
if result == "":
690+
if nimDir != "" and nimDir.dirExists:
691+
nimVersion = getNimVersion(nimDir) & " from " & nimDir
692+
result = nimDir / "nimsuggest"
693+
else:
694+
nimVersion = getNimVersion("")
695+
result = findExe "nimsuggest"
696+
ls.showMessage(fmt "Using {nimVersion}", MessageType.Info)
668697

669698
proc createOrRestartNimsuggest(ls: LanguageServer, projectFile: string, uri = ""): void {.gcsafe.} =
670699
let
671700
configuration = ls.getWorkspaceConfiguration().waitFor()
672-
nimsuggestPath = expandTilde(configuration.nimsuggestPath.get("nimsuggest"))
673701
workingDir = ls.getWorkingDir(projectFile).waitFor()
702+
nimsuggestPath = ls.getNimSuggestPath(configuration, workingDir)
674703
timeout = configuration.timeout.get(REQUEST_TIMEOUT)
675704
restartCallback = proc (ns: Nimsuggest) {.gcsafe.} =
676705
warn "Restarting the server due to requests being to slow", projectFile = projectFile

suggestapi.nim

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,9 @@ proc createNimsuggest*(root: string,
320320
thread: Thread[tuple[pipe: AsyncPipe, process: Process]]
321321
stderrThread: Thread[tuple[root: string, process: Process]]
322322
input = pipe.asyncPipeInput
323-
fullPath = findExe(nimsuggestPath)
324323

325-
info "Starting nimsuggest", root = root, timeout = timeout, path = nimsuggestPath,
326-
fullPath = fullPath, workingDir = workingDir
324+
info "Starting nimsuggest", root = root, timeout = timeout, path = nimsuggestPath,
325+
workingDir = workingDir
327326

328327
result = Nimsuggest()
329328
result.requestQueue = Deque[SuggestCall]()
@@ -332,7 +331,7 @@ proc createNimsuggest*(root: string,
332331
result.timeoutCallback = timeoutCallback
333332
result.errorCallback = errorCallback
334333
335-
if fullPath != "":
334+
if nimsuggestPath != "":
336335
result.protocolVersion = detectNimsuggestVersion(root, nimsuggestPath, workingDir)
337336
if result.protocolVersion > HighestSupportedNimSuggestProtocolVersion:
338337
result.protocolVersion = HighestSupportedNimSuggestProtocolVersion

0 commit comments

Comments
 (0)