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/args.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/formatter/args.c (limited to 'src/formatter/args.c') diff --git a/src/formatter/args.c b/src/formatter/args.c new file mode 100644 index 00000000..9467c9fe --- /dev/null +++ b/src/formatter/args.c @@ -0,0 +1,55 @@ +// Logic for formatting arguments and argument lists + +#include "../ast.h" +#include "../stdlib/datatypes.h" +#include "../stdlib/optionals.h" +#include "../stdlib/text.h" +#include "formatter.h" +#include "types.h" + +OptionalText_t format_inline_arg(arg_ast_t *arg, Table_t comments) { + if (range_has_comment(arg->start, arg->end, comments)) return NONE_TEXT; + if (arg->name == NULL && arg->value) return must(format_inline_code(arg->value, comments)); + Text_t code = Text$from_str(arg->name); + if (arg->type) code = Texts(code, ":", must(format_inline_type(arg->type, comments))); + if (arg->value) code = Texts(code, " = ", must(format_inline_code(arg->value, comments))); + return code; +} + +Text_t format_arg(arg_ast_t *arg, Table_t comments, Text_t indent) { + OptionalText_t inline_arg = format_inline_arg(arg, comments); + if (inline_arg.length >= 0 && inline_arg.length <= MAX_WIDTH) return inline_arg; + if (arg->name == NULL && arg->value) return format_code(arg->value, comments, indent); + Text_t code = Text$from_str(arg->name); + if (arg->type) code = Texts(code, ":", format_type(arg->type, comments, indent)); + if (arg->value) code = Texts(code, " = ", format_code(arg->value, comments, indent)); + return code; +} + +OptionalText_t format_inline_args(arg_ast_t *args, Table_t comments) { + Text_t code = EMPTY_TEXT; + for (; args; args = args->next) { + if (args->name && args->next && args->type == args->next->type && args->value == args->next->value) { + code = Texts(code, Text$from_str(args->name), ","); + } else { + code = Texts(code, must(format_inline_arg(args, comments))); + if (args->next) code = Texts(code, ", "); + } + if (args->next && range_has_comment(args->end, args->next->start, comments)) return NONE_TEXT; + } + return code; +} + +Text_t format_args(arg_ast_t *args, Table_t comments, Text_t indent) { + OptionalText_t inline_args = format_inline_args(args, comments); + if (inline_args.length >= 0 && inline_args.length <= MAX_WIDTH) return inline_args; + Text_t code = EMPTY_TEXT; + for (; args; args = args->next) { + if (args->name && args->next && args->type == args->next->type && args->value == args->next->value) { + code = Texts(code, Text$from_str(args->name), ","); + } else { + add_line(&code, Texts(format_arg(args, 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/args.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/formatter/args.c') diff --git a/src/formatter/args.c b/src/formatter/args.c index 9467c9fe..2bd115cd 100644 --- a/src/formatter/args.c +++ b/src/formatter/args.c @@ -6,6 +6,7 @@ #include "../stdlib/text.h" #include "formatter.h" #include "types.h" +#include "utils.h" OptionalText_t format_inline_arg(arg_ast_t *arg, Table_t comments) { if (range_has_comment(arg->start, arg->end, comments)) return NONE_TEXT; -- cgit v1.2.3 From b5dee4f7ed1b8181df22a9824b7026a125ebbc53 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 26 Aug 2025 00:45:56 -0400 Subject: Actual type formatting --- src/formatter/args.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/formatter/args.c') diff --git a/src/formatter/args.c b/src/formatter/args.c index 2bd115cd..6bd07b28 100644 --- a/src/formatter/args.c +++ b/src/formatter/args.c @@ -12,7 +12,7 @@ OptionalText_t format_inline_arg(arg_ast_t *arg, Table_t comments) { if (range_has_comment(arg->start, arg->end, comments)) return NONE_TEXT; if (arg->name == NULL && arg->value) return must(format_inline_code(arg->value, comments)); Text_t code = Text$from_str(arg->name); - if (arg->type) code = Texts(code, ":", must(format_inline_type(arg->type, comments))); + if (arg->type) code = Texts(code, ":", must(format_type(arg->type))); if (arg->value) code = Texts(code, " = ", must(format_inline_code(arg->value, comments))); return code; } @@ -22,7 +22,7 @@ Text_t format_arg(arg_ast_t *arg, Table_t comments, Text_t indent) { if (inline_arg.length >= 0 && inline_arg.length <= MAX_WIDTH) return inline_arg; if (arg->name == NULL && arg->value) return format_code(arg->value, comments, indent); Text_t code = Text$from_str(arg->name); - if (arg->type) code = Texts(code, ":", format_type(arg->type, comments, indent)); + if (arg->type) code = Texts(code, ":", format_type(arg->type)); if (arg->value) code = Texts(code, " = ", format_code(arg->value, comments, indent)); return code; } -- cgit v1.2.3 From db4048734179c047d64284281f72a531f166f11e Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 30 Aug 2025 13:57:48 -0400 Subject: Tweak arg formatting --- src/formatter/args.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/formatter/args.c') diff --git a/src/formatter/args.c b/src/formatter/args.c index 6bd07b28..8bbbbd58 100644 --- a/src/formatter/args.c +++ b/src/formatter/args.c @@ -13,7 +13,7 @@ OptionalText_t format_inline_arg(arg_ast_t *arg, Table_t comments) { if (arg->name == NULL && arg->value) return must(format_inline_code(arg->value, comments)); Text_t code = Text$from_str(arg->name); if (arg->type) code = Texts(code, ":", must(format_type(arg->type))); - if (arg->value) code = Texts(code, " = ", must(format_inline_code(arg->value, comments))); + if (arg->value) code = Texts(code, "=", must(format_inline_code(arg->value, comments))); return code; } @@ -23,7 +23,7 @@ Text_t format_arg(arg_ast_t *arg, Table_t comments, Text_t indent) { if (arg->name == NULL && arg->value) return format_code(arg->value, comments, indent); Text_t code = Text$from_str(arg->name); if (arg->type) code = Texts(code, ":", format_type(arg->type)); - if (arg->value) code = Texts(code, " = ", format_code(arg->value, comments, indent)); + if (arg->value) code = Texts(code, "=", format_code(arg->value, comments, indent)); return code; } -- cgit v1.2.3 From 688b0ae7bd433c4d7daf96505581f90e719f033c Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 30 Aug 2025 15:04:44 -0400 Subject: Formatting tweaks to argument lists --- src/formatter/args.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/formatter/args.c') diff --git a/src/formatter/args.c b/src/formatter/args.c index 8bbbbd58..34d234df 100644 --- a/src/formatter/args.c +++ b/src/formatter/args.c @@ -49,7 +49,8 @@ Text_t format_args(arg_ast_t *args, Table_t comments, Text_t indent) { if (args->name && args->next && args->type == args->next->type && args->value == args->next->value) { code = Texts(code, Text$from_str(args->name), ","); } else { - add_line(&code, Texts(format_arg(args, comments, indent), ","), indent); + code = Texts(code, "\n", indent, single_indent, + format_arg(args, comments, Texts(indent, single_indent, single_indent)), ","); } } return code; -- cgit v1.2.3 From f38fa3389c881ef8341011c05d7b2435686068e7 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 30 Aug 2025 15:54:15 -0400 Subject: Arg indentation tweak --- src/formatter/args.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/formatter/args.c') diff --git a/src/formatter/args.c b/src/formatter/args.c index 34d234df..9809645f 100644 --- a/src/formatter/args.c +++ b/src/formatter/args.c @@ -49,8 +49,8 @@ Text_t format_args(arg_ast_t *args, Table_t comments, Text_t indent) { if (args->name && args->next && args->type == args->next->type && args->value == args->next->value) { code = Texts(code, Text$from_str(args->name), ","); } else { - code = Texts(code, "\n", indent, single_indent, - format_arg(args, comments, Texts(indent, single_indent, single_indent)), ","); + code = + Texts(code, "\n", indent, single_indent, format_arg(args, comments, Texts(indent, single_indent)), ","); } } return code; -- cgit v1.2.3 From 9090a2a09f119510f40c5385f3790cc6b6539abc Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 30 Aug 2025 15:59:57 -0400 Subject: Arg formatting tweaks --- src/formatter/args.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src/formatter/args.c') diff --git a/src/formatter/args.c b/src/formatter/args.c index 9809645f..997a1e39 100644 --- a/src/formatter/args.c +++ b/src/formatter/args.c @@ -29,14 +29,14 @@ Text_t format_arg(arg_ast_t *arg, Table_t comments, Text_t indent) { OptionalText_t format_inline_args(arg_ast_t *args, Table_t comments) { Text_t code = EMPTY_TEXT; - for (; args; args = args->next) { - if (args->name && args->next && args->type == args->next->type && args->value == args->next->value) { - code = Texts(code, Text$from_str(args->name), ","); + for (arg_ast_t *arg = args; arg; arg = arg->next) { + if (arg->name && arg->next && arg->type == arg->next->type && arg->value == arg->next->value) { + code = Texts(code, Text$from_str(arg->name), ","); } else { - code = Texts(code, must(format_inline_arg(args, comments))); - if (args->next) code = Texts(code, ", "); + code = Texts(code, must(format_inline_arg(arg, comments))); + if (arg->next) code = Texts(code, ", "); } - if (args->next && range_has_comment(args->end, args->next->start, comments)) return NONE_TEXT; + if (arg->next && range_has_comment(arg->end, arg->next->start, comments)) return NONE_TEXT; } return code; } @@ -45,12 +45,12 @@ Text_t format_args(arg_ast_t *args, Table_t comments, Text_t indent) { OptionalText_t inline_args = format_inline_args(args, comments); if (inline_args.length >= 0 && inline_args.length <= MAX_WIDTH) return inline_args; Text_t code = EMPTY_TEXT; - for (; args; args = args->next) { - if (args->name && args->next && args->type == args->next->type && args->value == args->next->value) { - code = Texts(code, Text$from_str(args->name), ","); + for (arg_ast_t *arg = args; arg; arg = arg->next) { + if (arg->name && arg->next && arg->type == arg->next->type && arg->value == arg->next->value) { + code = Texts(code, Text$from_str(arg->name), ","); } else { - code = - Texts(code, "\n", indent, single_indent, format_arg(args, comments, Texts(indent, single_indent)), ","); + code = Texts(code, "\n", indent, single_indent, format_arg(arg, comments, Texts(indent, single_indent))); + if (args->next) code = Texts(code, ","); } } return code; -- cgit v1.2.3