diff options
-rw-r--r-- | compiler/lexer.nim | 3 | ||||
-rw-r--r-- | compiler/sem.nim | 4 | ||||
-rw-r--r-- | compiler/semexprs.nim | 6 | ||||
-rw-r--r-- | compiler/suggest.nim | 28 | ||||
-rw-r--r-- | nimsuggest/nimsuggest.nim | 33 | ||||
-rw-r--r-- | nimsuggest/nimsuggest.nim.cfg | 2 |
6 files changed, 51 insertions, 25 deletions
diff --git a/compiler/lexer.nim b/compiler/lexer.nim index afdf17baa..04419d92f 100644 --- a/compiler/lexer.nim +++ b/compiler/lexer.nim @@ -213,7 +213,8 @@ proc openLexer*(lex: var TLexer, fileIdx: int32, inputstream: PLLStream; lex.currLineIndent = 0 inc(lex.lineNumber, inputstream.lineOffset) lex.cache = cache - lex.previousToken.fileIndex = fileIdx + when defined(nimsuggest): + lex.previousToken.fileIndex = fileIdx proc openLexer*(lex: var TLexer, filename: string, inputstream: PLLStream; cache: IdentCache) = diff --git a/compiler/sem.nim b/compiler/sem.nim index 21a5c435a..2ad506b41 100644 --- a/compiler/sem.nim +++ b/compiler/sem.nim @@ -167,6 +167,8 @@ proc commonType*(x, y: PType): PType = proc newSymS(kind: TSymKind, n: PNode, c: PContext): PSym = result = newSym(kind, considerQuotedIdent(n), getCurrOwner(c), n.info) + when defined(nimsuggest): + suggestDecl(c, n, result) proc newSymG*(kind: TSymKind, n: PNode, c: PContext): PSym = proc `$`(kind: TSymKind): string = substr(system.`$`(kind), 2).toLowerAscii @@ -191,6 +193,8 @@ proc newSymG*(kind: TSymKind, n: PNode, c: PContext): PSym = result = newSym(kind, considerQuotedIdent(n), getCurrOwner(c), n.info) #if kind in {skForVar, skLet, skVar} and result.owner.kind == skModule: # incl(result.flags, sfGlobal) + when defined(nimsuggest): + suggestDecl(c, n, result) proc semIdentVis(c: PContext, kind: TSymKind, n: PNode, allowed: TSymFlags): PSym diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index a419cd000..3dc174527 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -1054,7 +1054,9 @@ proc builtinFieldAccess(c: PContext, n: PNode, flags: TExprFlags): PNode = # here at all! #if isSymChoice(n.sons[1]): return when defined(nimsuggest): - if gCmd == cmdIdeTools: suggestExpr(c, n) + if gCmd == cmdIdeTools: + suggestExpr(c, n) + if exactEquals(gTrackPos, n[1].info): suggestExprNoCheck(c, n) var s = qualifiedLookUp(c, n, {checkAmbiguity, checkUndeclared, checkModule}) if s != nil: @@ -2234,6 +2236,8 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode = of nkCall, nkInfix, nkPrefix, nkPostfix, nkCommand, nkCallStrLit: # check if it is an expression macro: checkMinSonsLen(n, 1) + #when defined(nimsuggest): + # if gIdeCmd == ideCon and gTrackPos == n.info: suggestExprNoCheck(c, n) let mode = if nfDotField in n.flags: {} else: {checkUndeclared} var s = qualifiedLookUp(c, n.sons[0], mode) if s != nil: diff --git a/compiler/suggest.nim b/compiler/suggest.nim index c780f8084..e5a9f424a 100644 --- a/compiler/suggest.nim +++ b/compiler/suggest.nim @@ -181,7 +181,7 @@ proc `$`*(suggest: Suggest): string = result.add(sep) when not defined(noDocgen): result.add(suggest.doc.escape) - if suggestVersion == 2: + if suggestVersion == 0: result.add(sep) result.add($suggest.quality) if suggest.section == ideSug: @@ -202,6 +202,10 @@ proc suggestResult(s: Suggest) = proc produceOutput(a: var Suggestions) = if gIdeCmd in {ideSug, ideCon}: a.sort cmpSuggestions + when false: + # debug code + writeStackTrace() + if a.len > 10: a.setLen(10) if not isNil(suggestionResultHook): for s in a: suggestionResultHook(s) @@ -332,7 +336,7 @@ proc suggestEverything(c: PContext, n, f: PNode, outputs: var Suggestions) = var pm: PrefixMatch if filterSym(it, f, pm): outputs.add(symToSuggest(it, isLocal = isLocal, $ideSug, 0, pm, c.inTypeContext > 0, scopeN)) - if scope == c.topLevelScope and f.isNil: break + #if scope == c.topLevelScope and f.isNil: break proc suggestFieldAccess(c: PContext, n, field: PNode, outputs: var Suggestions) = # special code that deals with ``myObj.``. `n` is NOT the nkDotExpr-node, but @@ -340,7 +344,7 @@ proc suggestFieldAccess(c: PContext, n, field: PNode, outputs: var Suggestions) var typ = n.typ var pm: PrefixMatch when defined(nimsuggest): - if n.kind == nkSym and n.sym.kind == skError and suggestVersion == 2: + if n.kind == nkSym and n.sym.kind == skError and suggestVersion == 0: # consider 'foo.|' where 'foo' is some not imported module. let fullPath = findModule(n.sym.name.s, n.info.toFullPath) if fullPath.len == 0: @@ -429,7 +433,7 @@ var lastLineInfo*: TLineInfo proc findUsages(info: TLineInfo; s: PSym; usageSym: var PSym) = - if suggestVersion < 2: + if suggestVersion == 1: if usageSym == nil and isTracked(info, s.name.s.len): usageSym = s suggestResult(symToSuggest(s, isLocal=false, $ideUse, 100, PrefixMatch.None, false, 0)) @@ -460,7 +464,7 @@ proc ensureSeq[T](x: var seq[T]) = proc suggestSym*(info: TLineInfo; s: PSym; usageSym: var PSym; isDecl=true) {.inline.} = ## misnamed: should be 'symDeclared' when defined(nimsuggest): - if suggestVersion == 2: + if suggestVersion == 0: if s.allUsages.isNil: s.allUsages = @[info] else: @@ -509,7 +513,6 @@ proc sugExpr(c: PContext, n: PNode, outputs: var Suggestions) = # line as the object to prevent this from happening: let prefix = if n.len == 2 and n[1].info.line == n[0].info.line and not gTrackPosAttached: n[1] else: nil - echo n[1].kind suggestFieldAccess(c, obj, prefix, outputs) #if optIdeDebug in gGlobalOptions: @@ -519,8 +522,7 @@ proc sugExpr(c: PContext, n: PNode, outputs: var Suggestions) = let prefix = if gTrackPosAttached: nil else: n suggestEverything(c, n, prefix, outputs) -proc suggestExpr*(c: PContext, n: PNode) = - if not exactEquals(gTrackPos, n.info): return +proc suggestExprNoCheck*(c: PContext, n: PNode) = # This keeps semExpr() from coming here recursively: if c.compilesContextId > 0: return inc(c.compilesContextId) @@ -545,6 +547,16 @@ proc suggestExpr*(c: PContext, n: PNode) = produceOutput(outputs) suggestQuit() +proc suggestExpr*(c: PContext, n: PNode) = + if exactEquals(gTrackPos, n.info): suggestExprNoCheck(c, n) + +proc suggestDecl*(c: PContext, n: PNode; s: PSym) = + let attached = gTrackPosAttached + if attached: inc(c.inTypeContext) + defer: + if attached: dec(c.inTypeContext) + suggestExpr(c, n) + proc suggestStmt*(c: PContext, n: PNode) = suggestExpr(c, n) diff --git a/nimsuggest/nimsuggest.nim b/nimsuggest/nimsuggest.nim index ee1647fbf..9a1da0e51 100644 --- a/nimsuggest/nimsuggest.nim +++ b/nimsuggest/nimsuggest.nim @@ -38,10 +38,9 @@ Options: --epc use emacs epc mode --debug enable debug output --log enable verbose logging to nimsuggest.log file - --v2 use version 2 of the protocol; more features and - much faster + --v1 use version 1 of the protocol; for backwards compatibility --refresh perform automatic refreshes to keep the analysis precise - --tester implies --v2 and --stdin and outputs a line + --tester implies --stdin and outputs a line '""" & DummyEof & """' for the tester The server then listens to the connection and takes line-based commands. @@ -50,7 +49,7 @@ In addition, all command line options of Nim that do not affect code generation are supported. """ type - Mode = enum mstdin, mtcp, mepc, mcmdline + Mode = enum mstdin, mtcp, mepc, mcmdsug, mcmdcon CachedMsg = object info: TLineInfo msg: string @@ -165,7 +164,7 @@ proc execute(cmd: IdeCmd, file, dirtyfile: string, line, col: int; else: msgs.structuredErrorHook = nil msgs.writelnHook = proc (s: string) = discard - if cmd == ideUse and suggestVersion != 2: + if cmd == ideUse and suggestVersion != 0: graph.resetAllModules() var isKnownFile = true let dirtyIdx = file.fileInfoIdx(isKnownFile) @@ -176,11 +175,11 @@ proc execute(cmd: IdeCmd, file, dirtyfile: string, line, col: int; gTrackPos = newLineInfo(dirtyIdx, line, col) gTrackPosAttached = false gErrorCounter = 0 - if suggestVersion < 2: + if suggestVersion == 1: graph.usageSym = nil if not isKnownFile: graph.compileProject(cache) - if suggestVersion == 2 and gIdeCmd in {ideUse, ideDus} and + if suggestVersion == 0 and gIdeCmd in {ideUse, ideDus} and dirtyfile.len == 0: discard "no need to recompile anything" else: @@ -190,7 +189,7 @@ proc execute(cmd: IdeCmd, file, dirtyfile: string, line, col: int; if gIdeCmd != ideMod: graph.compileProject(cache, modIdx) if gIdeCmd in {ideUse, ideDus}: - let u = if suggestVersion >= 2: graph.symFromInfo(gTrackPos) else: graph.usageSym + let u = if suggestVersion != 1: graph.symFromInfo(gTrackPos) else: graph.usageSym if u != nil: listUsages(u) else: @@ -503,8 +502,10 @@ proc mainCommand(graph: ModuleGraph; cache: IdentCache) = of mstdin: createThread(inputThread, replStdin, (gPort, gAddress)) of mtcp: createThread(inputThread, replTcp, (gPort, gAddress)) of mepc: createThread(inputThread, replEpc, (gPort, gAddress)) - of mcmdline: createThread(inputThread, replCmdline, + of mcmdsug: createThread(inputThread, replCmdline, (gPort, "sug \"" & options.gProjectFull & "\":" & gAddress)) + of mcmdcon: createThread(inputThread, replCmdline, + (gPort, "con \"" & options.gProjectFull & "\":" & gAddress)) mainThread(graph, cache) joinThread(inputThread) close(requests) @@ -525,17 +526,21 @@ proc processCmdLine*(pass: TCmdLinePass, cmd: string) = gAddress = p.val gMode = mtcp of "stdin": gMode = mstdin - of "cmdline": - gMode = mcmdline - suggestVersion = 2 + of "cmdsug": + gMode = mcmdsug gAddress = p.val + incl(gGlobalOptions, optIdeDebug) + of "cmdcon": + gMode = mcmdcon + gAddress = p.val + incl(gGlobalOptions, optIdeDebug) of "epc": gMode = mepc gVerbosity = 0 # Port number gotta be first. of "debug": incl(gGlobalOptions, optIdeDebug) - of "v2": suggestVersion = 2 + of "v2": suggestVersion = 0 + of "v1": suggestVersion = 1 of "tester": - suggestVersion = 2 gMode = mstdin gEmitEof = true gRefresh = false diff --git a/nimsuggest/nimsuggest.nim.cfg b/nimsuggest/nimsuggest.nim.cfg index 2e14a4dd3..38e74b3c7 100644 --- a/nimsuggest/nimsuggest.nim.cfg +++ b/nimsuggest/nimsuggest.nim.cfg @@ -22,4 +22,4 @@ define:nimsuggest --path:"$nim" --threads:on --noNimblePath ---path:"../../compiler" +--path:"../compiler" |