Skip to content

Commit 073449a

Browse files
committed
feat: call execute after accepting, but before applying semantic brackets
Related to Saghen/blink.compat#14
1 parent 0386120 commit 073449a

File tree

4 files changed

+21
-39
lines changed

4 files changed

+21
-39
lines changed

lua/blink/cmp/completion/accept/init.lua

+18-22
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,7 @@ local function accept(ctx, item, callback)
99
local sources = require('blink.cmp.sources.lib')
1010
require('blink.cmp.completion.trigger').hide()
1111

12-
-- let the source execute the item itself if it indicates it can
13-
if sources.should_execute(item) then
14-
sources.execute(ctx, item)
15-
callback()
16-
return
17-
end
18-
19-
-- start the resolve immediately since text changes can invalidate the item
12+
-- Start the resolve immediately since text changes can invalidate the item
2013
-- with some LSPs (i.e. rust-analyzer) causing them to return the item as-is
2114
-- without i.e. auto-imports
2215
sources
@@ -63,24 +56,27 @@ local function accept(ctx, item, callback)
6356
})
6457
end
6558

66-
-- Check semantic tokens for brackets, if needed, and apply additional text edits
67-
if brackets_status == 'check_semantic_token' then
68-
-- TODO: since we apply the additional text edits after, auto imported functions will not
69-
-- get auto brackets. If we apply them before, we have to modify the textEdit to compensate
70-
brackets_lib.add_brackets_via_semantic_token(vim.bo.filetype, item, function()
59+
-- Let the source execute the item itself
60+
sources.execute(ctx, item):map(function()
61+
-- Check semantic tokens for brackets, if needed, and apply additional text edits
62+
if brackets_status == 'check_semantic_token' then
63+
-- TODO: since we apply the additional text edits after, auto imported functions will not
64+
-- get auto brackets. If we apply them before, we have to modify the textEdit to compensate
65+
brackets_lib.add_brackets_via_semantic_token(vim.bo.filetype, item, function()
66+
require('blink.cmp.completion.trigger').show_if_on_trigger_character({ is_accept = true })
67+
require('blink.cmp.signature.trigger').show_if_on_trigger_character()
68+
callback()
69+
end)
70+
else
7171
require('blink.cmp.completion.trigger').show_if_on_trigger_character({ is_accept = true })
7272
require('blink.cmp.signature.trigger').show_if_on_trigger_character()
7373
callback()
74-
end)
75-
else
76-
require('blink.cmp.completion.trigger').show_if_on_trigger_character({ is_accept = true })
77-
require('blink.cmp.signature.trigger').show_if_on_trigger_character()
78-
callback()
79-
end
74+
end
8075

81-
-- Notify the rust module that the item was accessed
82-
-- TODO: why is this so slow? (10ms)
83-
vim.schedule(function() require('blink.cmp.fuzzy').access(item) end)
76+
-- Notify the rust module that the item was accessed
77+
-- TODO: why is this so slow? (10ms)
78+
vim.schedule(function() require('blink.cmp.fuzzy').access(item) end)
79+
end)
8480
end)
8581
:catch(function(err) vim.notify(err, vim.log.levels.ERROR) end)
8682
end

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

-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ local config = require('blink.cmp.config')
1717
--- @field listen_on_completions fun(callback: fun(context: blink.cmp.Context, items: blink.cmp.CompletionItem[]))
1818
--- @field apply_max_items_for_completions fun(context: blink.cmp.Context, items: blink.cmp.CompletionItem[]): blink.cmp.CompletionItem[]
1919
--- @field resolve fun(item: blink.cmp.CompletionItem): blink.cmp.Task
20-
--- @field should_execute fun(item: blink.cmp.CompletionItem): boolean
2120
--- @field execute fun(context: blink.cmp.Context, item: blink.cmp.CompletionItem): blink.cmp.Task
2221
---
2322
--- @field get_signature_help_trigger_characters fun(): { trigger_characters: string[], retrigger_characters: string[] }
@@ -164,13 +163,6 @@ end
164163

165164
--- Execute ---
166165

