src/indent.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <stdlib.h> | ||
| 2 | #include <string.h> | ||
| 3 | #include <sys/types.h> | ||
| 4 | #include "indent.h" | ||
| 5 | #include "block-iter.h" | ||
| 6 | #include "buffer.h" | ||
| 7 | #include "change.h" | ||
| 8 | #include "move.h" | ||
| 9 | #include "regexp.h" | ||
| 10 | #include "selection.h" | ||
| 11 | #include "util/log.h" | ||
| 12 | #include "util/xmalloc.h" | ||
| 13 | |||
| 14 | 51 | String make_indent(const LocalOptions *options, size_t width) | |
| 15 | { | ||
| 16 |
2/2✓ Branch 2 → 3 taken 20 times.
✓ Branch 2 → 4 taken 31 times.
|
51 | if (width == 0) { |
| 17 | 20 | return string_new(0); | |
| 18 | } | ||
| 19 | |||
| 20 | 31 | bool use_spaces = use_spaces_for_indent(options); | |
| 21 | 31 | size_t tw = options->tab_width; | |
| 22 |
2/2✓ Branch 4 → 5 taken 8 times.
✓ Branch 4 → 7 taken 23 times.
|
31 | size_t ntabs = use_spaces ? 0 : indent_level(width, tw); |
| 23 | 8 | size_t nspaces = use_spaces ? width : indent_remainder(width, tw); | |
| 24 | 31 | size_t nbytes = ntabs + nspaces; | |
| 25 | 31 | BUG_ON(nbytes == 0); | |
| 26 | |||
| 27 | 31 | String str = string_new(nbytes + 1); // +1 for efficiency in several callers | |
| 28 | 31 | memset(str.buffer, '\t', ntabs); | |
| 29 | 31 | memset(str.buffer + ntabs, ' ', nspaces); | |
| 30 | 31 | str.len = nbytes; | |
| 31 | 31 | return str; | |
| 32 | } | ||
| 33 | |||
| 34 | 10 | static String make_simple_indent(const LocalOptions *options, size_t level) | |
| 35 | { | ||
| 36 | 10 | bool use_spaces = use_spaces_for_indent(options); | |
| 37 |
2/2✓ Branch 2 → 3 taken 9 times.
✓ Branch 2 → 4 taken 1 time.
|
10 | size_t nbytes = use_spaces ? level * options->indent_width : level; |
| 38 | 10 | String indent = STRING_INIT; | |
| 39 |
2/2✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 9 times.
|
11 | string_append_memset(&indent, use_spaces ? ' ' : '\t', nbytes); |
| 40 | 10 | return indent; | |
| 41 | } | ||
| 42 | |||
| 43 | // Return true if the contents of `line` triggers an additional level | ||
| 44 | // of auto-indent on the next line | ||
| 45 | 37 | static bool line_contents_increases_indent ( | |
| 46 | const LocalOptions *options, | ||
| 47 | StringView line | ||
| 48 | ) { | ||
| 49 |
2/2✓ Branch 2 → 3 taken 36 times.
✓ Branch 2 → 16 taken 1 time.
|
37 | if (!line.length) { |
| 50 | return false; | ||
| 51 | } | ||
| 52 | |||
| 53 | 36 | static const regex_t *re1, *re2; | |
| 54 |
2/2✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 7 taken 35 times.
|
36 | if (!re1) { |
| 55 | // TODO: Make these patterns configurable via a local option | ||
| 56 | 1 | re1 = regexp_compile_or_fatal_error("\\{[\t ]*(//.*|/\\*.*\\*/[\t ]*)?$"); | |
| 57 | 1 | re2 = regexp_compile_or_fatal_error("\\}[\t ]*(//.*|/\\*.*\\*/[\t ]*)?$"); | |
| 58 | } | ||
| 59 | |||
| 60 |
2/2✓ Branch 7 → 8 taken 13 times.
✓ Branch 7 → 12 taken 23 times.
|
36 | if (options->brace_indent) { |
| 61 |
2/2✓ Branch 9 → 10 taken 12 times.
✓ Branch 9 → 16 taken 1 time.
|
13 | if (regexp_exec(re1, line.data, line.length, 0, NULL, 0)) { |
| 62 | return true; | ||
| 63 | } | ||
| 64 |
1/2✓ Branch 11 → 12 taken 12 times.
✗ Branch 11 → 16 not taken.
|
12 | if (regexp_exec(re2, line.data, line.length, 0, NULL, 0)) { |
| 65 | return false; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | 35 | const InternedRegexp *ir = options->indent_regex; | |
| 70 |
2/2✓ Branch 12 → 13 taken 2 times.
✓ Branch 12 → 16 taken 33 times.
|
35 | if (!ir) { |
| 71 | return false; | ||
| 72 | } | ||
| 73 | |||
| 74 | 2 | BUG_ON(ir->str[0] == '\0'); | |
| 75 | 2 | return regexp_exec(&ir->re, line.data, line.length, 0, NULL, 0); | |
| 76 | } | ||
| 77 | |||
| 78 | 37 | String get_indent_for_next_line(const LocalOptions *options, StringView line) | |
| 79 | { | ||
| 80 | 37 | size_t curr_width = get_indent_width(line, options->tab_width); | |
| 81 | 37 | size_t next_width = next_indent_width(curr_width, options->indent_width); | |
| 82 | 37 | bool increase = line_contents_increases_indent(options, line); | |
| 83 |
2/2✓ Branch 5 → 6 taken 35 times.
✓ Branch 5 → 7 taken 2 times.
|
72 | return make_indent(options, increase ? next_width : curr_width); |
| 84 | } | ||
| 85 | |||
| 86 | 64 | IndentInfo get_indent_info(const LocalOptions *options, StringView line) | |
| 87 | { | ||
| 88 | 64 | const char *buf = line.data; | |
| 89 | 64 | const size_t len = line.length; | |
| 90 | 64 | const size_t tw = options->tab_width; | |
| 91 | 64 | const size_t iw = options->indent_width; | |
| 92 | 64 | const bool space_indent = use_spaces_for_indent(options); | |
| 93 | 64 | IndentInfo info = {.sane = true}; | |
| 94 | 64 | size_t spaces = 0; | |
| 95 | 64 | size_t tabs = 0; | |
| 96 | 64 | size_t pos = 0; | |
| 97 | |||
| 98 |
2/2✓ Branch 16 → 3 taken 631 times.
✓ Branch 16 → 17 taken 5 times.
|
636 | for (; pos < len; pos++) { |
| 99 |
2/2✓ Branch 3 → 4 taken 564 times.
✓ Branch 3 → 5 taken 67 times.
|
631 | if (buf[pos] == ' ') { |
| 100 | 564 | info.width++; | |
| 101 | 564 | spaces++; | |
| 102 |
2/2✓ Branch 5 → 6 taken 8 times.
✓ Branch 5 → 17 taken 59 times.
|
67 | } else if (buf[pos] == '\t') { |
| 103 | 8 | info.width = next_indent_width(info.width, tw); | |
| 104 | 8 | tabs++; | |
| 105 | } else { | ||
| 106 | break; | ||
| 107 | } | ||
| 108 |
4/4✓ Branch 9 → 10 taken 146 times.
✓ Branch 9 → 15 taken 426 times.
✓ Branch 10 → 11 taken 145 times.
✓ Branch 10 → 15 taken 1 time.
|
572 | if (indent_remainder(info.width, iw) == 0 && info.sane) { |
| 109 |
2/2✓ Branch 11 → 12 taken 137 times.
✓ Branch 11 → 13 taken 8 times.
|
145 | info.sane = space_indent ? !tabs : !spaces; |
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | 64 | info.level = indent_level(info.width, iw); | |
| 114 | 64 | info.wsonly = (pos == len); | |
| 115 | 64 | info.bytes = spaces + tabs; | |
| 116 | 64 | return info; | |
| 117 | } | ||
| 118 | |||
| 119 | 49 | size_t get_indent_width(StringView line, unsigned int tab_width) | |
| 120 | { | ||
| 121 | 49 | const char *buf = line.data; | |
| 122 | 49 | size_t width = 0; | |
| 123 |
2/2✓ Branch 8 → 3 taken 139 times.
✓ Branch 8 → 9 taken 4 times.
|
143 | for (size_t i = 0, n = line.length; i < n; i++) { |
| 124 |
2/2✓ Branch 3 → 4 taken 88 times.
✓ Branch 3 → 5 taken 51 times.
|
139 | if (buf[i] == ' ') { |
| 125 | 88 | width++; | |
| 126 |
2/2✓ Branch 5 → 6 taken 6 times.
✓ Branch 5 → 9 taken 45 times.
|
51 | } else if (buf[i] == '\t') { |
| 127 | 6 | width = next_indent_width(width, tab_width); | |
| 128 | } else { | ||
| 129 | break; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | 49 | return width; | |
| 133 | } | ||
| 134 | |||
| 135 | 22 | static ssize_t get_current_indent_bytes ( | |
| 136 | const char *buf, | ||
| 137 | size_t cursor_offset, | ||
| 138 | unsigned int iw, | ||
| 139 | unsigned int tw | ||
| 140 | ) { | ||
| 141 | 22 | size_t bytes = 0; | |
| 142 | 22 | size_t width = 0; | |
| 143 | |||
| 144 |
2/2✓ Branch 11 → 3 taken 20 times.
✓ Branch 11 → 12 taken 2 times.
|
22 | for (size_t i = 0; i < cursor_offset; i++) { |
| 145 |
1/2✓ Branch 4 → 5 taken 20 times.
✗ Branch 4 → 6 not taken.
|
20 | if (indent_remainder(width, iw) == 0) { |
| 146 | 20 | bytes = 0; | |
| 147 | 20 | width = 0; | |
| 148 | } | ||
| 149 |
1/3✗ Branch 6 → 7 not taken.
✗ Branch 6 → 9 not taken.
✓ Branch 6 → 15 taken 20 times.
|
20 | switch (buf[i]) { |
| 150 | ✗ | case '\t': | |
| 151 | ✗ | width = next_indent_width(width, tw); | |
| 152 | ✗ | break; | |
| 153 | ✗ | case ' ': | |
| 154 | ✗ | width++; | |
| 155 | ✗ | break; | |
| 156 | default: | ||
| 157 | // Cursor not at indentation | ||
| 158 | return -1; | ||
| 159 | } | ||
| 160 | ✗ | bytes++; | |
| 161 | } | ||
| 162 | |||
| 163 |
1/2✓ Branch 13 → 14 taken 2 times.
✗ Branch 13 → 15 not taken.
|
2 | if (indent_remainder(width, iw)) { |
| 164 | // Cursor at middle of indentation level | ||
| 165 | return -1; | ||
| 166 | } | ||
| 167 | |||
| 168 | 2 | return (ssize_t)bytes; | |
| 169 | } | ||
| 170 | |||
| 171 | 3 | size_t get_indent_level_bytes_left(const LocalOptions *options, const BlockIter *cursor) | |
| 172 | { | ||
| 173 | 3 | BlockIter bol = *cursor; | |
| 174 | 3 | size_t cursor_offset = block_iter_bol(&bol); | |
| 175 |
1/2✓ Branch 3 → 4 taken 3 times.
✗ Branch 3 → 7 not taken.
|
3 | if (cursor_offset == 0) { |
| 176 | return 0; // cursor at BOL | ||
| 177 | } | ||
| 178 | |||
| 179 | 3 | StringView line = block_iter_get_line(&bol); | |
| 180 | 3 | unsigned int iw = options->indent_width; | |
| 181 | 3 | unsigned int tw = options->tab_width; | |
| 182 | 3 | ssize_t ibytes = get_current_indent_bytes(line.data, cursor_offset, iw, tw); | |
| 183 | 3 | return MAX(ibytes, 0); | |
| 184 | } | ||
| 185 | |||
| 186 | 19 | size_t get_indent_level_bytes_right(const LocalOptions *options, const BlockIter *cursor) | |
| 187 | { | ||
| 188 | 19 | unsigned int iw = options->indent_width; | |
| 189 | 19 | unsigned int tw = options->tab_width; | |
| 190 | 19 | CurrentLineRef lr = get_current_line_and_offset(*cursor); | |
| 191 | 19 | ssize_t ibytes = get_current_indent_bytes(lr.line.data, lr.cursor_offset, iw, tw); | |
| 192 |
2/2✓ Branch 4 → 5 taken 2 times.
✓ Branch 4 → 15 taken 17 times.
|
19 | if (ibytes < 0) { |
| 193 | return 0; | ||
| 194 | } | ||
| 195 | |||
| 196 | 2 | size_t width = 0; | |
| 197 |
1/2✓ Branch 14 → 6 taken 3 times.
✗ Branch 14 → 15 not taken.
|
3 | for (size_t i = lr.cursor_offset, n = lr.line.length; i < n; i++) { |
| 198 |
2/3✗ Branch 6 → 7 not taken.
✓ Branch 6 → 9 taken 1 time.
✓ Branch 6 → 15 taken 2 times.
|
3 | switch (lr.line.data[i]) { |
| 199 | ✗ | case '\t': | |
| 200 | ✗ | width = next_indent_width(width, tw); | |
| 201 | ✗ | break; | |
| 202 | 1 | case ' ': | |
| 203 | 1 | width++; | |
| 204 | 1 | break; | |
| 205 | default: | ||
| 206 | // No full indentation level at cursor position | ||
| 207 | return 0; | ||
| 208 | } | ||
| 209 |
1/2✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 1 time.
|
1 | if (indent_remainder(width, iw) == 0) { |
| 210 | ✗ | return i - lr.cursor_offset + 1; | |
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | return 0; | ||
| 215 | } | ||
| 216 | |||
| 217 | 10 | static void increase_indent(View *view, size_t lines, size_t levels) | |
| 218 | { | ||
| 219 | 10 | BUG_ON(lines == 0); | |
| 220 | 10 | BUG_ON(levels == 0); | |
| 221 | 10 | BUG_ON(!block_iter_is_bol(&view->cursor)); | |
| 222 | 10 | const LocalOptions *options = &view->buffer->options; | |
| 223 | 10 | String indent = make_simple_indent(options, levels); | |
| 224 | 10 | size_t i = 0; | |
| 225 | |||
| 226 | 30 | do { | |
| 227 | 30 | StringView line = block_iter_get_line(&view->cursor); | |
| 228 | 30 | IndentInfo info = get_indent_info(options, line); | |
| 229 |
1/2✗ Branch 11 → 12 not taken.
✓ Branch 11 → 14 taken 30 times.
|
30 | if (info.wsonly) { |
| 230 | ✗ | if (info.bytes) { | |
| 231 | // Remove indentation | ||
| 232 | ✗ | buffer_delete_bytes(view, info.bytes); | |
| 233 | } | ||
| 234 |
1/2✓ Branch 14 → 15 taken 30 times.
✗ Branch 14 → 16 not taken.
|
30 | } else if (info.sane) { |
| 235 | // Insert whitespace | ||
| 236 | 30 | buffer_insert_bytes(view, indent.buffer, indent.len); | |
| 237 | } else { | ||
| 238 | // Replace whole indentation with sane one | ||
| 239 | ✗ | String rep = make_simple_indent(options, info.level + levels); | |
| 240 | ✗ | buffer_replace_bytes(view, info.bytes, rep.buffer, rep.len); | |
| 241 | ✗ | string_free(&rep); | |
| 242 | } | ||
| 243 |
3/4✓ Branch 20 → 21 taken 20 times.
✓ Branch 20 → 23 taken 10 times.
✓ Branch 22 → 9 taken 20 times.
✗ Branch 22 → 23 not taken.
|
30 | } while (++i < lines && block_iter_eat_line(&view->cursor)); |
| 244 | |||
| 245 |
1/2✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 10 times.
|
10 | WARN_ON(i != lines); |
| 246 | 10 | string_free(&indent); | |
| 247 | 10 | } | |
| 248 | |||
| 249 | 9 | static void decrease_indent(View *view, size_t lines, size_t levels) | |
| 250 | { | ||
| 251 | 9 | BUG_ON(lines == 0); | |
| 252 | 9 | BUG_ON(levels == 0); | |
| 253 | 9 | BUG_ON(!block_iter_is_bol(&view->cursor)); | |
| 254 | 9 | const LocalOptions *options = &view->buffer->options; | |
| 255 | 9 | const size_t indent_width = options->indent_width; | |
| 256 | 9 | const bool space_indent = use_spaces_for_indent(options); | |
| 257 | 9 | size_t i = 0; | |
| 258 | |||
| 259 | 25 | do { | |
| 260 | 25 | StringView line = block_iter_get_line(&view->cursor); | |
| 261 | 25 | IndentInfo info = get_indent_info(options, line); | |
| 262 |
1/2✗ Branch 11 → 12 not taken.
✓ Branch 11 → 14 taken 25 times.
|
25 | if (info.wsonly) { |
| 263 | ✗ | if (info.bytes) { | |
| 264 | // Remove indentation | ||
| 265 | ✗ | buffer_delete_bytes(view, info.bytes); | |
| 266 | } | ||
| 267 |
3/4✓ Branch 14 → 15 taken 23 times.
✓ Branch 14 → 20 taken 2 times.
✓ Branch 15 → 16 taken 23 times.
✗ Branch 15 → 20 not taken.
|
48 | } else if (info.level && info.sane) { |
| 268 | 23 | size_t n = MIN(levels, info.level); | |
| 269 |
1/2✓ Branch 16 → 17 taken 23 times.
✗ Branch 16 → 18 not taken.
|
23 | if (space_indent) { |
| 270 | 23 | n *= indent_width; | |
| 271 | } | ||
| 272 | 23 | buffer_delete_bytes(view, n); | |
| 273 |
1/2✗ Branch 20 → 21 not taken.
✓ Branch 20 → 27 taken 2 times.
|
2 | } else if (info.bytes) { |
| 274 | // Replace whole indentation with sane one | ||
| 275 | ✗ | if (info.level > levels) { | |
| 276 | ✗ | String indent = make_simple_indent(options, info.level - levels); | |
| 277 | ✗ | buffer_replace_bytes(view, info.bytes, indent.buffer, indent.len); | |
| 278 | ✗ | string_free(&indent); | |
| 279 | } else { | ||
| 280 | ✗ | buffer_delete_bytes(view, info.bytes); | |
| 281 | } | ||
| 282 | } | ||
| 283 |
3/4✓ Branch 27 → 28 taken 16 times.
✓ Branch 27 → 30 taken 9 times.
✓ Branch 29 → 9 taken 16 times.
✗ Branch 29 → 30 not taken.
|
25 | } while (++i < lines && block_iter_eat_line(&view->cursor)); |
| 284 | |||
| 285 |
1/2✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 9 times.
|
9 | WARN_ON(i != lines); |
| 286 | 9 | } | |
| 287 | |||
| 288 | 19 | static void do_indent_lines(View *view, size_t lines, int levels) | |
| 289 | { | ||
| 290 | 19 | begin_change_chain(); | |
| 291 | 19 | block_iter_bol(&view->cursor); | |
| 292 |
2/2✓ Branch 4 → 5 taken 10 times.
✓ Branch 4 → 6 taken 9 times.
|
19 | if (levels > 0) { |
| 293 | 10 | increase_indent(view, lines, levels); | |
| 294 | } else { | ||
| 295 | 9 | decrease_indent(view, lines, -levels); | |
| 296 | } | ||
| 297 | 19 | end_change_chain(view); | |
| 298 | 19 | } | |
| 299 | |||
| 300 | 20 | void indent_lines(View *view, int levels) | |
| 301 | { | ||
| 302 |
2/2✓ Branch 2 → 3 taken 19 times.
✓ Branch 2 → 26 taken 1 time.
|
20 | if (unlikely(levels == 0)) { |
| 303 | 1 | return; | |
| 304 | } | ||
| 305 | |||
| 306 | 19 | int width = view->buffer->options.indent_width; | |
| 307 | 19 | BUG_ON(width < 1 || width > INDENT_WIDTH_MAX); | |
| 308 | 19 | long x = view_get_preferred_x(view) + (levels * width); | |
| 309 | 19 | x = MAX(x, 0); | |
| 310 | |||
| 311 |
2/2✓ Branch 6 → 7 taken 10 times.
✓ Branch 6 → 9 taken 9 times.
|
19 | if (view->selection == SELECT_NONE) { |
| 312 | 10 | do_indent_lines(view, 1, levels); | |
| 313 | 10 | goto out; | |
| 314 | } | ||
| 315 | |||
| 316 | 9 | view->selection = SELECT_LINES; | |
| 317 | 9 | SelectionInfo info = init_selection(view); | |
| 318 | 9 | view->cursor = info.si; | |
| 319 | 9 | size_t lines = get_nr_selected_lines(&info); | |
| 320 |
1/2✓ Branch 11 → 12 taken 9 times.
✗ Branch 11 → 26 not taken.
|
9 | if (unlikely(lines == 0)) { |
| 321 | return; | ||
| 322 | } | ||
| 323 | |||
| 324 | 9 | do_indent_lines(view, lines, levels); | |
| 325 |
1/2✓ Branch 13 → 14 taken 9 times.
✗ Branch 13 → 19 not taken.
|
9 | if (info.swapped) { |
| 326 | // Cursor should be at beginning of selection | ||
| 327 | 9 | block_iter_bol(&view->cursor); | |
| 328 | 9 | view->sel_so = block_iter_get_offset(&view->cursor); | |
| 329 |
2/2✓ Branch 18 → 17 taken 36 times.
✓ Branch 18 → 24 taken 9 times.
|
45 | while (--lines) { |
| 330 | 36 | block_iter_prev_line(&view->cursor); | |
| 331 | } | ||
| 332 | } else { | ||
| 333 | ✗ | BlockIter save = view->cursor; | |
| 334 | ✗ | while (--lines) { | |
| 335 | ✗ | block_iter_prev_line(&view->cursor); | |
| 336 | } | ||
| 337 | ✗ | view->sel_so = block_iter_get_offset(&view->cursor); | |
| 338 | ✗ | view->cursor = save; | |
| 339 | } | ||
| 340 | |||
| 341 | 19 | out: | |
| 342 | 19 | move_to_preferred_x(view, x); | |
| 343 | } | ||
| 344 |