dte test coverage


Directory: ./
File: src/syntax/color.c
Date: 2025-12-03 13:13:24
Coverage Exec Excl Total
Lines: 97.1% 66 0 68
Functions: 100.0% 10 0 10
Branches: 83.3% 15 0 18

Line Branch Exec Source
1 #include <stdlib.h>
2 #include <string.h>
3 #include "color.h"
4 #include "command/serialize.h"
5 #include "util/array.h"
6 #include "util/bsearch.h"
7 #include "util/debug.h"
8 #include "util/xmalloc.h"
9
10 static const char builtin_style_names[NR_BSE][16] = {
11 [BSE_ACTIVETAB] = "activetab",
12 [BSE_COMMANDLINE] = "commandline",
13 [BSE_CURRENTLINE] = "currentline",
14 [BSE_DEFAULT] = "default",
15 [BSE_DIALOG] = "dialog",
16 [BSE_ERRORMSG] = "errormsg",
17 [BSE_INACTIVETAB] = "inactivetab",
18 [BSE_INFOMSG] = "infomsg",
19 [BSE_LINENUMBER] = "linenumber",
20 [BSE_NOLINE] = "noline",
21 [BSE_NONTEXT] = "nontext",
22 [BSE_SELECTION] = "selection",
23 [BSE_STATUSLINE] = "statusline",
24 [BSE_TABBAR] = "tabbar",
25 [BSE_WSERROR] = "wserror",
26 };
27
28 24 UNITTEST {
29 24 CHECK_BSEARCH_STR_ARRAY(builtin_style_names);
30 24 }
31
32 9743 static TermStyle *find_real_style(StyleMap *styles, const char *name)
33 {
34 9743 ssize_t idx = BSEARCH_IDX(name, builtin_style_names, vstrcmp);
35
2/2
✓ Branch 3 → 4 taken 302 times.
✓ Branch 3 → 7 taken 9441 times.
9743 if (idx >= 0) {
36 302 BUG_ON(idx >= ARRAYLEN(builtin_style_names));
37 302 return &styles->builtin[idx];
38 }
39 9441 return hashmap_get(&styles->other, name);
40 }
41
42 9109 static const TermStyle *find_real_style_const(const StyleMap *styles, const char *name)
43 {
44 9109 return find_real_style((StyleMap*)styles, name);
45 }
46
47 634 bool set_highlight_style(StyleMap *styles, const char *name, const TermStyle *style)
48 {
49 634 TermStyle *existing = find_real_style(styles, name);
50
2/2
✓ Branch 3 → 4 taken 301 times.
✓ Branch 3 → 5 taken 333 times.
634 if (existing) {
51 301 bool different = !same_style(style, existing);
52 301 *existing = *style;
53 301 return different;
54 }
55
56 333 hashmap_insert(&styles->other, xstrdup(name), XMEMDUP(style));
57 333 return true;
58 }
59
60 4602 const TermStyle *find_style(const StyleMap *styles, const char *name)
61 {
62 4602 const TermStyle *style = find_real_style_const(styles, name);
63
2/2
✓ Branch 3 → 4 taken 4507 times.
✓ Branch 3 → 6 taken 95 times.
4602 if (style) {
64 return style;
65 }
66
67 4507 const char *dot = strchr(name, '.');
68
1/2
✓ Branch 4 → 5 taken 4507 times.
✗ Branch 4 → 6 not taken.
4507 return dot ? find_real_style_const(styles, dot + 1) : NULL;
69 }
70
71 18 void clear_hl_styles(StyleMap *styles)
72 {
73 18 hashmap_clear(&styles->other, free);
74 18 }
75
76 1 void collect_builtin_styles(PointerArray *a, const char *prefix)
77 {
78 1 COLLECT_STRINGS(builtin_style_names, a, prefix);
79 1 }
80
81 typedef struct {
82 const char *name;
83 TermStyle style;
84 } HlStyle;
85
86 304 static int hlstyle_cmp(const void *ap, const void *bp)
87 {
88 304 const HlStyle *a = ap;
89 304 const HlStyle *b = bp;
90 304 return strcmp(a->name, b->name);
91 }
92
93 104 void string_append_hl_style(String *s, const char *name, const TermStyle *style)
94 {
95 104 string_append_literal(s, "hi ");
96 104 string_append_escaped_arg(s, name, true);
97 104 string_append_byte(s, ' ');
98
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 104 times.
104 if (unlikely(name[0] == '-')) {
99 string_append_literal(s, "-- ");
100 }
101 104 char buf[TERM_STYLE_BUFSIZE];
102 104 string_append_cstring(s, term_style_to_string(buf, style));
103 104 }
104
105 2 String dump_hl_styles(const StyleMap *styles)
106 {
107 2 String buf = string_new(4096);
108 2 string_append_literal(&buf, "# UI colors:\n");
109
2/2
✓ Branch 8 → 5 taken 30 times.
✓ Branch 8 → 9 taken 2 times.
32 for (size_t i = 0; i < NR_BSE; i++) {
110 30 string_append_hl_style(&buf, builtin_style_names[i], &styles->builtin[i]);
111 30 string_append_byte(&buf, '\n');
112 }
113
114 2 const HashMap *hl_styles = &styles->other;
115 2 const size_t count = hl_styles->count;
116
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 2 times.
2 if (unlikely(count == 0)) {
117 return buf;
118 }
119
120 // Copy the HashMap entries into an array
121 2 HlStyle *array = xmallocarray(count, sizeof(*array));
122 2 size_t n = 0;
123
2/2
✓ Branch 15 → 13 taken 74 times.
✓ Branch 15 → 16 taken 2 times.
76 for (HashMapIter it = hashmap_iter(hl_styles); hashmap_next(&it); ) {
124 74 const TermStyle *style = it.entry->value;
125 74 array[n++] = (HlStyle) {
126 74 .name = it.entry->key,
127 74 .style = *style,
128 };
129 }
130
131 // Sort the array
132 2 BUG_ON(n != count);
133 2 qsort(array, count, sizeof(array[0]), hlstyle_cmp);
134
135 2 string_append_literal(&buf, "\n# Syntax colors:\n");
136
2/2
✓ Branch 24 → 21 taken 74 times.
✓ Branch 24 → 25 taken 2 times.
76 for (size_t i = 0; i < count; i++) {
137 74 string_append_hl_style(&buf, array[i].name, &array[i].style);
138 74 string_append_byte(&buf, '\n');
139 }
140
141 2 free(array);
142 2 return buf;
143 }
144