Skip to content

Commit 7527674

Browse files
committed
refactor: introduce fzf-lua as an alternative of telescope
1 parent 5174b3f commit 7527674

File tree

7 files changed

+173
-37
lines changed

7 files changed

+173
-37
lines changed

lua/core/settings.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,10 @@ settings["dashboard_image"] = {
233233
[[⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⠿⠿⢿⠿⠷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀]],
234234
}
235235

236+
-- Set the search backend to use here.
237+
-- telescope is enough for most cases.
238+
-- fzf is more powerful for searching in huge repo but needs fzf binary installed.
239+
---@type "telescope"|"fzf"
240+
settings["search_backend"] = "telescope"
241+
236242
return require("modules.utils").extend_config(settings, "user.settings")

lua/keymap/completion.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,17 @@ function M.lsp(buf)
2525
:with_silent()
2626
:with_buffer(buf)
2727
:with_desc("lsp: Toggle outline"),
28-
["n|gto"] = map_cr("Telescope lsp_document_symbols")
28+
["n|gto"] = map_callback(function()
29+
local search_backend = require("core.settings").search_backend
30+
if search_backend == "fzf" then
31+
local prompt_position = require("telescope.config").values.layout_config.horizontal.prompt_position
32+
require("fzf-lua").lsp_document_symbols({
33+
fzf_opts = { ["--layout"] = prompt_position == "top" and "reverse" or "default" },
34+
})
35+
else
36+
require("telescope.builtin").lsp_document_symbols()
37+
end
38+
end)
2939
:with_silent()
3040
:with_buffer(buf)
3141
:with_desc("lsp: Toggle outline in Telescope"),

lua/keymap/tool.lua

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

