Skip to content

Commit

Permalink
julefmt: minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Apr 1, 2024
1 parent d07228b commit 7c9012f
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions src/format.jule
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,10 @@ impl Formatter {
}

fn fn_decl(&self, mut d: &ast::FnDecl) {
self.write_comments_except(d.token.row)
if !d.is_anon() {
self.write(self.indent)
}
if d.directives.len != 0 {
self.directives(d.directives)
self.write(self.indent)
Expand Down Expand Up @@ -699,7 +703,8 @@ impl Formatter {
let mut decl: T = nil
match type Node {
| &ast::EnumItemDecl
| &ast::VarDecl:
| &ast::VarDecl
| &ast::FnDecl:
decl = nodes[i]
| ast::Node:
let node = nodes[i]
Expand All @@ -710,7 +715,7 @@ impl Formatter {
break loop
}
}
if !all && self.row != -1 && decl.token.row-1 != self.row {
if !all && self.row != -1 && decl.token.row - self.row > 1 {
break loop
}
self.row = decl.token.row
Expand All @@ -723,13 +728,6 @@ impl Formatter {
| &ast::EnumItemDecl:
self.enum_item(decl)
| &ast::FnDecl:
if !decl.cpp_linked {
// If it is the first node to check, write declaration to catch.
if max == 0 {
self.fn_decl(decl)
}
break loop
}
self.fn_decl(decl)
}
let line = self.buf[n:]
Expand All @@ -755,6 +753,17 @@ impl Formatter {
}
}

fn write_impl_nodes[T](&self, mut &nodes: []T) {
self.write("\n")
let mut j = 0
for j < nodes.len {
self.group_decls[T, T](nodes, j)
if j < nodes.len {
self.write("\n")
}
}
}

fn impl_decl(&self, mut d: &ast::Impl) {
self.write("impl ")
if d.is_trait_impl() {
Expand All @@ -767,9 +776,10 @@ impl Formatter {
self.pop_row_comments(d.end.row)
ret
}

self.write(" {")
self.add_indent()
let (mut si, mut mi, mut statics) = 0, 0, -1
let (mut si, mut mi, mut statics, mut methods) = 0, 0, -1, -1
for {
let mut s: &ast::VarDecl = nil
let mut m: &ast::FnDecl = nil
Expand All @@ -785,31 +795,30 @@ impl Formatter {
if s != nil && (m == nil || s.token.row < m.token.row) {
if statics == -1 {
statics = si
let mut mtds = d.methods[methods:mi]
self.write_impl_nodes[&ast::FnDecl](mtds)
methods = -1
}
si++
continue
}
self.write("\n")
if statics != -1 {
let mut vars = d.statics[statics:si]
let mut j = -1
self.group_decls[&ast::VarDecl, &ast::VarDecl](vars, j)
self.write_impl_nodes[&ast::VarDecl](vars)
statics = -1
self.write("\n")
}
self.write_comments_except(m.token.row)
self.write(self.indent)
self.fn_decl(m)
self.write("\n")
if methods == -1 {
methods = mi
}
mi++
}
if statics != -1 {
if mi > 0 || self.buf[self.buf.len-1] != '\n' {
self.write("\n")
}
let mut vars = d.statics[statics:si]
let mut j = -1
self.group_decls[&ast::VarDecl, &ast::VarDecl](vars, j)
self.write_impl_nodes[&ast::VarDecl](vars)
}
if methods != -1 {
let mut mtds = d.methods[methods:mi]
self.write_impl_nodes[&ast::FnDecl](mtds)
}
self.add_global_padding_for_comment(d.end.row)
self.write_comments_except(d.end.row)
Expand Down

0 comments on commit 7c9012f

Please sign in to comment.