diff --git a/src/format.jule b/src/format.jule index d6eb034..f4c681e 100644 --- a/src/format.jule +++ b/src/format.jule @@ -188,6 +188,7 @@ impl Formatter { } fn enum_item(&self, mut &item: &ast::EnumItemDecl) { + self.write(self.indent) self.write(item.ident) if !item.auto_expr() { self.write(" = ") @@ -280,6 +281,7 @@ impl Formatter { // Only parses last field. // Leading fields used to calculate maximum declaration length of group for padding. fn field(&self, mut f: &ast::FieldDecl, mut max: int) { + self.write(self.indent) if f.public { self.write("pub ") max -= 4 @@ -313,7 +315,7 @@ impl Formatter { for i < flds.len { let old = self.i let f = flds[i] - self.group_decls[&Field, &Field](flds, i) + self.field_group_decls(flds, i) if old != i { if i < flds.len { self.write("\n") @@ -374,6 +376,7 @@ impl Formatter { } fn type_alias_decl(&self, mut d: &ast::TypeAliasDecl) { + self.write(self.indent) if d.public { self.write("pub ") } @@ -387,6 +390,7 @@ impl Formatter { } fn var_decl(&self, mut d: &ast::VarDecl) { + self.write(self.indent) if d.directives.len != 0 { self.directives(d.directives) self.write("\n") @@ -422,22 +426,75 @@ impl Formatter { } } + fn field_group_decls(&self, mut fields: []&Field, mut &i: int) { + let mut start = i + let mut row = -1 + let mut max = 0 + let mut field_max = 0 + let n = self.buf.len + _ = n, start, max + loop: + for i < fields.len { + let mut decl = fields[i] + if row != -1 && decl.token.row - 1 != row { + break loop + } + row = decl.token.row + let mut fm = 0 + if decl.f.public { + fm += "pub ".len + } + if decl.f.mutable { + fm += "mut ".len + } + fm += decl.f.ident.len + if fm > field_max { + field_max = fm + } + i++ + } + + const CAP = 1 << 4 + let mut lines = make([]str, 0, CAP) + let mut j = start + for j < i; j++ { + self.field(fields[j].f, field_max) + lines = append(lines, self.buf[n:]) + let diff = self.buf.len - n + if max < diff { + max = diff + } + self.buf = self.buf[:self.buf.len - diff] + } + + for _, line in lines { + row = fields[start].token.row + self.write_comments_except(row) + self.write(line) + let c = self.cm.first(row) + if c != nil && c.row == row { + self.cm.drop_first() + self.write(strings::repeat(" ", max - line.len + 1)) + self.write(c.txt) + } + self.write("\n") + start++ + } + } + fn group_decls[T, Node](&self, mut nodes: []Node, mut &i: int) { const CAP = 1 << 4 let mut lines = make([]str, 0, CAP) - let mut comments = make([]&Comment, 0, CAP) let mut start = i let mut row = -1 let mut max = 0 - let mut field_max = 0 let n = self.buf.len loop: for i < nodes.len { let mut decl: T = nil match type Node { - | &ast::EnumItemDecl - | &Field: + | &ast::EnumItemDecl: decl = nodes[i] | ast::Node: let node = nodes[i] @@ -452,13 +509,6 @@ impl Formatter { break loop } row = decl.token.row - self.write_comments_except(row) - if self.cm.map.len > 0 && self.cm.map[0].row == row { - comments = append(comments, self.cm.pop(row)) - } else { - let mut c: &Comment - comments = append(comments, c) - } match type T { | &ast::VarDecl: self.var_decl(decl) @@ -466,20 +516,6 @@ impl Formatter { self.type_alias_decl(decl) | &ast::EnumItemDecl: self.enum_item(decl) - | &Field: - let mut fm = 0 - if decl.f.public { - fm += "pub ".len - } - if decl.f.mutable { - fm += "mut ".len - } - fm += decl.f.ident.len - if fm > field_max { - field_max = fm - } - i++ - continue } let diff = self.buf.len - n if max < diff { @@ -490,30 +526,18 @@ impl Formatter { i++ } - match type T { - | &Field: - for start < i; start++ { - self.field(nodes[start].f, field_max) - lines = append(lines, self.buf[n:]) - let diff = self.buf.len - n - if max < diff { - max = diff - } - self.buf = self.buf[:self.buf.len - diff] - } - |: - _ = start, field_max - } - - for j, line in lines { - self.write(self.indent) + for _, line in lines { + row = nodes[start].token.row + self.write_comments_except(row) self.write(line) - let c = comments[j] - if c != nil { + let c = self.cm.first(row) + if c != nil && c.row == row { + self.cm.drop_first() self.write(strings::repeat(" ", max - line.len + 1)) self.write(c.txt) } self.write("\n") + start++ } } @@ -532,7 +556,6 @@ impl Formatter { self.add_indent() for (_, mut s) in d.statics { self.write_comments(s.token.row) - self.write(self.indent) self.var_decl(s) self.write("\n") }