@@ -12,6 +12,7 @@ function sources_context.new(context, sources_groups, on_completions_callback)
12
12
self .active_request = nil
13
13
self .queued_request_context = nil
14
14
self .last_successful_completions = nil
15
+ self .last_sources_group_idx = nil
15
16
--- @type fun ( context : blink.cmp.Context , items : blink.cmp.CompletionItem[] )
16
17
self .on_completions_callback = on_completions_callback
17
18
@@ -32,21 +33,25 @@ function sources_context:get_completions(context)
32
33
33
34
-- Create a task to get the completions for the first sources group,
34
35
-- falling back to the next sources group iteratively if there are no items
35
- local request = self :get_completions_for_group (self .sources_groups [1 ], context )
36
+ local request = self :get_completions_for_group (1 , self .sources_groups [1 ], context )
36
37
for idx , sources_group in ipairs (self .sources_groups ) do
37
38
if idx > 1 then
38
- request = request :map (function (items )
39
- if # items > 0 then return items end
40
- return self :get_completions_for_group (sources_group , context )
39
+ request = request :map (function (res )
40
+ if # res . items > 0 then return res end
41
+ return self :get_completions_for_group (idx , sources_group , context )
41
42
end )
42
43
end
43
44
end
44
45
45
46
-- Send response upstream and run the queued request, if it exists
46
- self .active_request = request :map (function (items )
47
+ self .active_request = request :map (function (response )
47
48
self .active_request = nil
48
- self .last_successful_completions = items
49
- self .on_completions_callback (context , items )
49
+ self .last_successful_completions = response .items
50
+ -- only send upstream if the response contains something new
51
+ if not response .is_cached or response .sources_group_idx ~= self .last_sources_group_idx then
52
+ self .on_completions_callback (context , response .items )
53
+ end
54
+ self .last_sources_group_idx = response .sources_group_idx
50
55
51
56
-- todo: when the queued request results in 100% cached content, we end up
52
57
-- calling the on_completions_callback with the same data, which triggers
@@ -59,10 +64,11 @@ function sources_context:get_completions(context)
59
64
end )
60
65
end
61
66
67
+ --- @param sources_group_idx number
62
68
--- @param sources_group blink.cmp.Source[]
63
69
--- @param context blink.cmp.Context
64
70
--- @return blink.cmp.Task
65
- function sources_context :get_completions_for_group (sources_group , context )
71
+ function sources_context :get_completions_for_group (sources_group_idx , sources_group , context )
66
72
-- get completions for each source in the group
67
73
local tasks = vim .tbl_map (function (source )
68
74
-- the source indicates we should refetch when this character is typed
@@ -85,23 +91,25 @@ function sources_context:get_completions_for_group(sources_group, context)
85
91
return async .task
86
92
.await_all (tasks )
87
93
:map (function (tasks_results )
94
+ local is_cached = true
88
95
local items = {}
89
96
-- for each task, filter the items and add them to the list
90
97
-- if the source should show the completions
91
98
for idx , task_result in ipairs (tasks_results ) do
92
99
if task_result .status == async .STATUS .COMPLETED then
100
+ is_cached = is_cached and (task_result .result .is_cached or false )
93
101
local source = sources_group [idx ]
94
102
--- @type blink.cmp.CompletionResponse
95
103
local response = task_result .result
96
104
response .items = source :filter_completions (response )
97
105
if source :should_show_completions (response ) then vim .list_extend (items , response .items ) end
98
106
end
99
107
end
100
- return items
108
+ return { sources_group_idx = sources_group_idx , is_cached = is_cached , items = items }
101
109
end )
102
110
:catch (function (err )
103
111
vim .print (' failed to get completions for group with error: ' .. err )
104
- return { is_incomplete_forward = false , is_incomplete_backward = false , items = {} }
112
+ return { sources_group_idx = sources_group_idx , is_cached = false , items = {} }
105
113
end )
106
114
end
107
115
0 commit comments