Skip to content

Commit a6636c1

Browse files
committed
feat: use enabled function instead of blocked_filetypes
And changes the logic to check for buftype ~= 'prompt' rather than blocking ~= '' Closes #440
1 parent 9fa3fe2 commit a6636c1

File tree

7 files changed

+49
-52
lines changed

7 files changed

+49
-52
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,12 @@ MiniDeps.add({
252252
-- ['<C-f>'] = { 'scroll_documentation_down', 'fallback' },
253253
keymap = 'default',
254254

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

258262
snippets = {
259263
-- Function to use when expanding LSP provided snippets

lua/blink-cmp.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
return require('blink.cmp')
1+
return require("blink.cmp")
2+

lua/blink/cmp/config/init.lua

+33-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--- @class (exact) blink.cmp.ConfigStrict
2-
--- @field blocked_filetypes string[]
2+
--- @field enabled fun(): boolean
33
--- @field keymap blink.cmp.KeymapConfig
44
--- @field completion blink.cmp.CompletionConfig
55
--- @field sources blink.cmp.SourceConfig
@@ -12,44 +12,50 @@
1212
--- but this seems to be a bug. See https://github.com/LuaLS/lua-language-server/issues/2561
1313
--- Much easier than copying every class and marking everything as optional for now :)
1414

15-
local validate = require('blink.cmp.config.utils').validate
15+
local validate = require("blink.cmp.config.utils").validate
1616
--- @type blink.cmp.ConfigStrict
1717
local config = {
18-
blocked_filetypes = {},
19-
keymap = require('blink.cmp.config.keymap').default,
20-
completion = require('blink.cmp.config.completion').default,
21-
fuzzy = require('blink.cmp.config.fuzzy').default,
22-
sources = require('blink.cmp.config.sources').default,
23-
signature = require('blink.cmp.config.signature').default,
24-
snippets = require('blink.cmp.config.snippets').default,
25-
appearance = require('blink.cmp.config.appearance').default,
18+
enabled = function()
19+
return vim.bo.buftype ~= "prompt"
20+
end,
21+
keymap = require("blink.cmp.config.keymap").default,
22+
completion = require("blink.cmp.config.completion").default,
23+
fuzzy = require("blink.cmp.config.fuzzy").default,
24+
sources = require("blink.cmp.config.sources").default,
25+
signature = require("blink.cmp.config.signature").default,
26+
snippets = require("blink.cmp.config.snippets").default,
27+
appearance = require("blink.cmp.config.appearance").default,
2628
}
2729

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

3133
--- @param self blink.cmp.ConfigStrict
3234
function M.validate(self)
33-
validate('config', {
34-
blocked_filetypes = { self.blocked_filetypes, 'table' },
35-
keymap = { self.keymap, 'table' },
36-
completion = { self.completion, 'table' },
37-
sources = { self.sources, 'table' },
38-
signature = { self.signature, 'table' },
39-
snippets = { self.snippets, 'table' },
40-
appearance = { self.appearance, 'table' },
41-
})
42-
require('blink.cmp.config.completion').validate(self.completion)
43-
require('blink.cmp.config.sources').validate(self.sources)
44-
require('blink.cmp.config.signature').validate(self.signature)
45-
require('blink.cmp.config.snippets').validate(self.snippets)
46-
require('blink.cmp.config.appearance').validate(self.appearance)
35+
validate("config", {
36+
enabled = { self.enabled, "function" },
37+
keymap = { self.keymap, "table" },
38+
completion = { self.completion, "table" },
39+
sources = { self.sources, "table" },
40+
signature = { self.signature, "table" },
41+
snippets = { self.snippets, "table" },
42+
appearance = { self.appearance, "table" },
43+
})
44+
require("blink.cmp.config.completion").validate(self.completion)
45+
require("blink.cmp.config.sources").validate(self.sources)
46+
require("blink.cmp.config.signature").validate(self.signature)
47+
require("blink.cmp.config.snippets").validate(self.snippets)
48+
require("blink.cmp.config.appearance").validate(self.appearance)
4749
end
4850

4951
--- @param user_config blink.cmp.Config
5052
function M.merge_with(user_config)
51-
config = vim.tbl_deep_extend('force', config, user_config)
52-
M.validate(config)
53+
config = vim.tbl_deep_extend("force", config, user_config)
54+
M.validate(config)
5355
end
5456

55-
return setmetatable(M, { __index = function(_, k) return config[k] end })
57+
return setmetatable(M, {
58+
__index = function(_, k)
59+
return config[k]
60+
end,
61+
})

lua/blink/cmp/keymap/init.lua

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
local utils = require('blink.cmp.lib.utils')
21
local keymap = {}
32

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

2928
-- This is not called when the plugin loads since it first checks if the binary is
3029
-- installed. As a result, when lazy-loaded on InsertEnter, the event may be missed
31-
if vim.api.nvim_get_mode().mode == 'i' and not utils.is_blocked_buffer() then
30+
if vim.api.nvim_get_mode().mode == 'i' and require('blink.cmp.config').enabled() then
3231
require('blink.cmp.keymap.apply').keymap_to_current_buffer(mappings)
3332
end
3433
end

lua/blink/cmp/lib/buffer_events.lua

+2-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ end
3636

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

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

5049
vim.api.nvim_create_autocmd('TextChangedI', {
5150
callback = function()
52-
if utils.is_blocked_buffer() then return end
51+
if not require('blink.cmp.config').enabled() then return end
5352
if snippet.active() and not self.show_in_snippet and not self.has_context() then return end
5453

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

75-
if utils.is_blocked_buffer() then return end
74+
if not require('blink.cmp.config').enabled() then return end
7675
if snippet.active() and not self.show_in_snippet and not self.has_context() then return end
7776

7877
opts.on_cursor_moved(ev.event, is_ignored)

lua/blink/cmp/lib/utils.lua

-12
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,6 @@ function utils.deduplicate(arr)
4040
return vim.tbl_keys(hash)
4141
end
4242

43-
--- Determines whether the current buffer is a "special" buffer or if the filetype is in the list of ignored filetypes
44-
--- @return boolean
45-
function utils.is_blocked_buffer()
46-
local bufnr = vim.api.nvim_get_current_buf()
47-
local buftype = vim.api.nvim_get_option_value('buftype', { buf = bufnr })
48-
local blocked_filetypes = require('blink.cmp.config').blocked_filetypes or {}
49-
local buf_filetype = vim.api.nvim_get_option_value('filetype', { buf = bufnr })
50-
51-
if vim.tbl_contains(blocked_filetypes, buf_filetype) then return true end
52-
return buftype ~= ''
53-
end
54-
5543
--- Gets characters around the cursor and returns the range, 0-indexed
5644
--- @param range 'prefix' | 'full'
5745
--- @param regex_str string

lua/blink/cmp/signature/trigger.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ function trigger.activate()
4949
local is_on_trigger = trigger.is_trigger_character(char)
5050
local is_on_retrigger = trigger.is_trigger_character(char, true)
5151

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

0 commit comments

Comments
 (0)