Skip to content

Commit ee80318

Browse files
committed
feat: allow caller to use custom CTX (#1757)
This allows opening document symbols via an action: ```lua local fzf = require("fzf-lua") local document_symbols_for_selected = function(selected, _) local file = fzf.path.entry_to_file(selected[1]) vim.schedule(function() local buf = vim.fn.bufadd(file.path) vim.fn.bufload(buf) fzf.lsp_document_symbols({ ctx = { buf = buf }, lsp_params = { textDocument = vim.lsp.util.make_text_document_params(buf) }, }) end) end fzf.files({ actions = { ["ctrl-y"] = document_symbols_for_selected } }) ```
1 parent 29e2909 commit ee80318

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

lua/fzf-lua/core.lua

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ end
257257

258258
-- conditionally update the context if fzf-lua
259259
-- interface isn't open
260-
M.CTX = function(includeBuflist)
260+
M.CTX = function(opts)
261+
opts = opts or {}
261262
-- save caller win/buf context, ignore when fzf
262263
-- is already open (actions.sym_lsym|grep_lgrep)
263264
local winobj = utils.fzf_winobj()
@@ -271,10 +272,12 @@ M.CTX = function(includeBuflist)
271272
-- not to resume or a different picker, i.e. hide files and open buffers
272273
or winobj and winobj:hidden()
273274
then
275+
-- custom bufnr from caller? (#1757)
276+
local bufnr = tonumber(opts.buf) or tonumber(opts.bufnr) or vim.api.nvim_get_current_buf()
274277
M.__CTX = {
275278
mode = vim.api.nvim_get_mode().mode,
276-
bufnr = vim.api.nvim_get_current_buf(),
277-
bname = vim.api.nvim_buf_get_name(0),
279+
bufnr = bufnr,
280+
bname = vim.api.nvim_buf_get_name(bufnr),
278281
winid = vim.api.nvim_get_current_win(),
279282
alt_bufnr = vim.fn.bufnr("#"),
280283
tabnr = vim.fn.tabpagenr(),
@@ -294,7 +297,7 @@ M.CTX = function(includeBuflist)
294297
-- perhaps a min impact optimization but since only
295298
-- buffers/tabs use these we only include the current
296299
-- list of buffers when requested
297-
if includeBuflist and not M.__CTX.buflist then
300+
if opts.includeBuflist and not M.__CTX.buflist then
298301
-- also add a map for faster lookups than `utils.tbl_contains`
299302
-- TODO: is it really faster since we must use string keys?
300303
M.__CTX.bufmap = {}
@@ -353,7 +356,7 @@ M.fzf = function(contents, opts)
353356
end
354357
-- update context and save a copy in options (for actions)
355358
-- call before creating the window or fzf_winobj is not nil
356-
opts.__CTX = M.CTX()
359+
opts.__CTX = M.CTX(opts.ctx)
357360
if opts.fn_pre_win then
358361
opts.fn_pre_win(opts)
359362
end

lua/fzf-lua/providers/buffers.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ M.buffers = function(opts)
207207
-- save as a func ref for resume to reuse
208208
opts._fn_pre_fzf = function()
209209
shell.set_protected(id)
210-
core.CTX(true) -- include `nvim_list_bufs` in context
210+
core.CTX({ includeBuflist = true }) -- include `nvim_list_bufs` in context
211211
end
212212

213213
if opts.fzf_opts["--header-lines"] == nil then
@@ -236,7 +236,7 @@ end
236236
M.buffer_lines = function(opts)
237237
if not opts then return end
238238

239-
opts.fn_pre_fzf = function() core.CTX(true) end
239+
opts.fn_pre_fzf = function() core.CTX({ includeBuflist = true }) end
240240
opts.fn_pre_fzf()
241241

242242
local contents = function(cb)
@@ -448,7 +448,7 @@ M.tabs = function(opts)
448448
-- save as a func ref for resume to reuse
449449
opts._fn_pre_fzf = function()
450450
shell.set_protected(id)
451-
core.CTX(true) -- include `nvim_list_bufs` in context
451+
core.CTX({ includeBuflist = true }) -- include `nvim_list_bufs` in context
452452
end
453453

454454
opts = core.set_header(opts, opts.headers or { "actions", "cwd" })

0 commit comments

Comments
 (0)