Skip to content

Commit

Permalink
chore: rename all types with blink.cmp prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
Saghen committed Aug 30, 2024
1 parent e248579 commit e837718
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 60 deletions.
76 changes: 45 additions & 31 deletions lua/blink/cmp/config.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
--- @class KeymapConfig
--- @class blink.cmp.KeymapConfig
--- @field show string | string[]
--- @field accept string | string[]
--- @field select_prev string | string[]
Expand All @@ -10,77 +10,91 @@
--- @field snippet_forward string | string[]
--- @field snippet_backward string | string[]

--- @class TriggerConfig
--- @class blink.cmp.TriggerConfig
--- @field context_regex string
--- @field blocked_trigger_characters string[]

--- @class SourceConfig
--- @field providers SourceProviderConfig[]
--- @class blink.cmp.SourceConfig
--- @field providers blink.cmp.SourceProviderConfig[]
---
--- @class SourceProviderConfig
--- @field module string
--- @class blink.cmp.SourceProviderConfig
--- @field [1] string
--- @field fallback_for string[] | nil
--- @field keyword_length number | nil
--- @field score_offset number | nil
--- @field deduplicate DeduplicateConfig | nil
--- @field deduplicate blink.cmp.DeduplicateConfig | nil
--- @field trigger_characters string[] | nil
--- @field override blink.cmp.OverrideConfig | nil
--- @field opts table | nil
--- @field override table | nil
---
--- @class DeduplicateConfig
--- @class blink.cmp.DeduplicateConfig
--- @field enabled boolean
--- @field priority number
---
--- @class SourceOverrideConfig
--- @field completions fun(context: ShowContext, callback: fun(items: CompletionItem[]), orig_fn: fun(context: ShowContext, callback: fun(items: CompletionItem[])))
--- @field resolve fun(orig_fn: fun(item: CompletionItem, callback: fun(resolved_item: CompletionItem | nil)), item: CompletionItem, callback: fun(resolved_item: CompletionItem | nil))
--- @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 FuzzyConfig
--- @class blink.cmp.FuzzyConfig
--- @field use_frecency boolean
--- @field use_proximity boolean
--- @field max_items number
--- @field sorts ("label" | "kind" | "score")[]

--- @class WindowConfig
--- @field autocomplete AutocompleteConfig
--- @field documentation DocumentationConfig
--- @class blink.cmp.WindowConfig
--- @field autocomplete blink.cmp.AutocompleteConfig
--- @field documentation blink.cmp.DocumentationConfig

--- @class AutocompleteConfig
--- @class blink.cmp.AutocompleteConfig
--- @field min_width number
--- @field max_width number
--- @field max_height number
--- @field order "top_down" | "bottom_up"
--- @field direction_priority ("n" | "s")[]
--- @field preselect boolean

--- @class DocumentationDirectionPriorityConfig
--- @class blink.cmp.DocumentationDirectionPriorityConfig
--- @field autocomplete_north ("n" | "s" | "e" | "w")[]
--- @field autocomplete_south ("n" | "s" | "e" | "w")[]
---
--- @class DocumentationConfig
--- @class blink.cmp.DocumentationConfig
--- @field min_width number
--- @field max_width number
--- @field max_height number
--- @field direction_priority DocumentationDirectionPriorityConfig
--- @field direction_priority blink.cmp.DocumentationDirectionPriorityConfig
--- @field auto_show boolean
--- @field debounce_ms number
--- @field delay_ms number

--- @class CmpConfig
--- @field trigger TriggerConfig
--- @field fuzzy FuzzyConfig
--- @field sources SourceConfig
--- @field windows WindowConfig
--- @class blink.cmp.CmpConfig
--- @field trigger blink.cmp.TriggerConfig
--- @field fuzzy blink.cmp.FuzzyConfig
--- @field sources blink.cmp.SourceConfig
--- @field windows blink.cmp.WindowConfig
--- @field highlight_ns number
--- @field kind_icons table<string, string>

