Skip to content

Commit 653b262

Browse files
committed
feat: add execute function for sources
1 parent 3ac471b commit 653b262

File tree

6 files changed

+65
-8
lines changed

6 files changed

+65
-8
lines changed

lua/blink/cmp/accept/init.lua

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@ local text_edits_lib = require('blink.cmp.accept.text-edits')
22
local brackets_lib = require('blink.cmp.accept.brackets')
33

44
--- Applies a completion item to the current buffer
5+
--- @param ctx blink.cmp.Context
56
--- @param item blink.cmp.CompletionItem
6-
local function accept(item)
7+
local function accept(ctx, item)
8+
local sources = require('blink.cmp.sources.lib')
79
require('blink.cmp.trigger.completion').hide()
810

11+
-- let the source execute the item itself if it indicates it can
12+
if sources.should_execute(item) then
13+
sources.execute(ctx, item)
14+
return
15+
end
16+
917
-- start the resolve immediately since text changes can invalidate the item
1018
-- with some LSPs (i.e. rust-analyzer) causing them to return the item as-is
1119
-- without i.e. auto-imports
12-
require('blink.cmp.sources.lib')
20+
sources
1321
.resolve(item)
1422
:map(function(resolved_item)
1523
local all_text_edits =

lua/blink/cmp/init.lua

+7-5
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,19 @@ cmp.accept = function()
152152
end
153153

154154
cmp.select_and_accept = function()
155-
if not cmp.windows.autocomplete.win:is_open() then return end
155+
local autocomplete = require('blink.cmp.windows.autocomplete')
156+
if not autocomplete.win:is_open() then return end
156157

157158
vim.schedule(function()
158159
-- select an item if none is selected
159-
if not cmp.windows.autocomplete.get_selected_item() then
160+
if not autocomplete.get_selected_item() then
160161
-- avoid running auto_insert since we're about to accept anyway
161-
cmp.windows.autocomplete.select_next({ skip_auto_insert = true })
162+
autocomplete.select_next({ skip_auto_insert = true })
162163
end
163164

164-
local item = cmp.windows.autocomplete.get_selected_item()
165-
if item ~= nil then require('blink.cmp.accept')(item) end
165+
local ctx = autocomplete.context
166+
local item = autocomplete.get_selected_item()
167+
if item ~= nil and ctx ~= nil then require('blink.cmp.accept')(ctx, item) end
166168
end)
167169
return true
168170
end

lua/blink/cmp/sources/lib/init.lua

+26
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ local config = require('blink.cmp.config')
1616
--- @field listen_on_completions fun(callback: fun(context: blink.cmp.Context, items: blink.cmp.CompletionItem[]))
1717
--- @field apply_max_items_for_completions fun(context: blink.cmp.Context, items: blink.cmp.CompletionItem[]): blink.cmp.CompletionItem[]
1818
--- @field resolve fun(item: blink.cmp.CompletionItem): blink.cmp.Task
19+
--- @field should_execute fun(item: blink.cmp.CompletionItem): boolean
20+
--- @field execute fun(context: blink.cmp.Context, item: blink.cmp.CompletionItem): blink.cmp.Task
1921
--- @field get_signature_help_trigger_characters fun(): { trigger_characters: string[], retrigger_characters: string[] }
2022
--- @field get_signature_help fun(context: blink.cmp.SignatureHelpContext, callback: fun(signature_help: lsp.SignatureHelp | nil)): (fun(): nil) | nil
2123
--- @field cancel_signature_help fun()
@@ -167,6 +169,30 @@ function sources.resolve(item)
167169
return item_source:resolve(item):catch(function(err) vim.print('failed to resolve item with error: ' .. err) end)
168170
end
169171

172+
--- Execute ---
173+
174+
function sources.should_execute(item)
175+
for _, source in pairs(sources.providers) do
176+
if source.id == item.source_id then return source:should_execute(item) end
177+
end
178+
return false
179+
end
180+
181+
function sources.execute(context, item)
182+
local item_source = nil
183+
for _, source in pairs(sources.providers) do
184+
if source.id == item.source_id then
185+
item_source = source
186+
break
187+
end
188+
end
189+
if item_source == nil then return async.task.new(function(resolve) resolve() end) end
190+
191+
return item_source
192+
:execute(context, item)
193+
:catch(function(err) vim.print('failed to execute item with error: ' .. err) end)
194+
end
195+
170196
--- Signature help ---
171197

172198
function sources.get_signature_help_trigger_characters()

lua/blink/cmp/sources/lib/provider/init.lua

+14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
--- @field get_completions fun(self: blink.cmp.SourceProvider, context: blink.cmp.Context, enabled_sources: string[]): blink.cmp.Task
1414
--- @field should_show_items fun(self: blink.cmp.SourceProvider, context: blink.cmp.Context, enabled_sources: string[], response: blink.cmp.CompletionResponse): boolean
1515
--- @field resolve fun(self: blink.cmp.SourceProvider, item: blink.cmp.CompletionItem): blink.cmp.Task
16+
--- @field should_execute fun(self: blink.cmp.SourceProvider, item: blink.cmp.CompletionItem): boolean
17+
--- @field execute fun(self: blink.cmp.SourceProvider, context: blink.cmp.Context, item: blink.cmp.CompletionItem, callback: fun()): blink.cmp.Task
1618
--- @field get_signature_help_trigger_characters fun(self: blink.cmp.SourceProvider): string[]
1719
--- @field get_signature_help fun(self: blink.cmp.SourceProvider, context: blink.cmp.SignatureHelpContext): blink.cmp.Task
1820
--- @field reload (fun(self: blink.cmp.SourceProvider): nil) | nil
@@ -134,6 +136,18 @@ function source:resolve(item)
134136
return tasks[item]
135137
end
136138

139+
--- Execute ---
140+
141+
function source:should_execute(item)
142+
if self.module.should_execute == nil then return self.module.execute ~= nil end
143+
return self.module:should_execute(item)
144+
end
145+
146+
function source:execute(context, item)
147+
if self.module.execute == nil then return async.task.new(function(resolve) resolve() end) end
148+
return async.task.new(function(resolve) self.module:execute(context, item, resolve) end)
149+
end
150+
137151
--- Signature help ---
138152

139153
function source:get_signature_help_trigger_characters()

lua/blink/cmp/sources/lib/types.lua

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
--- @field filter_completions? fun(self: blink.cmp.Source, response: blink.cmp.CompletionResponse): blink.cmp.CompletionItem[]
1717
--- @field should_show_completions? fun(self: blink.cmp.Source, context: blink.cmp.Context, response: blink.cmp.CompletionResponse): boolean
1818
--- @field resolve? fun(self: blink.cmp.Source, item: blink.cmp.CompletionItem, callback: fun(resolved_item?: lsp.CompletionItem)): ((fun(): nil) | nil)
19+
--- @field should_execute? fun(self: blink.cmp.Source, item: blink.cmp.CompletionItem): boolean Optional function to check if the source should execute the specified item
20+
--- @field execute? fun(self: blink.cmp.Source, context: blink.cmp.Context, item: blink.cmp.CompletionItem, callback: fun()): (fun(): nil) | nil
1921
--- @field get_signature_help_trigger_characters? fun(self: blink.cmp.Source): string[]
2022
--- @field get_signature_help? fun(self: blink.cmp.Source, context: blink.cmp.SignatureHelpContext, callback: fun(signature_help: lsp.SignatureHelp | nil)): (fun(): nil) | nil
2123
--- @field reload? fun(self: blink.cmp.Source): nil
@@ -27,6 +29,8 @@
2729
--- @field filter_completions? fun(self: blink.cmp.Source, response: blink.cmp.CompletionResponse): blink.cmp.CompletionItem[]
2830
--- @field should_show_completions? fun(self: blink.cmp.Source, context: blink.cmp.Context, response: blink.cmp.CompletionResponse): boolean
2931
--- @field resolve? fun(self: blink.cmp.Source, item: blink.cmp.CompletionItem, callback: fun(resolved_item: lsp.CompletionItem | nil)): ((fun(): nil) | nil)
32+
--- @field should_execute? fun(self: blink.cmp.Source, item: blink.cmp.CompletionItem): boolean
33+
--- @field execute? fun(self: blink.cmp.Source, context: blink.cmp.Context, item: blink.cmp.CompletionItem, callback: fun()): (fun(): nil) | nil
3034
--- @field get_signature_help_trigger_characters? fun(self: blink.cmp.Source): string[]
3135
--- @field get_signature_help? fun(self: blink.cmp.Source, context: blink.cmp.SignatureHelpContext, callback: fun(signature_help: lsp.SignatureHelp | nil)): (fun(): nil) | nil
3236
--- @field reload? fun(self: blink.cmp.Source): nil

lua/blink/cmp/windows/autocomplete.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ end
193193
---------- Selection/Accept ----------
194194

195195
function autocomplete.accept()
196+
local context = autocomplete.context
197+
if context == nil then return end
198+
196199
local selected_item = autocomplete.get_selected_item()
197200
if selected_item == nil then return end
198201

@@ -202,7 +205,7 @@ function autocomplete.accept()
202205
end
203206

204207
-- apply
205-
require('blink.cmp.accept')(selected_item)
208+
require('blink.cmp.accept')(context, selected_item)
206209
return true
207210
end
208211

0 commit comments

Comments
 (0)