From 978835e3dd8dd59a1efaa869f2f603eb9eea5a3f Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 25 Aug 2025 23:59:09 -0400 Subject: Split out utility functions --- src/formatter/utils.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/formatter/utils.h (limited to 'src/formatter/utils.h') diff --git a/src/formatter/utils.h b/src/formatter/utils.h new file mode 100644 index 00000000..a8def627 --- /dev/null +++ b/src/formatter/utils.h @@ -0,0 +1,28 @@ +// This file defines utility functions for autoformatting code + +#pragma once + +#include + +#include "../ast.h" +#include "../stdlib/datatypes.h" + +#define MAX_WIDTH 100 + +#define must(expr) \ + ({ \ + OptionalText_t _expr = expr; \ + if (_expr.length < 0) return NONE_TEXT; \ + (Text_t) _expr; \ + }) + +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); +Text_t indent_code(Text_t code); +Text_t parenthesize(Text_t code, Text_t indent); +CONSTFUNC ast_t *unwrap_block(ast_t *ast); +CONSTFUNC const char *binop_tomo_operator(ast_e tag); -- cgit v1.2.3 From 5b06702dde3f53510a3d64a8156a349914afa605 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 26 Aug 2025 02:54:56 -0400 Subject: Fixes for unops --- src/formatter/utils.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/formatter/utils.h') diff --git a/src/formatter/utils.h b/src/formatter/utils.h index a8def627..df9c9b92 100644 --- a/src/formatter/utils.h +++ b/src/formatter/utils.h @@ -26,3 +26,5 @@ Text_t indent_code(Text_t code); Text_t parenthesize(Text_t code, Text_t indent); CONSTFUNC ast_t *unwrap_block(ast_t *ast); CONSTFUNC const char *binop_tomo_operator(ast_e tag); +OptionalText_t termify_inline(ast_t *ast, Table_t comments); +Text_t termify(ast_t *ast, Table_t comments, Text_t indent); -- cgit v1.2.3 From d25d5642392f93623d1eb4d11156d293fe6df546 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 26 Aug 2025 14:39:11 -0400 Subject: Formatting for min/max and cleanup for ints/nums --- src/formatter/utils.h | 1 + 1 file changed, 1 insertion(+) (limited to 'src/formatter/utils.h') diff --git a/src/formatter/utils.h b/src/formatter/utils.h index df9c9b92..9dc230e4 100644 --- a/src/formatter/utils.h +++ b/src/formatter/utils.h @@ -6,6 +6,7 @@ #include "../ast.h" #include "../stdlib/datatypes.h" +#include "../stdlib/optionals.h" #define MAX_WIDTH 100 -- cgit v1.2.3 From cb1d36c6d8bc84f3422c71ab3eb29606e80f7837 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 26 Aug 2025 14:58:51 -0400 Subject: Formatting for reductions --- src/formatter/utils.h | 1 - 1 file changed, 1 deletion(-) (limited to 'src/formatter/utils.h') diff --git a/src/formatter/utils.h b/src/formatter/utils.h index 9dc230e4..d847c2fb 100644 --- a/src/formatter/utils.h +++ b/src/formatter/utils.h @@ -26,6 +26,5 @@ CONSTFUNC bool should_have_blank_line(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); -CONSTFUNC const char *binop_tomo_operator(ast_e tag); OptionalText_t termify_inline(ast_t *ast, Table_t comments); Text_t termify(ast_t *ast, Table_t comments, Text_t indent); -- cgit v1.2.3 From 2d3021728bf5c74f8bf6854c1ae4951a6ebbf9c7 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 29 Aug 2025 14:04:55 -0400 Subject: Formatting tweaks/fixes --- src/formatter/utils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/formatter/utils.h') 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); -- cgit v1.2.3 From f2ad722780f96dc5fb7d93f536c7267741be3f6f Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 30 Aug 2025 14:29:50 -0400 Subject: Fix line gaps --- src/formatter/utils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/formatter/utils.h') diff --git a/src/formatter/utils.h b/src/formatter/utils.h index 0a00be6c..880da0a9 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 int suggested_blank_lines(ast_t *ast); +CONSTFUNC int suggested_blank_lines(ast_t *first, ast_t *second); Text_t indent_code(Text_t code); Text_t parenthesize(Text_t code, Text_t indent); CONSTFUNC ast_t *unwrap_block(ast_t *ast); -- cgit v1.2.3