From 8f51a4ec23773cc96ec6b3ca336a5d70eebb2fb2 Mon Sep 17 00:00:00 2001 From: Liam Dyer Date: Sun, 29 Dec 2024 14:34:24 -0500 Subject: [PATCH] feat: support configuring clipboard register for snippets Closes #800 --- docs/configuration/reference.md | 2 ++ lua/blink/cmp/snippets.lua | 0 lua/blink/cmp/sources/snippets/builtin.lua | 2 +- lua/blink/cmp/sources/snippets/init.lua | 15 ++++++++------- lua/blink/cmp/sources/snippets/registry.lua | 15 ++++++++++----- 5 files changed, 21 insertions(+), 13 deletions(-) delete mode 100644 lua/blink/cmp/snippets.lua diff --git a/docs/configuration/reference.md b/docs/configuration/reference.md index 90cd2af7..893bf5c1 100644 --- a/docs/configuration/reference.md +++ b/docs/configuration/reference.md @@ -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 = { diff --git a/lua/blink/cmp/snippets.lua b/lua/blink/cmp/snippets.lua deleted file mode 100644 index e69de29b..00000000 diff --git a/lua/blink/cmp/sources/snippets/builtin.lua b/lua/blink/cmp/sources/snippets/builtin.lua index 3e5df03f..b66ca1ce 100644 --- a/lua/blink/cmp/sources/snippets/builtin.lua +++ b/lua/blink/cmp/sources/snippets/builtin.lua @@ -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' diff --git a/lua/blink/cmp/sources/snippets/init.lua b/lua/blink/cmp/sources/snippets/init.lua index 74d20729..b670f770 100644 --- a/lua/blink/cmp/sources/snippets/init.lua +++ b/lua/blink/cmp/sources/snippets/init.lua @@ -1,19 +1,20 @@ --- @class blink.cmp.SnippetsOpts ---- @field friendly_snippets boolean ---- @field search_paths string[] ---- @field global_snippets string[] ---- @field extended_filetypes table ---- @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 +--- @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 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 diff --git a/lua/blink/cmp/sources/snippets/registry.lua b/lua/blink/cmp/sources/snippets/registry.lua index e16809f8..6e8f71c5 100644 --- a/lua/blink/cmp/sources/snippets/registry.lua +++ b/lua/blink/cmp/sources/snippets/registry.lua @@ -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'), @@ -18,6 +18,8 @@ local default_config = { global_snippets = { 'all' }, extended_filetypes = {}, ignored_filetypes = {}, + --- @type string? + clipboard_register = nil, } --- @param config blink.cmp.SnippetsOpts @@ -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