Skip to content

Commit

Permalink
nimsuggest: fix nimsuggest#96 ; nimsuggest can handle files not yet k…
Browse files Browse the repository at this point in the history
…nown; also add refresh option
  • Loading branch information
timotheecour committed Nov 14, 2018
1 parent 6e8ed8c commit 611dc5f
Showing 1 changed file with 52 additions and 26 deletions.
78 changes: 52 additions & 26 deletions nimsuggest/nimsuggest.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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:
Expand Down Expand Up @@ -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() =
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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]

Expand Down

0 comments on commit 611dc5f

Please sign in to comment.