dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 50.6% 39 / 0 / 77
Functions: 85.7% 6 / 0 / 7
Branches: 30.0% 12 / 2 / 42

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