dte test coverage


Directory: ./
File: src/bookmark.c
Date: 2025-11-12 12:04:10
Coverage Exec Excl Total
Lines: 91.7% 44 0 48
Functions: 100.0% 6 0 6
Branches: 45.5% 10 0 22

Line Branch Exec Source
1 #include <stdbool.h>
2 #include <stdlib.h>
3 #include "bookmark.h"
4 #include "buffer.h"
5 #include "editor.h"
6 #include "move.h"
7 #include "search.h"
8 #include "selection.h"
9 #include "util/debug.h"
10
11 1 FileLocation *get_current_file_location(const View *v)
12 {
13 1 const Buffer *b = v->buffer;
14
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1 time.
1 char *filename = b->abs_filename ? xstrdup(b->abs_filename) : NULL;
15 1 return new_file_location(filename, b->id, v->cy + 1, v->cx_char + 1);
16 }
17
18 1 bool file_location_go(Window *window, const FileLocation *loc)
19 {
20 1 View *view = window_open_buffer(window, loc->filename, true, NULL);
21
1/2
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 13 not taken.
1 if (!view) {
22 // Failed to open file; error message should be visible
23 return false;
24 }
25
26
1/2
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 7 not taken.
1 if (window->view != view) {
27 1 set_view(view);
28 // Force centering view to cursor, because file changed
29 1 view->force_center = true;
30 }
31
32
1/2
✓ Branch 7 → 8 taken 1 time.
✗ Branch 7 → 10 not taken.
1 if (loc->pattern) {
33
1/2
✓ Branch 9 → 12 taken 1 time.
✗ Branch 9 → 13 not taken.
1 if (!search_tag(view, loc->pattern)) {
34 return false;
35 }
36 } else if (loc->line > 0) {
37 move_to_filepos(view, loc->line, MAX(loc->column, 1));
38 }
39
40 1 return unselect(view);
41 }
42
43 1 static bool file_location_return(Window *window, const FileLocation *loc)
44 {
45 1 Buffer *buffer = find_buffer_by_id(&window->editor->buffers, loc->buffer_id);
46 1 View *view;
47
1/2
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 5 not taken.
1 if (buffer) {
48 1 view = window_find_or_create_view(window, buffer);
49 } else {
50 if (!loc->filename) {
51 // Can't restore closed buffer that had no filename; try again
52 return false;
53 }
54 view = window_open_buffer(window, loc->filename, true, NULL);
55 }
56
57
0/2
✗ Branch 7 → 8 not taken.
✗ Branch 7 → 11 not taken.
1 if (!view) {
58 // Open failed; don't try again
59 return true;
60 }
61
62 1 set_view(view);
63 1 move_to_filepos(view, loc->line, loc->column);
64 1 return unselect(view);
65 }
66
67 303 void file_location_free(FileLocation *loc)
68 {
69 303 free(loc->filename);
70 303 free(loc->pattern);
71 303 free(loc);
72 303 }
73
74 302 size_t bookmark_push(PointerArray *bookmarks, FileLocation *loc)
75 {
76 302 const size_t max_entries = 256;
77
2/2
✓ Branch 2 → 3 taken 44 times.
✓ Branch 2 → 5 taken 258 times.
302 if (bookmarks->count == max_entries) {
78 44 file_location_free(ptr_array_remove_index(bookmarks, 0));
79 }
80
81 302 BUG_ON(bookmarks->count >= max_entries);
82 302 ptr_array_append(bookmarks, loc);
83 302 return bookmarks->count;
84 }
85
86 1 size_t bookmark_pop(PointerArray *bookmarks, Window *window)
87 {
88 1 void **ptrs = bookmarks->ptrs;
89 1 size_t count = bookmarks->count;
90 1 size_t popped = 0;
91
92
2/2
✓ Branch 6 → 3 taken 1 time.
✓ Branch 6 → 7 taken 1 time.
2 for (bool go = true; count > 0 && go; popped++) {
93 1 FileLocation *loc = ptrs[--count];
94 1 go = !file_location_return(window, loc);
95 1 file_location_free(loc);
96 }
97
98 1 bookmarks->count = count;
99 1 return popped;
100 }
101