nimsuggest: maxresults limit; fixed local symbol usages priorizations

This commit is contained in:
Andreas Rumpf 2017-03-10 11:29:16 +01:00
commit 68181e6da2
4 changed files with 62 additions and 49 deletions

View file

@ -63,6 +63,7 @@ type
var var
suggestionResultHook*: proc (result: Suggest) {.closure.} suggestionResultHook*: proc (result: Suggest) {.closure.}
suggestVersion*: int suggestVersion*: int
suggestMaxResults* = 10_000
#template sectionSuggest(): expr = "##begin\n" & getStackTrace() & "##end\n" #template sectionSuggest(): expr = "##begin\n" & getStackTrace() & "##end\n"
@ -104,11 +105,11 @@ proc cmpSuggestions(a, b: Suggest): int =
# independent of hashing order: # independent of hashing order:
result = cmp(a.name.s, b.name.s) result = cmp(a.name.s, b.name.s)
proc symToSuggest(s: PSym, isLocal: bool, section: string, li: TLineInfo; proc symToSuggest(s: PSym, isLocal: bool, section: IdeCmd, info: TLineInfo;
quality: range[0..100]; prefix: PrefixMatch; quality: range[0..100]; prefix: PrefixMatch;
inTypeContext: bool; scope: int): Suggest = inTypeContext: bool; scope: int): Suggest =
new(result) new(result)
result.section = parseIdeCmd(section) result.section = section
result.quality = quality result.quality = quality
result.isGlobal = sfGlobal in s.flags result.isGlobal = sfGlobal in s.flags
result.tokenLen = s.name.s.len result.tokenLen = s.name.s.len
@ -120,15 +121,10 @@ proc symToSuggest(s: PSym, isLocal: bool, section: string, li: TLineInfo;
result.globalUsages = s.allUsages.len result.globalUsages = s.allUsages.len
var c = 0 var c = 0
for u in s.allUsages: for u in s.allUsages:
if u.fileIndex == li.fileIndex: inc c if u.fileIndex == info.fileIndex: inc c
result.localUsages = c result.localUsages = c
if optIdeTerse in gGlobalOptions: result.symkind = s.kind
result.symkind = s.kind if optIdeTerse notin gGlobalOptions:
result.filePath = toFullPath(li)
result.line = toLinenumber(li)
result.column = toColumn(li)
else:
result.symkind = s.kind
result.qualifiedPath = @[] result.qualifiedPath = @[]
if not isLocal and s.kind != skModule: if not isLocal and s.kind != skModule:
let ow = s.owner let ow = s.owner
@ -143,11 +139,12 @@ proc symToSuggest(s: PSym, isLocal: bool, section: string, li: TLineInfo;
result.forth = typeToString(s.typ) result.forth = typeToString(s.typ)
else: else:
result.forth = "" result.forth = ""
result.filePath = toFullPath(li)
result.line = toLinenumber(li)
result.column = toColumn(li)
when not defined(noDocgen): when not defined(noDocgen):
result.doc = s.extractDocComment result.doc = s.extractDocComment
let infox = if section in {ideUse, ideHighlight, ideOutline}: info else: s.info
result.filePath = toFullPath(infox)
result.line = toLinenumber(infox)
result.column = toColumn(infox)
proc `$`*(suggest: Suggest): string = proc `$`*(suggest: Suggest): string =
result = $suggest.section result = $suggest.section
@ -188,11 +185,6 @@ proc `$`*(suggest: Suggest): string =
result.add(sep) result.add(sep)
result.add($suggest.prefix) result.add($suggest.prefix)
proc symToSuggest(s: PSym, isLocal: bool, section: string;
quality: range[0..100], prefix: PrefixMatch; inTypeContext: bool;
scope: int): Suggest =
result = symToSuggest(s, isLocal, section, s.info, quality, prefix, inTypeContext, scope)
proc suggestResult(s: Suggest) = proc suggestResult(s: Suggest) =
if not isNil(suggestionResultHook): if not isNil(suggestionResultHook):
suggestionResultHook(s) suggestionResultHook(s)
@ -205,7 +197,7 @@ proc produceOutput(a: var Suggestions) =
when false: when false:
# debug code # debug code
writeStackTrace() writeStackTrace()
if a.len > 10: a.setLen(10) if a.len > suggestMaxResults: a.setLen(suggestMaxResults)
if not isNil(suggestionResultHook): if not isNil(suggestionResultHook):
for s in a: for s in a:
suggestionResultHook(s) suggestionResultHook(s)
@ -241,10 +233,10 @@ proc fieldVisible*(c: PContext, f: PSym): bool {.inline.} =
result = true result = true
break break
proc suggestField(c: PContext, s: PSym; f: PNode; outputs: var Suggestions) = proc suggestField(c: PContext, s: PSym; f: PNode; info: TLineInfo; outputs: var Suggestions) =
var pm: PrefixMatch var pm: PrefixMatch
if filterSym(s, f, pm) and fieldVisible(c, s): if filterSym(s, f, pm) and fieldVisible(c, s):
outputs.add(symToSuggest(s, isLocal=true, $ideSug, 100, pm, c.inTypeContext > 0, 0)) outputs.add(symToSuggest(s, isLocal=true, ideSug, info, 100, pm, c.inTypeContext > 0, 0))
proc getQuality(s: PSym): range[0..100] = proc getQuality(s: PSym): range[0..100] =
if s.typ != nil and s.typ.len > 1: if s.typ != nil and s.typ.len > 1:
@ -263,25 +255,25 @@ template wholeSymTab(cond, section: untyped) =
let it {.inject.} = item let it {.inject.} = item
var pm {.inject.}: PrefixMatch var pm {.inject.}: PrefixMatch
if cond: if cond:
outputs.add(symToSuggest(it, isLocal = isLocal, section, getQuality(it), outputs.add(symToSuggest(it, isLocal = isLocal, section, info, getQuality(it),
pm, c.inTypeContext > 0, scopeN)) pm, c.inTypeContext > 0, scopeN))
proc suggestSymList(c: PContext, list, f: PNode, outputs: var Suggestions) = proc suggestSymList(c: PContext, list, f: PNode; info: TLineInfo, outputs: var Suggestions) =
for i in countup(0, sonsLen(list) - 1): for i in countup(0, sonsLen(list) - 1):
if list.sons[i].kind == nkSym: if list.sons[i].kind == nkSym:
suggestField(c, list.sons[i].sym, f, outputs) suggestField(c, list.sons[i].sym, f, info, outputs)
#else: InternalError(list.info, "getSymFromList") #else: InternalError(list.info, "getSymFromList")
proc suggestObject(c: PContext, n, f: PNode, outputs: var Suggestions) = proc suggestObject(c: PContext, n, f: PNode; info: TLineInfo, outputs: var Suggestions) =
case n.kind case n.kind
of nkRecList: of nkRecList:
for i in countup(0, sonsLen(n)-1): suggestObject(c, n.sons[i], f, outputs) for i in countup(0, sonsLen(n)-1): suggestObject(c, n.sons[i], f, info, outputs)
of nkRecCase: of nkRecCase:
var L = sonsLen(n) var L = sonsLen(n)
if L > 0: if L > 0:
suggestObject(c, n.sons[0], f, outputs) suggestObject(c, n.sons[0], f, info, outputs)
for i in countup(1, L-1): suggestObject(c, lastSon(n.sons[i]), f, outputs) for i in countup(1, L-1): suggestObject(c, lastSon(n.sons[i]), f, info, outputs)
of nkSym: suggestField(c, n.sym, f, outputs) of nkSym: suggestField(c, n.sym, f, info, outputs)
else: discard else: discard
proc nameFits(c: PContext, s: PSym, n: PNode): bool = proc nameFits(c: PContext, s: PSym, n: PNode): bool =
@ -305,8 +297,9 @@ proc argsFit(c: PContext, candidate: PSym, n, nOrig: PNode): bool =
result = false result = false
proc suggestCall(c: PContext, n, nOrig: PNode, outputs: var Suggestions) = proc suggestCall(c: PContext, n, nOrig: PNode, outputs: var Suggestions) =
let info = n.info
wholeSymTab(filterSym(it, nil, pm) and nameFits(c, it, n) and argsFit(c, it, n, nOrig), wholeSymTab(filterSym(it, nil, pm) and nameFits(c, it, n) and argsFit(c, it, n, nOrig),
$ideCon) ideCon)
proc typeFits(c: PContext, s: PSym, firstArg: PType): bool {.inline.} = proc typeFits(c: PContext, s: PSym, firstArg: PType): bool {.inline.} =
if s.typ != nil and sonsLen(s.typ) > 1 and s.typ.sons[1] != nil: if s.typ != nil and sonsLen(s.typ) > 1 and s.typ.sons[1] != nil:
@ -323,7 +316,8 @@ proc typeFits(c: PContext, s: PSym, firstArg: PType): bool {.inline.} =
proc suggestOperations(c: PContext, n, f: PNode, typ: PType, outputs: var Suggestions) = proc suggestOperations(c: PContext, n, f: PNode, typ: PType, outputs: var Suggestions) =
assert typ != nil assert typ != nil
wholeSymTab(filterSymNoOpr(it, f, pm) and typeFits(c, it, typ), $ideSug) let info = n.info
wholeSymTab(filterSymNoOpr(it, f, pm) and typeFits(c, it, typ), ideSug)
proc suggestEverything(c: PContext, n, f: PNode, outputs: var Suggestions) = proc suggestEverything(c: PContext, n, f: PNode, outputs: var Suggestions) =
# do not produce too many symbols: # do not produce too many symbols:
@ -335,7 +329,8 @@ proc suggestEverything(c: PContext, n, f: PNode, outputs: var Suggestions) =
for it in items(scope.symbols): for it in items(scope.symbols):
var pm: PrefixMatch var pm: PrefixMatch
if filterSym(it, f, pm): if filterSym(it, f, pm):
outputs.add(symToSuggest(it, isLocal = isLocal, $ideSug, 0, pm, c.inTypeContext > 0, scopeN)) outputs.add(symToSuggest(it, isLocal = isLocal, ideSug, n.info, 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) = proc suggestFieldAccess(c: PContext, n, field: PNode, outputs: var Suggestions) =
@ -356,8 +351,8 @@ proc suggestFieldAccess(c: PContext, n, field: PNode, outputs: var Suggestions)
else: else:
for it in items(n.sym.tab): for it in items(n.sym.tab):
if filterSym(it, field, pm): if filterSym(it, field, pm):
outputs.add(symToSuggest(it, isLocal=false, $ideSug, 100, pm, c.inTypeContext > 0, -100)) outputs.add(symToSuggest(it, isLocal=false, ideSug, n.info, 100, pm, c.inTypeContext > 0, -100))
outputs.add(symToSuggest(m, isLocal=false, $ideMod, 100, PrefixMatch.None, outputs.add(symToSuggest(m, isLocal=false, ideMod, n.info, 100, PrefixMatch.None,
c.inTypeContext > 0, -99)) c.inTypeContext > 0, -99))
if typ == nil: if typ == nil:
@ -367,11 +362,11 @@ proc suggestFieldAccess(c: PContext, n, field: PNode, outputs: var Suggestions)
# 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, field, pm): if filterSym(it, field, pm):
outputs.add(symToSuggest(it, isLocal=false, $ideSug, 100, pm, c.inTypeContext > 0, -99)) outputs.add(symToSuggest(it, isLocal=false, ideSug, n.info, 100, pm, c.inTypeContext > 0, -99))
else: else:
for it in items(n.sym.tab): for it in items(n.sym.tab):
if filterSym(it, field, pm): if filterSym(it, field, pm):
outputs.add(symToSuggest(it, isLocal=false, $ideSug, 100, pm, c.inTypeContext > 0, -99)) outputs.add(symToSuggest(it, isLocal=false, ideSug, n.info, 100, pm, c.inTypeContext > 0, -99))
else: else:
# fallback: # fallback:
suggestEverything(c, n, field, outputs) suggestEverything(c, n, field, outputs)
@ -379,7 +374,7 @@ proc suggestFieldAccess(c: PContext, n, field: PNode, outputs: var Suggestions)
# look up if the identifier belongs to the enum: # look up if the identifier belongs to the enum:
var t = typ var t = typ
while t != nil: while t != nil:
suggestSymList(c, t.n, field, outputs) suggestSymList(c, t.n, field, n.info, outputs)
t = t.sons[0] t = t.sons[0]
suggestOperations(c, n, field, typ, outputs) suggestOperations(c, n, field, typ, outputs)
else: else:
@ -388,11 +383,11 @@ proc suggestFieldAccess(c: PContext, n, field: PNode, outputs: var Suggestions)
if typ.kind == tyObject: if typ.kind == tyObject:
var t = typ var t = typ
while true: while true:
suggestObject(c, t.n, field, outputs) suggestObject(c, t.n, field, n.info, outputs)
if t.sons[0] == nil: break if t.sons[0] == nil: break
t = skipTypes(t.sons[0], skipPtrs) t = skipTypes(t.sons[0], skipPtrs)
elif typ.kind == tyTuple and typ.n != nil: elif typ.kind == tyTuple and typ.n != nil:
suggestSymList(c, typ.n, field, outputs) suggestSymList(c, typ.n, field, n.info, outputs)
suggestOperations(c, n, field, orig, outputs) suggestOperations(c, n, field, orig, outputs)
if typ != orig: if typ != orig:
suggestOperations(c, n, field, typ, outputs) suggestOperations(c, n, field, typ, outputs)
@ -436,23 +431,23 @@ proc findUsages(info: TLineInfo; s: PSym; usageSym: var PSym) =
if suggestVersion == 1: if suggestVersion == 1:
if usageSym == nil and isTracked(info, s.name.s.len): if usageSym == nil and isTracked(info, s.name.s.len):
usageSym = s usageSym = s
suggestResult(symToSuggest(s, isLocal=false, $ideUse, 100, PrefixMatch.None, false, 0)) suggestResult(symToSuggest(s, isLocal=false, ideUse, info, 100, PrefixMatch.None, false, 0))
elif s == usageSym: elif s == usageSym:
if lastLineInfo != info: if lastLineInfo != info:
suggestResult(symToSuggest(s, isLocal=false, $ideUse, info, 100, PrefixMatch.None, false, 0)) suggestResult(symToSuggest(s, isLocal=false, ideUse, info, 100, PrefixMatch.None, false, 0))
lastLineInfo = info lastLineInfo = info
when defined(nimsuggest): when defined(nimsuggest):
proc listUsages*(s: PSym) = proc listUsages*(s: PSym) =
#echo "usages ", len(s.allUsages) #echo "usages ", len(s.allUsages)
for info in s.allUsages: for info in s.allUsages:
let x = if info == s.info and info.col == s.info.col: "def" else: "use" let x = if info == s.info and info.col == s.info.col: ideDef else: ideUse
suggestResult(symToSuggest(s, isLocal=false, x, info, 100, PrefixMatch.None, false, 0)) suggestResult(symToSuggest(s, isLocal=false, x, info, 100, PrefixMatch.None, false, 0))
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, 100, PrefixMatch.None, false, 0)) suggestResult(symToSuggest(s, isLocal=false, ideDef, info, 100, PrefixMatch.None, false, 0))
suggestQuit() suggestQuit()
proc ensureIdx[T](x: var T, y: int) = proc ensureIdx[T](x: var T, y: int) =
@ -476,13 +471,13 @@ proc suggestSym*(info: TLineInfo; s: PSym; usageSym: var PSym; isDecl=true) {.in
findDefinition(info, s) findDefinition(info, s)
elif gIdeCmd == ideDus and s != nil: elif gIdeCmd == ideDus and s != nil:
if isTracked(info, s.name.s.len): if isTracked(info, s.name.s.len):
suggestResult(symToSuggest(s, isLocal=false, $ideDef, 100, PrefixMatch.None, false, 0)) suggestResult(symToSuggest(s, isLocal=false, ideDef, info, 100, PrefixMatch.None, false, 0))
findUsages(info, s, usageSym) findUsages(info, s, usageSym)
elif gIdeCmd == ideHighlight and info.fileIndex == gTrackPos.fileIndex: elif gIdeCmd == ideHighlight and info.fileIndex == gTrackPos.fileIndex:
suggestResult(symToSuggest(s, isLocal=false, $ideHighlight, info, 100, PrefixMatch.None, false, 0)) suggestResult(symToSuggest(s, isLocal=false, ideHighlight, info, 100, PrefixMatch.None, false, 0))
elif gIdeCmd == ideOutline and info.fileIndex == gTrackPos.fileIndex and elif gIdeCmd == ideOutline and info.fileIndex == gTrackPos.fileIndex and
isDecl: isDecl:
suggestResult(symToSuggest(s, isLocal=false, $ideOutline, info, 100, PrefixMatch.None, false, 0)) suggestResult(symToSuggest(s, isLocal=false, ideOutline, info, 100, PrefixMatch.None, false, 0))
proc markUsed(info: TLineInfo; s: PSym; usageSym: var PSym) = proc markUsed(info: TLineInfo; s: PSym; usageSym: var PSym) =
incl(s.flags, sfUsed) incl(s.flags, sfUsed)
@ -574,7 +569,7 @@ proc suggestSentinel*(c: PContext) =
for it in items(scope.symbols): for it in items(scope.symbols):
var pm: PrefixMatch var pm: PrefixMatch
if filterSymNoOpr(it, nil, pm): if filterSymNoOpr(it, nil, pm):
outputs.add(symToSuggest(it, isLocal = isLocal, $ideSug, 0, PrefixMatch.None, false, scopeN)) outputs.add(symToSuggest(it, isLocal = isLocal, ideSug, newLineInfo(gTrackPos.fileIndex, -1, -1), 0, PrefixMatch.None, false, scopeN))
dec(c.compilesContextId) dec(c.compilesContextId)
produceOutput(outputs) produceOutput(outputs)

View file

@ -40,6 +40,7 @@ Options:
--log enable verbose logging to nimsuggest.log file --log enable verbose logging to nimsuggest.log file
--v1 use version 1 of the protocol; for backwards compatibility --v1 use version 1 of the protocol; for backwards compatibility
--refresh perform automatic refreshes to keep the analysis precise --refresh perform automatic refreshes to keep the analysis precise
--maxresults:N limit the number of suggestions to N
--tester implies --stdin and outputs a line --tester implies --stdin and outputs a line
'""" & DummyEof & """' for the tester '""" & DummyEof & """' for the tester
@ -550,6 +551,8 @@ proc processCmdLine*(pass: TCmdLinePass, cmd: string) =
gRefresh = parseBool(p.val) gRefresh = parseBool(p.val)
else: else:
gRefresh = true gRefresh = true
of "maxresults":
suggestMaxResults = parseInt(p.val)
else: processSwitch(pass, p) else: processSwitch(pass, p)
of cmdArgument: of cmdArgument:
options.gProjectName = unixToNativePath(p.key) options.gProjectName = unixToNativePath(p.key)

View file

@ -304,8 +304,7 @@ proc runTest(filename: string): int =
proc main() = proc main() =
var failures = 0 var failures = 0
if os.paramCount() > 0: if os.paramCount() > 0:
let f = os.paramStr(1) let x = os.paramStr(1)
let x = getAppDir() / f
let xx = expandFilename x let xx = expandFilename x
failures += runTest(xx) failures += runTest(xx)
failures += runEpcTest(xx) failures += runEpcTest(xx)

View file

@ -0,0 +1,16 @@
discard """
$nimsuggest --tester --maxresults:2 $file
>sug $1
sug;;skProc;;tdot4.main;;proc (inp: string): string;;$file;;10;;5;;"";;100;;None
sug;;skProc;;strutils.replace;;proc (s: string, sub: string, by: string): string{.noSideEffect, gcsafe, locks: 0.};;$lib/pure/strutils.nim;;1497;;5;;"Replaces `sub` in `s` by the string `by`.";;100;;None
"""
import strutils
proc main(inp: string): string =
# use replace here and see if it occurs in the result, it should gain
# priority:
result = inp.replace(" ", "a").replace("b", "c")
echo "string literal here".#[!]#