Skip to content

Commit

Permalink
feat: support configuring clipboard register for snippets
Browse files Browse the repository at this point in the history
Closes #800
  • Loading branch information
Saghen committed Dec 29, 2024
1 parent c593e83 commit 8f51a4e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
2 changes: 2 additions & 0 deletions docs/configuration/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,8 @@ sources.providers = {
get_filetype = function(context)
return vim.bo.filetype
end
-- Set to '+' to use the system clipboard, or '"' to use the unnamed register
clipboard_register = nil,
}
},
luasnip = {
Expand Down
Empty file removed lua/blink/cmp/snippets.lua
Empty file.
2 changes: 1 addition & 1 deletion lua/blink/cmp/sources/snippets/builtin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function builtin.lazy.TM_DIRECTORY() return vim.fn.expand('%:p:h') end

function builtin.lazy.TM_FILEPATH() return vim.fn.expand('%:p') end

function builtin.lazy.CLIPBOARD() return vim.fn.getreg(vim.v.register, true) end
function builtin.lazy.CLIPBOARD(opts) return vim.fn.getreg(opts.clipboard_register or vim.v.register, true) end

local function buf_to_ws_part()
local LSP_WORSKPACE_PARTS = 'LSP_WORSKPACE_PARTS'
Expand Down
15 changes: 8 additions & 7 deletions lua/blink/cmp/sources/snippets/init.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
--- @class blink.cmp.SnippetsOpts
--- @field friendly_snippets boolean
--- @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 friendly_snippets? boolean
--- @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 clipboard_register? string

local snippets = {}

function snippets.new(opts)
--- @type blink.cmp.SnippetsOpts
opts = opts or {}
local self = setmetatable({}, { __index = snippets })
--- @type table<string, blink.cmp.CompletionItem[]>
self.cache = {}
--- @type blink.cmp.SnippetsOpts
self.registry = require('blink.cmp.sources.snippets.registry').new(opts)
self.get_filetype = opts.get_filetype or function() return vim.bo.filetype end
return self
Expand Down
15 changes: 10 additions & 5 deletions lua/blink/cmp/sources/snippets/registry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
--- for the original implementation
--- Original License: MIT

---@class blink.cmp.Snippet
---@field prefix string
---@field body string[] | string
---@field description? string
--- @class blink.cmp.Snippet
--- @field prefix string
--- @field body string[] | string
--- @field description? string

local registry = {
builtin_vars = require('blink.cmp.sources.snippets.builtin'),
Expand All @@ -18,6 +18,8 @@ local default_config = {
global_snippets = { 'all' },
extended_filetypes = {},
ignored_filetypes = {},
--- @type string?
clipboard_register = nil,
}

--- @param config blink.cmp.SnippetsOpts
Expand Down Expand Up @@ -127,7 +129,10 @@ function registry:expand_vars(snippet)
if eager_vars[data.name] then
resolved_snippet = resolved_snippet:gsub('%$[{]?(' .. data.name .. ')[}]?', eager_vars[data.name])
elseif lazy_vars[data.name] then
resolved_snippet = resolved_snippet:gsub('%$[{]?(' .. data.name .. ')[}]?', lazy_vars[data.name]())
resolved_snippet = resolved_snippet:gsub(
'%$[{]?(' .. data.name .. ')[}]?',
lazy_vars[data.name]({ clipboard_register = self.config.clipboard_register })
)
end
end
end
Expand Down

0 comments on commit 8f51a4e

Please sign in to comment.