Skip to content

Commit f12c27c

Browse files
authored
put triple-string literals on same line if it fits (fixes #94) (#115)
Instead of counting triple-string literals as monoliths, detect how long their first line is and use it to fit it on a single line, when possible ```nim const str = """x """ ``` becomes ```nim const str = """x """ ```
1 parent 0145a67 commit f12c27c

10 files changed

Lines changed: 135 additions & 24 deletions

File tree

src/nph.nim

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ type
3333
const
3434
Version = gorge("git describe --long --dirty --always --tags")
3535
Usage =
36-
"nph - Nim formatter " & Version &
37-
"""
36+
"nph - Nim formatter " & Version & """
3837
Usage:
3938
nph [options] nimfiles...
4039

src/phrenderer.nim

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,16 @@ type
107107
TOutput = TSrcGen | TSrcLen
108108

109109
proc `+`(a: LineLen, b: int): LineLen =
110-
(a[0] + b, a[1])
110+
if a.nl:
111+
a
112+
else:
113+
(a.len + b, false)
111114

112115
proc `+`(a, b: LineLen): LineLen =
113-
(a[0] + b[0], a[1] or b[1])
116+
if a.nl:
117+
a
118+
else:
119+
(a.len + b.len, b.nl)
114120

115121
proc isDocComment(s: string): bool =
116122
s.startsWith("##")
@@ -201,21 +207,22 @@ proc initSrcGen(g: var TSrcGen, config: ConfigRef) =
201207
g.pendingWhitespace = 0
202208
g.config = config
203209

204-
proc containsNL(s: string): bool =
210+
proc nllen(s: string): int =
205211
for i in 0 ..< s.len:
206-
case s[i]
207-
of '\r', '\n':
208-
return true
209-
else:
210-
discard
212+
if s[i] in {'\r', '\n'}:
213+
return i
211214

212-
result = false
215+
-1
213216

214217
proc addTok(g: var TSrcLen, kind: TokType, s: string) =
215218
if not g.nl:
216-
g.nl = containsNL(s)
217-
if not g.nl:
219+
let nllen = s.nllen
220+
if nllen >= 0:
221+
g.nl = true
222+
g.firstLen += nllen
223+
else:
218224
g.firstLen += s.len
225+
219226
g.tokens.add TRenderTok(kind: kind, length: s.len)
220227

221228
proc addTok(g: var TSrcGen, kind: TokType, s: string) =
@@ -583,9 +590,7 @@ template withSrcLen(g: TSrcGen, body: untyped): LineLen =
583590
let discount = g.pendingWhitespace
584591
body
585592
let post =
586-
if sl.nl:
587-
MaxLineLen + 1
588-
elif sl.firstLen > 0:
593+
if sl.firstLen > discount:
589594
sl.firstLen - discount
590595
else:
591596
0
@@ -648,11 +653,13 @@ proc nlsubImpl(g: TOutput, n: PNode, flags: SubFlags): (bool, LineLen) =
648653
else:
649654
let ll = lsub(g, n, flags)
650655
case n.kind
656+
of nkTripleStrLit:
657+
(true, (ll.len, false))
651658
of nkPar, nkClosure, nkCurly, nkBracket, nkTableConstr, nkStmtListExpr,
652659
nkTupleConstr:
653-
(true, (1, ll[1]))
660+
(true, (1, false))
654661
of nkPragma, nkPragmaExpr:
655-
(true, (2, ll[1]))
662+
(true, (2, false))
656663
else:
657664
(false, ll)
658665

@@ -662,13 +669,13 @@ proc nlsub(g: TOutput, n: PNode, flags: SubFlags = {}): LineLen =
662669

663670
proc fits(g: TSrcLen, x: LineLen): bool =
664671
# Line lengths are computed assuming no extra line breaks
665-
not x[1]
672+
not x.nl
666673

667674
proc fits(g: TSrcGen, x: LineLen): bool =
668-
x[0] <= MaxLineLen
675+
not x.nl and x.len <= MaxLineLen
669676

670677
proc overflows(g: TOutput, x: LineLen): bool =
671-
not fits(g, (g.lineLen + x[0], x[1]))
678+
not fits(g, (g.lineLen + x.len, x.nl))
672679

673680
proc putWithSpace(g: var TOutput, kind: TokType, s: string) =
674681
put(g, kind, s)
@@ -1713,7 +1720,6 @@ proc gsub(g: var TOutput, n: PNode, flags: SubFlags, extra: int) =
17131720
of nkInfix:
17141721
if n.len < 3:
17151722
put(g, tkOpr, "Too few children for nkInfix")
1716-
17171723
return
17181724

17191725
let flags = flags * {sfNoIndent, sfLongIndent}

tests/after/comments.nim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,3 +529,9 @@ command a: # comment
529529

530530
{.gcsafe.}: # comment
531531
discard
532+
533+
if xxxxxxxxx and (
534+
# comment
535+
y
536+
):
537+
discard

tests/after/comments.nim.nph.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,3 +2114,24 @@ sons:
21142114
- kind: "nkDiscardStmt"
21152115
sons:
21162116
- kind: "nkEmpty"
2117+
- kind: "nkIfStmt"
2118+
sons:
2119+
- kind: "nkElifBranch"
2120+
sons:
2121+
- kind: "nkInfix"
2122+
sons:
2123+
- kind: "nkIdent"
2124+
ident: "and"
2125+
- kind: "nkIdent"
2126+
ident: "xxxxxxxxx"
2127+
- kind: "nkPar"
2128+
sons:
2129+
- kind: "nkIdent"
2130+
prefix:
2131+
- "# comment"
2132+
ident: "y"
2133+
- kind: "nkStmtList"
2134+
sons:
2135+
- kind: "nkDiscardStmt"
2136+
sons:
2137+
- kind: "nkEmpty"

tests/after/exprs.nim

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,12 @@ let bbbbbbccccb = [
199199
int64 533, 444444444444444444, 555555555555555555, 6666666666666666, 6777777777777777,
200200
888888888888888,
201201
]
202+
203+
let xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx =
204+
"""xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
205+
206+
"""
207+
208+
let xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = """
209+
210+
"""

tests/after/exprs.nim.nph.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,3 +2049,21 @@ sons:
20492049
intVal: 6777777777777777
20502050
- kind: "nkInt64Lit"
20512051
intVal: 888888888888888
2052+
- kind: "nkLetSection"
2053+
sons:
2054+
- kind: "nkIdentDefs"
2055+
sons:
2056+
- kind: "nkIdent"
2057+
ident: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
2058+
- kind: "nkEmpty"
2059+
- kind: "nkTripleStrLit"
2060+
strVal: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\u000A\u000A"
2061+
- kind: "nkLetSection"
2062+
sons:
2063+
- kind: "nkIdentDefs"
2064+
sons:
2065+
- kind: "nkIdent"
2066+
ident: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
2067+
- kind: "nkEmpty"
2068+
- kind: "nkTripleStrLit"
2069+
strVal: "\u000A"

tests/before/comments.nim

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,4 +525,9 @@ command a:# comment
525525

526526
{.gcsafe.}: # comment
527527
discard
528-
528+
529+
if xxxxxxxxx and (
530+
# comment
531+
y
532+
):
533+
discard

tests/before/comments.nim.nph.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,3 +2110,24 @@ sons:
21102110
- kind: "nkDiscardStmt"
21112111
sons:
21122112
- kind: "nkEmpty"
2113+
- kind: "nkIfStmt"
2114+
sons:
2115+
- kind: "nkElifBranch"
2116+
sons:
2117+
- kind: "nkInfix"
2118+
sons:
2119+
- kind: "nkIdent"
2120+
ident: "and"
2121+
- kind: "nkIdent"
2122+
ident: "xxxxxxxxx"
2123+
- kind: "nkPar"
2124+
sons:
2125+
- kind: "nkIdent"
2126+
prefix:
2127+
- "# comment"
2128+
ident: "y"
2129+
- kind: "nkStmtList"
2130+
sons:
2131+
- kind: "nkDiscardStmt"
2132+
sons:
2133+
- kind: "nkEmpty"

tests/before/exprs.nim

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,12 @@ let aaaaaaaaaaaaaaaaa = [f(aaaaaaaaaaaa, bbbbbbbbbbbbbbbbbb, ccccccccccccccccccc
128128

129129
let bbbbbbb = ["aaaaaaaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbbbbbbbbbbbb", "ccccccccccccccccccccccccccccc", "dddddddddddddddddddddd"]
130130

131-
let bbbbbbccccb = [int64 533, 444444444444444444, 555555555555555555, 6666666666666666, 6777777777777777, 888888888888888]
131+
let bbbbbbccccb = [int64 533, 444444444444444444, 555555555555555555, 6666666666666666, 6777777777777777, 888888888888888]
132+
133+
let xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = """xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
134+
135+
"""
136+
137+
let xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = """
138+
139+
"""

tests/before/exprs.nim.nph.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,3 +2049,21 @@ sons:
20492049
intVal: 6777777777777777
20502050
- kind: "nkInt64Lit"
20512051
intVal: 888888888888888
2052+
- kind: "nkLetSection"
2053+
sons:
2054+
- kind: "nkIdentDefs"
2055+
sons:
2056+
- kind: "nkIdent"
2057+
ident: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
2058+
- kind: "nkEmpty"
2059+
- kind: "nkTripleStrLit"
2060+
strVal: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\u000A\u000A"
2061+
- kind: "nkLetSection"
2062+
sons:
2063+
- kind: "nkIdentDefs"
2064+
sons:
2065+
- kind: "nkIdent"
2066+
ident: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
2067+
- kind: "nkEmpty"
2068+
- kind: "nkTripleStrLit"
2069+
strVal: "\u000A"

0 commit comments

Comments
 (0)