diff options
author | PMunch <peterme@peterme.net> | 2018-11-23 15:45:48 +0100 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2018-11-23 15:45:48 +0100 |
commit | a20169af91b63c6e855ec37ab3faf50e54152bb4 (patch) | |
tree | 837485bc251a2284ce60b003a4a71f2ebd14f62b /nimsuggest | |
parent | 88d707cb88b3c0c93aed5e261beffa3a2e6d2c66 (diff) | |
download | Nim-a20169af91b63c6e855ec37ab3faf50e54152bb4.tar.gz |
Made nimsuggest importable as a library and add Nim-path override option (#9784)
* Made nimsuggest importable as a library and add Nim-path override option * Remove leftover debug output
Diffstat (limited to 'nimsuggest')
-rw-r--r-- | nimsuggest/nimsuggest.nim | 101 |
1 files changed, 100 insertions, 1 deletions
diff --git a/nimsuggest/nimsuggest.nim b/nimsuggest/nimsuggest.nim index d84cb6f17..89b5bf4ed 100644 --- a/nimsuggest/nimsuggest.nim +++ b/nimsuggest/nimsuggest.nim @@ -614,4 +614,103 @@ proc handleCmdLine(cache: IdentCache; conf: ConfigRef) = discard self.loadConfigsAndRunMainCommand(cache, conf) -handleCmdline(newIdentCache(), newConfigRef()) +when isMainModule: + handleCmdline(newIdentCache(), newConfigRef()) +else: + export Suggest + export IdeCmd + export AbsoluteFile + type NimSuggest* = ref object + graph: ModuleGraph + idle: int + cachedMsgs: CachedMsgs + + proc initNimSuggest*(project: string, nimPath: string = ""): NimSuggest = + var retval: ModuleGraph + proc mockCommand(graph: ModuleGraph) = + retval = graph + let conf = graph.config + clearPasses(graph) + registerPass graph, verbosePass + registerPass graph, semPass + conf.cmd = cmdIdeTools + wantMainModule(conf) + + if not fileExists(conf.projectFull): + quit "cannot find file: " & conf.projectFull.string + + add(conf.searchPaths, conf.libpath) + + # do not stop after the first error: + conf.errorMax = high(int) + # do not print errors, but log them + conf.writelnHook = proc (s: string) = log(s) + conf.structuredErrorHook = nil + + # compile the project before showing any input so that we already + # can answer questions right away: + compileProject(graph) + + + proc mockCmdLine(pass: TCmdLinePass, cmd: string; conf: ConfigRef) = + conf.suggestVersion = 0 + let a = unixToNativePath(project) + if dirExists(a) and not fileExists(a.addFileExt("nim")): + conf.projectName = findProjectNimFile(conf, a) + # don't make it worse, report the error the old way: + if conf.projectName.len == 0: conf.projectName = a + else: + conf.projectName = a + # if processArgument(pass, p, argsCount): break + let + cache = newIdentCache() + conf = newConfigRef() + self = NimProg( + suggestMode: true, + processCmdLine: mockCmdLine, + mainCommand: mockCommand + ) + self.initDefinesProg(conf, "nimsuggest") + + self.processCmdLineAndProjectPath(conf) + + if gMode != mstdin: + conf.writelnHook = proc (msg: string) = discard + # Find Nim's prefix dir. + if nimPath == "": + let binaryPath = findExe("nim") + if binaryPath == "": + raise newException(IOError, + "Cannot find Nim standard library: Nim compiler not in PATH") + conf.prefixDir = AbsoluteDir binaryPath.splitPath().head.parentDir() + if not dirExists(conf.prefixDir / RelativeDir"lib"): + conf.prefixDir = AbsoluteDir"" + else: + conf.prefixDir = AbsoluteDir nimPath + + #msgs.writelnHook = proc (line: string) = log(line) + myLog("START " & conf.projectFull.string) + + discard self.loadConfigsAndRunMainCommand(cache, conf) + if gLogging: + for it in conf.searchPaths: + log(it.string) + + retval.doStopCompile = proc (): bool = false + return NimSuggest(graph: retval, idle: 0, cachedMsgs: @[]) + + proc runCmd*(nimsuggest: NimSuggest, cmd: IdeCmd, file, dirtyfile: AbsoluteFile, line, col: int): seq[Suggest] = + var retval: seq[Suggest] = @[] + let conf = nimsuggest.graph.config + conf.ideCmd = cmd + conf.writelnHook = proc (line: string) = + retval.add(Suggest(section: ideMsg, doc: line)) + conf.suggestionResultHook = proc (s: Suggest) = + retval.add(s) + if conf.ideCmd == ideKnown: + retval.add(Suggest(section: ideKnown, quality: ord(fileInfoKnown(conf, file)))) + else: + if conf.ideCmd == ideChk: + for cm in nimsuggest.cachedMsgs: errorHook(conf, cm.info, cm.msg, cm.sev) + execute(conf.ideCmd, file, dirtyfile, line, col, nimsuggest.graph) + return retval |