@@ -107,10 +107,16 @@ type
107107 TOutput = TSrcGen | TSrcLen
108108
109109proc `+` (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
112115proc `+` (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
115121proc 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
214217proc 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
221228proc 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
663670proc 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
667674proc fits (g: TSrcGen , x: LineLen ): bool =
668- x[ 0 ] <= MaxLineLen
675+ not x.nl and x.len <= MaxLineLen
669676
670677proc 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
673680proc 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}
0 commit comments