suggest: try to find the implementation of a symbol when def is used (#15555)

* suggest: try to find the implementation of a symbol when def is used

* suggest: return all declarations of the symbol on `def`
This commit is contained in:
alaviss
2020-10-14 10:08:56 -05:00
committed by GitHub
parent 977bccdbff
commit c2ba4ef979
4 changed files with 33 additions and 7 deletions

View File

@@ -361,3 +361,7 @@ proc mydiv(a, b): int {.raises: [].} =
## Tool changes
- `nimsuggest` will now return both the forward declaration and the
implementation location upon a `def` query. Previously the behavior was
just to return the forward declaration.

View File

@@ -1943,6 +1943,7 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind,
if not comesFromShadowScope:
excl(proto.flags, sfForward)
incl(proto.flags, sfWasForwarded)
suggestSym(c.config, s.info, proto, c.graph.usageSym)
closeScope(c) # close scope with wrong parameter symbols
openScope(c) # open scope for old (correct) parameter symbols
if proto.ast[genericParamsPos].kind != nkEmpty:

View File

@@ -119,7 +119,8 @@ proc getTokenLenFromSource(conf: ConfigRef; ident: string; info: TLineInfo): int
proc symToSuggest(conf: ConfigRef; s: PSym, isLocal: bool, section: IdeCmd, info: TLineInfo;
quality: range[0..100]; prefix: PrefixMatch;
inTypeContext: bool; scope: int): Suggest =
inTypeContext: bool; scope: int;
useSuppliedInfo = false): Suggest =
new(result)
result.section = section
result.quality = quality
@@ -156,7 +157,11 @@ proc symToSuggest(conf: ConfigRef; s: PSym, isLocal: bool, section: IdeCmd, info
result.forth = ""
when defined(nimsuggest) and not defined(noDocgen) and not defined(leanCompiler):
result.doc = s.extractDocComment
let infox = if section in {ideUse, ideHighlight, ideOutline}: info else: s.info
let infox =
if useSuppliedInfo or section in {ideUse, ideHighlight, ideOutline}:
info
else:
s.info
result.filePath = toFullPath(conf, infox)
result.line = toLinenumber(infox)
result.column = toColumn(infox)
@@ -463,11 +468,14 @@ when defined(nimsuggest):
let x = if info == s.info and info.col == s.info.col: ideDef else: ideUse
suggestResult(conf, symToSuggest(conf, s, isLocal=false, x, info, 100, PrefixMatch.None, false, 0))
proc findDefinition(conf: ConfigRef; info: TLineInfo; s: PSym) =
proc findDefinition(conf: ConfigRef; info: TLineInfo; s: PSym; usageSym: var PSym) =
if s.isNil: return
if isTracked(info, conf.m.trackPos, s.name.s.len):
suggestResult(conf, symToSuggest(conf, s, isLocal=false, ideDef, info, 100, PrefixMatch.None, false, 0))
suggestQuit()
if isTracked(info, conf.m.trackPos, s.name.s.len) or (s == usageSym and sfForward notin s.flags):
suggestResult(conf, symToSuggest(conf, s, isLocal=false, ideDef, info, 100, PrefixMatch.None, false, 0, useSuppliedInfo = s == usageSym))
if sfForward notin s.flags:
suggestQuit()
else:
usageSym = s
proc ensureIdx[T](x: var T, y: int) =
if x.len <= y: x.setLen(y+1)
@@ -487,7 +495,7 @@ proc suggestSym*(conf: ConfigRef; info: TLineInfo; s: PSym; usageSym: var PSym;
if conf.ideCmd == ideUse:
findUsages(conf, info, s, usageSym)
elif conf.ideCmd == ideDef:
findDefinition(conf, info, s)
findDefinition(conf, info, s, usageSym)
elif conf.ideCmd == ideDus and s != nil:
if isTracked(info, conf.m.trackPos, s.name.s.len):
suggestResult(conf, symToSuggest(conf, s, isLocal=false, ideDef, info, 100, PrefixMatch.None, false, 0))

View File

@@ -0,0 +1,13 @@
discard """
$nimsuggest --tester $file
>def $1
def;;skProc;;tdef_forward.hello;;proc (): string;;$file;;8;;5;;"";;100
def;;skProc;;tdef_forward.hello;;proc (): string;;$file;;12;;5;;"";;100
"""
proc hello(): string
hel#[!]#lo()
proc hello(): string =
"Hello"