nimsuggest: added 'chk', 'outline' and 'highlight' features

This commit is contained in:
Araq 2015-11-01 23:26:19 +01:00
commit d673fb3911
8 changed files with 167 additions and 71 deletions

View file

@ -819,6 +819,8 @@ type
constraint*: PNode # additional constraints like 'lit|result'; also constraint*: PNode # additional constraints like 'lit|result'; also
# misused for the codegenDecl pragma in the hope # misused for the codegenDecl pragma in the hope
# it won't cause problems # it won't cause problems
when defined(nimsuggest):
allUsages*: seq[TLineInfo]
TTypeSeq* = seq[PType] TTypeSeq* = seq[PType]
TLockLevel* = distinct int16 TLockLevel* = distinct int16

View file

@ -30,7 +30,7 @@ var
## XXX: we should implement recycling of file IDs ## XXX: we should implement recycling of file IDs
## if the user keeps renaming modules, the file IDs will keep growing ## if the user keeps renaming modules, the file IDs will keep growing
proc getModule(fileIdx: int32): PSym = proc getModule*(fileIdx: int32): PSym =
if fileIdx >= 0 and fileIdx < gCompiledModules.len: if fileIdx >= 0 and fileIdx < gCompiledModules.len:
result = gCompiledModules[fileIdx] result = gCompiledModules[fileIdx]

View file

@ -815,24 +815,33 @@ proc getMessageStr(msg: TMsgKind, arg: string): string =
type type
TErrorHandling = enum doNothing, doAbort, doRaise TErrorHandling = enum doNothing, doAbort, doRaise
proc handleError(msg: TMsgKind, eh: TErrorHandling, s: string) = proc quit(msg: TMsgKind) =
template quit = if defined(debug) or msg == errInternal or hintStackTrace in gNotes:
if defined(debug) or msg == errInternal or hintStackTrace in gNotes: if stackTraceAvailable() and isNil(writelnHook):
if stackTraceAvailable() and isNil(writelnHook): writeStackTrace()
writeStackTrace() else:
else: styledMsgWriteln(fgRed, "No stack traceback available\n" &
styledMsgWriteln(fgRed, "No stack traceback available\nTo create a stacktrace, rerun compilation with ./koch temp " & options.command & " <file>") "To create a stacktrace, rerun compilation with ./koch temp " &
quit 1 options.command & " <file>")
quit 1
proc log*(s: string) {.procvar.} =
var f: File
if open(f, "nimsuggest.log", fmAppend):
f.writeln(s)
close(f)
proc handleError(msg: TMsgKind, eh: TErrorHandling, s: string) =
if msg >= fatalMin and msg <= fatalMax: if msg >= fatalMin and msg <= fatalMax:
quit() if gCmd == cmdIdeTools: log(s)
quit(msg)
if msg >= errMin and msg <= errMax: if msg >= errMin and msg <= errMax:
inc(gErrorCounter) inc(gErrorCounter)
options.gExitcode = 1'i8 options.gExitcode = 1'i8
if gErrorCounter >= gErrorMax: if gErrorCounter >= gErrorMax:
quit() quit(msg)
elif eh == doAbort and gCmd != cmdIdeTools: elif eh == doAbort and gCmd != cmdIdeTools:
quit() quit(msg)
elif eh == doRaise: elif eh == doRaise:
raiseRecoverableError(s) raiseRecoverableError(s)

View file

@ -86,7 +86,8 @@ type # please make sure we have under 32 options
gcNone, gcBoehm, gcGo, gcMarkAndSweep, gcRefc, gcV2, gcGenerational gcNone, gcBoehm, gcGo, gcMarkAndSweep, gcRefc, gcV2, gcGenerational
IdeCmd* = enum IdeCmd* = enum
ideNone, ideSug, ideCon, ideDef, ideUse, ideDus ideNone, ideSug, ideCon, ideDef, ideUse, ideDus, ideChk, ideMod,
ideHighlight, ideOutline
var var
gIdeCmd*: IdeCmd gIdeCmd*: IdeCmd
@ -422,6 +423,10 @@ proc parseIdeCmd*(s: string): IdeCmd =
of "def": ideDef of "def": ideDef
of "use": ideUse of "use": ideUse
of "dus": ideDus of "dus": ideDus
of "chk": ideChk
of "mod": ideMod
of "highlight": ideHighlight
of "outline": ideOutline
else: ideNone else: ideNone
proc `$`*(c: IdeCmd): string = proc `$`*(c: IdeCmd): string =
@ -431,4 +436,8 @@ proc `$`*(c: IdeCmd): string =
of ideDef: "def" of ideDef: "def"
of ideUse: "use" of ideUse: "use"
of ideDus: "dus" of ideDus: "dus"
of ideChk: "chk"
of ideMod: "mod"
of ideNone: "none" of ideNone: "none"
of ideHighlight: "highlight"
of ideOutline: "outline"

