From 7c9012fa88e4df265145546093a435a2ff58b4a3 Mon Sep 17 00:00:00 2001 From: mertcandav Date: Mon, 1 Apr 2024 20:52:12 +0300 Subject: [PATCH] julefmt: minor improvements --- src/format.jule | 55 ++++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/src/format.jule b/src/format.jule index 8c406ed..a483848 100644 --- a/src/format.jule +++ b/src/format.jule @@ -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) @@ -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] @@ -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 @@ -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:] @@ -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() { @@ -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 @@ -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)