aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/formatter/formatter.c11
-rw-r--r--src/formatter/utils.c13
-rw-r--r--src/formatter/utils.h2
-rw-r--r--src/tomo.c2
4 files changed, 16 insertions, 12 deletions
diff --git a/src/formatter/formatter.c b/src/formatter/formatter.c
index 89535071..161c70d4 100644
--- a/src/formatter/formatter.c
+++ b/src/formatter/formatter.c
@@ -386,7 +386,8 @@ Text_t format_code(ast_t *ast, Table_t comments, Text_t indent) {
bool gap_before_comment = false;
const char *comment_pos = ast->start;
for (ast_list_t *stmt = Match(ast, Block)->statements; stmt; stmt = stmt->next) {
- if (should_have_blank_line(stmt->ast)) add_line(&code, Text(""), indent);
+ for (int blanks = suggested_blank_lines(stmt->ast); blanks > 0; blanks--)
+ add_line(&code, Text(""), indent);
for (OptionalText_t comment;
(comment = next_comment(comments, &comment_pos, stmt->ast->start)).length > 0;) {
@@ -400,9 +401,11 @@ Text_t format_code(ast_t *ast, Table_t comments, Text_t indent) {
add_line(&code, fmt(stmt->ast, comments, indent), indent);
comment_pos = stmt->ast->end;
- if (should_have_blank_line(stmt->ast) && stmt->next && !should_have_blank_line(stmt->next->ast))
- add_line(&code, Text(""), indent);
- else gap_before_comment = true;
+ if (stmt->next) {
+ for (int blanks = suggested_blank_lines(stmt->ast) - suggested_blank_lines(stmt->next->ast); blanks > 0;
+ blanks--)
+ add_line(&code, Text(""), indent);
+ } else gap_before_comment = true;
}
for (OptionalText_t comment; (comment = next_comment(comments, &comment_pos, ast->end)).length > 0;) {
diff --git a/src/formatter/utils.c b/src/formatter/utils.c
index 084a6667..1f4e64d8 100644
--- a/src/formatter/utils.c
+++ b/src/formatter/utils.c
@@ -38,7 +38,7 @@ bool range_has_comment(const char *start, const char *end, Table_t comments) {
return (comment.length >= 0);
}
-CONSTFUNC bool should_have_blank_line(ast_t *ast) {
+CONSTFUNC int suggested_blank_lines(ast_t *ast) {
switch (ast->tag) {
case If:
case When:
@@ -46,14 +46,15 @@ CONSTFUNC bool should_have_blank_line(ast_t *ast) {
case While:
case For:
case Block:
- case FunctionDef:
+ case Defer: return 1;
+ case Use: return 1;
+ case ConvertDef:
+ case FunctionDef: return 1;
case StructDef:
case EnumDef:
case LangDef:
- case ConvertDef:
- case Extend:
- case Defer: return true;
- default: return false;
+ case Extend: return 2;
+ default: return 0;
}
}
diff --git a/src/formatter/utils.h b/src/formatter/utils.h
index d847c2fb..0a00be6c 100644
--- a/src/formatter/utils.h
+++ b/src/formatter/utils.h
@@ -22,7 +22,7 @@ extern const Text_t single_indent;
void add_line(Text_t *code, Text_t line, Text_t indent);
OptionalText_t next_comment(Table_t comments, const char **pos, const char *end);
bool range_has_comment(const char *start, const char *end, Table_t comments);
-CONSTFUNC bool should_have_blank_line(ast_t *ast);
+CONSTFUNC int suggested_blank_lines(ast_t *ast);
Text_t indent_code(Text_t code);
Text_t parenthesize(Text_t code, Text_t indent);
CONSTFUNC ast_t *unwrap_block(ast_t *ast);
diff --git a/src/tomo.c b/src/tomo.c
index 5cdb3f9d..a8bba555 100644
--- a/src/tomo.c
+++ b/src/tomo.c
@@ -75,7 +75,7 @@ static const char *paths_str(List_t paths) {
static OptionalList_t files = NONE_LIST, args = NONE_LIST, uninstall = NONE_LIST, libraries = NONE_LIST;
static OptionalBool_t verbose = false, quiet = false, show_version = false, show_parse_tree = false,
- do_format_code = false, format_inplace = true, show_prefix = false, stop_at_transpile = false,
+ do_format_code = false, format_inplace = false, show_prefix = false, stop_at_transpile = false,
stop_at_obj_compilation = false, compile_exe = false, should_install = false, clean_build = false,
source_mapping = true, show_changelog = false;