From c00676c1605d2fa7c6a5198d0306b3ee18f4cbcc Mon Sep 17 00:00:00 2001 From: mertcandav Date: Thu, 28 Mar 2024 20:43:14 +0300 Subject: [PATCH] julefmt: improve parameter formatting --- src/format.jule | 87 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 61 insertions(+), 26 deletions(-) diff --git a/src/format.jule b/src/format.jule index a10c078..1bc9e4a 100644 --- a/src/format.jule +++ b/src/format.jule @@ -335,29 +335,26 @@ impl Formatter { self.row = d.end.row } - fn fn_decl(&self, mut d: &ast::FnDecl) { - if d.directives.len != 0 { - self.directives(d.directives) - self.write("\n") - self.write(self.indent) - } - if d.public { - self.write("pub ") - } - if d.statically { - self.write("static ") - } - if d.unsafety { - self.write("unsafe ") - } - self.write("fn") - if !lex::is_anon_ident(d.ident) { - self.write(" ") - self.write(d.ident) + fn params(&self, mut ¶ms: []&ast::ParamDecl) { + if params.len == 0 { + self.write("()") + ret } - self.generics(d.generics) self.write("(") - for (i, mut p) in d.params { + let mut indented = false + let mut row = params[0].token.row + for (i, mut p) in params { + if row != p.token.row { + self.write("\n") + if !indented { + indented = true + self.add_indent() + } + self.write(self.indent) + } else { + self.write(" ") + } + self.pop_row_comments_by_c(p.token.row, p.token.column) if p.mutable { self.write("mut ") } @@ -373,13 +370,47 @@ impl Formatter { } else if p.reference { self.write("_: ") } + self.pop_row_comments_by_c(p.kind.token.row, p.kind.token.column) self.format_type(p.kind) } - if i + 1 < d.params.len { - self.write(", ") + if i + 1 < params.len { + if params[i + 1].token.row != p.token.row { + self.pop_row_comments(p.token.row) + } + self.write(",") + } else { + self.pop_row_comments(p.token.row) } + row = p.token.row + } + if indented { + self.done_indent() } self.write(")") + } + + fn fn_decl(&self, mut d: &ast::FnDecl) { + if d.directives.len != 0 { + self.directives(d.directives) + self.write("\n") + self.write(self.indent) + } + if d.public { + self.write("pub ") + } + if d.statically { + self.write("static ") + } + if d.unsafety { + self.write("unsafe ") + } + self.write("fn") + if !lex::is_anon_ident(d.ident) { + self.write(" ") + self.write(d.ident) + } + self.generics(d.generics) + self.params(d.params) if d.result.kind != nil { self.write(": ") self.tf.result(d.result) @@ -719,7 +750,7 @@ impl Formatter { statics = -1 self.write("\n") } - self.write_comments(m.token.row) + self.write_comments_except(m.token.row) self.write(self.indent) self.fn_decl(m) self.write("\n") @@ -754,6 +785,12 @@ impl Formatter { } self.group_decls[&ast::VarDecl, ast::Node](self.f.nodes, self.i) ret + | &ast::FnDecl: + if node.token.row - self.write_comments_except(node.token.row) > 1 { + self.write("\n") + } + self.fn_decl((&ast::FnDecl)(node.data)) + ret } if node.token.row - self.write_comments(node.token.row) > 1 { self.write("\n") @@ -761,8 +798,6 @@ impl Formatter { match type node.data { | &ast::EnumDecl: self.enum_decl((&ast::EnumDecl)(node.data)) - | &ast::FnDecl: - self.fn_decl((&ast::FnDecl)(node.data)) | &ast::StructDecl: self.struct_decl((&ast::StructDecl)(node.data)) | &ast::TraitDecl: