Skip to content

Commit

Permalink
julefmt: minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Mar 31, 2024
1 parent e1d2dc2 commit da243a1
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions src/format.jule
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,7 @@ impl ScopeFormatter {
})
self.write("\n")
self.fmt.add_indent()
self.format_stmts(c.scope)
self.format_stmts(c.scope, false)
self.fmt.done_indent()
self.write(self.fmt.indent)
self.write("}")
Expand All @@ -1158,7 +1158,7 @@ impl ScopeFormatter {
})
self.write("\n")
self.fmt.add_indent()
self.format_stmts(c.default.scope)
self.format_stmts(c.default.scope, false)
self.fmt.done_indent()
self.write(self.fmt.indent)
self.write("}")
Expand Down Expand Up @@ -1199,14 +1199,14 @@ impl ScopeFormatter {
self.fmt.pop_row_comments(self.fmt.row)
self.write("\n")
self.fmt.add_indent()
self.format_stmts(c.scope)
self.format_stmts(c.scope, false)
self.fmt.done_indent()
}
if mc.default != nil {
self.write(self.fmt.indent)
self.write("|:\n")
self.fmt.add_indent()
self.format_stmts(mc.default.scope)
self.format_stmts(mc.default.scope, false)
self.fmt.done_indent()
}
self.fmt.write_comments_except(mc.end.row)
Expand Down Expand Up @@ -1306,7 +1306,7 @@ impl ScopeFormatter {
}
}

fn format_stmts(&self, mut &scope: &ast::ScopeTree) {
fn format_stmts(&self, mut &scope: &ast::ScopeTree, inline: bool) {
defer {
self.fmt.row = scope.end.row
}
Expand Down Expand Up @@ -1339,7 +1339,9 @@ impl ScopeFormatter {
| &ast::VarDecl:
self.fmt.group_decls[&ast::VarDecl, ast::Node](scope.stmts, i)
|:
self.write(self.fmt.indent)
if !inline {
self.write(self.fmt.indent)
}
self.format_stmt(stmt.data)
self.fmt.pop_row_comments(self.fmt.row)
}
Expand Down Expand Up @@ -1373,11 +1375,22 @@ impl ScopeFormatter {
self.write(" ")
self.fmt.write_comment(c)
})
self.write("\n")
self.fmt.add_indent()
self.format_stmts(scope)
self.fmt.done_indent()
if n == self.fmt.buf.len {
let inline = scope.stmts.len == 1 &&
self.fmt.row == scope.end.row &&
scope.stmts[0].token.row == scope.end.row
if inline {
self.write(" ")
} else {
self.write("\n")
self.fmt.add_indent()
}
self.format_stmts(scope, inline)
if !inline {
self.fmt.done_indent()
}
if inline {
self.fmt.buf[self.fmt.buf.len - 1] = ' '
} else if n == self.fmt.buf.len {
self.fmt.buf = self.fmt.buf[:n - 1]
} else {
self.write(self.fmt.indent)
Expand Down Expand Up @@ -1690,7 +1703,9 @@ impl ExprFormatter {
self.fmt.add_global_padding_for_comment(lit.end.row)
self.fmt.write_comments_except(lit.end.row)
self.fmt.done_indent()
self.write(self.fmt.indent)
if lined {
self.write(self.fmt.indent)
}
}

fn brace_lit(&self, mut lit: &ast::BraceLit) {
Expand Down

0 comments on commit da243a1

Please sign in to comment.