--- @type CmpConfig
--- @type blink.cmp.CmpConfig
local config = {
keymap = {
show = '<C-space>',
hide = '<C-e>',
accept = '<Tab>',
select_prev = { '<Up>', '<C-j>' },
select_next = { '<Down>', '<C-k>' },

show_documentation = {},
hide_documentation = {},
scroll_documentation_up = '<C-b>',
scroll_documentation_down = '<C-f>',

snippet_forward = '<Tab>',
snippet_backward = '<S-Tab>',
},
Expand All @@ -96,9 +110,9 @@ local config = {
},
sources = {
providers = {
{ module = 'blink.cmp.sources.lsp' },
{ module = 'blink.cmp.sources.buffer' },
{ module = 'blink.cmp.sources.snippets' },
{ 'blink.cmp.sources.lsp' },
{ 'blink.cmp.sources.buffer' },
{ 'blink.cmp.sources.snippets' },
},
},
windows = {
Expand Down Expand Up @@ -159,10 +173,10 @@ local config = {
},
}

--- @class CmpConfig
--- @class blink.cmp.CmpConfig
local M = {}

--- @param opts CmpConfig
--- @param opts blink.cmp.CmpConfig
function M.merge_with(opts) config = vim.tbl_deep_extend('force', config, opts or {}) end

return setmetatable(M, { __index = function(_, k) return config[k] end })
5 changes: 4 additions & 1 deletion lua/blink/cmp/init.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
local cmp = {}

--- @param opts CmpConfig
--- @param opts blink.cmp.CmpConfig
cmp.setup = function(opts)
local config = require('blink.cmp.config')
config.merge_with(opts)

require('blink.cmp.keymap').setup(config.keymap)

cmp.add_default_highlights()
-- todo: do we need to clear first?
vim.api.nvim_create_autocmd('ColorScheme', { callback = cmp.add_default_highlights })

-- STRUCTURE
Expand Down Expand Up @@ -59,6 +60,8 @@ cmp.add_default_highlights = function()
end
end

------- Public API -------

cmp.show = function()
vim.schedule(function() cmp.trigger.show() end)
return true
Expand Down
2 changes: 1 addition & 1 deletion lua/blink/cmp/sources/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ end

--- Public API

--- @class Source
--- @class blink.cmp.Source
local buffer = {}

function buffer.completions(_, callback)
Expand Down
10 changes: 5 additions & 5 deletions lua/blink/cmp/sources/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ end

function sources.listen_on_completions(callback) sources.on_completions_callback = callback end

--- @param context ShowContext
--- @param context blink.cmp.ShowContext
function sources.completions(context)
-- a new context means we should refetch everything
local is_new_context = context.id ~= sources.current_context_id
Expand Down Expand Up @@ -97,7 +97,7 @@ function sources.completions(context)
end

--- @param source_name string
--- @param source_response CompletionResponse
--- @param source_response blink.cmp.CompletionResponse
--- @param cursor_column number
function sources.add_source_completions(source_name, source_response, cursor_column)
for _, item in ipairs(source_response.items) do
Expand All @@ -116,7 +116,7 @@ function sources.some_in_flight()
return false
end

--- @param context ShowContext
--- @param context blink.cmp.ShowContext
function sources.send_completions(context)
local sources_responses = sources.sources_responses
-- apply source filters
Expand Down Expand Up @@ -145,8 +145,8 @@ function sources.cancel_completions()
end
end

--- @param item CompletionItem
--- @param callback fun(resolved_item: CompletionItem | nil)
--- @param item blink.cmp.CompletionItem
--- @param callback fun(resolved_item: blink.cmp.CompletionItem | nil)
--- @return fun(): nil Cancelation function
function sources.resolve(item, callback)
local item_source = sources.registered[item.source]
Expand Down
2 changes: 1 addition & 1 deletion lua/blink/cmp/sources/lsp.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
--- @class Source
--- @class blink.cmp.Source
local lsp = {}

---@param capability string|table|nil Server capability (possibly nested
Expand Down
4 changes: 2 additions & 2 deletions lua/blink/cmp/sources/snippets.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
--- @class Source
--- @class blink.cmp.Source
local snippets = {}

function snippets.completions(_, callback)
Expand Down Expand Up @@ -52,7 +52,7 @@ function snippets.filter_completions(context, sources_responses)
end
sources_responses = copied_sources_responses

-- don't show if since a trigger character triggered this
-- don't show if a trigger character triggered this
-- todo: the idea here is that situations like `text.|` shouldn't show
-- the buffer completions since it's likely not helpful
sources_responses.snippets.items = {}
Expand Down
28 changes: 14 additions & 14 deletions lua/blink/cmp/trigger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@
-- (provided by the sources) or anything matching the `context_regex`, we create a new `context`.
-- This can be used downstream to determine if we should make new requests to the sources or not.

--- @class TriggerBounds
--- @class blink.cmp.TriggerBounds
--- @field line string
--- @field line_number number
--- @field start_col number
--- @field end_col number
---
--- @class TriggerContext
--- @class blink.cmp.TriggerContext
--- @field id number
--- @field bounds TriggerBounds
--- @field bounds blink.cmp.TriggerBounds
--- @field treesitter_node table | nil
---
--- @class ShowContext : TriggerContext
--- @class blink.cmp.ShowContext : blink.cmp.TriggerContext
--- @field trigger_character string | nil
---
--- @class TriggerEventTargets
--- @field on_show fun(context: ShowContext)
--- @class blink.cmp.TriggerEventTargets
--- @field on_show fun(context: blink.cmp.ShowContext)
--- @field on_hide fun()
---
--- @class Trigger
--- @field context TriggerContext | nil
--- @class blink.cmp.Trigger
--- @field context blink.cmp.TriggerContext | nil
--- @field context_last_id number
--- @field context_regex string
--- @field event_targets TriggerEventTargets
--- @field event_targets blink.cmp.TriggerEventTargets

local sources = require('blink.cmp.sources')

--- @class Trigger
--- @class blink.cmp.Trigger
local trigger = {
context_last_id = -1,
context = nil,
Expand Down Expand Up @@ -101,10 +101,10 @@ function trigger.activate_autocmds()
return trigger
end

--- @class TriggerOptions
--- @class blink.cmp.TriggerOptions
--- @field trigger_character string|nil
---
--- @param opts TriggerOptions|nil
--- @param opts blink.cmp.TriggerOptions|nil
function trigger.show(opts)
opts = opts or {}

Expand Down Expand Up @@ -132,7 +132,7 @@ function trigger.listen_on_hide(callback) trigger.event_targets.on_hide = callba

------ Context ------
-- Gets the current context, always updating the bounds to the current position
--- @return TriggerContext
--- @return blink.cmp.TriggerContext
function trigger.get_context()
local bounds = helpers.get_query(trigger.context_regex)
-- local treesitter_node = helpers.get_treesitter_node_at_cursor()
Expand Down Expand Up @@ -162,7 +162,7 @@ end

-- Moves forward and backwards around the cursor looking for word boundaries
--- @param regex string
--- @return TriggerBounds
--- @return blink.cmp.TriggerBounds
function helpers.get_query(regex)
local bufnr = vim.api.nvim_get_current_buf()
local cursor_line = vim.api.nvim_win_get_cursor(0)[1]
Expand Down
2 changes: 1 addition & 1 deletion lua/blink/cmp/types.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
--- @class CompletionItem : lsp.CompletionItem
--- @class blink.cmp.CompletionItem : lsp.CompletionItem
--- @field score_offset number | nil
--- @field source string
--- @field cursor_column number
Expand Down
9 changes: 5 additions & 4 deletions lua/blink/cmp/windows/lib.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local win = {}

--- @class WindowOptions
--- @class blink.cmp.WindowOptions
--- @field min_width number
--- @field max_width number
--- @field max_height number
Expand All @@ -11,10 +11,11 @@ local win = {}
--- @field padding boolean
--- @field scrolloff number

--- @class Window
--- @field config WindowOptions
--- @class blink.cmp.Window
--- @field id number | nil
--- @field config blink.cmp.WindowOptions
---
--- @param config WindowOptions
--- @param config blink.cmp.WindowOptions
function win.new(config)
local self = setmetatable({}, { __index = win })

Expand Down

0 comments on commit e837718

Please sign in to comment.