From 91b6746fe1315aa9c09936b69cea3892c04c11af Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 25 Aug 2025 23:53:03 -0400 Subject: Split out formatter into subfiles --- src/formatter/enums.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/formatter/enums.c (limited to 'src/formatter/enums.c') diff --git a/src/formatter/enums.c b/src/formatter/enums.c new file mode 100644 index 00000000..430e72ea --- /dev/null +++ b/src/formatter/enums.c @@ -0,0 +1,45 @@ +// Logic for formatting enums and enum tags + +#include "../ast.h" +#include "../stdlib/datatypes.h" +#include "../stdlib/optionals.h" +#include "../stdlib/text.h" +#include "args.h" +#include "formatter.h" +#include "types.h" + +OptionalText_t format_inline_tag(tag_ast_t *tag, Table_t comments) { + if (range_has_comment(tag->start, tag->end, comments)) return NONE_TEXT; + Text_t code = Texts(Text$from_str(tag->name), "(", must(format_inline_args(tag->fields, comments))); + if (tag->secret) code = Texts(code, "; secret"); + return Texts(code, ")"); +} + +Text_t format_tag(tag_ast_t *tag, Table_t comments, Text_t indent) { + OptionalText_t inline_tag = format_inline_tag(tag, comments); + if (inline_tag.length >= 0) return inline_tag; + Text_t code = + Texts(Text$from_str(tag->name), "(", format_args(tag->fields, comments, Texts(indent, single_indent))); + if (tag->secret) code = Texts(code, "; secret"); + return Texts(code, ")"); +} + +OptionalText_t format_inline_tags(tag_ast_t *tags, Table_t comments) { + Text_t code = EMPTY_TEXT; + for (; tags; tags = tags->next) { + code = Texts(code, must(format_inline_tag(tags, comments))); + if (tags->next) code = Texts(code, ", "); + if (tags->next && range_has_comment(tags->end, tags->next->start, comments)) return NONE_TEXT; + } + return code; +} + +Text_t format_tags(tag_ast_t *tags, Table_t comments, Text_t indent) { + OptionalText_t inline_tags = format_inline_tags(tags, comments); + if (inline_tags.length >= 0) return inline_tags; + Text_t code = EMPTY_TEXT; + for (; tags; tags = tags->next) { + add_line(&code, Texts(format_tag(tags, comments, indent), ","), indent); + } + return code; +} -- cgit v1.2.3 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/enums.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/formatter/enums.c') diff --git a/src/formatter/enums.c b/src/formatter/enums.c index 430e72ea..7a89705d 100644 --- a/src/formatter/enums.c +++ b/src/formatter/enums.c @@ -5,8 +5,7 @@ #include "../stdlib/optionals.h" #include "../stdlib/text.h" #include "args.h" -#include "formatter.h" -#include "types.h" +#include "utils.h" OptionalText_t format_inline_tag(tag_ast_t *tag, Table_t comments) { if (range_has_comment(tag->start, tag->end, comments)) return NONE_TEXT; -- cgit v1.2.3 From 351a79b5d7c20f806b66fed19407677402ad0805 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 30 Aug 2025 13:59:51 -0400 Subject: Formatting tweak for enums --- src/formatter/enums.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'src/formatter/enums.c') diff --git a/src/formatter/enums.c b/src/formatter/enums.c index 7a89705d..893f055b 100644 --- a/src/formatter/enums.c +++ b/src/formatter/enums.c @@ -9,18 +9,25 @@ OptionalText_t format_inline_tag(tag_ast_t *tag, Table_t comments) { if (range_has_comment(tag->start, tag->end, comments)) return NONE_TEXT; - Text_t code = Texts(Text$from_str(tag->name), "(", must(format_inline_args(tag->fields, comments))); - if (tag->secret) code = Texts(code, "; secret"); - return Texts(code, ")"); + Text_t code = Text$from_str(tag->name); + if (tag->fields || tag->secret) { + code = Texts(code, "(", must(format_inline_args(tag->fields, comments))); + if (tag->secret) code = Texts(code, "; secret"); + code = Texts(code, ")"); + } + return code; } Text_t format_tag(tag_ast_t *tag, Table_t comments, Text_t indent) { OptionalText_t inline_tag = format_inline_tag(tag, comments); if (inline_tag.length >= 0) return inline_tag; - Text_t code = - Texts(Text$from_str(tag->name), "(", format_args(tag->fields, comments, Texts(indent, single_indent))); - if (tag->secret) code = Texts(code, "; secret"); - return Texts(code, ")"); + Text_t code = Text$from_str(tag->name); + if (tag->fields || tag->secret) { + code = Texts(code, "(", format_args(tag->fields, comments, Texts(indent, single_indent))); + if (tag->secret) code = Texts(code, "; secret"); + code = Texts(code, ")"); + } + return code; } OptionalText_t format_inline_tags(tag_ast_t *tags, Table_t comments) { -- cgit v1.2.3