dte test coverage


Directory: ./
File: src/wrap.c
Date: 2025-09-07 23:01:39
Exec Total Coverage
Lines: 76 80 95.0%
Functions: 6 6 100.0%
Branches: 36 52 69.2%

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 0 (5→3) taken 180 times.
✓ Branch 1 (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 0 (6→7) taken 58 times.
✓ Branch 1 (6→10) taken 2 times.
✓ Branch 2 (7→8) taken 6 times.
✓ Branch 3 (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 0 (10→11) taken 8 times.
✓ Branch 1 (10→14) taken 52 times.
60 if (pf->cur_width == 0) {
36
1/2
✗ Branch 0 (11→12) not taken.
✓ Branch 1 (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 0 (3→4) taken 2 times.
✗ Branch 1 (3→7) not taken.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (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 0 (3→4) taken 2 times.
✓ Branch 1 (3→7) taken 3 times.
✗ Branch 2 (5→6) not taken.
✓ Branch 3 (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 0 (3→4) taken 3 times.
✗ Branch 1 (3→7) not taken.
✓ Branch 2 (5→6) taken 3 times.
✗ Branch 3 (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 0 (5→6) taken 2 times.
✗ Branch 1 (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 0 (14→7) taken 1 times.
✓ Branch 1 (14→15) taken 1 times.
2 while (block_iter_prev_line(&bi)) {
85 1 line = block_iter_get_line(&bi);
86
1/2
✓ Branch 0 (9→10) taken 1 times.
✗ Branch 1 (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 0 (17→18) taken 2 times.
✗ Branch 1 (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 0 (20→16) not taken.
✓ Branch 1 (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 0 (2→3) not taken.
✓ Branch 1 (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 0 (5→6) not taken.
✓ Branch 1 (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 0 (10→11) not taken.
✓ Branch 1 (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 0 (19→13) taken 122 times.
✓ Branch 1 (19→20) taken 2 times.
124 while (i < len) {
136 122 size_t tmp = i;
137
2/2
✓ Branch 0 (14→15) taken 62 times.
✓ Branch 1 (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 0 (20→25) taken 60 times.
✓ Branch 1 (20→27) taken 2 times.
62 if (i == len) {
143 break;
144 }
145
146 size_t start = i;
147
1/2
✓ Branch 0 (25→21) taken 240 times.
✗ Branch 1 (25→26) not taken.
240 while (i < len) {
148 240 size_t tmp = i;
149
2/2
✓ Branch 0 (22→23) taken 180 times.
✓ Branch 1 (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 0 (27→28) taken 2 times.
✗ Branch 1 (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 0 (30→31) taken 2 times.
✗ Branch 1 (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