Skip to content

Commit

Permalink
feat!(snippets): filter_snippets function for disabling certain snipp…
Browse files Browse the repository at this point in the history
…et files (#996)

* feat(snippets): `filter_snippets` function for disabling certain snippet files

This can be used with eg.
```lua
filter_snippets = function(ft, file)
  return not (string.match(file, "friendly.snippets") and string.match(file, "framework"))
end,
```
to disable friendly-snippets frameworks.

Closes #291

* feat!(snippets): remove the `ignored_filetypes` config

This is redundant now that users can filter snippets with more control.
  • Loading branch information
stefanboca authored Jan 12, 2025
1 parent dadbd25 commit 996aba4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
3 changes: 1 addition & 2 deletions lua/blink/cmp/sources/snippets/default/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
--- @field search_paths? string[]
--- @field global_snippets? string[]
--- @field extended_filetypes? table<string, string[]>
--- @field ignored_filetypes? string[]
--- @field get_filetype? fun(context: blink.cmp.Context): string
--- @field filter_snippets? fun(filetype: string, file: string): boolean
--- @field clipboard_register? string

local snippets = {}
Expand All @@ -22,7 +22,6 @@ end

function snippets:get_completions(context, callback)
local filetype = self.get_filetype(context)
if vim.tbl_contains(self.registry.config.ignored_filetypes, filetype) then return callback() end

if not self.cache[filetype] then
local global_snippets = self.registry:get_global_snippets()
Expand Down
13 changes: 12 additions & 1 deletion lua/blink/cmp/sources/snippets/default/registry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ local default_config = {
search_paths = { vim.fn.stdpath('config') .. '/snippets' },
global_snippets = { 'all' },
extended_filetypes = {},
ignored_filetypes = {},
--- @type string?
clipboard_register = nil,
}
Expand All @@ -35,6 +34,18 @@ function registry.new(config)
end
self.registry = require('blink.cmp.sources.snippets.default.scan').register_snippets(self.config.search_paths)

if self.config.filter_snippets then
local filtered_registry = {}
for ft, files in pairs(self.registry) do
filtered_registry[ft] = {}
for _, file in ipairs(files) do
if self.config.filter_snippets(ft, file) then table.insert(filtered_registry[ft], file) end
end
end

self.registry = filtered_registry
end

return self
end

Expand Down

0 comments on commit 996aba4

Please sign in to comment.