dte test coverage


Directory: ./
File: src/insert.c
Date: 2026-01-09 16:07:09
Coverage Exec Excl Total
Lines: 91.5% 108 0 118
Functions: 100.0% 7 0 7
Branches: 70.0% 42 0 60

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 82 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 81 times.
82 size_t del_count = view->selection ? prepare_selection(view) : 0;
16 82 unselect(view);
17 82 buffer_replace_bytes(view, del_count, text, size);
18
2/2
✓ Branch 6 → 7 taken 47 times.
✓ Branch 6 → 8 taken 35 times.
129 block_iter_skip_bytes(&view->cursor, move_after ? size : 0);
19 82 }
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 12 void new_line(View *view, bool above_cursor, NewlineIndentType indent_type)
41 {
42 12 BlockIter *cursor = &view->cursor;
43
2/2
✓ Branch 2 → 3 taken 4 times.
✓ Branch 2 → 14 taken 8 times.
12 if (indent_type == NLI_COPY_INDENT) {
44 4 const LocalOptions *options = &view->buffer->options;
45 4 StringView line = get_current_line(*cursor);
46 4 size_t width = get_indent_width(line, options->tab_width);
47 4 String indent_and_nl = make_indent(options, width);
48 4 string_append_byte(&indent_and_nl, '\n');
49
50
2/2
✓ Branch 7 → 8 taken 2 times.
✓ Branch 7 → 9 taken 2 times.
4 if (above_cursor) {
51 2 block_iter_bol(cursor);
52 } else {
53 2 block_iter_eat_line(cursor);
54 }
55
56 4 buffer_insert_bytes(view, indent_and_nl.buffer, indent_and_nl.len);
57 4 block_iter_skip_bytes(cursor, indent_and_nl.len - 1);
58 4 string_free(&indent_and_nl);
59 4 return;
60 }
61
62
4/4
✓ Branch 14 → 15 taken 2 times.
✓ Branch 14 → 19 taken 6 times.
✓ Branch 16 → 17 taken 1 time.
✓ Branch 16 → 19 taken 1 time.
8 if (above_cursor && !block_iter_prev_line(cursor)) {
63 // The NLI_AUTO_INDENT and NLI_NO_INDENT types are handled
64 // by simulating a newline insertion (with or without an
65 // auto-indent). Inserting above the cursor when already
66 // on the first line is a special case, both because the
67 // positioning attempted by block_iter_prev_line() failed
68 // and because an auto-indent is never needed. Thus, we
69 // simply insert a newline at BOF/BOL instead.
70 1 block_iter_bof(cursor);
71 1 buffer_insert_bytes(view, "\n", 1);
72 1 return;
73 }
74
75 7 BlockIter tmp = *cursor;
76 7 bool auto_indent = (indent_type == NLI_AUTO_INDENT);
77
2/4
✓ Branch 19 → 20 taken 7 times.
✗ Branch 19 → 23 not taken.
✓ Branch 21 → 22 taken 7 times.
✗ Branch 21 → 23 not taken.
7 bool use_ref_line = auto_indent && block_iter_find_non_empty_line_bwd(&tmp);
78 7 StringView line = use_ref_line ? block_iter_get_line(&tmp) : strview(NULL);
79 7 block_iter_eol(cursor);
80
81 7 size_t ins_count = insert_nl_and_autoindent(view, line, 0);
82 7 block_iter_skip_bytes(cursor, ins_count);
83 }
84
85 // Go to beginning of whitespace (tabs and spaces) under cursor and
86 // return number of whitespace bytes surrounding cursor
87 24 static size_t goto_beginning_of_whitespace(BlockIter *cursor)
88 {
89 24 BlockIter tmp = *cursor;
90 24 return block_iter_skip_blanks_fwd(&tmp) + block_iter_skip_blanks_bwd(cursor);
91 }
92
93 25 static void insert_nl(View *view)
94 {
95 // Prepare deleted text (selection or whitespace around cursor)
96 25 size_t del_count = 0;
97
2/2
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 5 taken 24 times.
25 if (view->selection) {
98 1 del_count = prepare_selection(view);
99 1 unselect(view);
100 } else {
101 // Trim whitespace around cursor
102 24 del_count = goto_beginning_of_whitespace(&view->cursor);
103 }
104
105 // Get reference line, for calculating auto-indent size (if applicable)
106 25 StringView line = STRING_VIEW_INIT;
107
1/2
✓ Branch 6 → 7 taken 25 times.
✗ Branch 6 → 17 not taken.
25 if (view->buffer->options.auto_indent) {
108 25 BlockIter bi = view->cursor;
109 25 size_t len = block_iter_bol(&bi);
110 25 line = block_iter_get_line(&bi);
111 25 line.length = len; // Current line will be split at cursor position
112
2/2
✓ Branch 9 → 10 taken 5 times.
✓ Branch 9 → 16 taken 20 times.
25 if (strview_isblank(line)) {
113 // This line is (or will become) whitespace only; find previous,
114 // non-whitespace line
115
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)) {
116 5 line = block_iter_get_line(&bi);
117 } else {
118 line.length = 0;
119 }
120 }
121 }
122
123 25 begin_change(CHANGE_MERGE_NONE);
124 25 size_t ins_count = insert_nl_and_autoindent(view, line, del_count);
125 25 end_change();
126 25 block_iter_skip_bytes(&view->cursor, ins_count); // Move after inserted text
127 25 }
128
129 1 static int get_indent_of_matching_brace(const View *view)
130 {
131 1 unsigned int tab_width = view->buffer->options.tab_width;
132 1 BlockIter bi = view->cursor;
133 1 int level = 0;
134
135
1/2
✓ Branch 14 → 3 taken 4 times.
✗ Branch 14 → 15 not taken.
4 while (block_iter_prev_line(&bi)) {
136 4 StringView line = block_iter_get_line(&bi);
137
2/2
✓ Branch 5 → 6 taken 1 time.
✓ Branch 5 → 9 taken 3 times.
4 if (line_has_opening_brace(line)) {
138
1/2
✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 9 not taken.
1 if (level++ == 0) {
139 1 return get_indent_width(line, tab_width);
140 }
141 }
142
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 3 times.
3 if (line_has_closing_brace(line)) {
143 level--;
144 }
145 }
146
147 return -1;
148 }
149
150 147 void insert_ch(View *view, CodePoint ch)
151 {
152
2/2
✓ Branch 2 → 3 taken 25 times.
✓ Branch 2 → 5 taken 122 times.
147 if (ch == '\n') {
153 25 insert_nl(view);
154 25 return;
155 }
156
157 122 const Buffer *buffer = view->buffer;
158 122 const LocalOptions *options = &buffer->options;
159 122 char buf[8];
160 122 char *ins = buf;
161 122 char *alloc = NULL;
162 122 size_t del_count = 0;
163 122 size_t ins_count = 0;
164
165
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 122 times.
122 if (view->selection) {
166 // Prepare text to be deleted (selection)
167 del_count = prepare_selection(view);
168 unselect(view);
169
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 12 taken 122 times.
122 } else if (options->overwrite) {
170 // Delete character under cursor unless we're at end of line
171 BlockIter bi = view->cursor;
172 del_count = block_iter_is_eol(&bi) ? 0 : block_iter_next_column(&bi);
173
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) {
174 1 StringView line = get_current_line(view->cursor);
175
1/2
✓ Branch 16 → 17 taken 1 time.
✗ Branch 16 → 26 not taken.
1 if (strview_isblank(line)) {
176 1 int width = get_indent_of_matching_brace(view);
177
1/2
✓ Branch 18 → 19 taken 1 time.
✗ Branch 18 → 26 not taken.
1 if (width >= 0) {
178 // Replace current (whitespace only) line with some indent + '}'
179 1 block_iter_bol(&view->cursor);
180 1 del_count = line.length;
181
1/2
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 26 taken 1 time.
1 if (width) {
182 String indent = make_indent(options, width);
183 BUG_ON(!indent.len); // `width > 0` never produces `indent.len == 0`
184 ins_count = indent.len;
185 ins = alloc = string_steal_cstring(&indent);
186 // '}' will be replace the terminating NUL
187 }
188 }
189 }
190 }
191
192 // Prepare text to be inserted
193
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) {
194 12 static_assert(sizeof(buf) >= INDENT_WIDTH_MAX);
195 12 ins_count = options->indent_width;
196 12 memset(ins, ' ', ins_count);
197 } else {
198 110 static_assert(sizeof(buf) >= UTF8_MAX_SEQ_LEN);
199 110 ins_count += u_set_char_raw(ins + ins_count, ch);
200 }
201
202 // Make edit to Buffer (and record Change in undo history)
203 122 begin_change(del_count ? CHANGE_MERGE_NONE : CHANGE_MERGE_INSERT);
204 122 buffer_replace_bytes(view, del_count, ins, ins_count);
205 122 end_change();
206 122 free(alloc);
207
208 // Move cursor after inserted text
209 122 block_iter_skip_bytes(&view->cursor, ins_count);
210 }
211