aboutsummaryrefslogtreecommitdiff
path: root/src/formatter/args.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-09-06 14:47:45 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-09-06 14:47:45 -0400
commita8316252db95e3d77f9f0e9beb89cfcb4573d5b1 (patch)
treee5905bce9611e35ccb2f84481232fca0e657ff42 /src/formatter/args.c
parenta0ac652cd1eebdc42425b34f1685f8cb20cb4eea (diff)
parent73246764f88f6f652316ee0c138a990d836698a7 (diff)
Merge branch 'main' into simplified-quotes
Diffstat (limited to 'src/formatter/args.c')
-rw-r--r--src/formatter/args.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/formatter/args.c b/src/formatter/args.c
new file mode 100644
index 00000000..997a1e39
--- /dev/null
+++ b/src/formatter/args.c
@@ -0,0 +1,57 @@
+// 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"
+#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;
+ 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)));
+ 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));
+ 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 (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(arg, comments)));
+ if (arg->next) code = Texts(code, ", ");
+ }
+ if (arg->next && range_has_comment(arg->end, arg->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 (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(arg, comments, Texts(indent, single_indent)));
+ if (args->next) code = Texts(code, ",");
+ }
+ }
+ return code;
+}