136136
-- Plugin: telescope
137137
["n|<C-p>"] = map_callback(function()
138-
_command_panel()
138+
local search_backend = require("core.settings").search_backend
139+
if search_backend == "fzf" then
140+
local prompt_position = require("telescope.config").values.layout_config.horizontal.prompt_position
141+
require("fzf-lua").keymaps({
142+
fzf_opts = { ["--layout"] = prompt_position == "top" and "reverse" or "default" },
143+
})
144+
else
145+
_command_panel()
146+
end
139147
end)
140148
:with_noremap()
141149
:with_silent()
@@ -159,8 +167,21 @@ local mappings = {
159167
:with_silent()
160168
:with_desc("tool: Find patterns"),
161169
["v|<leader>fs"] = map_callback(function()
162-
local opts = vim.fn.getcwd() == vim_path and { additional_args = { "--no-ignore" } } or {}
163-
require("telescope-live-grep-args.shortcuts").grep_visual_selection(opts)
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()
177+
require("fzf-lua").grep_project({
178+
search = text,
179+
rg_opts = opts,
180+
})
181+
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)
184+
end
164185
end)
165186
:with_noremap()
166187
:with_silent()
@@ -187,6 +208,15 @@ local mappings = {
187208
:with_noremap()
188209
:with_silent()
189210
:with_desc("tool: Resume last search"),
211+
["n|<leader>fR"] = map_callback(function()
212+
local search_backend = require("core.settings").search_backend
213+
if search_backend == "fzf" then
214+
require("fzf-lua").resume()
215+
end
216+
end)
217+
:with_noremap()
218+
:with_silent()
219+
:with_desc("tool: Resume last search"),
190220

191221
-- Plugin: dap
192222
["n|<F6>"] = map_callback(function()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
return function()
2+
local icons = { ui = require("modules.utils.icons").get("ui", true) }
3+
4+
require("modules.utils").load_plugin("fzf-lua", {
5+
{ "telescope" },
6+
defaults = {
7+
prompt = icons.ui.Telescope .. " ",
8+
},
9+
})
10+
end

lua/modules/configs/tool/search.lua

Lines changed: 102 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,108 @@
11
return function()
2+
local search_backend = require("core.settings").search_backend
23
local builtin = require("telescope.builtin")
34
local extensions = require("telescope").extensions
45
local vim_path = require("core.global").vim_path
6+
local prompt_position = require("telescope.config").values.layout_config.horizontal.prompt_position
7+
local fzf_opts = { fzf_opts = { ["--layout"] = prompt_position == "top" and "reverse" or "default" } }
8+
local files = function()
9+
if search_backend == "fzf" then
10+
return function()
11+
local opts = {}
12+
local fzf = require("fzf-lua")
13+
opts = vim.tbl_deep_extend("force", opts, fzf_opts)
14+
if vim.fn.getcwd() == vim_path then
15+
fzf.files(vim.tbl_deep_extend("force", opts, { no_ignore = true }))
16+
elseif vim.fn.isdirectory(".git") == 1 then
17+
fzf.git_files(opts)
18+
else
19+
fzf.files(opts)
20+
end
21+
end
22+
else
23+
return function(opts)
24+
opts = opts or {}
25+
if vim.fn.getcwd() == vim_path then
26+
builtin.find_files(vim.tbl_deep_extend("force", opts, { no_ignore = true }))
27+
elseif vim.fn.isdirectory(".git") == 1 then
28+
builtin.git_files(opts)
29+
else
30+
builtin.find_files(opts)
31+
end
32+
end
33+
end
34+
end
35+
36+
local old_files = function()
37+
if search_backend == "fzf" then
38+
return function()
39+
local opts = {}
40+
local fzf = require("fzf-lua")
41+
opts = vim.tbl_deep_extend("force", opts, fzf_opts)
42+
fzf.oldfiles(opts)
43+
end
44+
else
45+
return function(opts)
46+
opts = opts or {}
47+
builtin.oldfiles(opts)
48+
end
49+
end
50+
end
51+
52+
local word_in_project = function()
53+
if search_backend == "fzf" then
54+
return function()
55+
local opts = {}
56+
local fzf = require("fzf-lua")
57+
opts = vim.tbl_deep_extend("force", opts, fzf_opts)
58+
if vim.fn.getcwd() == vim_path then
59+
opts = vim.tbl_deep_extend("force", opts, { no_ignore = true })
60+
end
61+
fzf.live_grep(opts)
62+
end
63+
else
64+
return function(opts)
65+
opts = opts or {}
66+
if vim.fn.getcwd() == vim_path then
67+
opts["additional_args"] = { "--no-ignore" }
68+
end
69+
extensions.live_grep_args.live_grep_args(opts)
70+
end
71+
end
72+
end
73+
74+
local word_under_cursor = function()
75+
if search_backend == "fzf" then
76+
return function()
77+
local opts = {}
78+
local fzf = require("fzf-lua")
79+
opts = vim.tbl_deep_extend("force", opts, fzf_opts)
80+
if vim.fn.getcwd() == vim_path then
81+
opts = vim.tbl_deep_extend("force", opts, { no_ignore = true })
82+
end
83+
fzf.grep_cword(opts)
84+
end
85+
else
86+
return function(opts)
87+
opts = opts or {}
88+
if vim.fn.getcwd() == vim_path then
89+
opts["additional_args"] = { "--no-ignore" }
90+
end
91+
builtin.grep_string(opts)
92+
end
93+
end
94+
end
595

696
require("modules.utils").load_plugin("search", {
97+
prompt_position = prompt_position,
798
collections = {
899
-- Search using filenames
9100
file = {
10101
initial_tab = 1,
11102
tabs = {
12103
{
13104
name = "Files",
14-
tele_func = function(opts)
15-
opts = opts or {}
16-
if vim.fn.getcwd() == vim_path then
17-
builtin.find_files(vim.tbl_deep_extend("force", opts, { no_ignore = true }))
18-
elseif vim.fn.isdirectory(".git") == 1 then
19-
builtin.git_files(opts)
20-
else
21-
builtin.find_files(opts)
22-
end
23-
end,
105+
tele_func = files(),
24106
},
25107
{
26108
name = "Frecency",
@@ -30,9 +112,7 @@ return function()
30112
},
31113
{
32114
name = "Oldfiles",
33-
tele_func = function()
34-
builtin.oldfiles()
35-
end,
115+
tele_func = old_files(),
36116
},
37117
{
38118
name = "Buffers",
@@ -48,27 +128,15 @@ return function()
48128
tabs = {
49129
{
50130
name = "Word in project",
51-
tele_func = function()
52-
local opts = {}
53-
if vim.fn.getcwd() == vim_path then
54-
opts["additional_args"] = { "--no-ignore" }
55-
end
56-
extensions.live_grep_args.live_grep_args(opts)
57-
end,
131+
tele_func = word_in_project(),
58132
},
59133
{
60134
name = "Word under cursor",
61-
tele_func = function(opts)
62-
opts = opts or {}
63-
if vim.fn.getcwd() == vim_path then
64-
opts["additional_args"] = { "--no-ignore" }
65-
end
66-
builtin.grep_string(opts)
67-
end,
135+
tele_func = word_under_cursor(),
68136
},
69137
},
70138
},
71-
-- Search by git (branches, commits)
139+
-- Search Git objects (branches, commits)
72140
git = {
73141
initial_tab = 1,
74142
tabs = {
@@ -114,6 +182,12 @@ return function()
114182
extensions.projects.projects({})
115183
end,
116184
},
185+
{
186+
name = "Zoxide",
187+
tele_func = function()
188+
extensions.zoxide.list()
189+
end,
190+
},
117191
},
118192
},
119193
-- Miscellaneous

lua/modules/configs/tool/telescope.lua

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,15 @@ return function()
1818
entry_prefix = " ",
1919
scroll_strategy = "limit",
2020
results_title = false,
21-
layout_strategy = "horizontal",
21+
layout_strategy = "flex",
2222
path_display = { "smart" }, -- absolute
2323
dynamic_preview_title = true,
2424
selection_strategy = "reset",
25-
sorting_strategy = "ascending",
2625
color_devicons = true,
2726
file_ignore_patterns = { ".git/", ".cache", "build/", "%.class", "%.pdf", "%.mkv", "%.mp4", "%.zip" },
2827
layout_config = {
2928
horizontal = {
30-
prompt_position = "top",
3129
preview_width = 0.55,
32-
results_width = 0.8,
3330
},
3431
vertical = {
3532
mirror = false,

lua/modules/plugins/tool.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local tool = {}
2+
local settings = require("core.settings")
23

34
tool["monaqa/dial.nvim"] = {
45
lazy = true,
@@ -62,6 +63,14 @@ tool["folke/which-key.nvim"] = {
6263
event = { "CursorHold", "CursorHoldI" },
6364
config = require("tool.which-key"),
6465
}
66+
if settings.search_backend == "fzf" then
67+
-- require fzf binary installed
68+
tool["ibhagwan/fzf-lua"] = {
69+
lazy = true,
70+
event = "VeryLazy",
71+
config = require("tool.fzf-lua"),
72+
}
73+
end
6574

6675
----------------------------------------------------------------------
6776
-- Telescope Plugins --
@@ -79,7 +88,7 @@ tool["nvim-telescope/telescope.nvim"] = {
7988
{ "nvim-telescope/telescope-live-grep-args.nvim" },
8089
{ "nvim-telescope/telescope-fzf-native.nvim", build = "make" },
8190
{
82-
"FabianWirth/search.nvim",
91+
"ayamir/search.nvim",
8392
config = require("tool.search"),
8493
},
8594
{

0 commit comments

Comments
 (0)