22
33#include <string.h>
44
5+ #include "completion/matchers.h"
56#include "dged/buffer.h"
67#include "dged/buffer_view.h"
78#include "dged/buffers.h"
89#include "dged/minibuffer.h"
910
11+ #include "dged/vec.h"
1012#include "main/completion.h"
1113
1214static bool is_space (const struct codepoint * c ) {
@@ -19,6 +21,9 @@ typedef void (*on_buffer_selected_cb)(struct buffer *);
1921struct buffer_completion {
2022 struct buffer * buffer ;
2123 on_buffer_selected_cb on_buffer_selected ;
24+ size_t match_begin ;
25+ size_t match_end ;
26+ size_t score ;
2227};
2328
2429struct buffer_provider_data {
@@ -52,31 +57,35 @@ static void buffer_comp_cleanup(void *data) {
5257 free (bc );
5358}
5459
55- struct needle_match_ctx {
56- const char * needle ;
57- struct completion * completions ;
58- uint32_t max_ncompletions ;
59- uint32_t ncompletions ;
60+ typedef VEC (struct completion ) completion_vec ;
61+
62+ struct buffer_completions {
63+ completion_vec * completions ;
6064 on_buffer_selected_cb on_buffer_selected ;
65+ struct s8 needle ;
6166};
6267
63- static void buffer_matches (struct buffer * buffer , void * userdata ) {
64- struct needle_match_ctx * ctx = (struct needle_match_ctx * )userdata ;
65-
66- if (strncmp (ctx -> needle , buffer -> name , strlen (ctx -> needle )) == 0 &&
67- ctx -> ncompletions < ctx -> max_ncompletions ) {
68+ static void fill_buffer_completions (struct buffer * buffer , void * userdata ) {
69+ struct buffer_completions * ctx = (struct buffer_completions * )userdata ;
6870
71+ size_t match_begin , match_end ;
72+ uint32_t score ;
73+ if (filter_contains (ctx -> needle , s8 (buffer -> name ), & match_begin , & match_end ,
74+ & score )) {
6975 struct buffer_completion * comp_data =
7076 calloc (1 , sizeof (struct buffer_completion ));
7177 comp_data -> buffer = buffer ;
7278 comp_data -> on_buffer_selected = ctx -> on_buffer_selected ;
73- ctx -> completions [ctx -> ncompletions ] = (struct completion ){
79+ comp_data -> match_begin = match_begin ;
80+ comp_data -> match_end = match_end ;
81+
82+ struct completion comp = (struct completion ){
7483 .render = buffer_comp_render ,
7584 .selected = buffer_comp_selected ,
7685 .cleanup = buffer_comp_cleanup ,
7786 .data = comp_data ,
7887 };
79- ++ ctx -> ncompletions ;
88+ VEC_PUSH ( ctx -> completions , comp ) ;
8089 }
8190}
8291
@@ -110,20 +119,18 @@ static void buffer_complete(struct completion_context ctx, bool deletion,
110119 free (txt .text );
111120 }
112121
113- struct completion * completions = calloc (50 , sizeof (struct completion ));
114-
115- struct needle_match_ctx match_ctx = (struct needle_match_ctx ){
116- .needle = needle ,
117- .max_ncompletions = 50 ,
118- .completions = completions ,
119- .ncompletions = 0 ,
122+ completion_vec completions ;
123+ VEC_INIT (& completions , 32 );
124+ struct buffer_completions match_ctx = (struct buffer_completions ){
125+ .completions = & completions ,
120126 .on_buffer_selected = pd -> on_buffer_selected ,
127+ .needle = s8 (needle ),
121128 };
122129
123- buffers_for_each (buffers , buffer_matches , & match_ctx );
124- ctx .add_completions (match_ctx .completions , match_ctx .ncompletions );
125- free (completions );
130+ buffers_for_each (buffers , fill_buffer_completions , & match_ctx );
126131 free (needle );
132+ ctx .add_completions (VEC_ENTRIES (& completions ), VEC_SIZE (& completions ));
133+ VEC_DESTROY (& completions );
127134}
128135
129136static void cleanup_provider (void * data ) {
0 commit comments