@@ -6,14 +6,15 @@ local config = require('blink.cmp.config')
6
6
--- @field current_signature_help blink.cmp.Task | nil
7
7
--- @field sources_registered boolean
8
8
--- @field providers table<string , blink.cmp.SourceProvider>
9
- --- @field on_completions_callback fun ( context : blink.cmp.Context , items : blink.cmp.CompletionItem[] )
9
+ --- @field on_completions_callback fun ( context : blink.cmp.Context , enabled_sources : table<string , blink.cmp.SourceProvider> , responses : table<string , blink.cmp.CompletionResponse> )
10
10
---
11
11
--- @field register fun ()
12
12
--- @field get_enabled_providers fun ( context ?: blink.cmp.Context ): table<string , blink.cmp.SourceProvider>
13
13
--- @field get_trigger_characters fun (): string[]
14
14
--- @field request_completions fun ( context : blink.cmp.Context )
15
15
--- @field cancel_completions fun ()
16
16
--- @field listen_on_completions fun ( callback : fun ( context : blink.cmp.Context , items : blink.cmp.CompletionItem[] ))
17
+ --- @field apply_max_items_for_completions fun ( context : blink.cmp.Context , items : blink.cmp.CompletionItem[] ): blink.cmp.CompletionItem[]
17
18
--- @field resolve fun ( item : blink.cmp.CompletionItem , callback : fun ( resolved_item : lsp.CompletionItem | nil )): ( fun (): nil ) | nil
18
19
--- @field get_signature_help_trigger_characters fun (): { trigger_characters : string[] , retrigger_characters : string[] }
19
20
--- @field get_signature_help fun ( context : blink.cmp.SignatureHelpContext , callback : fun ( signature_help : lsp.SignatureHelp | nil )): ( fun (): nil ) | nil
@@ -39,8 +40,9 @@ function sources.register()
39
40
end
40
41
41
42
function sources .get_enabled_providers (context )
42
- local mode_providers = type (config .sources .completion ) == ' function' and config .sources .completion (context )
43
- or config .sources .completion
43
+ local mode_providers = type (config .sources .completion .enabled_providers ) == ' function'
44
+ and config .sources .completion .enabled_providers (context )
45
+ or config .sources .completion .enabled_providers
44
46
--- @cast mode_providers string[]
45
47
46
48
--- @type table<string , blink.cmp.SourceProvider>
@@ -70,7 +72,17 @@ function sources.get_trigger_characters()
70
72
return trigger_characters
71
73
end
72
74
73
- function sources .listen_on_completions (callback ) sources .on_completions_callback = callback end
75
+ function sources .listen_on_completions (callback )
76
+ sources .on_completions_callback = function (context , enabled_sources , responses )
77
+ local items = {}
78
+ for id , response in pairs (responses ) do
79
+ if sources .providers [id ]:should_show_items (context , enabled_sources , response .items ) then
80
+ vim .list_extend (items , response .items )
81
+ end
82
+ end
83
+ callback (context , items )
84
+ end
85
+ end
74
86
75
87
function sources .request_completions (context )
76
88
-- create a new context if the id changed or if we haven't created one yet
@@ -82,6 +94,13 @@ function sources.request_completions(context)
82
94
sources .get_enabled_providers (context ),
83
95
sources .on_completions_callback
84
96
)
97
+ -- send cached completions if they exist to immediately trigger updates
98
+ elseif sources .current_context :get_cached_completions () ~= nil then
99
+ sources .on_completions_callback (
100
+ context ,
101
+ sources .current_context :get_sources (),
102
+ sources .current_context :get_cached_completions ()
103
+ )
85
104
end
86
105
87
106
sources .current_context :get_completions (context )
@@ -94,6 +113,33 @@ function sources.cancel_completions()
94
113
end
95
114
end
96
115
116
+ --- Limits the number of items per source as configured
117
+ function sources .apply_max_items_for_completions (context , items )
118
+ local enabled_sources = sources .get_enabled_providers (context )
119
+
120
+ -- get the configured max items for each source
121
+ local total_items_for_sources = {}
122
+ local max_items_for_sources = {}
123
+ for id , source in pairs (sources .providers ) do
124
+ max_items_for_sources [id ] = source .config .max_items (context , enabled_sources , items )
125
+ total_items_for_sources [id ] = 0
126
+ end
127
+
128
+ -- no max items configured, return as-is
129
+ if # vim .tbl_keys (max_items_for_sources ) == 0 then return items end
130
+
131
+ -- apply max items
132
+ local filtered_items = {}
133
+ for _ , item in ipairs (items ) do
134
+ local max_items = max_items_for_sources [item .source_id ]
135
+ total_items_for_sources [item .source_id ] = total_items_for_sources [item .source_id ] + 1
136
+ if max_items == nil or total_items_for_sources [item .source_id ] <= max_items then
137
+ table.insert (filtered_items , item )
138
+ end
139
+ end
140
+ return filtered_items
141
+ end
142
+
97
143
--- Resolve ---
98
144
99
145
function sources .resolve (item , callback )
0 commit comments