Skip to content

Commit f909e5b

Browse files
authored
Indent stacked dot calls (#122)
#32 introduced call chaining, but when called outside of a `let`, the lack of indent makes it hard to tell where the dot expression ends and where the next expression starts To make the expression boundaries more visible, apply an indent to the dotted calls, like so: ```nim # old aaaa .f() .g() # new aaaa .f() .g() ```
1 parent a7c0d5c commit f909e5b

6 files changed

Lines changed: 670 additions & 12 deletions

File tree

src/phrenderer.nim

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,7 +1503,7 @@ proc gsub(g: var TOutput, n: PNode, flags: SubFlags, extra: int) =
15031503
false
15041504
ind = condIndent(g, doPars)
15051505

1506-
gsub(g, n[0])
1506+
gsub(g, n[0], flags * {sfNoIndent, sfLongIndent})
15071507

15081508
var i = 1
15091509
while i < n.len and n[i].kind notin postExprBlocks:
@@ -1522,8 +1522,27 @@ proc gsub(g: var TOutput, n: PNode, flags: SubFlags, extra: int) =
15221522
optNL(g)
15231523
put(g, tkParRi, $tkParRi)
15241524
elif n.len >= 1:
1525-
gsub(g, n[0], flags = (flags * {sfStackDot}) + {sfStackDotInCall})
1525+
let
1526+
nameFlags =
1527+
(flags * {sfStackDot, sfNoIndent, sfLongIndent}) + {sfStackDotInCall}
1528+
stackDot =
1529+
if n[0].kind == nkDotExpr:
1530+
let dot = n[0]
1531+
sfStackDot in flags or (
1532+
g.overflows(lsub(g, dot[0]) + lsub(g, dot[1]) + 1) and
1533+
isStackedCall(dot[0], true)
1534+
)
1535+
else:
1536+
false
1537+
1538+
gsub(g, n[0], nameFlags)
1539+
1540+
# The way stacked calls work, the `.name` part will be indented when on a
1541+
# new line by the nkDotExpr handler - when that happens, we must also
1542+
# indent the list of parameters
1543+
let ind = g.condIndent(stackDot, flagIndent(flags))
15261544
glist(g, n, tkParLe, start = 1, flags = {lfLongSepAtEnd})
1545+
g.dedent(ind)
15271546
else:
15281547
put(g, tkParLe, "(")
15291548
put(g, tkParRi, ")")
@@ -1647,12 +1666,13 @@ proc gsub(g: var TOutput, n: PNode, flags: SubFlags, extra: int) =
16471666
)
16481667
stackNL = stackDot and sfStackDotInCall in flags
16491668
subFlags =
1650-
{sfParDo} + (
1669+
flags * {sfNoIndent, sfLongIndent} + {sfParDo} + (
16511670
if stackDot:
16521671
{sfStackDot}
16531672
else:
16541673
{}
16551674
)
1675+
wid = flagIndent(flags)
16561676

16571677
gsub(g, n[0], flags = subFlags)
16581678

@@ -1664,8 +1684,12 @@ proc gsub(g: var TOutput, n: PNode, flags: SubFlags, extra: int) =
16641684
gmids(g, n)
16651685
elif stackNL:
16661686
optNL(g)
1687+
1688+
# Careful, this indent must be matched for the parameter list in nkCall!
1689+
g.optIndent(wid)
16671690
put(g, tkDot, ".")
1668-
gsub(g, n[1])
1691+
gsub(g, n[1], {sfNoIndent})
1692+
g.dedent(wid)
16691693
of nkBind:
16701694
putWithSpace(g, tkBind, "bind")
16711695
gsub(g, n[0])

tests/after/comments.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -445,21 +445,21 @@ echo dotexpr.dot # after dotexpr in command
445445

446446
dotexpr
447447
# between dotexpr and dotonnewline
448-
.dotonnewline
448+
.dotonnewline
449449

450450
if true:
451451
echo dotexpr.dot # after dotexpr in command ind
452452
# between two dotepxrs ind
453453
dotexpr
454454
# between dotexpr and dotonnewline ind
455-
.dotonnewline
455+
.dotonnewline
456456

457457
block:
458458
f.x
459459
# comment between the dots
460-
.z()
460+
.z()
461461
# also after call
462-
.d() # far eol of dotexpr
462+
.d() # far eol of dotexpr
463463

464464
# after dotexpr ind
465465

tests/after/exprs.nim

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ discard a .. -b
142142
discard -1 .. -2
143143

144144
aaaaaaa.bbbbbbbbb
145-
.longdotcall().ccccccccc.dddddddddd.eeeeeeeee
146-
.sdcsd(a0000000, b000000, c000000).fffffff.ggggggg.hhhhhhhh.csdcsdcs.sdcsdcsd.csdcsdcsdcsd.csdcsdcs.dcsdcsdcsdcs.sdc
145+
.longdotcall().ccccccccc.dddddddddd.eeeeeeeee
146+
.sdcsd(a0000000, b000000, c000000).fffffff.ggggggg.hhhhhhhh.csdcsdcs.sdcsdcsd.csdcsdcsdcsd.csdcsdcs.dcsdcsdcsdcs.sdc
147147

148148
mynums = myNums
149149
.replace1("one", "o1ne")
@@ -154,8 +154,8 @@ mynums = myNums
154154
.replace6("six", "s6ix")
155155
.replace7("seven", "s7even")
156156
aaaaaaaaaaaaaaaaaaaaa.bbbbbbbbbbbbbbbbbbbbbb
157-
.ccccccccccccccccccccccccccc().ccccccccccccccccccccccccc
158-
.ddddddddddddddddd() # no newline before previous dotexpr
157+
.ccccccccccccccccccccccccccc().ccccccccccccccccccccccccc
158+
.ddddddddddddddddd() # no newline before previous dotexpr
159159

160160
let xxxxxxxxx = block:
161161
f()
@@ -244,3 +244,53 @@ let xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx =
244244
let xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = """
245245
246246
"""
247+
248+
csasacdsa
249+
.casdcasdcsadc(
250+
aaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccccc
251+
)
252+
.cccccccccc(eeeeeeeeeeeeeeeeeeeeee, bbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccc)
253+
.ddddddd()
254+
.eeeeeeeee()
255+
256+
if csasacdsa
257+
.casdcasdcsadc(
258+
aaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccccc
259+
)
260+
.cccccccccc(eeeeeeeeeeeeeeeeeeeeee, bbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccc)
261+
.ddddddd()
262+
.eeeeeeeee():
263+
discard
264+
265+
if csasacdsa
266+
.casdcasdcsadc(
267+
aaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccccc
268+
)
269+
.cccccccccc(eeeeeeeeeeeeeeeeeeeeee, bbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccc)
270+
.ddddddd()
271+
.eeeeeeeee() and
272+
csasacdsa
273+
.casdcasdcsadc(
274+
aaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccccc
275+
)
276+
.cccccccccc(eeeeeeeeeeeeeeeeeeeeee, bbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccc)
277+
.ddddddd()
278+
.eeeeeeeee():
279+
discard
280+
281+
let x = csasacdsa
282+
.casdcasdcsadc(
283+
aaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccccc
284+
)
285+
.cccccccccc(eeeeeeeeeeeeeeeeeeeeee, bbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccc)
286+
.ddddddd()
287+
.eeeeeeeee()
288+
289+
let x = csasacdsa
290+
.casdcasdcsadc(
291+
aaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbb, ccccccccccccccccccccccc
292+
)
293+
.cccccccccc(eeeeeeeeeeeeeeeeeeeeee, bbbbbbbbbbbbbbbbbbbb, cccccccccccccccccccc)
294+
.ddddddd()
295+
.eeeeeeeee().valueOr:
296+
discard

0 commit comments

Comments
 (0)