Skip to content

Commit

Permalink
julefmt: improve parameter formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Mar 28, 2024
1 parent 0c680e8 commit c00676c
Showing 1 changed file with 61 additions and 26 deletions.
87 changes: 61 additions & 26 deletions src/format.jule
Original file line number Diff line number Diff line change
Expand Up @@ -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 &params: []&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 ")
}
Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -754,15 +785,19 @@ 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")
}
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:
Expand Down

0 comments on commit c00676c

Please sign in to comment.