@@ -437,7 +437,7 @@ proc lsub(g: TSrcGen; n: PNode): int =
437437 of nkTableConstr:
438438 result = if n.len > 0 : lcomma (g, n) + 2 else : len (" {:}" )
439439 of nkClosedSymChoice, nkOpenSymChoice:
440- result = lsons (g, n) + len ( " () " ) + n.len - 1
440+ if n.len > 0 : result += lsub (g, n[ 0 ])
441441 of nkTupleTy: result = lcomma (g, n) + len (" tuple[]" )
442442 of nkTupleClassTy: result = len (" tuple" )
443443 of nkDotExpr: result = lsons (g, n) + 1
@@ -529,10 +529,12 @@ proc lsub(g: TSrcGen; n: PNode): int =
529529 if n[0 ].kind != nkEmpty: result = result + lsub (g, n[0 ]) + 2
530530 of nkExceptBranch:
531531 result = lcomma (g, n, 0 , - 2 ) + lsub (g, lastSon (n)) + len (" except_:_" )
532+ of nkObjectTy:
533+ result = len (" object_" )
532534 else : result = MaxLineLen + 1
533535
534536proc fits (g: TSrcGen , x: int ): bool =
535- result = x + g.lineLen <= MaxLineLen
537+ result = x <= MaxLineLen
536538
537539type
538540 TSubFlag = enum
@@ -572,7 +574,7 @@ proc gcommaAux(g: var TSrcGen, n: PNode, ind: int, start: int = 0,
572574 for i in start.. n.len + theEnd:
573575 var c = i < n.len + theEnd
574576 var sublen = lsub (g, n[i]) + ord (c)
575- if not fits (g, sublen) and (ind + sublen < MaxLineLen ): optNL (g, ind)
577+ if not fits (g, g.lineLen + sublen) and (ind + sublen < MaxLineLen ): optNL (g, ind)
576578 let oldLen = g.tokens.len
577579 gsub (g, n[i])
578580 if c:
@@ -1139,10 +1141,12 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) =
11391141 putWithSpace (g, tkColon, " :" )
11401142 gsub (g, n, 1 )
11411143 of nkInfix:
1144+ let oldLineLen = g.lineLen # we cache this because lineLen gets updated below
11421145 infixArgument (g, n, 1 )
11431146 put (g, tkSpaces, Space )
11441147 gsub (g, n, 0 ) # binary operator
1145- if n.len == 3 and not fits (g, lsub (g, n[2 ]) + lsub (g, n[0 ]) + 1 ):
1148+ # eg: `n1 == n2` decompses as following sum:
1149+ if n.len == 3 and not fits (g, oldLineLen + lsub (g, n[1 ]) + lsub (g, n[2 ]) + lsub (g, n[0 ]) + len (" " )):
11461150 optNL (g, g.indent + longIndentWid)
11471151 else :
11481152 put (g, tkSpaces, Space )
0 commit comments