Skip to content

Commit fb725fe

Browse files
authored
Adds support for nimble dump entryPoints (#212)
* Adds support for nimble dump entryPoints * update * disable tprojectsetup
1 parent 0bdc9ba commit fb725fe

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

nimlangserver.nim

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ type
100100
name: string
101101
nimDir: Option[string]
102102
nimblePath: Option[string]
103+
entryPoints: seq[string] #when it's empty, means the nimble version doesnt dump it.
104+
105+
106+
proc getNimbleEntryPoints(dumpInfo: NimbleDumpInfo, nimbleProjectPath: string): seq[string] =
107+
if dumpInfo.entryPoints.len > 0:
108+
return dumpInfo.entryPoints.mapIt(nimbleProjectPath / it)
109+
#Nimble doesnt include the entry points, returning the nimble project file as the entry point
110+
let sourceDir = nimbleProjectPath / dumpInfo.srcDir
111+
@[sourceDir / (dumpInfo.name & ".nim")]
103112

104113
proc getVersionFromNimble(): string =
105114
#We should static run nimble dump instead
@@ -166,7 +175,9 @@ proc getNimbleDumpInfo(ls: LanguageServer, nimbleFile: string): NimbleDumpInfo =
166175
result.nimDir = some line[(1 + line.find '"')..^2]
167176
if line.startsWith("nimblePath"):
168177
result.nimblePath = some line[(1 + line.find '"')..^2]
169-
178+
if line.startsWith("entryPoints"):
179+
result.entryPoints = line[(1 + line.find '"')..^2].split(',').mapIt(it.strip(chars = {' ', '"'}))
180+
170181
var nimbleFile = nimbleFile
171182
if nimbleFile == "" and result.nimblePath.isSome:
172183
nimbleFile = result.nimblePath.get
@@ -251,13 +262,28 @@ proc getProjectFile(fileUri: string, ls: LanguageServer): Future[string] {.async
251262
else:
252263
trace "getProjectFile does not match", uri = fileUri, matchedRegex = mapping.fileRegex
253264

265+
once: #once we refactor the project to chronos, we may move this code into init. Right now it hangs for some odd reason
266+
let rootPath = ls.initializeParams.getRootPath
267+
if rootPath != "":
268+
let nimbleFiles = walkFiles(rootPath / "*.nimble").toSeq
269+
if nimbleFiles.len > 0:
270+
let nimbleFile = nimbleFiles[0]
271+
let nimbleDumpInfo = ls.getNimbleDumpInfo(nimbleFile)
272+
let entryPoints = nimbleDumpInfo.getNimbleEntryPoints(ls.initializeParams.getRootPath)
273+
for entryPoint in entryPoints:
274+
debug "Starting nimsuggest for entry point ", entry = entryPoint
275+
if not ls.projectFiles.hasKey(entryPoint):
276+
ls.createOrRestartNimsuggest(entryPoint)
277+
# let ns = await ls.projectFiles[entryPoint]
278+
254279
let otherNsProject = await ls.isKnownByAnyNimsuggest(fileUri)
255280
if otherNsProject.isSome:
256281
debug "File is known by nimsuggest", uri = fileUri, projectFile = otherNsProject.get
257282
result = otherNsProject.get
258283
else:
259284
result = ls.getProjectFileAutoGuess(fileUri)
260-
debug "getProjectFile", project = result
285+
286+
debug "getProjectFile", project = result, fileUri = fileUri
261287

262288
proc showMessage(ls: LanguageServer, message: string, typ: MessageType) =
263289
proc notify() =
@@ -387,7 +413,11 @@ proc initialize(p: tuple[ls: LanguageServer, pipeInput: AsyncInputStream], param
387413
result.capabilities.renameProvider = %* {
388414
"prepareProvider": true
389415
}
390-
debug "Initialize completed."
416+
debug "Initialize completed. Trying to start nimsuggest instances"
417+
#If we are in a nimble project here, we try to start the entry points
418+
419+
420+
391421

392422
proc requiresDynamicRegistrationForDidChangeConfiguration(ls: LanguageServer): bool =
393423
ls.clientCapabilities.workspace.isSome and
@@ -807,6 +837,8 @@ proc warnIfUnknown(ls: LanguageServer, ns: Nimsuggest, uri: string, projectFile:
807837

808838
proc didOpen(ls: LanguageServer, params: DidOpenTextDocumentParams):
809839
Future[void] {.async, gcsafe.} =
840+
841+
810842
with params.textDocument:
811843
debug "New document opened for URI:", uri = uri
812844
let
@@ -1027,13 +1059,6 @@ proc status(ls: LanguageServer, params: NimLangServerStatusParams): Future[NimLa
10271059
path: ns.nimsuggestPath,
10281060
port: ns.port,
10291061
)
1030-
for openFile in ns.openFiles:
1031-
let openFilePath = openFile.uriToPath
1032-
let isKnown = await ns.isKnown(openFilePath)
1033-
nsStatus.openFiles.add openFilePath
1034-
if not isKnown:
1035-
nsStatus.unknownFiles.add openFilePath
1036-
10371062
status.nimsuggestInstances.add nsStatus
10381063

10391064
for openFile in ls.openFiles.keys:

suggestapi.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ proc createNimsuggest*(root: string,
364364
if input.readable:
365365
let line = await input.readLine
366366
if line == failedToken:
367-
result.markFailed "Nimsuggest process crashed."
367+
result.markFailed &"Nimsuggest process crashed. Nimsuggest path {nimsuggestPath}"
368368
else:
369369
result.port = line.parseInt
370370
debug "Started nimsuggest", port = result.port, root = root
@@ -501,5 +501,7 @@ proc `mod`*(nimsuggest: Nimsuggest, file: string, dirtyfile = ""): Future[seq[Su
501501

502502
proc isKnown*(nimsuggest: Nimsuggest, filePath: string): Future[bool] {.async.} =
503503
let sug = await nimsuggest.known(filePath)
504+
if sug.len == 0:
505+
return false
504506
debug "isKnown", filePath = filePath, sug = sug[0].forth
505507
return sug.len > 0 and sug[0].forth == "true"

tests/all.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import
2-
tnimlangserver, tsuggestapi,
3-
tprojectsetup
2+
tnimlangserver, tsuggestapi
3+
# tprojectsetup

0 commit comments

Comments
 (0)