dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 49.4% 39 / 0 / 79
Functions: 85.7% 6 / 0 / 7
Branches: 28.6% 12 / 2 / 44

src/msg.c
Line Branch Exec Source
1 #include <stdlib.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include "msg.h"
5 #include "editor.h"
6 #include "util/debug.h"
7 #include "util/numtostr.h"
8 #include "util/path.h"
9 #include "util/xmalloc.h"
10
11 1 static void free_message(Message *m)
12 {
13
1/2
✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 4 not taken.
1 if (m->loc) {
14 1 file_location_free(m->loc);
15 }
16 1 free(m);
17 1 }
18
19 Message *new_message(const char *msg, size_t len)
20 {
21 Message *m = xmalloc(xadd3(sizeof(*m), len, 1));
22 m->loc = NULL;
23 if (len) {
24 memcpy(m->msg, msg, len);
25 }
26 m->msg[len] = '\0';
27 return m;
28 }
29
30 1 void add_message(MessageList *msgs, Message *m)
31 {
32 1 ptr_array_append(&msgs->array, m);
33 1 }
34
35 // Jump to the FileLocation of the current Message
36 1 bool activate_current_message(const MessageList *msgs, Window *window, ErrorBuffer *ebuf)
37 {
38 1 size_t count = msgs->array.count;
39
1/2
✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 12 not taken.
1 if (count == 0) {
40 return false;
41 }
42
43 1 size_t pos = msgs->pos;
44 1 BUG_ON(pos >= count);
45 1 const Message *m = msgs->array.ptrs[pos];
46 1 const FileLocation *loc = m->loc;
47
3/6
✓ Branch 5 → 6 taken 1 time.
✗ Branch 5 → 9 not taken.
✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 9 not taken.
✓ Branch 8 → 9 taken 1 time.
✗ Branch 8 → 12 not taken.
1 if (loc && loc->filename && !file_location_go(window, ebuf, loc)) {
48 // Failed to jump to location; error message is visible
49 return false;
50 }
51
52
1/2
✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 11 not taken.
1 if (count == 1) {
53 1 return info_msg(ebuf, "%s", m->msg);
54 }
55
56 return info_msg(ebuf, "[%zu/%zu] %s", pos + 1, count, m->msg);
57 }
58
59 // Like activate_current_message(), but also pushing the previous
60 // FileLocation on the bookmark stack if the cursor moves
61 1 void activate_current_message_save (
62 const MessageList *msgs,
63 PointerArray *bookmarks,
64 const View *view,
65 ErrorBuffer *ebuf
66 ) {
67 1 size_t nmsgs = msgs->array.count;
68
1/2
✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 12 not taken.
1 if (nmsgs == 0) {
69 return;
70 }
71
72 1 const BlockIter save = view->cursor;
73 1 const unsigned long line = view->cy + 1;
74 1 const unsigned long col = view->cx_char + 1;
75 1 activate_current_message(msgs, view->window, ebuf);
76
77 1 const BlockIter *cursor = &view->window->editor->view->cursor;
78
2/6
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 7 not taken.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 1 time.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 12 not taken.
1 if (nmsgs == 1 && cursor->blk == save.blk && cursor->offset == save.offset) {
79 // Only one message and cursor position not changed; don't bookmark.
80 // TODO: Make this condition configurable (some people may prefer
81 // to *always* push a bookmark)
82 return;
83 }
84
85 // Active view or cursor position changed, or MAY change due to
86 // there being multiple Messages to navigate with `msg -n|-p`;
87 // bookmark the previous location
88 1 const Buffer *b = view->buffer;
89
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 1 time.
1 char *filename = b->abs_filename ? xstrdup(b->abs_filename) : NULL;
90 1 bookmark_push(bookmarks, new_file_location(filename, b->id, line, col));
91 }
92
93 37 void clear_messages(MessageList *msgs)
94 {
95 37 msgs->pos = 0;
96 37 ptr_array_free_cb(&msgs->array, FREE_FUNC(free_message));
97 37 }
98
99 1 String dump_messages(const MessageList *messages)
100 {
101 1 size_t count = messages->array.count;
102 1 char cwd[8192];
103
2/4
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 5 not taken.
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 6 not taken.
1 if (unlikely(!getcwd(cwd, sizeof cwd)) || count == 0) {
104 1 return string_new(0);
105 }
106
107 String buf = string_new(4096);
108 for (size_t i = 0; i < count; i++) {
109 char *ptr = string_reserve_space(&buf, DECIMAL_STR_MAX(i));
110 buf.len += buf_umax_to_str(i + 1, ptr);
111 string_append_literal(&buf, ": ");
112
113 const Message *m = messages->array.ptrs[i];
114 const FileLocation *loc = m->loc;
115 if (!loc || !loc->filename) {
116 goto append_msg;
117 }
118
119 if (path_is_absolute(loc->filename)) {
120 char *rel = path_relative(loc->filename, cwd);
121 string_append_cstring(&buf, rel);
122 free(rel);
123 } else {
124 string_append_cstring(&buf, loc->filename);
125 }
126
127 string_append_byte(&buf, ':');
128
129 if (loc->pattern) {
130 string_append_literal(&buf, " /");
131 string_append_cstring(&buf, loc->pattern);
132 string_append_literal(&buf, "/\n");
133 continue;
134 }
135
136 if (loc->line != 0) {
137 string_append_cstring(&buf, ulong_to_str(loc->line));
138 string_append_byte(&buf, ':');
139 if (loc->column != 0) {
140 string_append_cstring(&buf, ulong_to_str(loc->column));
141 string_append_byte(&buf, ':');
142 }
143 }
144
145 string_append_literal(&buf, " ");
146
147 append_msg:
148 string_append_cstring(&buf, m->msg);
149 string_append_byte(&buf, '\n');
150 }
151
152 return buf;
153 }
154