dte test coverage


Directory: ./
File: src/bookmark.c
Date: 2025-02-14 16:55:22
Exec Total Coverage
Lines: 46 50 92.0%
Functions: 6 6 100.0%
Branches: 10 22 45.5%

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 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1 times.
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 0 (3→4) taken 1 times.
✗ Branch 1 (3→14) not taken.
1 if (!view) {
22 // Failed to open file; error message should be visible
23 return false;
24 }
25
26
1/2
✓ Branch 0 (4→5) taken 1 times.
✗ Branch 1 (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 0 (7→8) taken 1 times.
✗ Branch 1 (7→10) not taken.
1 if (loc->pattern) {
33
1/2
✓ Branch 0 (9→12) taken 1 times.
✗ Branch 1 (9→14) 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 unselect(view);
41 1 return true;
42 }
43
44 1 static bool file_location_return(Window *window, const FileLocation *loc)
45 {
46 1 Buffer *buffer = find_buffer_by_id(&window->editor->buffers, loc->buffer_id);
47 1 View *view;
48
1/2
✓ Branch 0 (3→4) taken 1 times.
✗ Branch 1 (3→5) not taken.
1 if (buffer) {
49 1 view = window_find_or_create_view(window, buffer);
50 } else {
51 if (!loc->filename) {
52 // Can't restore closed buffer that had no filename; try again
53 return false;
54 }
55 view = window_open_buffer(window, loc->filename, true, NULL);
56 }
57
58
0/2
✗ Branch 0 (7→8) not taken.
✗ Branch 1 (7→12) not taken.
1 if (!view) {
59 // Open failed; don't try again
60 return true;
61 }
62
63 1 set_view(view);
64 1 unselect(view);
65 1 move_to_filepos(view, loc->line, loc->column);
66 1 return true;
67 }
68
69 303 void file_location_free(FileLocation *loc)
70 {
71 303 free(loc->filename);
72 303 free(loc->pattern);
73 303 free(loc);
74 303 }
75
76 302 void bookmark_push(PointerArray *bookmarks, FileLocation *loc)
77 {
78 302 const size_t max_entries = 256;
79
2/2
✓ Branch 0 (2→3) taken 44 times.
✓ Branch 1 (2→5) taken 258 times.
302 if (bookmarks->count == max_entries) {
80 44 file_location_free(ptr_array_remove_index(bookmarks, 0));
81 }
82 302 BUG_ON(bookmarks->count >= max_entries);
83 302 ptr_array_append(bookmarks, loc);
84 302 }
85
86 1 void bookmark_pop(PointerArray *bookmarks, Window *window)
87 {
88 1 void **ptrs = bookmarks->ptrs;
89 1 size_t count = bookmarks->count;
90 1 bool go = true;
91
2/2
✓ Branch 0 (5→3) taken 1 times.
✓ Branch 1 (5→6) taken 1 times.
2 while (count > 0 && go) {
92 1 FileLocation *loc = ptrs[--count];
93 1 go = !file_location_return(window, loc);
94 1 file_location_free(loc);
95 }
96 1 bookmarks->count = count;
97 1 }
98