@@ -613,41 +613,49 @@ impl formatter {
613613 self.formatType(d.Kind)
614614 }
615615
616- fn varDecl(&self, mut d: &ast::Var) {
617- if len(d.Directives) != 0 {
618- self.directives(d.Directives)
619- }
620- if d.Binded {
621- self.write("cpp let ")
622- } else {
623- if d.Statically {
624- self.write("static ")
625- } else if d.Constant {
626- self.write("const ")
627- } else if d.Setter == nil || d.Setter.Id == token::Id.Eq {
628- self.write("let ")
616+ fn varDecl(&self, mut d: &ast::Var, head: bool) {
617+ if head {
618+ if len(d.Directives) != 0 {
619+ self.directives(d.Directives)
620+ }
621+ if d.Binded {
622+ self.write("cpp let ")
623+ } else {
624+ if d.Statically {
625+ self.write("static ")
626+ } else if d.Constant {
627+ self.write("const ")
628+ } else if d.Setter == nil || d.Setter.Id == token::Id.Eq {
629+ self.write("let ")
630+ }
631+ }
632+ if d.Mutable {
633+ self.write("mut ")
634+ }
635+ if d.Reference {
636+ self.write("&")
629637 }
630- }
631- if d.Mutable {
632- self.write("mut ")
633- }
634- if d.Reference {
635- self.write("&")
636638 }
637639 self.write(d.Ident)
640+ self.row = d.Token.Row
638641 if d.Kind != nil {
639642 self.write(": ")
640643 self.formatType(d.Kind)
644+ self.row = d.Kind.Token.Row
641645 }
642646 if d.Expr != nil {
643647 self.write(" ")
644648 self.write(d.Setter.Kind)
645649 self.write(" ")
646650 self.formatExpr(d.Expr)
651+ self.row = d.Expr.End.Row
647652 }
648653 }
649654
650655 fn getMax(self, lines: [][]byte, rows: []int): (max: int, n: int) {
656+ if len(lines) == 0 || len(rows) == 0 {
657+ ret 0, 0
658+ }
651659 if !self.isPopsRowCommentsByF(rows[0], -1) {
652660 ret
653661 }
@@ -741,6 +749,61 @@ impl formatter {
741749 ret
742750 }
743751
752+ fn varGroup(&self, mut root: &ast::Var) {
753+ mut lines := make([][]byte, 0, len(root.Group))
754+ mut rows := make([]int, 0, len(root.Group))
755+
756+ self.buf.Write(self.indent)!
757+ self.write("const (\n")
758+ self.addIndent()
759+ mut n := self.buf.Len()
760+ for (_, mut v) in root.Group {
761+ self.varDecl(v, false)
762+ mut line := cloneBuf(self.ubuf()[n:])
763+ lines = append(lines, line)
764+ rows = append(rows, self.row)
765+ self.setBuf(self.ubuf()[:n])
766+ }
767+ n = 0
768+ mut max := 0
769+ mut rowBreak := indexRowBrake(rows)
770+ for j, line in lines {
771+ isPub := isPub(root.Group[j].Ident) // Ignore private ones.
772+ row := rows[j]
773+ if j == rowBreak {
774+ self.write("\n")
775+ rowBreak = j + indexRowBrake(rows[j:])
776+ }
777+ self.writeCommentsExcept(row, isPub)
778+ if isPub {
779+ self.buf.Write(self.indent)!
780+ self.buf.Write(line)!
781+ }
782+ // Handle comments if same-line condition is not appear or
783+ // current row is not same as next row. So, we can move line comments
784+ // to last statement of inline statements.
785+ if len(lines)-j < 2 || rows[j] != rows[j+1] {
786+ if n == 0 {
787+ max, n = self.getMax(lines[j:rowBreak], rows[j:rowBreak])
788+ }
789+ if n > 0 {
790+ self.popRowCommentsByF(row, -1, isPub, fn(c: &comment) {
791+ self.write(strings::Repeat(" ", paddingAbs(max-utf8::RuneCount(line))+1))
792+ self.writeComment(c)
793+ })
794+ n--
795+ }
796+ }
797+ if isPub {
798+ self.write("\n")
799+ }
800+ }
801+ self.doneIndent()
802+ self.buf.Write(self.indent)!
803+ self.write(")")
804+ self.row = rows[len(rows)-1] + 1 // Add +1 offset for "\n)" suffix.
805+ }
806+
744807 fn groupDecls[T, Node](&self, mut nodes: []Node, mut &i: int, writer: fn(mut &d: T)) {
745808 if len(nodes) == 0 {
746809 ret
@@ -841,7 +904,11 @@ impl formatter {
841904 } else {
842905 self.writeCommentsExcept(d.Token.Row, true)
843906 }
844- self.varDecl(d)
907+ if d.Group != nil {
908+ self.varGroup(d)
909+ } else {
910+ self.varDecl(d, true)
911+ }
845912 ret
846913 | &ast::Func:
847914 mut d := node.Data.(&ast::Func)
@@ -1604,4 +1671,13 @@ fn cloneBuf(b: []byte): []byte {
16041671 mut rb := make([]byte, len(b))
16051672 copy(rb, b)
16061673 ret rb
1674+ }
1675+
1676+ fn indexRowBrake(rows: []int): int {
1677+ for i, r in rows {
1678+ if i > 0 && r-rows[i-1] > 1 {
1679+ ret i
1680+ }
1681+ }
1682+ ret len(rows)
16071683}
0 commit comments