1 // Logic for formatting arguments and argument lists
4 #include "../stdlib/datatypes.h"
5 #include "../stdlib/optionals.h"
6 #include "../stdlib/text.h"
11 OptionalText_t format_inline_arg(arg_ast_t *arg, Table_t comments) {
12 if (range_has_comment(arg->start, arg->end, comments)) return NONE_TEXT;
13 if (arg->name == NULL && arg->value) return must(format_inline_code(arg->value, comments));
14 Text_t code = Text$from_str(arg->name);
15 if (arg->type) code = Texts(code, ":", must(format_type(arg->type)));
16 if (arg->value) code = Texts(code, "=", must(format_inline_code(arg->value, comments)));
20 Text_t format_arg(arg_ast_t *arg, Table_t comments, Text_t indent) {
21 OptionalText_t inline_arg = format_inline_arg(arg, comments);
22 if (inline_arg.tag != TEXT_NONE && inline_arg.length <= MAX_WIDTH) return inline_arg;
23 if (arg->name == NULL && arg->value) return format_code(arg->value, comments, indent);
24 Text_t code = Text$from_str(arg->name);
25 if (arg->type) code = Texts(code, ":", format_type(arg->type));
26 if (arg->value) code = Texts(code, "=", format_code(arg->value, comments, indent));
30 OptionalText_t format_inline_args(arg_ast_t *args, Table_t comments) {
31 Text_t code = EMPTY_TEXT;
32 for (arg_ast_t *arg = args; arg; arg = arg->next) {
33 if (arg->name && arg->next && arg->type == arg->next->type && arg->value == arg->next->value) {
34 code = Texts(code, Text$from_str(arg->name), ",");
36 code = Texts(code, must(format_inline_arg(arg, comments)));
37 if (arg->next) code = Texts(code, ", ");
39 if (arg->next && range_has_comment(arg->end, arg->next->start, comments)) return NONE_TEXT;
44 Text_t format_args(arg_ast_t *args, Table_t comments, Text_t indent) {
45 OptionalText_t inline_args = format_inline_args(args, comments);
46 if (inline_args.tag != TEXT_NONE && inline_args.length <= MAX_WIDTH) return inline_args;
47 Text_t code = EMPTY_TEXT;
48 for (arg_ast_t *arg = args; arg; arg = arg->next) {
49 if (arg->name && arg->next && arg->type == arg->next->type && arg->value == arg->next->value) {
50 code = Texts(code, Text$from_str(arg->name), ",");
52 code = Texts(code, "\n", indent, single_indent, format_arg(arg, comments, Texts(indent, single_indent)));
53 if (args->next) code = Texts(code, ",");