From a6636c1c38704c1581750b29abb0addabd198b89 Mon Sep 17 00:00:00 2001 From: Liam Dyer Date: Mon, 9 Dec 2024 12:30:57 -0500 Subject: [PATCH] feat: use `enabled` function instead of blocked_filetypes And changes the logic to check for buftype ~= 'prompt' rather than blocking ~= '' Closes #440 --- README.md | 8 +++- lua/blink-cmp.lua | 3 +- lua/blink/cmp/config/init.lua | 60 ++++++++++++++++------------- lua/blink/cmp/keymap/init.lua | 5 +-- lua/blink/cmp/lib/buffer_events.lua | 5 +-- lua/blink/cmp/lib/utils.lua | 12 ------ lua/blink/cmp/signature/trigger.lua | 8 ++-- 7 files changed, 49 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index f24a19a7..dd102671 100644 --- a/README.md +++ b/README.md @@ -252,8 +252,12 @@ MiniDeps.add({ -- [''] = { '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 diff --git a/lua/blink-cmp.lua b/lua/blink-cmp.lua index fb3e8313..d4a11f54 100644 --- a/lua/blink-cmp.lua +++ b/lua/blink-cmp.lua @@ -1 +1,2 @@ -return require('blink.cmp') +return require("blink.cmp") + diff --git a/lua/blink/cmp/config/init.lua b/lua/blink/cmp/config/init.lua index 3ff982fe..f446a9f9 100644 --- a/lua/blink/cmp/config/init.lua +++ b/lua/blink/cmp/config/init.lua @@ -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 @@ -12,17 +12,19 @@ --- 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 @@ -30,26 +32,30 @@ 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, +}) diff --git a/lua/blink/cmp/keymap/init.lua b/lua/blink/cmp/keymap/init.lua index 0e402749..0b0d5973 100644 --- a/lua/blink/cmp/keymap/init.lua +++ b/lua/blink/cmp/keymap/init.lua @@ -1,4 +1,3 @@ -local utils = require('blink.cmp.lib.utils') local keymap = {} function keymap.setup() @@ -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 diff --git a/lua/blink/cmp/lib/buffer_events.lua b/lua/blink/cmp/lib/buffer_events.lua index 03ac314d..536fa38f 100644 --- a/lua/blink/cmp/lib/buffer_events.lua +++ b/lua/blink/cmp/lib/buffer_events.lua @@ -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 = '' @@ -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 @@ -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) diff --git a/lua/blink/cmp/lib/utils.lua b/lua/blink/cmp/lib/utils.lua index 5b656319..ad7a206c 100644 --- a/lua/blink/cmp/lib/utils.lua +++ b/lua/blink/cmp/lib/utils.lua @@ -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 diff --git a/lua/blink/cmp/signature/trigger.lua b/lua/blink/cmp/signature/trigger.lua index 5e41e0ae..e263346a 100644 --- a/lua/blink/cmp/signature/trigger.lua +++ b/lua/blink/cmp/signature/trigger.lua @@ -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