@@ -464,20 +464,25 @@ impl formatter {
464464
465465 // Only parses last field.
466466 // Leading fields used to calculate maximum declaration length of group for padding.
467- fn field(&self, mut f: &field, mut max : int) {
467+ fn field(&self, mut f: &field, mut fieldMax: int, mut typeMax : int) {
468468 self.buf.Write(self.indent)!
469469 if f.f.Mutable {
470470 self.write("mut ")
471- max -= 4
471+ fieldMax -= 4
472472 }
473473 self.write(f.f.Ident)
474- max -= utf8::RuneCountStr(f.f.Ident)
474+ fieldMax -= utf8::RuneCountStr(f.f.Ident)
475475 self.write(": ")
476- if max > 0 {
477- self.write(strings::Repeat(" ", max ))
476+ if fieldMax > 0 {
477+ self.write(strings::Repeat(" ", fieldMax ))
478478 }
479+ n := len(self.ubuf())
479480 self.formatType(f.f.Kind)
481+ typeMax -= len(self.ubuf()) - n
480482 if len(f.expr) != 0 {
483+ if typeMax > 0 {
484+ self.write(strings::Repeat(" ", typeMax))
485+ }
481486 self.write(" = ")
482487 self.buf.Write(f.expr)!
483488 }
@@ -676,17 +681,20 @@ impl formatter {
676681
677682 // Reports whether all fields is actually written.
678683 fn fieldGroupDecls(&self, mut fields: []&field, mut &i: int): (wr: bool) {
679- const Cap = 1 << 4
680- mut lines := make([][]byte, 0, Cap)
681- mut rows := make([]int, 0, Cap)
684+ mut lines := make([][]byte, 0, len(fields))
685+ mut rows := make([]int, 0, len(fields))
682686
683687 mut start := i
684688 self.row = -1
685689 mut fieldMax := 0
690+ mut typeMax := 0
686691 mut n := self.buf.Len()
687692 for i < len(fields) {
688693 mut decl := fields[i]
689- if self.row != -1 && decl.token.Row-1 != self.row {
694+ row := self.row // save the first row data to after use
695+ // If the row is not first, compare current row with the previous one.
696+ // If the difference greatest than one row, end the iteration.
697+ if row != -1 && decl.token.Row-rows[(i-start)-1] > 1 {
690698 break
691699 }
692700 self.row = decl.token.Row
@@ -695,22 +703,34 @@ impl formatter {
695703 fm += len("mut ")
696704 }
697705 fm += utf8::RuneCountStr(decl.f.Ident)
698- if fm > fieldMax {
699- fieldMax = fm
706+ self.formatType(decl.f.Kind)
707+ tm := len(self.ubuf()) - n
708+ self.setBuf(self.ubuf()[:n])
709+ if row != -1 && decl.token.Row != self.row {
710+ break
700711 }
701712 if decl.f.Default != nil {
702713 self.formatExpr(decl.f.Default)
703714 decl.expr = cloneBuf(self.ubuf()[n:])
704715 self.setBuf(self.ubuf()[:n])
705716 }
706- rows = append(rows, self.row)
717+ if row != -1 && decl.token.Row != self.row {
718+ break
719+ }
720+ rows = append(rows, decl.token.Row)
721+ if fm > fieldMax {
722+ fieldMax = fm
723+ }
724+ if tm > typeMax {
725+ typeMax = tm
726+ }
707727 i++
708728 }
709729
710730 mut j := start
711731 for j < i; j++ {
712732 mut f := fields[j]
713- self.field(f, fieldMax)
733+ self.field(f, fieldMax, typeMax )
714734 mut line := cloneBuf(self.ubuf()[n:])
715735 lines = append(lines, line)
716736 self.setBuf(self.ubuf()[:n])
0 commit comments