167-
function sources.should_execute(item)
168-
for _, source in pairs(sources.providers) do
169-
if source.id == item.source_id then return source:should_execute(item) end
170-
end
171-
return false
172-
end
173-
174166
function sources.execute(context, item)
175167
local item_source = nil
176168
for _, source in pairs(sources.providers) do

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
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
1716
--- @field execute fun(self: blink.cmp.SourceProvider, context: blink.cmp.Context, item: blink.cmp.CompletionItem, callback: fun()): blink.cmp.Task
1817
--- @field get_signature_help_trigger_characters fun(self: blink.cmp.SourceProvider): { trigger_characters: string[], retrigger_characters: string[] }
1918
--- @field get_signature_help fun(self: blink.cmp.SourceProvider, context: blink.cmp.SignatureHelpContext): blink.cmp.Task
@@ -65,7 +64,9 @@ function source:get_completions(context, enabled_sources)
6564
-- and the data doesn't need to be updated
6665
if self.last_response ~= nil and self.last_response.context.id == context.id then
6766
if utils.should_run_request(context, self.last_response) == false then
68-
return async.task.new(function(resolve) resolve(require('blink.cmp.lib.utils').shallow_copy(self.last_response)) end)
67+
return async.task.new(
68+
function(resolve) resolve(require('blink.cmp.lib.utils').shallow_copy(self.last_response)) end
69+
)
6970
end
7071
end
7172

@@ -138,11 +139,6 @@ end
138139

139140
--- Execute ---
140141

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-
146142
function source:execute(context, item)
147143
if self.module.execute == nil then return async.task.new(function(resolve) resolve() end) end
148144
return async.task.new(function(resolve) self.module:execute(context, item, resolve) end)

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

-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
--- @field get_completions? fun(self: blink.cmp.Source, context: blink.cmp.Context, callback: fun(response?: blink.cmp.CompletionResponse)): (fun(): nil) | nil
1616
--- @field should_show_completions? fun(self: blink.cmp.Source, context: blink.cmp.Context, response: blink.cmp.CompletionResponse): boolean
1717
--- @field resolve? fun(self: blink.cmp.Source, item: blink.cmp.CompletionItem, callback: fun(resolved_item?: lsp.CompletionItem)): ((fun(): nil) | nil)
18-
--- @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
1918
--- @field execute? fun(self: blink.cmp.Source, context: blink.cmp.Context, item: blink.cmp.CompletionItem, callback: fun()): (fun(): nil) | nil
2019
--- @field get_signature_help_trigger_characters? fun(self: blink.cmp.Source): string[]
2120
--- @field get_signature_help? fun(self: blink.cmp.Source, context: blink.cmp.SignatureHelpContext, callback: fun(signature_help: lsp.SignatureHelp | nil)): (fun(): nil) | nil
@@ -27,7 +26,6 @@
2726
--- @field get_completions? fun(self: blink.cmp.Source, context: blink.cmp.Context, callback: fun(response: blink.cmp.CompletionResponse | nil)): (fun(): nil) | nil
2827
--- @field should_show_completions? fun(self: blink.cmp.Source, context: blink.cmp.Context, response: blink.cmp.CompletionResponse): boolean
2928
--- @field resolve? fun(self: blink.cmp.Source, item: blink.cmp.CompletionItem, callback: fun(resolved_item: lsp.CompletionItem | nil)): ((fun(): nil) | nil)
30-
--- @field should_execute? fun(self: blink.cmp.Source, item: blink.cmp.CompletionItem): boolean
3129
--- @field execute? fun(self: blink.cmp.Source, context: blink.cmp.Context, item: blink.cmp.CompletionItem, callback: fun()): (fun(): nil) | nil
3230
--- @field get_signature_help_trigger_characters? fun(self: blink.cmp.Source): string[]
3331
--- @field get_signature_help? fun(self: blink.cmp.Source, context: blink.cmp.SignatureHelpContext, callback: fun(signature_help: lsp.SignatureHelp | nil)): (fun(): nil) | nil

0 commit comments

Comments
 (0)