dte test coverage


Directory: ./
File: src/wrap.c
Date: 2025-12-03 13:13:24
Coverage Exec Excl Total
Lines: 95.0% 76 0 80
Functions: 100.0% 6 0 6
Branches: 69.2% 36 0 52

Line Branch Exec Source
1 #include <stdbool.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "wrap.h"
5 #include "buffer.h"
6 #include "indent.h"
7 #include "selection.h"
8 #include "util/string-view.h"
9 #include "util/string.h"
10 #include "util/unicode.h"
11 #include "util/utf8.h"
12
13 typedef struct {
14 String buf;
15 char *indent;
16 size_t indent_len;
17 size_t indent_width;
18 size_t cur_width;
19 size_t text_width;
20 } ParagraphFormatter;
21
22 60 static void add_word(ParagraphFormatter *pf, const char *word, size_t len)
23 {
24 60 size_t i = 0;
25 60 size_t word_width = 0;
26
2/2
✓ Branch 5 → 3 taken 180 times.
✓ Branch 5 → 6 taken 60 times.
240 while (i < len) {
27 180 word_width += u_char_width(u_get_char(word, len, &i));
28 }
29
30
4/4
✓ Branch 6 → 7 taken 58 times.
✓ Branch 6 → 10 taken 2 times.
✓ Branch 7 → 8 taken 6 times.
✓ Branch 7 → 10 taken 52 times.
60 if (pf->cur_width && pf->cur_width + 1 + word_width > pf->text_width) {
31 6 string_append_byte(&pf->buf, '\n');
32 6 pf->cur_width = 0;
33 }
34
35
2/2
✓ Branch 10 → 11 taken 8 times.
✓ Branch 10 → 14 taken 52 times.
60 if (pf->cur_width == 0) {
36
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 8 times.
8 if (pf->indent_len) {
37 string_append_buf(&pf->buf, pf->indent, pf->indent_len);
38 }
39 8 pf->cur_width = pf->indent_width;
40 } else {
41 52 string_append_byte(&pf->buf, ' ');
42 52 pf->cur_width++;
43 }
44
45 60 string_append_buf(&pf->buf, word, len);
46 60 pf->cur_width += word_width;
47 60 }
48
49 2 static bool is_long_comment_delim(StringView sv)
50 {
51 // TODO: make this configurable
52
2/4
✓ Branch 3 → 4 taken 2 times.
✗ Branch 3 → 7 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 2 times.
2 return strview_equal_cstring(sv, "/*") || strview_equal_cstring(sv, "*/");
53 }
54
55 5 static bool is_paragraph_separator(StringView line)
56 {
57 5 strview_trim(&line);
58
3/4
✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 7 taken 3 times.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 2 times.
5 return (line.length == 0) || is_long_comment_delim(line);
59 }
60
61 3 static bool in_paragraph (
62 StringView line,
63 size_t para_indent_width,
64 unsigned int tab_width
65 ) {
66 3 size_t w = get_indent_width(line, tab_width);
67
2/4
✓ Branch 3 → 4 taken 3 times.
✗ Branch 3 → 7 not taken.
✓ Branch 5 → 6 taken 3 times.
✗ Branch 5 → 7 not taken.
3 return (w == para_indent_width) && !is_paragraph_separator(line);
68 }
69
70 2 static size_t paragraph_size(View *view)
71 {
72 2 BlockIter bi = view->cursor;
73 2 block_iter_bol(&bi);
74 2 StringView line = block_iter_get_line(&bi);
75
1/2
✓ Branch 5 → 6 taken 2 times.
✗ Branch 5 → 21 not taken.
2 if (is_paragraph_separator(line)) {
76 // Not in paragraph
77 return 0;
78 }
79
80 2 unsigned int tab_width = view->buffer->options.tab_width;
81 2 size_t para_indent_width = get_indent_width(line, tab_width);
82
83 // Go to beginning of paragraph
84
2/2
✓ Branch 14 → 7 taken 1 time.
✓ Branch 14 → 15 taken 1 time.
2 while (block_iter_prev_line(&bi)) {
85 1 line = block_iter_get_line(&bi);
86
1/2
✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 13 not taken.
1 if (!in_paragraph(line, para_indent_width, tab_width)) {
87 1 block_iter_eat_line(&bi);
88 1 break;
89 }
90 }
91 2 view->cursor = bi;
92
93 // Get size of paragraph
94 2 size_t size = 0;
95 2 do {
96 2 size_t bytes = block_iter_eat_line(&bi);
97
1/2
✓ Branch 17 → 18 taken 2 times.
✗ Branch 17 → 21 not taken.
2 if (!bytes) {
98 break;
99 }
100 2 size += bytes;
101 2 line = block_iter_get_line(&bi);
102
1/2
✗ Branch 20 → 16 not taken.
✓ Branch 20 → 21 taken 2 times.
2 } while (in_paragraph(line, para_indent_width, tab_width));
103 return size;
104 }
105
106 2 void wrap_paragraph(View *view, size_t text_width)
107 {
108 2 size_t len;
109
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 2 times.
2 if (view->selection) {
110 view->selection = SELECT_LINES;
111 len = prepare_selection(view);
112 } else {
113 2 len = paragraph_size(view);
114 }
115
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 2 times.
2 if (!len) {
116 return;
117 }
118
119 2 const LocalOptions *options = &view->buffer->options;
120 2 char *sel = block_iter_get_bytes(&view->cursor, len);
121 2 StringView sv = string_view(sel, len);
122 2 size_t indent_width = get_indent_width(sv, options->tab_width);
123 2 char *indent = make_indent(options, indent_width);
124
125 4 ParagraphFormatter pf = {
126 .buf = STRING_INIT,
127 .indent = indent,
128
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 2 times.
2 .indent_len = indent ? strlen(indent) : 0,
129 .indent_width = indent_width,
130 .cur_width = 0,
131 .text_width = text_width
132 };
133
134 62 for (size_t i = 0; true; ) {
135
2/2
✓ Branch 19 → 13 taken 122 times.
✓ Branch 19 → 20 taken 2 times.
124 while (i < len) {
136 122 size_t tmp = i;
137
2/2
✓ Branch 14 → 15 taken 62 times.
✓ Branch 14 → 16 taken 60 times.
122 if (!u_is_breakable_whitespace(u_get_char(sel, len, &tmp))) {
138 break;
139 }
140 62 i = tmp;
141 }
142
2/2
✓ Branch 20 → 25 taken 60 times.
✓ Branch 20 → 27 taken 2 times.
62 if (i == len) {
143 break;
144 }
145
146 size_t start = i;
147
1/2
✓ Branch 25 → 21 taken 240 times.
✗ Branch 25 → 26 not taken.
240 while (i < len) {
148 240 size_t tmp = i;
149
2/2
✓ Branch 22 → 23 taken 180 times.
✓ Branch 22 → 24 taken 60 times.
240 if (u_is_breakable_whitespace(u_get_char(sel, len, &tmp))) {
150 break;
151 }
152 180 i = tmp;
153 }
154
155 60 add_word(&pf, sel + start, i - start);
156 }
157
158
1/2
✓ Branch 27 → 28 taken 2 times.
✗ Branch 27 → 29 not taken.
2 if (pf.buf.len) {
159 2 string_append_byte(&pf.buf, '\n');
160 }
161 2 buffer_replace_bytes(view, len, pf.buf.buffer, pf.buf.len);
162
1/2
✓ Branch 30 → 31 taken 2 times.
✗ Branch 30 → 32 not taken.
2 if (pf.buf.len) {
163 2 block_iter_skip_bytes(&view->cursor, pf.buf.len - 1);
164 }
165
166 2 string_free(&pf.buf);
167 2 free(pf.indent);
168 2 free(sel);
169 2 unselect(view);
170 }
171