diff --git a/nimsuggest/nimsuggest.nim b/nimsuggest/nimsuggest.nim index 34b1cc4f7ec22..f20c961d41e05 100644 --- a/nimsuggest/nimsuggest.nim +++ b/nimsuggest/nimsuggest.nim @@ -61,6 +61,13 @@ type sev: Severity CachedMsgs = seq[CachedMsg] +type SuggestData = object + graph: ModuleGraph + cachedMsgs: CachedMsgs + +proc newSuggestData(graph: ModuleGraph): SuggestData = + result = SuggestData(graph: graph) + var gPort = 6000.Port gAddress = "" @@ -88,10 +95,13 @@ proc myLog(s: string) = const seps = {':', ';', ' ', '\t'} - Help = "usage: sug|con|def|use|dus|chk|mod|highlight|outline|known file.nim[;dirtyfile.nim]:line:col\n" & - "type 'quit' to quit\n" & - "type 'debug' to toggle debug mode on/off\n" & - "type 'terse' to toggle terse mode on/off" + Help = """ +usage: sug|con|def|use|dus|chk|mod|highlight|outline|known file.nim[;dirtyfile.nim]:line:col +type 'help' for this help +type 'quit' to quit +type 'refresh' to recompile main module +type 'debug' to toggle debug mode on/off +type 'terse' to toggle terse mode on/off""" type EUnexpectedCommand = object of Exception @@ -186,7 +196,7 @@ proc execute(cmd: IdeCmd, file, dirtyfile: AbsoluteFile, line, col: int; if conf.suggestVersion == 1: graph.usageSym = nil if not isKnownFile: - graph.compileProject() + graph.compileProject(dirtyIdx) if conf.suggestVersion == 0 and conf.ideCmd in {ideUse, ideDus} and dirtyfile.isEmpty: discard "no need to recompile anything" @@ -195,7 +205,8 @@ proc execute(cmd: IdeCmd, file, dirtyfile: AbsoluteFile, line, col: int; graph.markDirty dirtyIdx graph.markClientsDirty dirtyIdx if conf.ideCmd != ideMod: - graph.compileProject(modIdx) + if isKnownFile: + graph.compileProject(modIdx) if conf.ideCmd in {ideUse, ideDus}: let u = if conf.suggestVersion != 1: graph.symFromInfo(conf.m.trackPos) else: graph.usageSym if u != nil: @@ -381,7 +392,31 @@ proc replEpc(x: ThreadParams) {.thread.} = "unexpected call: " & epcAPI quit errMessage -proc execCmd(cmd: string; graph: ModuleGraph; cachedMsgs: CachedMsgs) = +proc recompileFullProject(graph: ModuleGraph) = + #echo "recompiling full project" + resetSystemArtifacts(graph) + graph.vm = nil + graph.resetAllModules() + GC_fullcollect() + compileProject(graph) + #echo GC_getStatistics() + +proc doRefresh(suggestData: SuggestData) = + var suggestData = suggestData + var cachedMsgs = suggestData.cachedMsgs.addr + let graph = suggestData.graph + let conf = graph.config + conf.ideCmd = ideChk + conf.writelnHook = proc (s: string) = echo s + suggestData.cachedMsgs.setLen 0 + conf.structuredErrorHook = proc (conf: ConfigRef; info: TLineInfo; msg: string; sev: Severity) = + cachedMsgs[].add(CachedMsg(info: info, msg: msg, sev: sev)) + conf.suggestionResultHook = proc (s: Suggest) = discard + recompileFullProject(graph) + +proc execCmd(cmd: string; suggestData: SuggestData) = + let graph = suggestData.graph + let cachedMsgs = suggestData.cachedMsgs let conf = graph.config template sentinel() = @@ -413,6 +448,10 @@ proc execCmd(cmd: string; graph: ModuleGraph; cachedMsgs: CachedMsgs) = of "chk": conf.ideCmd = ideChk of "highlight": conf.ideCmd = ideHighlight of "outline": conf.ideCmd = ideOutline + of "refresh": + doRefresh(suggestData) + sentinel() + return of "quit": sentinel() quit() @@ -440,15 +479,6 @@ proc execCmd(cmd: string; graph: ModuleGraph; cachedMsgs: CachedMsgs) = execute(conf.ideCmd, AbsoluteFile orig, AbsoluteFile dirtyfile, line, col, graph) sentinel() -proc recompileFullProject(graph: ModuleGraph) = - #echo "recompiling full project" - resetSystemArtifacts(graph) - graph.vm = nil - graph.resetAllModules() - GC_fullcollect() - compileProject(graph) - #echo GC_getStatistics() - proc mainThread(graph: ModuleGraph) = let conf = graph.config if gLogging: @@ -465,27 +495,23 @@ proc mainThread(graph: ModuleGraph) = conf.suggestionResultHook = sugResultHook graph.doStopCompile = proc (): bool = requests.peek() > 0 var idle = 0 - var cachedMsgs: CachedMsgs = @[] + + var suggestData = newSuggestData(graph) + while true: let (hasData, req) = requests.tryRecv() if hasData: conf.writelnHook = wrHook conf.suggestionResultHook = sugResultHook - execCmd(req, graph, cachedMsgs) + execCmd(req, suggestData) idle = 0 else: + # TODO: use async (eg via asyncnet) to avoid delay os.sleep 250 idle += 1 if idle == 20 and gRefresh: # we use some nimsuggest activity to enable a lazy recompile: - conf.ideCmd = ideChk - conf.writelnHook = proc (s: string) = discard - cachedMsgs.setLen 0 - conf.structuredErrorHook = proc (conf: ConfigRef; info: TLineInfo; msg: string; sev: Severity) = - cachedMsgs.add(CachedMsg(info: info, msg: msg, sev: sev)) - conf.suggestionResultHook = proc (s: Suggest) = discard - recompileFullProject(graph) - + doRefresh(suggestData) var inputThread: Thread[ThreadParams]