@@ -192,10 +192,10 @@ impl formatter {
192192 }
193193 }
194194
195- fn popRowCommentsByF(&self, row: int, col: int, buf: bool, f: fn(&comment)) {
195+ fn popRowCommentsByF(&self, row: int, col: int, buf: bool, f: fn(mut &comment)) {
196196 mut i := 0
197197 for i < len(self.cm.buf) {
198- c := self.cm.buf[i]
198+ mut c := self.cm.buf[i]
199199 if c.row == row && (col == -1 || c.col < col) {
200200 if buf {
201201 f(c)
@@ -772,17 +772,17 @@ impl formatter {
772772 }
773773 }
774774
775- fn getMax(self, lines: [][]byte, rows: []int): (max: int, n: int) {
775+ fn getMax(self, lines: [][]byte, rows: []int, comments: [][]&comment ): (max: int, n: int) {
776776 if len(lines) == 0 || len(rows) == 0 {
777777 ret 0, 0
778778 }
779- if !self.isPopsRowCommentsByF(rows [0], -1) {
779+ if len(comments [0]) == 0 {
780780 ret
781781 }
782782 max = utf8::RuneCount(lines[0])
783783 n = 1
784784 for n < len(lines); n++ {
785- if rows[n-1] == rows[n] || !self.isPopsRowCommentsByF(rows [n], -1) {
785+ if rows[n-1] == rows[n] || len(comments [n]) == 0 {
786786 break
787787 }
788788 diff := utf8::RuneCount(lines[n])
@@ -797,6 +797,7 @@ impl formatter {
797797 fn fieldGroupDecls(&self, mut fields: []&field, mut &i: int): (wr: bool) {
798798 mut lines := make([][]byte, 0, len(fields))
799799 mut rows := make([]int, 0, len(fields))
800+ mut comments := make([][]&comment, 0, len(fields))
800801
801802 mut start := i
802803 self.row = -1
@@ -831,6 +832,12 @@ impl formatter {
831832 if row != -1 && decl.token.Row != self.row {
832833 break
833834 }
835+ mut rowComments := make([]&comment, 0)
836+ mut &_rowComments := rowComments // for closure
837+ self.popRowCommentsByF(decl.token.Row, -1, true, fn(mut c: &comment) {
838+ unsafe { _rowComments = append(_rowComments, c) }
839+ })
840+ comments = append(comments, rowComments)
834841 rows = append(rows, decl.token.Row)
835842 if fm > fieldMax {
836843 fieldMax = fm
@@ -866,13 +873,15 @@ impl formatter {
866873 // to last statement of inline statements.
867874 if len(lines)-k < 2 || rows[k] != rows[k+1] {
868875 if n == 0 {
869- max, n = self.getMax(lines[k:], rows[k:])
876+ max, n = self.getMax(lines[k:], rows[k:], comments[k:] )
870877 }
871878 if n > 0 {
872- self.popRowCommentsByF(row, -1, isPub, fn(c: &comment) {
873- self.write(strings::Repeat(" ", paddingAbs(max-utf8::RuneCount(line))+1))
874- self.writeComment(c)
875- })
879+ if isPub {
880+ for _, c in comments[k] {
881+ self.write(strings::Repeat(" ", paddingAbs(max-utf8::RuneCount(line))+1))
882+ self.writeComment(c)
883+ }
884+ }
876885 n--
877886 }
878887 }
@@ -889,6 +898,7 @@ impl formatter {
889898 mut lines := make([][]byte, 0, len(vars[i:]))
890899 mut rows := make([]int, 0, len(vars[i:]))
891900 mut counts := make([][2]int, 0, len(vars[i:]))
901+ mut comments := make([][]&comment, 0, len(vars[i:]))
892902
893903 start := i
894904 mut n := self.buf.Len()
@@ -1027,6 +1037,21 @@ impl formatter {
10271037 rows = append(rows, self.row)
10281038 self.setBuf(self.ubuf()[:n])
10291039 }
1040+
1041+ // Collects comments separately.
1042+ // For line collection, they are needed.
1043+ n = 0
1044+ z = start
1045+ for z < i; z++ {
1046+ mut rowComments := make([]&comment, 0)
1047+ mut &_rowComments := rowComments // for closure
1048+ self.popRowCommentsByF(rows[n], -1, true, fn(mut c: &comment) {
1049+ unsafe { _rowComments = append(_rowComments, c) }
1050+ })
1051+ comments = append(comments, rowComments)
1052+ n++
1053+ }
1054+
10301055 n = 0
10311056 mut max := 0
10321057 mut rowBreak := indexRowBrake(rows)
@@ -1047,13 +1072,15 @@ impl formatter {
10471072 // to last statement of inline statements.
10481073 if len(lines)-j < 2 || rows[j] != rows[j+1] {
10491074 if n == 0 {
1050- max, n = self.getMax(lines[j:rowBreak], rows[j:rowBreak])
1075+ max, n = self.getMax(lines[j:rowBreak], rows[j:rowBreak], comments[j:rowBreak] )
10511076 }
10521077 if n > 0 {
1053- self.popRowCommentsByF(row, -1, isPub, fn(c: &comment) {
1054- self.write(strings::Repeat(" ", paddingAbs(max-utf8::RuneCount(line))+1))
1055- self.writeComment(c)
1056- })
1078+ if isPub {
1079+ for _, c in comments[j] {
1080+ self.write(strings::Repeat(" ", paddingAbs(max-utf8::RuneCount(line))+1))
1081+ self.writeComment(c)
1082+ }
1083+ }
10571084 n--
10581085 }
10591086 }
@@ -1117,6 +1144,7 @@ impl formatter {
11171144 const Cap = 1 << 4
11181145 mut lines := make([][]byte, 0, Cap)
11191146 mut rows := make([]int, 0, Cap)
1147+ mut comments := make([][]&comment, 0, Cap)
11201148
11211149 all := i == -1
11221150 if all {
@@ -1153,6 +1181,12 @@ impl formatter {
11531181 writer(decl)
11541182 mut line := cloneBuf(self.ubuf()[n:])
11551183 lines = append(lines, line)
1184+ mut rowComments := make([]&comment, 0)
1185+ mut &_rowComments := rowComments // for closure
1186+ self.popRowCommentsByF(row, -1, true, fn(mut c: &comment) {
1187+ unsafe { _rowComments = append(_rowComments, c) }
1188+ })
1189+ comments = append(comments, rowComments)
11561190 rows = append(rows, self.row)
11571191 self.setBuf(self.ubuf()[:n])
11581192 i++
@@ -1168,9 +1202,7 @@ impl formatter {
11681202 n = 0
11691203 mut max := 0
11701204 for j, line in lines {
1171- row := rows[j]
11721205 wr := len(line) > 0 // Do not write empty lines.
1173- self.writeCommentsExcept(row, wr)
11741206 if wr {
11751207 self.buf.Write(line)!
11761208 }
@@ -1179,13 +1211,15 @@ impl formatter {
11791211 // to last statement of inline statements.
11801212 if len(lines)-j < 2 || rows[j] != rows[j+1] {
11811213 if n == 0 {
1182- max, n = self.getMax(lines[j:], rows[j:])
1214+ max, n = self.getMax(lines[j:], rows[j:], comments[j:] )
11831215 }
11841216 if n > 0 {
1185- self.popRowCommentsByF(row, -1, wr, fn(c: &comment) {
1186- self.write(strings::Repeat(" ", paddingAbs(max-utf8::RuneCount(line))+1))
1187- self.writeComment(c)
1188- })
1217+ if wr {
1218+ for _, c in comments[j] {
1219+ self.write(strings::Repeat(" ", paddingAbs(max-utf8::RuneCount(line))+1))
1220+ self.writeComment(c)
1221+ }
1222+ }
11891223 n--
11901224 }
11911225 }
0 commit comments