Skip to content

Commit 93e1b11

Browse files
committed
refactor(tool): simplify fzf-lua usage and improve lazy loading
1 parent 0abb23b commit 93e1b11

File tree

5 files changed

+121
-177
lines changed

5 files changed

+121
-177
lines changed

lua/core/settings.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,11 @@ settings["background"] = "dark"
102102
settings["external_browser"] = "chrome-cli open"
103103

104104
-- Set the search backend here.
105-
-- `telescope` is sufficient for most use cases.
106-
-- `fzf` is faster for large repositories but requires the fzf binary.
105+
-- `telescope` is fine for most use cases.
106+
-- `fzf` is faster for large repos but needs the `fzf` binary in $PATH.
107+
-- If missing, errors are expected until the binary is installed.
107108
---@type "telescope"|"fzf"
108-
settings["search_backend"] = "telescope"
109+
settings["search_backend"] = "fzf"
109110

110111
-- Set to false to disable LSP inlay hints.
111112
---@type boolean

lua/keymap/completion.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ function M.lsp(buf)
2626
:with_buffer(buf)
2727
:with_desc("lsp: Toggle outline"),
2828
["n|gto"] = map_callback(function()
29-
local search_backend = require("core.settings").search_backend
30-
if search_backend == "fzf" then
29+
if require("core.settings").search_backend == "fzf" then
3130
local prompt_position = require("telescope.config").values.layout_config.horizontal.prompt_position
3231
require("fzf-lua").lsp_document_symbols({
3332
fzf_opts = { ["--layout"] = prompt_position == "top" and "reverse" or "default" },

lua/keymap/tool.lua

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ local mappings = {
135135

136136
-- Plugin: telescope
137137
["n|<C-p>"] = map_callback(function()
138-
local search_backend = require("core.settings").search_backend
139-
if search_backend == "fzf" then
138+
if require("core.settings").search_backend == "fzf" then
140139
local prompt_position = require("telescope.config").values.layout_config.horizontal.prompt_position
141140
require("fzf-lua").keymaps({
142141
fzf_opts = { ["--layout"] = prompt_position == "top" and "reverse" or "default" },
@@ -167,20 +166,17 @@ local mappings = {
167166
:with_silent()
168167
:with_desc("tool: Find patterns"),
169168
["v|<leader>fs"] = map_callback(function()
170-
local search_backend = require("core.settings").search_backend
171-
if search_backend == "fzf" then
172-
local default_opts = "--column --line-number --no-heading --color=always --smart-case"
173-
local opts = vim.fn.getcwd() == vim_path
174-
and default_opts .. " --no-ignore --hidden --glob '!.git/*'"
175-
or ""
176-
local text = require("fzf-lua.utils").get_visual_selection()
169+
local is_config = vim.uv.cwd() == vim_path
170+
if require("core.settings").search_backend == "fzf" then
177171
require("fzf-lua").grep_project({
178-
search = text,
179-
rg_opts = opts,
172+
search = require("fzf-lua.utils").get_visual_selection(),
173+
rg_opts = "--column --line-number --no-heading --color=always --smart-case"
174+
.. (is_config and " --no-ignore --hidden --glob '!.git/*'" or ""),
180175
})
181176
else
182-
local opts = vim.fn.getcwd() == vim_path and { additional_args = { "--no-ignore" } } or {}
183-
require("telescope-live-grep-args.shortcuts").grep_visual_selection(opts)
177+
require("telescope-live-grep-args.shortcuts").grep_visual_selection(
178+
is_config and { additional_args = { "--no-ignore" } } or {}
179+
)
184180
end
185181
end)
186182
:with_noremap()
@@ -209,8 +205,7 @@ local mappings = {
209205
:with_silent()
210206
:with_desc("tool: Resume last search"),
211207
["n|<leader>fR"] = map_callback(function()
212-
local search_backend = require("core.settings").search_backend
213-
if search_backend == "fzf" then
208+
if require("core.settings").search_backend == "fzf" then
214209
require("fzf-lua").resume()
215210
end
216211
end)
Lines changed: 98 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1,176 +1,125 @@
11
return function()
22
local vim_path = require("core.global").vim_path
33
local search_backend = require("core.settings").search_backend
4-
local builtin = require("telescope.builtin")
4+
local use_fzf = search_backend == "fzf"
5+
local fzf = use_fzf and require("fzf-lua")
56
local extensions = require("telescope").extensions
7+
local builtins = require("telescope.builtin")
68
local prompt_pos = require("telescope.config").values.layout_config.horizontal.prompt_position
7-
local fzf = (search_backend == "fzf") and require("fzf-lua")
8-
local fzf_opts = { fzf_opts = { ["--layout"] = (prompt_pos == "top" and "reverse" or "default") } }
99

10-
local function files()
11-
local opts = fzf and vim.tbl_deep_extend("force", {}, fzf_opts) or {}
12-
local cwd = vim.fn.getcwd()
13-
local is_root = (cwd == vim_path)
14-
local is_git = (vim.fn.isdirectory(".git") == 1)
10+
local base_opts = use_fzf and { fzf_opts = { ["--layout"] = (prompt_pos == "top" and "reverse" or "default") } }
11+
or {}
1512

16-
if is_root then
17-
if fzf then
18-
return fzf.files(vim.tbl_deep_extend("force", opts, { no_ignore = true }))
19-
end
20-
return builtin.find_files(vim.tbl_deep_extend("force", opts, { no_ignore = true }))
21-
end
22-
23-
if is_git then
24-
if fzf then
25-
return fzf.git_files(opts)
26-
end
27-
return builtin.git_files(opts)
28-
end
29-
30-
if fzf then
31-
return fzf.files(opts)
32-
end
33-
return builtin.find_files(opts)
13+
---Returns current directory and whether it's a Git repo root
14+
---@return string @Current working directory
15+
---@return boolean|nil @true if `.git` folder exists here, false if `.git` exists but isn't folder, nil if `.git` missing
16+
local function get_root_info()
17+
local cwd = vim.uv.cwd()
18+
local stat = vim.uv.fs_stat(".git")
19+
return cwd, stat and stat.type == "directory"
3420
end
3521

36-
local function oldfiles()
37-
if fzf then
38-
local opts = vim.tbl_deep_extend("force", {}, fzf_opts)
39-
return fzf.oldfiles(opts)
22+
---Creates a file search function based on backend and context
23+
---@param fzf_fn string @Name of the fzf-lua function to call (e.g. "files")
24+
---@param tb_fn function @Telescope builtin function to call (e.g. `builtin.find_files`)
25+
---@param git_only boolean @Whether to restrict search to git tracked files only
26+
---@return fun():any @A function that executes the selected search with proper options
27+
local function file_searcher(fzf_fn, tb_fn, git_only)
28+
return function()
29+
local cwd, is_git = get_root_info()
30+
local opts = vim.deepcopy(base_opts, true)
31+
if cwd == vim_path then
32+
opts.no_ignore = true
33+
return (use_fzf and fzf[fzf_fn] or tb_fn)(opts)
34+
elseif git_only and is_git then
35+
return (use_fzf and fzf.git_files or builtins.git_files)(opts)
36+
elseif not git_only then
37+
return (use_fzf and fzf[fzf_fn] or tb_fn)(opts)
38+
else
39+
-- fallback
40+
return (use_fzf and fzf.files or builtins.find_files)(opts)
41+
end
4042
end
41-
return builtin.oldfiles()
4243
end
4344

44-
local function make_grep(fzf_fn, tb_fn)
45+
---Creates a function that performs a live grep search using the appropriate backend
46+
---@param fzf_fn string @Name of the fzf-lua grep function to call (e.g. "live_grep")
47+
---@param tb_fn function @Telescope builtin grep function (e.g. `builtin.grep_string`)
48+
---@return fun():any @Function that runs the selected grep with proper options
49+
local function grep_searcher(fzf_fn, tb_fn)
4550
return function()
46-
local opts = fzf and vim.tbl_deep_extend("force", {}, fzf_opts) or {}
47-
local cwd = vim.fn.getcwd()
51+
local cwd = vim.uv.cwd()
52+
local opts = vim.deepcopy(base_opts, true)
4853
if cwd == vim_path then
49-
if fzf then
50-
opts = vim.tbl_deep_extend("force", opts, { no_ignore = true })
54+
if use_fzf then
55+
opts.no_ignore = true
5156
else
5257
opts = { additional_args = { "--no-ignore" } }
5358
end
5459
end
55-
if fzf then
56-
return fzf[fzf_fn](opts)
57-
end
58-
return tb_fn(opts)
60+
return use_fzf and fzf[fzf_fn](opts) or tb_fn(opts)
5961
end
6062
end
6163

62-
local word_in_project = make_grep("live_grep", extensions.live_grep_args.live_grep_args)
63-
local word_under_cursor = make_grep("grep_cword", builtin.grep_string)
64-
65-
require("modules.utils").load_plugin("search", {
66-
prompt_position = prompt_pos,
67-
collections = {
68-
-- Search using filenames
69-
file = {
70-
initial_tab = 1,
71-
tabs = {
72-
{ name = "Files", tele_func = files },
73-
{
74-
name = "Frecency",
75-
tele_func = function()
76-
extensions.frecency.frecency()
77-
end,
78-
},
79-
{ name = "Oldfiles", tele_func = oldfiles },
80-
{
81-
name = "Buffers",
82-
tele_func = function()
83-
builtin.buffers()
84-
end,
85-
},
86-
},
87-
},
88-
-- Search using patterns
89-
pattern = {
90-
initial_tab = 1,
91-
tabs = {
92-
{ name = "Word in project", tele_func = word_in_project },
93-
{ name = "Word under cursor", tele_func = word_under_cursor },
94-
},
64+
-- Tables of pickers
65+
local pickers = {
66+
file = {
67+
{ "Files", file_searcher("files", builtins.find_files, false) },
68+
{
69+
"Frecency",
70+
function()
71+
extensions.frecency.frecency()
72+
end,
9573
},
96-
-- Search Git objects (branches, commits)
97-
git = {
98-
initial_tab = 1,
99-
tabs = {
100-
{
101-
name = "Branches",
102-
tele_func = function()
103-
builtin.git_branches()
104-
end,
105-
},
106-
{
107-
name = "Commits",
108-
tele_func = function()
109-
builtin.git_commits()
110-
end,
111-
},
112-
{
113-
name = "Commit content",
114-
tele_func = function()
115-
extensions.advanced_git_search.search_log_content()
116-
end,
117-
},
118-
{
119-
name = "Diff current file with commit",
120-
tele_func = function()
121-
extensions.advanced_git_search.diff_commit_file()
122-
end,
123-
},
124-
},
125-
},
126-
-- Retrieve dossiers
127-
dossier = {
128-
initial_tab = 1,
129-
tabs = {
130-
{
131-
name = "Sessions",
132-
tele_func = function()
133-
extensions.persisted.persisted()
134-
end,
135-
},
136-
{
137-
name = "Projects",
138-
tele_func = function()
139-
extensions.projects.projects({})
140-
end,
141-
},
142-
{
143-
name = "Zoxide",
144-
tele_func = function()
145-
extensions.zoxide.list()
146-
end,
147-
},
148-
},
74+
{ "Oldfiles", use_fzf and function()
75+
fzf.oldfiles(base_opts)
76+
end or builtins.oldfiles },
77+
{ "Buffers", builtins.buffers },
78+
},
79+
pattern = {
80+
{ "Word in project", grep_searcher("live_grep", extensions.live_grep_args.live_grep_args) },
81+
{ "Word under cursor", grep_searcher("grep_cword", builtins.grep_string) },
82+
},
83+
git = {
84+
{ "Branches", builtins.git_branches },
85+
{ "Commits", builtins.git_commits },
86+
{ "Commit content", extensions.advanced_git_search.search_log_content },
87+
{ "Diff current file", extensions.advanced_git_search.diff_commit_file },
88+
},
89+
dossier = {
90+
{ "Sessions", extensions.persisted.persisted },
91+
{
92+
"Projects",
93+
function()
94+
extensions.projects.projects()
95+
end,
14996
},
150-
-- Miscellaneous
151-
misc = {
152-
initial_tab = 1,
153-
tabs = {
154-
{
155-
name = "Colorschemes",
156-
tele_func = function()
157-
builtin.colorscheme({ enable_preview = true })
158-
end,
159-
},
160-
{
161-
name = "Notify",
162-
tele_func = function()
163-
extensions.notify.notify()
164-
end,
165-
},
166-
{
167-
name = "Undo History",
168-
tele_func = function()
169-
extensions.undo.undo()
170-
end,
171-
},
172-
},
97+
{ "Zoxide", extensions.zoxide.list },
98+
},
99+
misc = {
100+
{
101+
"Colorschemes",
102+
function()
103+
builtins.colorscheme({ enable_preview = true })
104+
end,
173105
},
106+
{ "Notify", extensions.notify.notify },
107+
{ "Undo History", extensions.undo.undo },
174108
},
109+
}
110+
111+
-- Build collections
112+
local collections = {}
113+
for kind, list in pairs(pickers) do
114+
local init = { initial_tab = 1, tabs = {} }
115+
for _, entry in ipairs(list) do
116+
table.insert(init.tabs, { name = entry[1], tele_func = entry[2] })
117+
end
118+
collections[kind] = init
119+
end
120+
121+
require("modules.utils").load_plugin("search", {
122+
prompt_position = prompt_pos,
123+
collections = collections,
175124
})
176125
end

lua/modules/plugins/tool.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ if settings.use_chat then
7575
},
7676
}
7777
end
78-
if settings.search_backend == "fzf" then
79-
--- requires the fzf binary to be installed
80-
tool["ibhagwan/fzf-lua"] = {
81-
lazy = true,
82-
event = "VeryLazy",
83-
config = require("tool.fzf-lua"),
84-
}
85-
end
78+
-- Needs `fzf` installed and in $PATH
79+
tool["ibhagwan/fzf-lua"] = {
80+
lazy = true,
81+
cond = (settings.search_backend == "fzf"),
82+
cmd = "FzfLua",
83+
config = require("tool.fzf-lua"),
84+
dependencies = { "nvim-tree/nvim-web-devicons" },
85+
}
8686

8787
----------------------------------------------------------------------
8888
-- Telescope Plugins --

0 commit comments

Comments
 (0)