code / tomo

Lines41.3K C23.7K Markdown9.7K YAML5.0K Tomo2.3K
7 others 763
Python231 Shell230 make212 INI47 Text21 SVG16 Lua6
(51 lines)
1 // Logic for formatting enums and enum tags
3 #include "../ast.h"
4 #include "../stdlib/datatypes.h"
5 #include "../stdlib/optionals.h"
6 #include "../stdlib/text.h"
7 #include "args.h"
8 #include "utils.h"
10 OptionalText_t format_inline_tag(tag_ast_t *tag, Table_t comments) {
11 if (range_has_comment(tag->start, tag->end, comments)) return NONE_TEXT;
12 Text_t code = Text$from_str(tag->name);
13 if (tag->fields || tag->secret) {
14 code = Texts(code, "(", must(format_inline_args(tag->fields, comments)));
15 if (tag->secret) code = Texts(code, "; secret");
16 code = Texts(code, ")");
18 return code;
21 Text_t format_tag(tag_ast_t *tag, Table_t comments, Text_t indent) {
22 OptionalText_t inline_tag = format_inline_tag(tag, comments);
23 if (inline_tag.tag != TEXT_NONE) return inline_tag;
24 Text_t code = Text$from_str(tag->name);
25 if (tag->fields || tag->secret) {
26 code = Texts(code, "(", format_args(tag->fields, comments, Texts(indent, single_indent)));
27 if (tag->secret) code = Texts(code, "; secret");
28 code = Texts(code, ")");
30 return code;
33 OptionalText_t format_inline_tags(tag_ast_t *tags, Table_t comments) {
34 Text_t code = EMPTY_TEXT;
35 for (; tags; tags = tags->next) {
36 code = Texts(code, must(format_inline_tag(tags, comments)));
37 if (tags->next) code = Texts(code, ", ");
38 if (tags->next && range_has_comment(tags->end, tags->next->start, comments)) return NONE_TEXT;
40 return code;
43 Text_t format_tags(tag_ast_t *tags, Table_t comments, Text_t indent) {
44 OptionalText_t inline_tags = format_inline_tags(tags, comments);
45 if (inline_tags.tag != TEXT_NONE) return inline_tags;
46 Text_t code = EMPTY_TEXT;
47 for (; tags; tags = tags->next) {
48 add_line(&code, Texts(format_tag(tags, comments, indent), ","), indent);
50 return code;