Skip to content

Commit a892063

Browse files
committed
refactor(tool): simplify fzf-lua usage and improve lazy loading
Signed-off-by: Jint-lzxy <[email protected]>
1 parent 51b0ef3 commit a892063

File tree

4 files changed

+22
-27
lines changed

4 files changed

+22
-27
lines changed

lua/core/settings.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ settings["background"] = "dark"
100100
settings["external_browser"] = "chrome-cli open"
101101

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

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
@@ -100,8 +100,7 @@ local mappings = {
100100

101101
-- Plugin: telescope
102102
["n|<C-p>"] = map_callback(function()
103-
local search_backend = require("core.settings").search_backend
104-
if search_backend == "fzf" then
103+
if require("core.settings").search_backend == "fzf" then
105104
local prompt_position = require("telescope.config").values.layout_config.horizontal.prompt_position
106105
require("fzf-lua").keymaps({
107106
fzf_opts = { ["--layout"] = prompt_position == "top" and "reverse" or "default" },
@@ -132,20 +131,17 @@ local mappings = {
132131
:with_silent()
133132
:with_desc("tool: Find patterns"),
134133
["v|<leader>fs"] = map_callback(function()
135-
local search_backend = require("core.settings").search_backend
136-
if search_backend == "fzf" then
137-
local default_opts = "--column --line-number --no-heading --color=always --smart-case"
138-
local opts = vim.fn.getcwd() == vim_path
139-
and default_opts .. " --no-ignore --hidden --glob '!.git/*'"
140-
or ""
141-
local text = require("fzf-lua.utils").get_visual_selection()
134+
local is_config = vim.uv.cwd() == vim_path
135+
if require("core.settings").search_backend == "fzf" then
142136
require("fzf-lua").grep_project({
143-
search = text,
144-
rg_opts = opts,
137+
search = require("fzf-lua.utils").get_visual_selection(),
138+
rg_opts = "--column --line-number --no-heading --color=always --smart-case"
139+
.. (is_config and " --no-ignore --hidden --glob '!.git/*'" or ""),
145140
})
146141
else
147-
local opts = vim.fn.getcwd() == vim_path and { additional_args = { "--no-ignore" } } or {}
148-
require("telescope-live-grep-args.shortcuts").grep_visual_selection(opts)
142+
require("telescope-live-grep-args.shortcuts").grep_visual_selection(
143+
is_config and { additional_args = { "--no-ignore" } } or {}
144+
)
149145
end
150146
end)
151147
:with_noremap()
@@ -174,8 +170,7 @@ local mappings = {
174170
:with_silent()
175171
:with_desc("tool: Resume last search"),
176172
["n|<leader>fR"] = map_callback(function()
177-
local search_backend = require("core.settings").search_backend
178-
if search_backend == "fzf" then
173+
if require("core.settings").search_backend == "fzf" then
179174
require("fzf-lua").resume()
180175
end
181176
end)

lua/modules/plugins/tool.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ if settings.use_chat then
8282
},
8383
}
8484
end
85-
if settings.search_backend == "fzf" then
86-
-- requires the fzf binary to be installed
87-
tool["ibhagwan/fzf-lua"] = {
88-
lazy = true,
89-
event = "VeryLazy",
90-
config = require("tool.fzf-lua"),
91-
}
92-
end
85+
-- Needs `fzf` installed and in $PATH
86+
tool["ibhagwan/fzf-lua"] = {
87+
lazy = true,
88+
cond = (settings.search_backend == "fzf"),
89+
cmd = "FzfLua",
90+
config = require("tool.fzf-lua"),
91+
dependencies = { "nvim-tree/nvim-web-devicons" },
92+
}
9393

9494
----------------------------------------------------------------------
9595
-- Telescope Plugins --

0 commit comments

Comments
 (0)