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