Skip to content

Commit

Permalink
feat: rework sources system
Browse files Browse the repository at this point in the history
  • Loading branch information
Saghen committed Sep 25, 2024
1 parent ee32712 commit 3ee91b5
Show file tree
Hide file tree
Showing 14 changed files with 360 additions and 415 deletions.
2 changes: 1 addition & 1 deletion lua/blink/cmp/accept.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local utils = {}

local function accept(item)
local sources = require('blink.cmp.sources')
local sources = require('blink.cmp.sources.lib')
local fuzzy = require('blink.cmp.fuzzy')

local text_edit = item.textEdit
Expand Down
17 changes: 2 additions & 15 deletions lua/blink/cmp/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,15 @@
---
--- @class blink.cmp.SourceProviderConfig
--- @field [1] string
--- @field fallback_for string[] | nil
--- @field keyword_length number | nil
--- @field score_offset number | nil
--- @field deduplicate blink.cmp.DeduplicateConfig | nil
--- @field trigger_characters string[] | nil
--- @field override blink.cmp.OverrideConfig | nil
--- @field opts table | nil
---
--- @class blink.cmp.DeduplicateConfig
--- @field enabled boolean
--- @field priority number
---
--- @class blink.cmp.OverrideConfig
--- @field get_trigger_characters (fun(orig_fn: fun(): string[]): string[]) | nil
--- @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
--- @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
--- @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
--- @field cancel_completions (fun(orig_fn: fun() | nil): nil) | nil
---
--- @class blink.cmp.SourceOverrideConfig
--- @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[])))
--- @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))

--- @class blink.cmp.FuzzyConfig
--- @field use_frecency boolean
Expand Down Expand Up @@ -115,8 +102,8 @@ local config = {
sources = {
providers = {
{ 'blink.cmp.sources.lsp' },
{ 'blink.cmp.sources.buffer' },
{ 'blink.cmp.sources.snippets' },
{ 'blink.cmp.sources.buffer', score_offset = -9 },
{ 'blink.cmp.sources.snippets', score_offset = -3 },
},
},
windows = {
Expand Down
6 changes: 4 additions & 2 deletions lua/blink/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ cmp.setup = function(opts)
-- trigger -> sources -> fuzzy (filter/sort) -> windows (render)

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

-- sources fetch autocomplete items and documentation
cmp.sources = require('blink.cmp.sources')
cmp.sources = require('blink.cmp.sources.lib')
cmp.sources.register()

-- windows render and apply items
cmp.windows = {
Expand All @@ -38,7 +40,7 @@ cmp.setup = function(opts)
cmp.windows.autocomplete.close()
end)
cmp.sources.listen_on_completions(function(context, items)
-- avoid adding 1-4ms to insertion latency by scheduling for later
-- we avoid adding 1-4ms to insertion latency by scheduling for later
vim.schedule(function()
local filtered_items = cmp.fuzzy.filter_items(require('blink.cmp.util').get_query(), items)
if #filtered_items > 0 then
Expand Down
46 changes: 8 additions & 38 deletions lua/blink/cmp/sources/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@ end
--- @class blink.cmp.Source
local buffer = {}

function buffer.completions(_, callback)
local transformed_callback = function(items) callback({ isIncomplete = false, items = items }) end
function buffer.new(config) return setmetatable(config, { __index = buffer }) end

function buffer:get_completions(_, callback)
local transformed_callback = function(items)
callback({ is_incomplete_forward = false, is_incomplete_backward = false, items = items })
end

local buf_text = get_buf_text()
-- should take less than 2ms
Expand All @@ -79,43 +83,9 @@ function buffer.completions(_, callback)
else
transformed_callback({})
end
end

function buffer.should_show_completions(context, sources_responses)
local context_length = context.bounds.end_col - context.bounds.start_col
if context_length <= 3 then return false end
if sources_responses.lsp ~= nil and #sources_responses.lsp.items > 0 then return false end
return true
end

function buffer.filter_completions(context, sources_responses)
if sources_responses.buffer == nil then return sources_responses end

-- copy to avoid mutating the original
local copied_sources_responses = {}
for name, response in pairs(sources_responses) do
copied_sources_responses[name] = response
end
sources_responses = copied_sources_responses

-- get all of the unique labels
local unique_words = {}
for name, response in pairs(sources_responses) do
if name ~= 'buffer' then
for _, item in ipairs(response.items) do
unique_words[item.label] = true
end
end
end

-- filter any buffer words that already have a source
local filtered_buffer_items = {}
for _, item in ipairs(sources_responses.buffer.items) do
if not unique_words[item.label] then table.insert(filtered_buffer_items, item) end
end
sources_responses.buffer.items = filtered_buffer_items

return sources_responses
-- TODO: cancel run_async
return function() end
end

return buffer
160 changes: 0 additions & 160 deletions lua/blink/cmp/sources/init.lua

This file was deleted.

Loading

0 comments on commit 3ee91b5

Please sign in to comment.