mirror of
https://github.com/nim-lang/Nim.git
synced 2026-08-28 09:31:38 +00:00
compiler/suggest: add variable support to con (#12569)
This allows for the type of a variable to be retrieved.
This commit is contained in:
@@ -293,7 +293,7 @@ proc suggestObject(c: PContext, n, f: PNode; info: TLineInfo, outputs: var Sugge
|
|||||||
else: discard
|
else: discard
|
||||||
|
|
||||||
proc nameFits(c: PContext, s: PSym, n: PNode): bool =
|
proc nameFits(c: PContext, s: PSym, n: PNode): bool =
|
||||||
var op = n.sons[0]
|
var op = if n.kind in nkCallKinds: n.sons[0] else: n
|
||||||
if op.kind in {nkOpenSymChoice, nkClosedSymChoice}: op = op.sons[0]
|
if op.kind in {nkOpenSymChoice, nkClosedSymChoice}: op = op.sons[0]
|
||||||
var opr: PIdent
|
var opr: PIdent
|
||||||
case op.kind
|
case op.kind
|
||||||
@@ -317,6 +317,10 @@ proc suggestCall(c: PContext, n, nOrig: PNode, outputs: var Suggestions) =
|
|||||||
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 suggestVar(c: PContext, n: PNode, outputs: var Suggestions) =
|
||||||
|
let info = n.info
|
||||||
|
wholeSymTab(nameFits(c, it, n), 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 len(s.typ) > 1 and s.typ.sons[1] != nil:
|
if s.typ != nil and len(s.typ) > 1 and s.typ.sons[1] != nil:
|
||||||
# special rule: if system and some weird generic match via 'tyUntyped'
|
# special rule: if system and some weird generic match via 'tyUntyped'
|
||||||
@@ -601,6 +605,10 @@ proc suggestExprNoCheck*(c: PContext, n: PNode) =
|
|||||||
if x.kind == nkEmpty or x.typ == nil: break
|
if x.kind == nkEmpty or x.typ == nil: break
|
||||||
addSon(a, x)
|
addSon(a, x)
|
||||||
suggestCall(c, a, n, outputs)
|
suggestCall(c, a, n, outputs)
|
||||||
|
elif n.kind in nkIdentKinds:
|
||||||
|
var x = safeSemExpr(c, n)
|
||||||
|
if x.kind == nkEmpty or x.typ == nil: x = n
|
||||||
|
suggestVar(c, x, outputs)
|
||||||
|
|
||||||
dec(c.compilesContextId)
|
dec(c.compilesContextId)
|
||||||
if outputs.len > 0 and c.config.ideCmd in {ideSug, ideCon, ideDef}:
|
if outputs.len > 0 and c.config.ideCmd in {ideSug, ideCon, ideDef}:
|
||||||
|
|||||||
12
nimsuggest/tests/tcon_variable.nim
Normal file
12
nimsuggest/tests/tcon_variable.nim
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
let foo = "string"
|
||||||
|
var bar = "string"
|
||||||
|
bar#[!]#.add foo
|
||||||
|
bar.add foo#[!]#
|
||||||
|
|
||||||
|
discard """
|
||||||
|
$nimsuggest --tester $file
|
||||||
|
>con $1
|
||||||
|
con;;skVar;;tcon_variable.bar;;string;;$file;;2;;4;;"";;100
|
||||||
|
>con $2
|
||||||
|
con;;skLet;;tcon_variable.foo;;string;;$file;;1;;4;;"";;100
|
||||||
|
"""
|
||||||
Reference in New Issue
Block a user