Skip to content

Commit 3ee91b5

Browse files
committed
feat: rework sources system
1 parent ee32712 commit 3ee91b5

14 files changed

+360
-415
lines changed

lua/blink/cmp/accept.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
local utils = {}
22

33
local function accept(item)
4-
local sources = require('blink.cmp.sources')
4+
local sources = require('blink.cmp.sources.lib')
55
local fuzzy = require('blink.cmp.fuzzy')
66

77
local text_edit = item.textEdit

lua/blink/cmp/config.lua

+2-15
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,15 @@
1919
---
2020
--- @class blink.cmp.SourceProviderConfig
2121
--- @field [1] string
22-
--- @field fallback_for string[] | nil
2322
--- @field keyword_length number | nil
2423
--- @field score_offset number | nil
2524
--- @field deduplicate blink.cmp.DeduplicateConfig | nil
2625
--- @field trigger_characters string[] | nil
27-
--- @field override blink.cmp.OverrideConfig | nil
2826
--- @field opts table | nil
2927
---
3028
--- @class blink.cmp.DeduplicateConfig
3129
--- @field enabled boolean
3230
--- @field priority number
33-
---
34-
--- @class blink.cmp.OverrideConfig
35-
--- @field get_trigger_characters (fun(orig_fn: fun(): string[]): string[]) | nil
36-
--- @field completions (fun(context: blink.cmp.ShowContext, callback: fun(items: blink.cmp.CompletionItem[]), orig_fn: (fun(context: blink.cmp.ShowContext, callback: fun(items: blink.cmp.CompletionItem[]))): nil) | nil) | nil
37-
--- @field filter_completions (fun(context: blink.cmp.ShowContext, source_responses: table<string, blink.cmp.CompletionResponse>, orig_fn: (fun(context: blink.cmp.ShowContext, source_responses: table<string, blink.cmp.CompletionResponse>): blink.cmp.CompletionItem[]) | nil): blink.cmp.CompletionItem[]) | nil
38-
--- @field resolve (fun(item: blink.cmp.CompletionItem, callback: fun(resolved_item: lsp.CompletionItem | nil), orig_fn: (fun(item: blink.cmp.CompletionItem, callback: fun(resolved_item: lsp.CompletionItem | nil))) | nil): (fun(): nil) | nil) | nil
39-
--- @field cancel_completions (fun(orig_fn: fun() | nil): nil) | nil
40-
---
41-
--- @class blink.cmp.SourceOverrideConfig
42-
--- @field completions fun(context: blink.cmp.ShowContext, callback: fun(items: blink.cmp.CompletionItem[]), orig_fn: fun(context: blink.cmp.ShowContext, callback: fun(items: blink.cmp.CompletionItem[])))
43-
--- @field resolve fun(orig_fn: fun(item: blink.cmp.CompletionItem, callback: fun(resolved_item: blink.cmp.CompletionItem | nil)), item: blink.cmp.CompletionItem, callback: fun(resolved_item: blink.cmp.CompletionItem | nil))
4431

4532
--- @class blink.cmp.FuzzyConfig
4633
--- @field use_frecency boolean
@@ -115,8 +102,8 @@ local config = {
115102
sources = {
116103
providers = {
117104
{ 'blink.cmp.sources.lsp' },
118-
{ 'blink.cmp.sources.buffer' },
119-
{ 'blink.cmp.sources.snippets' },
105+
{ 'blink.cmp.sources.buffer', score_offset = -9 },
106+
{ 'blink.cmp.sources.snippets', score_offset = -3 },
120107
},
121108
},
122109
windows = {

lua/blink/cmp/init.lua

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ cmp.setup = function(opts)
1515
-- trigger -> sources -> fuzzy (filter/sort) -> windows (render)
1616

1717
-- trigger controls when to show the window and the current context for caching
18+
-- TODO: add first_trigger event for setting up the rest of the plugin
1819
cmp.trigger = require('blink.cmp.trigger').activate_autocmds()
1920

2021
-- sources fetch autocomplete items and documentation
21-
cmp.sources = require('blink.cmp.sources')
22+
cmp.sources = require('blink.cmp.sources.lib')
23+
cmp.sources.register()
2224

2325
-- windows render and apply items
2426
cmp.windows = {
@@ -38,7 +40,7 @@ cmp.setup = function(opts)
3840
cmp.windows.autocomplete.close()
3941
end)
4042
cmp.sources.listen_on_completions(function(context, items)
41-
-- avoid adding 1-4ms to insertion latency by scheduling for later
43+
-- we avoid adding 1-4ms to insertion latency by scheduling for later
4244
vim.schedule(function()
4345
local filtered_items = cmp.fuzzy.filter_items(require('blink.cmp.util').get_query(), items)
4446
if #filtered_items > 0 then

lua/blink/cmp/sources/buffer.lua

+8-38
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,12 @@ end
6565
--- @class blink.cmp.Source
6666
local buffer = {}
6767

68-
function buffer.completions(_, callback)
69-
local transformed_callback = function(items) callback({ isIncomplete = false, items = items }) end
68+
function buffer.new(config) return setmetatable(config, { __index = buffer }) end
69+
70+
function buffer:get_completions(_, callback)
71+
local transformed_callback = function(items)
72+
callback({ is_incomplete_forward = false, is_incomplete_backward = false, items = items })
73+
end
7074

7175
local buf_text = get_buf_text()
7276
-- should take less than 2ms
@@ -79,43 +83,9 @@ function buffer.completions(_, callback)
7983
else
8084
transformed_callback({})
8185
end
82-
end
83-
84-
function buffer.should_show_completions(context, sources_responses)
85-
local context_length = context.bounds.end_col - context.bounds.start_col
86-
if context_length <= 3 then return false end
87-
if sources_responses.lsp ~= nil and #sources_responses.lsp.items > 0 then return false end
88-
return true
89-
end
90-
91-
function buffer.filter_completions(context, sources_responses)
92-
if sources_responses.buffer == nil then return sources_responses end
93-
94-
-- copy to avoid mutating the original
95-
local copied_sources_responses = {}
96-
for name, response in pairs(sources_responses) do
97-
copied_sources_responses[name] = response
98-
end
99-
sources_responses = copied_sources_responses
100-
101-
-- get all of the unique labels
102-
local unique_words = {}
103-
for name, response in pairs(sources_responses) do
104-
if name ~= 'buffer' then
105-
for _, item in ipairs(response.items) do
106-
unique_words[item.label] = true
107-
end
108-
end
109-
end
110-
111-
-- filter any buffer words that already have a source
112-
local filtered_buffer_items = {}
113-
for _, item in ipairs(sources_responses.buffer.items) do
114-
if not unique_words[item.label] then table.insert(filtered_buffer_items, item) end
115-
end
116-
sources_responses.buffer.items = filtered_buffer_items
11786

118-
return sources_responses
87+
-- TODO: cancel run_async
88+
return function() end
11989
end
12090

12191
return buffer

lua/blink/cmp/sources/init.lua

-160
This file was deleted.

0 commit comments

Comments
 (0)