|
1 | 1 | return function() |
2 | 2 | local vim_path = require("core.global").vim_path |
3 | 3 | local search_backend = require("core.settings").search_backend |
4 | | - local builtin = require("telescope.builtin") |
| 4 | + local use_fzf = search_backend == "fzf" |
| 5 | + local fzf = require("fzf-lua") |
5 | 6 | local extensions = require("telescope").extensions |
| 7 | + local builtins = require("telescope.builtin") |
6 | 8 | 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") } } |
9 | 9 |
|
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 {} |
15 | 12 |
|
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" |
34 | 20 | end |
35 | 21 |
|
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 |
40 | 42 | end |
41 | | - return builtin.oldfiles() |
42 | 43 | end |
43 | 44 |
|
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) |
45 | 50 | 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) |
48 | 53 | 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 |
51 | 56 | else |
52 | 57 | opts = { additional_args = { "--no-ignore" } } |
53 | 58 | end |
54 | 59 | 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) |
59 | 61 | end |
60 | 62 | end |
61 | 63 |
|
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, |
95 | 73 | }, |
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, |
149 | 96 | }, |
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, |
173 | 105 | }, |
| 106 | + { "Notify", extensions.notify.notify }, |
| 107 | + { "Undo History", extensions.undo.undo }, |
174 | 108 | }, |
| 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, |
175 | 124 | }) |
176 | 125 | end |
0 commit comments