View file

@ -61,8 +61,8 @@ template semIdeForTemplateOrGeneric(c: PContext; n: PNode;
when defined(nimsuggest): when defined(nimsuggest):
assert gCmd == cmdIdeTools assert gCmd == cmdIdeTools
if requiresCheck: if requiresCheck:
if optIdeDebug in gGlobalOptions: #if optIdeDebug in gGlobalOptions:
echo "passing to safeSemExpr: ", renderTree(n) # echo "passing to safeSemExpr: ", renderTree(n)
discard safeSemExpr(c, n) discard safeSemExpr(c, n)
proc typeMismatch(n: PNode, formal, actual: PType) = proc typeMismatch(n: PNode, formal, actual: PType) =
@ -441,6 +441,8 @@ proc semStmtAndGenerateGenerics(c: PContext, n: PNode): PNode =
result = hloStmt(c, result) result = hloStmt(c, result)
if gCmd == cmdInteractive and not isEmptyType(result.typ): if gCmd == cmdInteractive and not isEmptyType(result.typ):
result = buildEchoStmt(c, result) result = buildEchoStmt(c, result)
if gCmd == cmdIdeTools:
appendToModule(c.module, result)
result = transformStmt(c.module, result) result = transformStmt(c.module, result)
proc recoverContext(c: PContext) = proc recoverContext(c: PContext) =

View file

@ -2083,11 +2083,6 @@ proc semExport(c: PContext, n: PNode): PNode =
x.add(newSymNode(s, a.info)) x.add(newSymNode(s, a.info))
strTableAdd(c.module.tab, s) strTableAdd(c.module.tab, s)
s = nextOverloadIter(o, c, a) s = nextOverloadIter(o, c, a)
when false:
if c.module.ast.isNil:
c.module.ast = newNodeI(nkStmtList, n.info)
assert c.module.ast.kind == nkStmtList
c.module.ast.add x
result = n result = n
proc shouldBeBracketExpr(n: PNode): bool = proc shouldBeBracketExpr(n: PNode): bool =

View file

@ -699,7 +699,7 @@ proc track(tracked: PEffects, n: PNode) =
if notGcSafe(op) and not importedFromC(a): if notGcSafe(op) and not importedFromC(a):
# and it's not a recursive call: # and it's not a recursive call:
if not (a.kind == nkSym and a.sym == tracked.owner): if not (a.kind == nkSym and a.sym == tracked.owner):
warnAboutGcUnsafe(n) if warnGcUnsafe in gNotes: warnAboutGcUnsafe(n)
markGcUnsafe(tracked, a) markGcUnsafe(tracked, a)
for i in 1 .. <len(n): trackOperand(tracked, n.sons[i], paramType(op, i)) for i in 1 .. <len(n): trackOperand(tracked, n.sons[i], paramType(op, i))
if a.kind == nkSym and a.sym.magic in {mNew, mNewFinalize, mNewSeq}: if a.kind == nkSym and a.sym.magic in {mNew, mNewFinalize, mNewSeq}:

View file

@ -13,6 +13,9 @@
import algorithm, sequtils import algorithm, sequtils
when defined(nimsuggest):
import passes, tables # importer
const const
sep = '\t' sep = '\t'
@ -26,16 +29,24 @@ type
doc*: string # Not escaped (yet) doc*: string # Not escaped (yet)
symkind*: TSymKind symkind*: TSymKind
forth*: string # XXX TODO object on symkind forth*: string # XXX TODO object on symkind
quality*: range[0..100] # matching quality
isGlobal*: bool # is a global variable
tokenLen*: int
var var
suggestionResultHook*: proc (result: Suggest) {.closure.} suggestionResultHook*: proc (result: Suggest) {.closure.}
suggestVersion*: int
#template sectionSuggest(): expr = "##begin\n" & getStackTrace() & "##end\n" #template sectionSuggest(): expr = "##begin\n" & getStackTrace() & "##end\n"
template origModuleName(m: PSym): string = m.name.s template origModuleName(m: PSym): string = m.name.s
proc symToSuggest(s: PSym, isLocal: bool, section: string, li: TLineInfo): Suggest = proc symToSuggest(s: PSym, isLocal: bool, section: string, li: TLineInfo;
quality: range[0..100]): Suggest =
result.section = parseIdeCmd(section) result.section = parseIdeCmd(section)
result.quality = quality
result.isGlobal = sfGlobal in s.flags
result.tokenLen = s.name.s.len
if optIdeTerse in gGlobalOptions: if optIdeTerse in gGlobalOptions:
result.symkind = s.kind result.symkind = s.kind
result.filePath = toFullPath(li) result.filePath = toFullPath(li)
@ -65,23 +76,41 @@ proc symToSuggest(s: PSym, isLocal: bool, section: string, li: TLineInfo): Sugge
proc `$`(suggest: Suggest): string = proc `$`(suggest: Suggest): string =
result = $suggest.section result = $suggest.section
result.add(sep) result.add(sep)
result.add($suggest.symkind) if suggest.section == ideHighlight:
result.add(sep) if suggest.symkind == skVar and suggest.isGlobal:
result.add(suggest.qualifiedPath.join(".")) result.add("skGlobalVar")
result.add(sep) elif suggest.symkind == skLet and suggest.isGlobal:
result.add(suggest.forth) result.add("skGlobalLet")
result.add(sep) else:
result.add(suggest.filePath) result.add($suggest.symkind)
result.add(sep) result.add(sep)
result.add($suggest.line) result.add($suggest.line)
result.add(sep) result.add(sep)
result.add($suggest.column) result.add($suggest.column)
result.add(sep) result.add(sep)
when not defined(noDocgen): result.add($suggest.tokenLen)
result.add(suggest.doc.escape) else:
result.add($suggest.symkind)
result.add(sep)
result.add(suggest.qualifiedPath.join("."))
result.add(sep)
result.add(suggest.forth)
result.add(sep)
result.add(suggest.filePath)
result.add(sep)
result.add($suggest.line)
result.add(sep)
result.add($suggest.column)
result.add(sep)
when not defined(noDocgen):
result.add(suggest.doc.escape)
if suggestVersion == 2:
result.add(sep)
result.add($suggest.quality)
proc symToSuggest(s: PSym, isLocal: bool, section: string): Suggest = proc symToSuggest(s: PSym, isLocal: bool, section: string;
result = symToSuggest(s, isLocal, section, s.info) quality: range[0..100]): Suggest =
result = symToSuggest(s, isLocal, section, s.info, quality)
proc suggestResult(s: Suggest) = proc suggestResult(s: Suggest) =
if not isNil(suggestionResultHook): if not isNil(suggestionResultHook):
@ -106,7 +135,7 @@ proc fieldVisible*(c: PContext, f: PSym): bool {.inline.} =
proc suggestField(c: PContext, s: PSym, outputs: var int) = proc suggestField(c: PContext, s: PSym, outputs: var int) =
if filterSym(s) and fieldVisible(c, s): if filterSym(s) and fieldVisible(c, s):
suggestResult(symToSuggest(s, isLocal=true, $ideSug)) suggestResult(symToSuggest(s, isLocal=true, $ideSug, 100))
inc outputs inc outputs
template wholeSymTab(cond, section: expr) {.immediate.} = template wholeSymTab(cond, section: expr) {.immediate.} =
@ -119,7 +148,7 @@ template wholeSymTab(cond, section: expr) {.immediate.} =
for item in entries: for item in entries:
let it {.inject.} = item let it {.inject.} = item
if cond: if cond:
suggestResult(symToSuggest(it, isLocal = isLocal, section)) suggestResult(symToSuggest(it, isLocal = isLocal, section, 100))
inc outputs inc outputs
proc suggestSymList(c: PContext, list: PNode, outputs: var int) = proc suggestSymList(c: PContext, list: PNode, outputs: var int) =
@ -188,7 +217,7 @@ proc suggestEverything(c: PContext, n: PNode, outputs: var int) =
if scope == c.topLevelScope: isLocal = false if scope == c.topLevelScope: isLocal = false
for it in items(scope.symbols): for it in items(scope.symbols):
if filterSym(it): if filterSym(it):
suggestResult(symToSuggest(it, isLocal = isLocal, $ideSug)) suggestResult(symToSuggest(it, isLocal = isLocal, $ideSug, 0))
inc outputs inc outputs
if scope == c.topLevelScope: break if scope == c.topLevelScope: break
@ -196,6 +225,23 @@ proc suggestFieldAccess(c: PContext, n: PNode, outputs: var int) =
# special code that deals with ``myObj.``. `n` is NOT the nkDotExpr-node, but # special code that deals with ``myObj.``. `n` is NOT the nkDotExpr-node, but
# ``myObj``. # ``myObj``.
var typ = n.typ var typ = n.typ
when defined(nimsuggest):
if n.kind == nkSym and n.sym.kind == skError and suggestVersion == 2:
# consider 'foo.|' where 'foo' is some not imported module.
let fullPath = findModule(n.sym.name.s, n.info.toFullPath)
if fullPath.len == 0:
# error: no known module name:
typ = nil
else:
let m = gImportModule(c.module, fullpath.fileInfoIdx)
if m == nil: typ = nil
else:
for it in items(n.sym.tab):
if filterSym(it):
suggestResult(symToSuggest(it, isLocal=false, $ideSug, 100))
inc outputs
suggestResult(symToSuggest(m, isLocal=false, $ideMod, 100))
if typ == nil: if typ == nil:
# a module symbol has no type for example: # a module symbol has no type for example:
if n.kind == nkSym and n.sym.kind == skModule: if n.kind == nkSym and n.sym.kind == skModule:
@ -203,12 +249,12 @@ proc suggestFieldAccess(c: PContext, n: PNode, outputs: var int) =
# all symbols accessible, because we are in the current module: # all symbols accessible, because we are in the current module:
for it in items(c.topLevelScope.symbols): for it in items(c.topLevelScope.symbols):
if filterSym(it): if filterSym(it):
suggestResult(symToSuggest(it, isLocal=false, $ideSug)) suggestResult(symToSuggest(it, isLocal=false, $ideSug, 100))
inc outputs inc outputs
else: else:
for it in items(n.sym.tab): for it in items(n.sym.tab):
if filterSym(it): if filterSym(it):
suggestResult(symToSuggest(it, isLocal=false, $ideSug)) suggestResult(symToSuggest(it, isLocal=false, $ideSug, 100))
inc outputs inc outputs
else: else:
# fallback: # fallback:
@ -263,12 +309,11 @@ proc findClosestCall(n: PNode): PNode =
result = findClosestCall(n.sons[i]) result = findClosestCall(n.sons[i])
if result != nil: return if result != nil: return
proc isTracked(current: TLineInfo, tokenLen: int): bool = proc isTracked*(current: TLineInfo, tokenLen: int): bool =
if current.fileIndex == gTrackPos.fileIndex: if current.fileIndex==gTrackPos.fileIndex and current.line==gTrackPos.line:
if current.line == gTrackPos.line: let col = gTrackPos.col
let col = gTrackPos.col if col >= current.col and col <= current.col+tokenLen-1:
if col >= current.col and col <= current.col+tokenLen-1: return true
return true
proc findClosestSym(n: PNode): PNode = proc findClosestSym(n: PNode): PNode =
if n.kind == nkSym and inCheckpoint(n.info) == cpExact: if n.kind == nkSym and inCheckpoint(n.info) == cpExact:
@ -278,23 +323,43 @@ proc findClosestSym(n: PNode): PNode =
result = findClosestSym(n.sons[i]) result = findClosestSym(n.sons[i])
if result != nil: return if result != nil: return
when defined(nimsuggest):
# Since TLineInfo defined a == operator that doesn't include the column,
# we map TLineInfo to a unique int here for this lookup table:
proc infoToInt(info: TLineInfo): int64 =
info.fileIndex + info.line.int64 shl 32 + info.col.int64 shl 48
proc addNoDup(s: PSym; info: TLineInfo) =
let infoAsInt = info.infoToInt
for infoB in s.allUsages:
if infoB.infoToInt == infoAsInt: return
s.allUsages.add(info)
var var
usageSym*: PSym usageSym*: PSym
lastLineInfo: TLineInfo lastLineInfo*: TLineInfo
proc findUsages(info: TLineInfo; s: PSym) = proc findUsages(info: TLineInfo; s: PSym) =
if usageSym == nil and isTracked(info, s.name.s.len): if suggestVersion < 2:
usageSym = s if usageSym == nil and isTracked(info, s.name.s.len):
suggestResult(symToSuggest(s, isLocal=false, $ideUse)) usageSym = s
elif s == usageSym: suggestResult(symToSuggest(s, isLocal=false, $ideUse, 100))
if lastLineInfo != info: elif s == usageSym:
suggestResult(symToSuggest(s, isLocal=false, $ideUse, info)) if lastLineInfo != info:
lastLineInfo = info suggestResult(symToSuggest(s, isLocal=false, $ideUse, info, 100))
lastLineInfo = info
when defined(nimsuggest):
proc listUsages*(s: PSym) =
#echo "usages ", len(s.allUsages)
for info in s.allUsages:
let x = if info == s.info and info.col == s.info.col: "def" else: "use"
suggestResult(symToSuggest(s, isLocal=false, x, info, 100))
proc findDefinition(info: TLineInfo; s: PSym) = proc findDefinition(info: TLineInfo; s: PSym) =
if s.isNil: return if s.isNil: return
if isTracked(info, s.name.s.len): if isTracked(info, s.name.s.len):
suggestResult(symToSuggest(s, isLocal=false, $ideDef)) suggestResult(symToSuggest(s, isLocal=false, $ideDef, 100))
suggestQuit() suggestQuit()
proc ensureIdx[T](x: var T, y: int) = proc ensureIdx[T](x: var T, y: int) =
@ -303,23 +368,36 @@ proc ensureIdx[T](x: var T, y: int) =
proc ensureSeq[T](x: var seq[T]) = proc ensureSeq[T](x: var seq[T]) =
if x == nil: newSeq(x, 0) if x == nil: newSeq(x, 0)
proc suggestSym*(info: TLineInfo; s: PSym) {.inline.} = proc suggestSym*(info: TLineInfo; s: PSym; isDecl=true) {.inline.} =
## misnamed: should be 'symDeclared' ## misnamed: should be 'symDeclared'
if gIdeCmd == ideUse: when defined(nimsuggest):
findUsages(info, s) if suggestVersion == 2:
elif gIdeCmd == ideDef: if s.allUsages.isNil:
findDefinition(info, s) s.allUsages = @[info]
elif gIdeCmd == ideDus and s != nil: else:
if isTracked(info, s.name.s.len): s.addNoDup(info)
suggestResult(symToSuggest(s, isLocal=false, $ideDef))
findUsages(info, s) if gIdeCmd == ideUse:
findUsages(info, s)
elif gIdeCmd == ideDef:
findDefinition(info, s)
elif gIdeCmd == ideDus and s != nil:
if isTracked(info, s.name.s.len):
suggestResult(symToSuggest(s, isLocal=false, $ideDef, 100))
findUsages(info, s)
elif gIdeCmd == ideHighlight and info.fileIndex == gTrackPos.fileIndex:
suggestResult(symToSuggest(s, isLocal=false, $ideHighlight, info, 100))
elif gIdeCmd == ideOutline and info.fileIndex == gTrackPos.fileIndex and
isDecl:
suggestResult(symToSuggest(s, isLocal=false, $ideOutline, info, 100))
proc markUsed(info: TLineInfo; s: PSym) = proc markUsed(info: TLineInfo; s: PSym) =
incl(s.flags, sfUsed) incl(s.flags, sfUsed)
if {sfDeprecated, sfError} * s.flags != {}: if {sfDeprecated, sfError} * s.flags != {}:
if sfDeprecated in s.flags: message(info, warnDeprecated, s.name.s) if sfDeprecated in s.flags: message(info, warnDeprecated, s.name.s)
if sfError in s.flags: localError(info, errWrongSymbolX, s.name.s) if sfError in s.flags: localError(info, errWrongSymbolX, s.name.s)
suggestSym(info, s) when defined(nimsuggest):
suggestSym(info, s, false)
proc useSym*(sym: PSym): PNode = proc useSym*(sym: PSym): PNode =
result = newSymNode(sym) result = newSymNode(sym)
@ -348,8 +426,9 @@ proc suggestExpr*(c: PContext, node: PNode) =
if n.kind == nkDotExpr: if n.kind == nkDotExpr:
var obj = safeSemExpr(c, n.sons[0]) var obj = safeSemExpr(c, n.sons[0])
suggestFieldAccess(c, obj, outputs) suggestFieldAccess(c, obj, outputs)
if optIdeDebug in gGlobalOptions:
echo "expression ", renderTree(obj), " has type ", typeToString(obj.typ) #if optIdeDebug in gGlobalOptions:
# echo "expression ", renderTree(obj), " has type ", typeToString(obj.typ)
#writeStackTrace() #writeStackTrace()
else: else:
suggestEverything(c, n, outputs) suggestEverything(c, n, outputs)
@ -370,7 +449,7 @@ proc suggestExpr*(c: PContext, node: PNode) =
suggestCall(c, a, n, outputs) suggestCall(c, a, n, outputs)
dec(c.compilesContextId) dec(c.compilesContextId)
if outputs > 0 and gIdeCmd notin {ideUse, ideDus}: suggestQuit() if outputs > 0 and gIdeCmd in {ideSug, ideCon, ideDef}: suggestQuit()
proc suggestStmt*(c: PContext, n: PNode) = proc suggestStmt*(c: PContext, n: PNode) =
suggestExpr(c, n) suggestExpr(c, n)