dte test coverage


Directory: ./
File: src/insert.c
Date: 2025-12-11 10:43:49
Coverage Exec Excl Total
Lines: 90.3% 93 0 103
Functions: 100.0% 7 0 7
Branches: 67.9% 38 0 56

Line Branch Exec Source
1 #include <stdlib.h>
2 #include <string.h>
3 #include "insert.h"
4 #include "block-iter.h"
5 #include "buffer.h"
6 #include "change.h"
7 #include "indent.h"
8 #include "options.h"
9 #include "selection.h"
10 #include "util/string.h"
11 #include "util/utf8.h"
12
13 77 void insert_text(View *view, const char *text, size_t size, bool move_after)
14 {
15
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 4 taken 76 times.
77 size_t del_count = view->selection ? prepare_selection(view) : 0;
16 77 unselect(view);
17 77 buffer_replace_bytes(view, del_count, text, size);
18
2/2
✓ Branch 6 → 7 taken 42 times.
✓ Branch 6 → 8 taken 35 times.
119 block_iter_skip_bytes(&view->cursor, move_after ? size : 0);
19 77 }
20
21 32 static size_t insert_nl_and_autoindent (
22 View *view,
23 StringView prev_line,
24 size_t del_count
25 ) {
26 32 String indent = get_indent_for_next_line(&view->buffer->options, prev_line);
27
2/2
✓ Branch 3 → 4 taken 14 times.
✓ Branch 3 → 8 taken 18 times.
32 if (indent.len == 0) {
28 14 BUG_ON(indent.alloc > 0); // Should be nothing to free
29 14 buffer_replace_bytes(view, del_count, "\n", 1);
30 14 return 1;
31 }
32
33 18 string_insert_buf(&indent, 0, "\n", 1); // Prepend newline to indent
34 18 size_t ins_count = indent.len; // Get length, before string_free() clears it
35 18 buffer_replace_bytes(view, del_count, indent.buffer, ins_count);
36 18 string_free(&indent);
37 18 return ins_count;
38 }
39
40 8 void new_line(View *view, bool auto_indent, bool above_cursor)
41 {
42
4/4
✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 8 taken 6 times.
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 8 taken 1 time.
8 if (above_cursor && block_iter_prev_line(&view->cursor) == 0) {
43 // Already on first line; insert newline at bof
44 1 block_iter_bof(&view->cursor);
45 1 buffer_insert_bytes(view, "\n", 1);
46 1 return;
47 }
48
49 7 BlockIter tmp = view->cursor;
50
2/4
✓ Branch 8 → 9 taken 7 times.
✗ Branch 8 → 12 not taken.
✓ Branch 10 → 11 taken 7 times.
✗ Branch 10 → 12 not taken.
7 bool use_ref_line = auto_indent && block_iter_find_non_empty_line_bwd(&tmp);
51 7 StringView line = use_ref_line ? block_iter_get_line(&tmp) : strview(NULL);
52 7 block_iter_eol(&view->cursor);
53
54 7 size_t ins_count = insert_nl_and_autoindent(view, line, 0);
55 7 block_iter_skip_bytes(&view->cursor, ins_count);
56 }
57
58 // Go to beginning of whitespace (tabs and spaces) under cursor and
59 // return number of whitespace bytes surrounding cursor
60 24 static size_t goto_beginning_of_whitespace(BlockIter *cursor)
61 {
62 24 BlockIter tmp = *cursor;
63 24 return block_iter_skip_blanks_fwd(&tmp) + block_iter_skip_blanks_bwd(cursor);
64 }
65
66 25 static void insert_nl(View *view)
67 {
68 // Prepare deleted text (selection or whitespace around cursor)
69 25 size_t del_count = 0;
70
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 5 taken 24 times.
25 if (view->selection) {
71 1 del_count = prepare_selection(view);
72 1 unselect(view);
73 } else {
74 // Trim whitespace around cursor
75 24 del_count = goto_beginning_of_whitespace(&view->cursor);
76 }
77
78 // Get reference line, for calculating auto-indent size (if applicable)
79 25 StringView line = STRING_VIEW_INIT;
80
1/2
✓ Branch 6 → 7 taken 25 times.
✗ Branch 6 → 17 not taken.
25 if (view->buffer->options.auto_indent) {
81 25 BlockIter bi = view->cursor;
82 25 size_t len = block_iter_bol(&bi);
83 25 line = block_iter_get_line(&bi);
84 25 line.length = len; // Current line will be split at cursor position
85
2/2
✓ Branch 9 → 10 taken 5 times.
✓ Branch 9 → 16 taken 20 times.
25 if (strview_isblank(line)) {
86 // This line is (or will become) whitespace only; find previous,
87 // non-whitespace line
88
2/4
✓ Branch 11 → 12 taken 5 times.
✗ Branch 11 → 15 not taken.
✓ Branch 13 → 14 taken 5 times.
✗ Branch 13 → 15 not taken.
5 if (block_iter_prev_line(&bi) && block_iter_find_non_empty_line_bwd(&bi)) {
89 5 line = block_iter_get_line(&bi);
90 } else {
91 line.length = 0;
92 }
93 }
94 }
95
96 25 begin_change(CHANGE_MERGE_NONE);
97 25 size_t ins_count = insert_nl_and_autoindent(view, line, del_count);
98 25 end_change();
99 25 block_iter_skip_bytes(&view->cursor, ins_count); // Move after inserted text
100 25 }
101
102 1 static int get_indent_of_matching_brace(const View *view)
103 {
104 1 unsigned int tab_width = view->buffer->options.tab_width;
105 1 BlockIter bi = view->cursor;
106 1 int level = 0;
107
108
1/2
✓ Branch 14 → 3 taken 4 times.
✗ Branch 14 → 15 not taken.
4 while (block_iter_prev_line(&bi)) {
109 4 StringView line = block_iter_get_line(&bi);
110
2/2
✓ Branch 5 → 6 taken 1 time.
✓ Branch 5 → 9 taken 3 times.
4 if (line_has_opening_brace(line)) {
111
1/2
✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 9 not taken.
1 if (level++ == 0) {
112 1 return get_indent_width(line, tab_width);
113 }
114 }
115
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 3 times.
3 if (line_has_closing_brace(line)) {
116 level--;
117 }
118 }
119
120 return -1;
121 }
122
123 147 void insert_ch(View *view, CodePoint ch)
124 {
125
2/2
✓ Branch 2 → 3 taken 25 times.
✓ Branch 2 → 5 taken 122 times.
147 if (ch == '\n') {
126 25 insert_nl(view);
127 25 return;
128 }
129
130 122 const Buffer *buffer = view->buffer;
131 122 const LocalOptions *options = &buffer->options;
132 122 char buf[8];
133 122 char *ins = buf;
134 122 char *alloc = NULL;
135 122 size_t del_count = 0;
136 122 size_t ins_count = 0;
137
138
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 122 times.
122 if (view->selection) {
139 // Prepare text to be deleted (selection)
140 del_count = prepare_selection(view);
141 unselect(view);
142
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 12 taken 122 times.
122 } else if (options->overwrite) {
143 // Delete character under cursor unless we're at end of line
144 BlockIter bi = view->cursor;
145 del_count = block_iter_is_eol(&bi) ? 0 : block_iter_next_column(&bi);
146
4/6
✓ Branch 12 → 13 taken 1 time.
✓ Branch 12 → 27 taken 121 times.
✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 27 not taken.
✓ Branch 14 → 15 taken 1 time.
✗ Branch 14 → 27 not taken.
122 } else if (ch == '}' && options->auto_indent && options->brace_indent) {
147 1 StringView line = get_current_line(view->cursor);
148
1/2
✓ Branch 16 → 17 taken 1 time.
✗ Branch 16 → 26 not taken.
1 if (strview_isblank(line)) {
149 1 int width = get_indent_of_matching_brace(view);
150
1/2
✓ Branch 18 → 19 taken 1 time.
✗ Branch 18 → 26 not taken.
1 if (width >= 0) {
151 // Replace current (whitespace only) line with some indent + '}'
152 1 block_iter_bol(&view->cursor);
153 1 del_count = line.length;
154
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 26 taken 1 time.
1 if (width) {
155 String indent = make_indent(options, width);
156 BUG_ON(!indent.len); // `width > 0` never produces `indent.len == 0`
157 ins_count = indent.len;
158 ins = alloc = string_steal_cstring(&indent);
159 // '}' will be replace the terminating NUL
160 }
161 }
162 }
163 }
164
165 // Prepare text to be inserted
166
3/4
✓ Branch 27 → 28 taken 12 times.
✓ Branch 27 → 30 taken 110 times.
✓ Branch 28 → 29 taken 12 times.
✗ Branch 28 → 30 not taken.
122 if (ch == '\t' && options->expand_tab) {
167 12 static_assert(sizeof(buf) >= INDENT_WIDTH_MAX);
168 12 ins_count = options->indent_width;
169 12 memset(ins, ' ', ins_count);
170 } else {
171 110 static_assert(sizeof(buf) >= UTF8_MAX_SEQ_LEN);
172 110 ins_count += u_set_char_raw(ins + ins_count, ch);
173 }
174
175 // Make edit to Buffer (and record Change in undo history)
176 122 begin_change(del_count ? CHANGE_MERGE_NONE : CHANGE_MERGE_INSERT);
177 122 buffer_replace_bytes(view, del_count, ins, ins_count);
178 122 end_change();
179 122 free(alloc);
180
181 // Move cursor after inserted text
182 122 block_iter_skip_bytes(&view->cursor, ins_count);
183 }
184