dte test coverage


Directory: ./
File: src/bookmark.c
Date: 2025-05-08 15:05:54
Exec Total Coverage
Lines: 44 48 91.7%
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→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 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→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 0 (3→4) taken 1 times.
✗ Branch 1 (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 0 (7→8) not taken.
✗ Branch 1 (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 void bookmark_push(PointerArray *bookmarks, FileLocation *loc)
75 {
76 302 const size_t max_entries = 256;
77
2/2
✓ Branch 0 (2→3) taken 44 times.
✓ Branch 1 (2→5) taken 258 times.
302 if (bookmarks->count == max_entries) {
78 44 file_location_free(ptr_array_remove_index(bookmarks, 0));
79 }
80 302 BUG_ON(bookmarks->count >= max_entries);
81 302 ptr_array_append(bookmarks, loc);
82 302 }
83
84 1 void bookmark_pop(PointerArray *bookmarks, Window *window)
85 {
86 1 void **ptrs = bookmarks->ptrs;
87 1 size_t count = bookmarks->count;
88 1 bool go = true;
89
2/2
✓ Branch 0 (5→3) taken 1 times.
✓ Branch 1 (5→6) taken 1 times.
2 while (count > 0 && go) {
90 1 FileLocation *loc = ptrs[--count];
91 1 go = !file_location_return(window, loc);
92 1 file_location_free(loc);
93 }
94 1 bookmarks->count = count;
95 1 }
96