Skip to content

Commit 34f7ef5

Browse files
committed
feat: new "global" picker (closes #2058)
1 parent 16515fe commit 34f7ef5

7 files changed

Lines changed: 75 additions & 4 deletions

File tree

lua/fzf-lua/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ function M.normalize_opts(opts, globals, __resume_key)
382382
end
383383

384384
-- `fzf_cli_args` is string, `_fzf_cli_args` is a table used internally
385-
opts._fzf_cli_args = {}
385+
opts._fzf_cli_args = type(opts._fzf_cli_args) == "table" and opts._fzf_cli_args or {}
386386

387387
-- backward compatibility, rhs overrides lhs
388388
-- (rhs being the "old" option)

lua/fzf-lua/defaults.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,14 @@ M.defaults.files = {
360360
winopts = { preview = { winopts = { cursorline = false } } },
361361
}
362362

363+
M.defaults.global = vim.tbl_deep_extend("force", M.defaults.files, {
364+
cwd_prompt = false,
365+
line_query = true,
366+
winopts = { preview = { winopts = { cursorline = true } } },
367+
-- Global has a buffers picker
368+
_ctx = { includeBuflist = true },
369+
})
370+
363371
-- Must construct our opts table in stages
364372
-- so we can reference 'M.globals.files'
365373
M.defaults.git = {

lua/fzf-lua/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ do
289289
tmux_buffers = { "fzf-lua.providers.tmux", "buffers" },
290290
profiles = { "fzf-lua.providers.meta", "profiles" },
291291
combine = { "fzf-lua.providers.meta", "combine" },
292+
global = { "fzf-lua.providers.meta", "global" },
292293
complete_path = { "fzf-lua.complete", "path" },
293294
complete_file = { "fzf-lua.complete", "file" },
294295
complete_line = { "fzf-lua.complete", "line" },

lua/fzf-lua/profiles/default-prompt.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ return {
66
winopts = { title_pos = "center", preview = { title_pos = "center" } },
77
-- Uses `cwd_prompt` by default
88
-- files = prompt("Files"),
9+
global = prompt("Global"),
910
buffers = prompt("Buffers"),
1011
tabs = prompt("Tabs"),
1112
lines = prompt("Lines"),

lua/fzf-lua/profiles/default-title.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ end
99
return {
1010
desc = "defaults using title for picker info",
1111
winopts = { title_pos = "center", preview = { title_pos = "center" } },
12+
global = title("Global"),
1213
files = title("Files"),
1314
buffers = title("Buffers"),
1415
tabs = title("Tabs"),

lua/fzf-lua/providers/meta.lua

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local uv = vim.uv or vim.loop
22
local core = require "fzf-lua.core"
33
local path = require "fzf-lua.path"
44
local utils = require "fzf-lua.utils"
5+
local libuv = require "fzf-lua.libuv"
56
local config = require "fzf-lua.config"
67

78
local M = {}
@@ -121,7 +122,7 @@ M.combine = function(t)
121122

122123
local cmds, opts = (function()
123124
local ret, opts = {}, nil
124-
for i, p in ipairs(t.pickers) do
125+
for _, p in ipairs(t.pickers) do
125126
-- local ok, msg, cmd, o = pcall(FzfLua[p], opts1)
126127
-- if not ok or not cmd then
127128
local _, cmd, o = FzfLua[p](opts1)
@@ -148,4 +149,61 @@ M.combine = function(t)
148149
return core.fzf_wrap(contents, opts)
149150
end
150151

152+
M.global = function(opts)
153+
opts = config.normalize_opts(opts, "global")
154+
if not opts then return end
155+
156+
if opts.line_query and not utils.has(opts, "fzf", { 0, 59 }) then
157+
utils.warn("'global' requires fzf >= 0.59, reverting to files.")
158+
return FzfLua.files(opts)
159+
end
160+
161+
-- Tells fzf_wrap to not start the fzf process
162+
opts._start = false
163+
local pickers = {
164+
files = { FzfLua.files(vim.deepcopy(opts)) },
165+
buffers = { FzfLua.buffers(vim.deepcopy(opts)) },
166+
lsp_document_symbols = { FzfLua.lsp_document_symbols(vim.deepcopy(opts)) },
167+
lsp_workspace_symbols = { FzfLua.lsp_workspace_symbols(vim.deepcopy(opts)) },
168+
}
169+
170+
-- Remove the `_start` directive from all pickers
171+
opts._start = nil
172+
for _, v in pairs(pickers) do
173+
v[3]._start = nil
174+
end
175+
176+
local get_picker = function(q)
177+
q = type(q) == "string" and q or ""
178+
if q:match("^%$") then
179+
return pickers.buffers, 2
180+
elseif q:match("^@") then
181+
return pickers.lsp_document_symbols, 2
182+
elseif q:match("^#") then
183+
return pickers.lsp_workspace_symbols, 2
184+
else
185+
return pickers.files, 1
186+
end
187+
end
188+
189+
-- Get starting picker
190+
local current_picker = get_picker()
191+
192+
---@diagnostic disable-next-line: redefined-local
193+
local contents, opts = current_picker[2], current_picker[3]
194+
195+
table.insert(opts._fzf_cli_args, "--bind=" .. libuv.shellescape("change:+transform:"
196+
.. FzfLua.shell.stringify_data(function(args, _, _)
197+
local q = args[1]
198+
local new_picker, sub = get_picker(q)
199+
-- Do we need to change the picker?
200+
if current_picker ~= new_picker then
201+
current_picker = new_picker
202+
return string.format("search(%s)+reload:%s", q:sub(sub), new_picker[2])
203+
end
204+
end, opts, "{q}")))
205+
206+
return core.fzf_wrap(contents, opts)
207+
end
208+
151209
return M

lua/fzf-lua/shell.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,12 @@ function LRU:get(id)
8383
end
8484

8585
-- Cache should be able to hold all function callbacks of a single picker
86-
-- max cache size of 50 should be more than enough, we don't want it to be
86+
-- max cache size of 100 should be more than enough, we don't want it to be
8787
-- too big as this will prevent clearing of referecnces to "opts" which
8888
-- prevents garabage collection from freeing the resources
89-
local function new_cache(size) return LRU:new(size or 50) end
89+
-- NOTE: with combine/global the no. of callbacks has increased significantly
90+
-- so monitor the number of callbacks
91+
local function new_cache(size) return LRU:new(size or 100) end
9092
local _cache = new_cache()
9193

9294
local M = {}

0 commit comments

Comments
 (0)