Skip to content

Commit 7ee193e

Browse files
authored
refactor: extracts nimble dump info type (#201)
1 parent 0f4463c commit 7ee193e

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

nimlangserver.nim

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ type
9393
Folder,
9494
Cfg,
9595
Nimble
96+
97+
NimbleDumpInfo = object
98+
srcDir: string
99+
name: string
100+
nimDir: Option[string]
101+
nimblePath: Option[string]
96102

97103
createJsonFlavor(LSPFlavour, omitOptionalFields = true)
98104
Option.useDefaultSerializationIn LSPFlavour
@@ -136,7 +142,19 @@ proc supportSignatureHelp(cc: ClientCapabilities): bool =
136142
let caps = cc.textDocument
137143
caps.isSome and caps.get.signatureHelp.isSome
138144
139-
proc getProjectFileAutoGuess(fileUri: string): string =
145+
proc getNimbleDumpInfo(ls: LanguageServer, nimbleFile: string): NimbleDumpInfo =
146+
let info = execProcess("nimble dump " & nimbleFile)
147+
for line in info.splitLines:
148+
if line.startsWith("srcDir"):
149+
result.srcDir = line[(1 + line.find '"')..^2]
150+
if line.startsWith("name"):
151+
result.name = line[(1 + line.find '"')..^2]
152+
if line.startsWith("nimDir"):
153+
result.nimDir = some line[(1 + line.find '"')..^2]
154+
if line.startsWith("nimblePath"):
155+
result.nimblePath = some line[(1 + line.find '"')..^2]
156+
157+
proc getProjectFileAutoGuess(ls: LanguageServer, fileUri: string): string =
140158
let file = fileUri.decodeUrl
141159
result = file
142160
let (dir, _, _) = result.splitFile()
@@ -157,13 +175,9 @@ proc getProjectFileAutoGuess(fileUri: string): string =
157175
certainty = Cfg
158176
if certainty <= Nimble:
159177
for nimble in walkFiles(path / "*.nimble"):
160-
let info = execProcess("nimble dump " & nimble)
161-
var sourceDir, name: string
162-
for line in info.splitLines:
163-
if line.startsWith("srcDir"):
164-
sourceDir = path / line[(1 + line.find '"')..^2]
165-
if line.startsWith("name"):
166-
name = line[(1 + line.find '"')..^2]
178+
let dumpInfo = ls.getNimbleDumpInfo(nimble)
179+
let name = dumpInfo.name
180+
let sourceDir = path / dumpInfo.srcDir
167181
let projectFile = sourceDir / (name & ".nim")
168182
if sourceDir.len != 0 and name.len != 0 and
169183
file.isRelativeTo(sourceDir) and fileExists(projectFile):
@@ -206,7 +220,7 @@ proc getProjectFile(fileUri: string, ls: LanguageServer): Future[string] {.async
206220
else:
207221
trace "getProjectFile does not match", uri = fileUri, matchedRegex = mapping.fileRegex
208222

209-
result = getProjectFileAutoGuess(fileUri)
223+
result = ls.getProjectFileAutoGuess(fileUri)
210224
debug "getProjectFile", project = result
211225

212226
proc showMessage(ls: LanguageServer, message: string, typ: MessageType) =
@@ -677,12 +691,8 @@ proc getNimVersion(nimDir: string): string =
677691

678692
proc getNimSuggestPath(ls: LanguageServer, conf: NlsConfig, workingDir: string): string =
679693
#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 = {'"', ' '})
694+
let nimbleDumpInfo = ls.getNimbleDumpInfo("")
695+
let nimDir = nimbleDumpInfo.nimDir.get ""
686696

687697
result = expandTilde(conf.nimsuggestPath.get(""))
688698
var nimVersion = ""

0 commit comments

Comments
 (0)