render post expr blocks better (#20871)

* render post expr blocks

* remove pointless diff

* fix PR split mistake
This commit is contained in:
metagn 2022-11-22 22:40:05 +03:00 • committed by GitHub
commit 5adfaa2a92
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 93 additions and 23 deletions

View file

@ -931,6 +931,7 @@ proc doParamsAux(g: var TSrcGen, params: PNode) =
put(g, tkParRi, ")") put(g, tkParRi, ")")
if params.len > 0 and params[0].kind != nkEmpty: if params.len > 0 and params[0].kind != nkEmpty:
put(g, tkSpaces, Space)
putWithSpace(g, tkOpr, "->") putWithSpace(g, tkOpr, "->")
gsub(g, params[0]) gsub(g, params[0])
@ -1001,6 +1002,30 @@ proc infixArgument(g: var TSrcGen, n: PNode, i: int) =
if needsParenthesis: if needsParenthesis:
put(g, tkParRi, ")") put(g, tkParRi, ")")
const postExprBlocks = {nkStmtList, nkStmtListExpr,
nkOfBranch, nkElifBranch, nkElse,
nkExceptBranch, nkFinally, nkDo}
proc postStatements(g: var TSrcGen, n: PNode, i: int, fromStmtList: bool) =
var i = i
if n[i].kind in {nkStmtList, nkStmtListExpr}:
if fromStmtList:
put(g, tkColon, ":")
else:
put(g, tkSpaces, Space)
put(g, tkDo, "do")
put(g, tkColon, ":")
gsub(g, n, i)
i.inc
for j in i ..< n.len:
if n[j].kind == nkDo:
optNL(g)
elif n[j].kind in {nkStmtList, nkStmtListExpr}:
optNL(g)
put(g, tkDo, "do")
put(g, tkColon, ":")
gsub(g, n, j)
proc isCustomLit(n: PNode): bool = proc isCustomLit(n: PNode): bool =
if n.len == 2 and n[0].kind == nkRStrLit: if n.len == 2 and n[0].kind == nkRStrLit:
let ident = n[1].getPIdent let ident = n[1].getPIdent
@ -1035,27 +1060,15 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext, fromStmtList = false) =
of nkCharLit: put(g, tkCharLit, atom(g, n)) of nkCharLit: put(g, tkCharLit, atom(g, n))
of nkNilLit: put(g, tkNil, atom(g, n)) # complex expressions of nkNilLit: put(g, tkNil, atom(g, n)) # complex expressions
of nkCall, nkConv, nkDotCall, nkPattern, nkObjConstr: of nkCall, nkConv, nkDotCall, nkPattern, nkObjConstr:
if n.len > 1 and n.lastSon.kind in {nkStmtList, nkStmtListExpr}: if n.len > 1 and n.lastSon.kind in postExprBlocks:
accentedName(g, n[0]) accentedName(g, n[0])
var i = 1 var i = 1
while i < n.len and n[i].kind notin {nkStmtList, nkStmtListExpr}: i.inc while i < n.len and n[i].kind notin postExprBlocks: i.inc
if i > 1: if i > 1:
put(g, tkParLe, "(") put(g, tkParLe, "(")
gcomma(g, n, 1, i - 1 - n.len) gcomma(g, n, 1, i - 1 - n.len)
put(g, tkParRi, ")") put(g, tkParRi, ")")
if fromStmtList: postStatements(g, n, i, fromStmtList)
put(g, tkColon, ":")
else:
put(g, tkSpaces, Space)
put(g, tkDo, "do")
put(g, tkColon, ":")
gsub(g, n, i)
i.inc
for j in i ..< n.len:
optNL(g)
put(g, tkDo, "do")
put(g, tkColon, ":")
gsub(g, n, j)
elif n.len >= 1: elif n.len >= 1:
case bracketKind(g, n[0]) case bracketKind(g, n[0])
of bkBracket: of bkBracket:
@ -1155,14 +1168,12 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext, fromStmtList = false) =
of nkCommand: of nkCommand:
accentedName(g, n[0]) accentedName(g, n[0])
put(g, tkSpaces, Space) put(g, tkSpaces, Space)
if n[^1].kind == nkStmtList: if n.len > 1 and n.lastSon.kind in postExprBlocks:
for i, child in n: var i = 1
if i > 1 and i < n.len - 1: while i < n.len and n[i].kind notin postExprBlocks: i.inc
put(g, tkComma, ",") if i > 1:
elif i == n.len - 1: gcomma(g, n, 1, i - 1 - n.len)
put(g, tkColon, ":") postStatements(g, n, i, fromStmtList)
if i > 0:
gsub(g, child)
else: else:
gcomma(g, n, 1) gcomma(g, n, 1)
of nkExprEqExpr, nkAsgn, nkFastAsgn: of nkExprEqExpr, nkAsgn, nkFastAsgn:
@ -1301,6 +1312,10 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext, fromStmtList = false) =
else: else:
put(g, tkSpaces, Space) put(g, tkSpaces, Space)
infixArgument(g, n, 2) infixArgument(g, n, 2)
if n.len > 3 and n.lastSon.kind in postExprBlocks:
var i = 3
while i < n.len and n[i].kind notin postExprBlocks: i.inc
postStatements(g, n, i, fromStmtList)
of nkPrefix: of nkPrefix:
gsub(g, n, 0) gsub(g, n, 0)
if n.len > 1: if n.len > 1:
@ -1317,6 +1332,10 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext, fromStmtList = false) =
put(g, tkParRi, ")") put(g, tkParRi, ")")
else: else:
gsub(g, n[1]) gsub(g, n[1])
if n.len > 2 and n.lastSon.kind in postExprBlocks:
var i = 2
while i < n.len and n[i].kind notin postExprBlocks: i.inc
postStatements(g, n, i, fromStmtList)
of nkPostfix: of nkPostfix:
gsub(g, n, 1) gsub(g, n, 1)
gsub(g, n, 0) gsub(g, n, 0)

View file

@ -271,5 +271,56 @@ func fn2(): int =
## comment ## comment
result = 1""" result = 1"""
block: # block calls
let a = deb:
foo(a, b, (c, d)):
e
f
do: g
of h: i
elif j: k
except m: n
do () -> u: v
finally: o
a + b:
c
d
do:
e
f
else: g
*a: b
do: c
doAssert a == """foo(a, b, (c, d)):
e
f
do:
g
of h:
i
elif j:
k
except m:
n
do -> u:
v
finally:
o
a + b:
c
d
do:
e
f
else:
g
*a:
b
do:
c"""
static: main() static: main()
main() main()