nimpretty: fixes #11616

This commit is contained in:
Araq
2019-07-18 12:13:22 +02:00
parent 7deb49e992
commit 6d8913ee14
3 changed files with 99 additions and 6 deletions

View File

@@ -1203,6 +1203,13 @@ proc parseFor(p: var TParser): PNode =
colcom(p, result)
addSon(result, parseStmt(p))
template nimprettyDontTouch(body) =
when defined(nimpretty):
inc p.em.keepIndents
body
when defined(nimpretty):
dec p.em.keepIndents
proc parseExpr(p: var TParser): PNode =
#| expr = (blockExpr
#| | ifExpr
@@ -1212,12 +1219,26 @@ proc parseExpr(p: var TParser): PNode =
#| | tryExpr)
#| / simpleExpr
case p.tok.tokType:
of tkBlock: result = parseBlock(p)
of tkIf: result = parseIfExpr(p, nkIfExpr)
of tkFor: result = parseFor(p)
of tkWhen: result = parseIfExpr(p, nkWhenExpr)
of tkCase: result = parseCase(p)
of tkTry: result = parseTry(p, isExpr=true)
of tkBlock:
nimprettyDontTouch:
result = parseBlock(p)
of tkIf:
nimprettyDontTouch:
result = parseIfExpr(p, nkIfExpr)
of tkFor:
nimprettyDontTouch:
result = parseFor(p)
of tkWhen:
nimprettyDontTouch:
result = parseIfExpr(p, nkWhenExpr)
of tkCase:
# Currently we think nimpretty is good enough with case expressions,
# so it is allowed to touch them:
#nimprettyDontTouch:
result = parseCase(p)
of tkTry:
nimprettyDontTouch:
result = parseTry(p, isExpr=true)
else: result = simpleExpr(p)
proc parseEnum(p: var TParser): PNode

View File

@@ -786,3 +786,39 @@ if true:
"foo1", "bar1", "foo2", "bar2", "foo3", "bar3", "foo4", "bar4", "foo5", "bar5", "foo6", "bar6", "foo7",
"zzz", "ggg", "ddd",
]
const b = true
let fooB =
if true:
if b: 7 else: 8
else: ord(b)
let foo = if cond:
if b: T else: F
else: b
let a =
[[aaadsfas, bbb],
[ccc, ddd]]
let b = [
[aaa, bbb],
[ccc, ddd]
]
# bug #11616
proc newRecordGen(ctx: Context; typ: TypRef): PNode =
result = nkTypeDef.t(
newId(typ.optSym.name, true, pragmas = [id(if typ.isUnion: "cUnion"
else: "cStruct")]),
empty(),
nkObjectTy.t(
empty(),
empty(),
nkRecList.t(
typ.recFields.map(newRecFieldGen))))
proc f =
# doesn't break the code, but leaving indentation as is would be nice.
let x = if true: callingProcWhatever()
else: callingADifferentProc()

View File

@@ -799,3 +799,39 @@ if true:
"bar5", "foo6", "bar6", "foo7",
"zzz", "ggg", "ddd",
]
const b = true
let fooB =
if true:
if b: 7 else: 8
else: ord(b)
let foo = if cond:
if b: T else: F
else: b
let a =
[[aaadsfas, bbb],
[ccc, ddd]]
let b = [
[aaa, bbb],
[ccc, ddd]
]
# bug #11616
proc newRecordGen(ctx: Context; typ: TypRef): PNode =
result = nkTypeDef.t(
newId(typ.optSym.name, true, pragmas = [id(if typ.isUnion: "cUnion"
else: "cStruct")]),
empty(),
nkObjectTy.t(
empty(),
empty(),
nkRecList.t(
typ.recFields.map(newRecFieldGen))))
proc f =
# doesn't break the code, but leaving indentation as is would be nice.
let x = if true: callingProcWhatever()
else: callingADifferentProc()