Docs expand using parameters (#21076)

* Trying to fix by changing renderer

* add renderExpandUsing flag

This flag makes the renderer expand parameters that use using statement to have their full type

* Update docs

* Make comment better explain why checking for nkSym

* Fix nil access when macro/template has parameter with no type

* Fix nil access when node is not semmed yet
This commit is contained in:
Jake Leahy 2022-12-12 17:44:17 +11:00 • committed by GitHub
commit cbeefc877c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 52 additions and 10 deletions

View file

@ -24,7 +24,7 @@ type
TRenderFlag* = enum
renderNone, renderNoBody, renderNoComments, renderDocComments,
renderNoPragmas, renderIds, renderNoProcDefs, renderSyms, renderRunnableExamples,
renderIr
renderIr, renderExpandUsing
TRenderFlags* = set[TRenderFlag]
TRenderTok* = object
kind*: TokType
@ -434,6 +434,24 @@ proc lsons(g: TSrcGen; n: PNode, start: int = 0, theEnd: int = - 1): int =
result = 0
for i in start..n.len + theEnd: inc(result, lsub(g, n[i]))
proc origUsingType(n: PNode): PSym {.inline.} =
## Returns the type that a parameter references. Check with referencesUsing first
## to check `n` is actually referencing a using node
# If the node is untyped the typ field will be nil
if n[0].sym.typ != nil:
n[0].sym.typ.sym
else: nil
proc referencesUsing(n: PNode): bool =
## Returns true if n references a using statement.
## e.g. proc foo(x) # x doesn't have type or def value so it references a using
result = n.kind == nkIdentDefs and
# Sometimes the node might not have been semmed (e.g. doc0) and will be nkIdent instead
n[0].kind == nkSym and
# Templates/macros can have parameters with no type (But their orig type will be nil)
n.origUsingType != nil and
n[1].kind == nkEmpty and n[2].kind == nkEmpty
proc lsub(g: TSrcGen; n: PNode): int =
# computes the length of a tree
if isNil(n): return 0
@ -474,8 +492,11 @@ proc lsub(g: TSrcGen; n: PNode): int =
of nkDo: result = lsons(g, n) + len("do__:_")
of nkConstDef, nkIdentDefs:
result = lcomma(g, n, 0, - 3)
if n[^2].kind != nkEmpty: result += lsub(g, n[^2]) + 2
if n[^1].kind != nkEmpty: result += lsub(g, n[^1]) + 3
if n.referencesUsing:
result += lsub(g, newSymNode(n.origUsingType)) + 2
else:
if n[^2].kind != nkEmpty: result += lsub(g, n[^2]) + 2
if n[^1].kind != nkEmpty: result += lsub(g, n[^1]) + 3
of nkVarTuple:
if n[^1].kind == nkEmpty:
result = lcomma(g, n, 0, - 2) + len("()")
@ -1284,7 +1305,11 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext, fromStmtList = false) =
gcomma(g, n, 0, -3)
if n.len >= 2 and n[^2].kind != nkEmpty:
putWithSpace(g, tkColon, ":")
gsub(g, n, n.len - 2)
gsub(g, n[^2], c)
elif n.referencesUsing and renderExpandUsing in g.flags:
putWithSpace(g, tkColon, ":")
gsub(g, newSymNode(n.origUsingType), c)
if n.len >= 1 and n[^1].kind != nkEmpty:
put(g, tkSpaces, Space)
putWithSpace(g, tkEquals, "=")