Skip to content

Commit

Permalink
feat: use enabled function instead of blocked_filetypes
Browse files Browse the repository at this point in the history
And changes the logic to check for buftype ~= 'prompt' rather than
blocking ~= ''

Closes #440
  • Loading branch information
Saghen committed Dec 9, 2024
1 parent 9fa3fe2 commit a6636c1
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 52 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,12 @@ MiniDeps.add({
-- ['<C-f>'] = { 'scroll_documentation_down', 'fallback' },
keymap = 'default',

-- Disables keymaps, completions and signature help for these filetypes
blocked_filetypes = {},
-- Enables keymaps, completions and signature help when true
enabled = function() return vim.bo.buftype ~= "prompt" end,
-- Example for blocking multiple filetypes
-- enabled = function()
-- return not vim.tbl_contains({ "lua", "markdown" }, vim.bo.filetype) and vim.bo.buftype ~= "prompt"
-- end,

snippets = {
-- Function to use when expanding LSP provided snippets
Expand Down
3 changes: 2 additions & 1 deletion lua/blink-cmp.lua
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
return require('blink.cmp')
return require("blink.cmp")

60 changes: 33 additions & 27 deletions lua/blink/cmp/config/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- @class (exact) blink.cmp.ConfigStrict
--- @field blocked_filetypes string[]
--- @field enabled fun(): boolean
--- @field keymap blink.cmp.KeymapConfig
--- @field completion blink.cmp.CompletionConfig
--- @field sources blink.cmp.SourceConfig
Expand All @@ -12,44 +12,50 @@
--- but this seems to be a bug. See https://github.com/LuaLS/lua-language-server/issues/2561
--- Much easier than copying every class and marking everything as optional for now :)

local validate = require('blink.cmp.config.utils').validate
local validate = require("blink.cmp.config.utils").validate
--- @type blink.cmp.ConfigStrict
local config = {
blocked_filetypes = {},
keymap = require('blink.cmp.config.keymap').default,
completion = require('blink.cmp.config.completion').default,
fuzzy = require('blink.cmp.config.fuzzy').default,
sources = require('blink.cmp.config.sources').default,
signature = require('blink.cmp.config.signature').default,
snippets = require('blink.cmp.config.snippets').default,
appearance = require('blink.cmp.config.appearance').default,
enabled = function()
return vim.bo.buftype ~= "prompt"
end,
keymap = require("blink.cmp.config.keymap").default,
completion = require("blink.cmp.config.completion").default,
fuzzy = require("blink.cmp.config.fuzzy").default,
sources = require("blink.cmp.config.sources").default,
signature = require("blink.cmp.config.signature").default,
snippets = require("blink.cmp.config.snippets").default,
appearance = require("blink.cmp.config.appearance").default,
}

--- @type blink.cmp.Config
local M = {}

--- @param self blink.cmp.ConfigStrict
function M.validate(self)
validate('config', {
blocked_filetypes = { self.blocked_filetypes, 'table' },
keymap = { self.keymap, 'table' },
completion = { self.completion, 'table' },
sources = { self.sources, 'table' },
signature = { self.signature, 'table' },
snippets = { self.snippets, 'table' },
appearance = { self.appearance, 'table' },
})
require('blink.cmp.config.completion').validate(self.completion)
require('blink.cmp.config.sources').validate(self.sources)
require('blink.cmp.config.signature').validate(self.signature)
require('blink.cmp.config.snippets').validate(self.snippets)
require('blink.cmp.config.appearance').validate(self.appearance)
validate("config", {
enabled = { self.enabled, "function" },
keymap = { self.keymap, "table" },
completion = { self.completion, "table" },
sources = { self.sources, "table" },
signature = { self.signature, "table" },
snippets = { self.snippets, "table" },
appearance = { self.appearance, "table" },
})
require("blink.cmp.config.completion").validate(self.completion)
require("blink.cmp.config.sources").validate(self.sources)
require("blink.cmp.config.signature").validate(self.signature)
require("blink.cmp.config.snippets").validate(self.snippets)
require("blink.cmp.config.appearance").validate(self.appearance)
end

--- @param user_config blink.cmp.Config
function M.merge_with(user_config)
config = vim.tbl_deep_extend('force', config, user_config)
M.validate(config)
config = vim.tbl_deep_extend("force", config, user_config)
M.validate(config)
end

return setmetatable(M, { __index = function(_, k) return config[k] end })
return setmetatable(M, {
__index = function(_, k)
return config[k]
end,
})
5 changes: 2 additions & 3 deletions lua/blink/cmp/keymap/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local utils = require('blink.cmp.lib.utils')
local keymap = {}

function keymap.setup()
Expand All @@ -21,14 +20,14 @@ function keymap.setup()
-- applied on other autocmds, such as LspAttach used by nvim-lspconfig and most configs
vim.api.nvim_create_autocmd('InsertEnter', {
callback = function()
if utils.is_blocked_buffer() then return end
if not require('blink.cmp.config').enabled() then return end
require('blink.cmp.keymap.apply').keymap_to_current_buffer(mappings)
end,
})

-- This is not called when the plugin loads since it first checks if the binary is
-- installed. As a result, when lazy-loaded on InsertEnter, the event may be missed
if vim.api.nvim_get_mode().mode == 'i' and not utils.is_blocked_buffer() then
if vim.api.nvim_get_mode().mode == 'i' and require('blink.cmp.config').enabled() then
require('blink.cmp.keymap.apply').keymap_to_current_buffer(mappings)
end
end
Expand Down
5 changes: 2 additions & 3 deletions lua/blink/cmp/lib/buffer_events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ end

--- Normalizes the autocmds + ctrl+c into a common api and handles ignored events
function buffer_events:listen(opts)
local utils = require('blink.cmp.lib.utils')
local snippet = require('blink.cmp.config').snippets

local last_char = ''
Expand All @@ -49,7 +48,7 @@ function buffer_events:listen(opts)

vim.api.nvim_create_autocmd('TextChangedI', {
callback = function()
if utils.is_blocked_buffer() then return end
if not require('blink.cmp.config').enabled() then return end
if snippet.active() and not self.show_in_snippet and not self.has_context() then return end

local is_ignored = self.ignore_next_text_changed
Expand All @@ -72,7 +71,7 @@ function buffer_events:listen(opts)
-- characters added so let textchanged handle it
if last_char ~= '' then return end

if utils.is_blocked_buffer() then return end
if not require('blink.cmp.config').enabled() then return end
if snippet.active() and not self.show_in_snippet and not self.has_context() then return end

opts.on_cursor_moved(ev.event, is_ignored)
Expand Down
12 changes: 0 additions & 12 deletions lua/blink/cmp/lib/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,6 @@ function utils.deduplicate(arr)
return vim.tbl_keys(hash)
end

--- Determines whether the current buffer is a "special" buffer or if the filetype is in the list of ignored filetypes
--- @return boolean
function utils.is_blocked_buffer()
local bufnr = vim.api.nvim_get_current_buf()
local buftype = vim.api.nvim_get_option_value('buftype', { buf = bufnr })
local blocked_filetypes = require('blink.cmp.config').blocked_filetypes or {}
local buf_filetype = vim.api.nvim_get_option_value('filetype', { buf = bufnr })

if vim.tbl_contains(blocked_filetypes, buf_filetype) then return true end
return buftype ~= ''
end

--- Gets characters around the cursor and returns the range, 0-indexed
--- @param range 'prefix' | 'full'
--- @param regex_str string
Expand Down
8 changes: 4 additions & 4 deletions lua/blink/cmp/signature/trigger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ function trigger.activate()
local is_on_trigger = trigger.is_trigger_character(char)
local is_on_retrigger = trigger.is_trigger_character(char, true)

-- ignore if in a special buffer
if utils.is_blocked_buffer() then
-- ignore if disabled
if not require('blink.cmp.config').enabled() then
return trigger.hide()
-- character forces a trigger according to the sources, refresh the existing context if it exists
-- character forces a trigger according to the sources, refresh the existing context if it exists
elseif is_on_trigger then
return trigger.show({ trigger_character = char })
-- character forces a re-trigger according to the sources, show if we have a context
-- character forces a re-trigger according to the sources, show if we have a context
elseif is_on_retrigger and trigger.context ~= nil then
return trigger.show()
end
Expand Down

0 comments on commit a6636c1

Please sign in to comment.