dte test coverage


Directory: ./
File: src/indent.c
Date: 2025-09-07 23:01:39
Exec Total Coverage
Lines: 95 107 88.8%
Functions: 8 8 100.0%
Branches: 44 54 81.5%

Line Branch Exec Source
1 #include <string.h>
2 #include <sys/types.h>
3 #include "indent.h"
4 #include "regexp.h"
5 #include "util/xmalloc.h"
6
7 44 char *make_indent(const LocalOptions *options, size_t width)
8 {
9
2/2
✓ Branch 0 (2→3) taken 27 times.
✓ Branch 1 (2→10) taken 17 times.
44 if (width == 0) {
10 return NULL;
11 }
12
13
2/2
✓ Branch 0 (3→4) taken 23 times.
✓ Branch 1 (3→6) taken 4 times.
27 if (use_spaces_for_indent(options)) {
14 23 char *str = xmalloc(width + 1);
15 23 str[width] = '\0';
16 23 return memset(str, ' ', width);
17 }
18
19 4 size_t tw = options->tab_width;
20 4 size_t ntabs = indent_level(width, tw);
21 4 size_t nspaces = indent_remainder(width, tw);
22 4 size_t n = ntabs + nspaces;
23 4 char *str = xmalloc(n + 1);
24 4 memset(str + ntabs, ' ', nspaces);
25 4 str[n] = '\0';
26 4 return memset(str, '\t', ntabs);
27 }
28
29 // Return true if the contents of `line` triggers an additional level
30 // of auto-indent on the next line
31 36 static bool line_contents_increases_indent (
32 const LocalOptions *options,
33 StringView line
34 ) {
35 36 static const regex_t *re1, *re2;
36
2/2
✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→6) taken 35 times.
36 if (!re1) {
37 // TODO: Make these patterns configurable via a local option
38 1 re1 = regexp_compile_or_fatal_error("\\{[\t ]*(//.*|/\\*.*\\*/[\t ]*)?$");
39 1 re2 = regexp_compile_or_fatal_error("\\}[\t ]*(//.*|/\\*.*\\*/[\t ]*)?$");
40 }
41
42
2/2
✓ Branch 0 (6→7) taken 13 times.
✓ Branch 1 (6→11) taken 23 times.
36 if (options->brace_indent) {
43
2/2
✓ Branch 0 (8→9) taken 12 times.
✓ Branch 1 (8→15) taken 1 times.
13 if (regexp_exec(re1, line.data, line.length, 0, NULL, 0)) {
44 return true;
45 }
46
1/2
✓ Branch 0 (10→11) taken 12 times.
✗ Branch 1 (10→15) not taken.
12 if (regexp_exec(re2, line.data, line.length, 0, NULL, 0)) {
47 return false;
48 }
49 }
50
51 35 const InternedRegexp *ir = options->indent_regex;
52
2/2
✓ Branch 0 (11→12) taken 2 times.
✓ Branch 1 (11→15) taken 33 times.
35 if (!ir) {
53 return false;
54 }
55
56 2 BUG_ON(ir->str[0] == '\0');
57 2 return regexp_exec(&ir->re, line.data, line.length, 0, NULL, 0);
58 }
59
60 36 char *get_indent_for_next_line(const LocalOptions *options, StringView line)
61 {
62 36 size_t curr_width = get_indent_width(line, options->tab_width);
63 36 size_t next_width = next_indent_width(curr_width, options->indent_width);
64 36 bool increase = line_contents_increases_indent(options, line);
65
2/2
✓ Branch 0 (5→6) taken 34 times.
✓ Branch 1 (5→7) taken 2 times.
70 return make_indent(options, increase ? next_width : curr_width);
66 }
67
68 64 IndentInfo get_indent_info(const LocalOptions *options, StringView line)
69 {
70 64 const char *buf = line.data;
71 64 const size_t len = line.length;
72 64 const size_t tw = options->tab_width;
73 64 const size_t iw = options->indent_width;
74 64 const bool space_indent = use_spaces_for_indent(options);
75 64 IndentInfo info = {.sane = true};
76 64 size_t spaces = 0;
77 64 size_t tabs = 0;
78 64 size_t pos = 0;
79
80
2/2
✓ Branch 0 (16→3) taken 631 times.
✓ Branch 1 (16→17) taken 5 times.
636 for (; pos < len; pos++) {
81
2/2
✓ Branch 0 (3→4) taken 564 times.
✓ Branch 1 (3→5) taken 67 times.
631 if (buf[pos] == ' ') {
82 564 info.width++;
83 564 spaces++;
84
2/2
✓ Branch 0 (5→6) taken 8 times.
✓ Branch 1 (5→17) taken 59 times.
67 } else if (buf[pos] == '\t') {
85 8 info.width = next_indent_width(info.width, tw);
86 8 tabs++;
87 } else {
88 break;
89 }
90
4/4
✓ Branch 0 (9→10) taken 146 times.
✓ Branch 1 (9→15) taken 426 times.
✓ Branch 2 (10→11) taken 145 times.
✓ Branch 3 (10→15) taken 1 times.
572 if (indent_remainder(info.width, iw) == 0 && info.sane) {
91
2/2
✓ Branch 0 (11→12) taken 137 times.
✓ Branch 1 (11→13) taken 8 times.
145 info.sane = space_indent ? !tabs : !spaces;
92 }
93 }
94
95 64 info.level = indent_level(info.width, iw);
96 64 info.wsonly = (pos == len);
97 64 info.bytes = spaces + tabs;
98 64 return info;
99 }
100
101 44 size_t get_indent_width(StringView line, unsigned int tab_width)
102 {
103 44 const char *buf = line.data;
104 44 size_t width = 0;
105
2/2
✓ Branch 0 (8→3) taken 129 times.
✓ Branch 1 (8→9) taken 3 times.
132 for (size_t i = 0, n = line.length; i < n; i++) {
106
2/2
✓ Branch 0 (3→4) taken 88 times.
✓ Branch 1 (3→5) taken 41 times.
129 if (buf[i] == ' ') {
107 88 width++;
108
1/2
✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→9) taken 41 times.
41 } else if (buf[i] == '\t') {
109 width = next_indent_width(width, tab_width);
110 } else {
111 break;
112 }
113 }
114 44 return width;
115 }
116
117 22 static ssize_t get_current_indent_bytes (
118 const char *buf,
119 size_t cursor_offset,
120 unsigned int iw,
121 unsigned int tw
122 ) {
123 22 size_t bytes = 0;
124 22 size_t width = 0;
125
126
2/2
✓ Branch 0 (11→3) taken 20 times.
✓ Branch 1 (11→12) taken 2 times.
22 for (size_t i = 0; i < cursor_offset; i++) {
127
1/2
✓ Branch 0 (4→5) taken 20 times.
✗ Branch 1 (4→6) not taken.
20 if (indent_remainder(width, iw) == 0) {
128 20 bytes = 0;
129 20 width = 0;
130 }
131
1/3
✗ Branch 0 (6→7) not taken.
✗ Branch 1 (6→9) not taken.
✓ Branch 2 (6→15) taken 20 times.
20 switch (buf[i]) {
132 case '\t':
133 width = next_indent_width(width, tw);
134 break;
135 case ' ':
136 width++;
137 break;
138 default:
139 // Cursor not at indentation
140 return -1;
141 }
142 bytes++;
143 }
144
145
1/2
✓ Branch 0 (13→14) taken 2 times.
✗ Branch 1 (13→15) not taken.
2 if (indent_remainder(width, iw)) {
146 // Cursor at middle of indentation level
147 return -1;
148 }
149
150 2 return (ssize_t)bytes;
151 }
152
153 3 size_t get_indent_level_bytes_left(const LocalOptions *options, const BlockIter *cursor)
154 {
155 3 BlockIter bol = *cursor;
156 3 size_t cursor_offset = block_iter_bol(&bol);
157
1/2
✓ Branch 0 (3→4) taken 3 times.
✗ Branch 1 (3→7) not taken.
3 if (cursor_offset == 0) {
158 return 0; // cursor at BOL
159 }
160
161 3 StringView line = block_iter_get_line(&bol);
162 3 unsigned int iw = options->indent_width;
163 3 unsigned int tw = options->tab_width;
164 3 ssize_t ibytes = get_current_indent_bytes(line.data, cursor_offset, iw, tw);
165 3 return MAX(ibytes, 0);
166 }
167
168 19 size_t get_indent_level_bytes_right(const LocalOptions *options, const BlockIter *cursor)
169 {
170 19 unsigned int iw = options->indent_width;
171 19 unsigned int tw = options->tab_width;
172 19 StringView line;
173 19 size_t cursor_offset = get_current_line_and_offset(cursor, &line);
174 19 ssize_t ibytes = get_current_indent_bytes(line.data, cursor_offset, iw, tw);
175
2/2
✓ Branch 0 (4→5) taken 2 times.
✓ Branch 1 (4→15) taken 17 times.
19 if (ibytes < 0) {
176 return 0;
177 }
178
179 2 size_t width = 0;
180
1/2
✓ Branch 0 (14→6) taken 3 times.
✗ Branch 1 (14→15) not taken.
3 for (size_t i = cursor_offset, n = line.length; i < n; i++) {
181
2/3
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→9) taken 1 times.
✓ Branch 2 (6→15) taken 2 times.
3 switch (line.data[i]) {
182 case '\t':
183 width = next_indent_width(width, tw);
184 break;
185 1 case ' ':
186 1 width++;
187 1 break;
188 default:
189 // No full indentation level at cursor position
190 return 0;
191 }
192
1/2
✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→13) taken 1 times.
1 if (indent_remainder(width, iw) == 0) {
193 return i - cursor_offset + 1;
194 }
195 }
196
197 return 0;
198 }
199