This commit is contained in:
Tomas Mirchev 2025-10-26 07:52:32 +02:00
parent be67d7a67a
commit 73e3a4c2b8
2 changed files with 660 additions and 279 deletions

View File

@ -1,2 +1,14 @@
require('plugins.finder')
require('plugins.filetree') require('plugins.filetree')
require('plugins.finder').setup({
file_cmd = 'fdfind',
grep_cmd = 'rg',
debug = false,
})
vim.keymap.set('n', '<leader>f', function()
require('plugins.finder').files()
end)
vim.keymap.set('n', '<leader>g', function()
require('plugins.finder').grep()
end)

View File

@ -1,323 +1,692 @@
-- Minimal fuzzy finder + content search for Neovim 0.11+ -- finder.lua — Minimal async fuzzy finder + grep for Neovim ≥ 0.11
-- Optional: `fdfind` or `fd` for file listing, and `rg` (ripgrep) for text search. -- Non-blocking, debounced, stable selection, correct scrolling, match highlights.
local Fuzzy = {} local M = {}
-------------------------------------------------------------------- ---------------------------------------------------------------------
-- 🧩 Helpers -- Config
-------------------------------------------------------------------- ---------------------------------------------------------------------
M.config = {
file_cmd = nil, -- "fd" | "fdfind" | nil (auto)
grep_cmd = 'rg', -- ripgrep binary
page_size = 60, -- soft cap; real viewport height is measured
debounce_ms = 80,
cache_ttl_sec = 20,
max_items = 5000, -- safety cap for massive outputs
debug = false,
}
-- Collect all files (try fdfind/fd first, then globpath) ---------------------------------------------------------------------
local function get_file_list() -- State
local handle = io.popen('fdfind --type f 2>/dev/null || fd --type f 2>/dev/null') ---------------------------------------------------------------------
if handle then local S = {
local result = handle:read('*a') active = false,
handle:close() mode = nil, -- "files" | "grep"
if result and result ~= '' then root = nil,
return vim.split(result, '\n', { trimempty = true })
end
end
return vim.fn.globpath('.', '**/*', false, true)
end
-- Create floating input + result windows cache = {},
local function open_float(prompt) timer = nil,
local input_buf = vim.api.nvim_create_buf(false, true) ns = vim.api.nvim_create_namespace('finder_ns'),
local result_buf = vim.api.nvim_create_buf(false, true) aug = nil,
-- mark both buffers as scratch/unlisted -- UI
for _, b in ipairs({ input_buf, result_buf }) do win_inp = nil,
vim.bo[b].bufhidden = 'wipe' buf_inp = nil,
vim.bo[b].buflisted = false win_res = nil,
vim.bo[b].swapfile = false buf_res = nil,
end
vim.bo[input_buf].buftype = 'prompt'
vim.bo[result_buf].buftype = 'nofile'
local width = math.floor(vim.o.columns * 0.7) -- Data
local height = 20 query = '',
local row = math.floor((vim.o.lines - height) / 2) items = {}, -- full set (files or grep lines)
local col = math.floor((vim.o.columns - width) / 2) filtered = {}, -- current view
positions = {}, -- positions[i] = { {scol, ecol}, ... } for filtered[i]
select = 1, -- absolute index in filtered (1-based)
scroll = 0, -- top index (0-based)
}
local input_win = vim.api.nvim_open_win(input_buf, true, { ---------------------------------------------------------------------
relative = 'editor', -- Utils
row = row, ---------------------------------------------------------------------
col = col, local function L(msg, data)
width = width, if not M.config.debug then
height = 1,
style = 'minimal',
border = 'rounded',
})
vim.fn.prompt_setprompt(input_buf, prompt)
local result_win = vim.api.nvim_open_win(result_buf, false, {
relative = 'editor',
row = row + 2,
col = col,
width = width,
height = height - 2,
style = 'minimal',
border = 'single',
})
return input_buf, result_buf, input_win, result_win
end
--------------------------------------------------------------------
-- 🔵 Highlight current selection
--------------------------------------------------------------------
function Fuzzy:highlight_selection()
if not self.result_buf then
return return
end end
if not self.ns_id then local s = '[finder] ' .. msg
self.ns_id = vim.api.nvim_create_namespace('FuzzyHighlight') if data ~= nil then
end s = s .. ' ' .. vim.inspect(data)
vim.api.nvim_buf_clear_namespace(self.result_buf, self.ns_id, 0, -1)
if self.matches and self.matches[self.cursor] then
local rel_cursor = self.cursor - (self.scroll or 0)
if rel_cursor >= 1 and rel_cursor <= self.page_size then
vim.api.nvim_buf_set_extmark(self.result_buf, self.ns_id, rel_cursor - 1, 0, {
end_line = rel_cursor,
hl_group = 'Visual',
hl_eol = true,
})
end
end end
vim.schedule(function()
vim.notify(s)
end)
end end
-------------------------------------------------------------------- local function now_sec()
-- 🔴 Close all floating windows return vim.loop.now() / 1000
-------------------------------------------------------------------- end
function Fuzzy.close() local function clamp(v, lo, hi)
local wins = { Fuzzy.input_win, Fuzzy.result_win } return (v < lo) and lo or ((v > hi) and hi or v)
for _, win in ipairs(wins) do end
if win and vim.api.nvim_win_is_valid(win) then local function cmd_exists(bin)
vim.api.nvim_win_close(win, true) return vim.fn.executable(bin) == 1
end
end
Fuzzy.active = false
end end
-------------------------------------------------------------------- local function debounce(fn, ms)
-- 🟢 File finder if S.timer then
-------------------------------------------------------------------- S.timer:stop()
function Fuzzy.open() S.timer:close()
if Fuzzy.active then S.timer = nil
Fuzzy.close()
end end
Fuzzy.active = true S.timer = vim.loop.new_timer()
S.timer:start(ms, 0, function()
Fuzzy.files = get_file_list() if S.timer then
Fuzzy.matches = Fuzzy.files S.timer:stop()
Fuzzy.cursor = 1 S.timer:close()
Fuzzy.scroll = 0 S.timer = nil
Fuzzy.page_size = 50
Fuzzy.input_buf, Fuzzy.result_buf, Fuzzy.input_win, Fuzzy.result_win = open_float('Search: ')
local function render_results()
local total = #Fuzzy.matches
if total == 0 then
vim.api.nvim_buf_set_lines(Fuzzy.result_buf, 0, -1, false, { '-- no matches --' })
return
end end
local start_idx = Fuzzy.scroll + 1 vim.schedule(fn)
local end_idx = math.min(start_idx + Fuzzy.page_size - 1, total) end)
local display = {} end
local function project_root()
local obj = vim.system({ 'git', 'rev-parse', '--show-toplevel' }, { text = true }):wait()
if obj.code == 0 and obj.stdout and obj.stdout ~= '' then
return vim.trim(obj.stdout)
end
return vim.loop.cwd()
end
local function resolve_file_cmd()
if M.config.file_cmd then
return M.config.file_cmd
end
if cmd_exists('fd') then
return 'fd'
end
if cmd_exists('fdfind') then
return 'fdfind'
end
return nil
end
local function page_rows()
if S.win_res and vim.api.nvim_win_is_valid(S.win_res) then
return vim.api.nvim_win_get_height(S.win_res)
end
return math.min(M.config.page_size, vim.o.lines)
end
---------------------------------------------------------------------
-- Render helpers
---------------------------------------------------------------------
local function ensure_visible()
local page = page_rows()
local sel = clamp(S.select, 1, #S.filtered)
local top = S.scroll + 1
local bot = S.scroll + page
if sel < top then
S.scroll = sel - 1
elseif sel > bot then
S.scroll = sel - page
end
S.scroll = clamp(S.scroll, 0, math.max(#S.filtered - page, 0))
end
---------------------------------------------------------------------
-- Render
---------------------------------------------------------------------
local function render()
if not (S.buf_res and vim.api.nvim_buf_is_valid(S.buf_res)) then
return
end
ensure_visible()
local total = #S.filtered
local view = {}
if total == 0 then
view = { '-- no matches --' }
else
local start_idx = S.scroll + 1
local end_idx = math.min(start_idx + page_rows() - 1, total)
for i = start_idx, end_idx do for i = start_idx, end_idx do
display[#display + 1] = Fuzzy.matches[i] view[#view + 1] = S.filtered[i]
end end
vim.api.nvim_buf_set_lines(Fuzzy.result_buf, 0, -1, false, display)
Fuzzy:highlight_selection()
end end
local function update_results(text) -- sanitize all lines to strings to avoid E5108 errors
if text == '' then for i = 1, #view do
Fuzzy.matches = Fuzzy.files if type(view[i]) ~= 'string' then
else view[i] = tostring(view[i] or '')
Fuzzy.matches = vim.fn.matchfuzzy(Fuzzy.files, text)
end end
Fuzzy.cursor, Fuzzy.scroll = 1, 0
render_results()
end end
vim.api.nvim_create_autocmd({ 'TextChangedI', 'TextChangedP' }, { vim.bo[S.buf_res].modifiable = true
buffer = Fuzzy.input_buf, vim.bo[S.buf_res].readonly = false
callback = function() vim.api.nvim_buf_set_lines(S.buf_res, 0, -1, false, view)
local text = vim.fn.getline('.'):gsub('^Search:%s*', '') vim.api.nvim_buf_clear_namespace(S.buf_res, S.ns, 0, -1)
update_results(text)
end,
})
vim.keymap.set('i', '<C-n>', function() -- match highlights (visible window only)
if Fuzzy.cursor < #Fuzzy.matches then vim.api.nvim_set_hl(0, 'FinderMatch', { link = 'Search', default = true })
Fuzzy.cursor = Fuzzy.cursor + 1 for i, _ in ipairs(view) do
if Fuzzy.cursor > Fuzzy.scroll + Fuzzy.page_size then local idx = S.scroll + i
Fuzzy.scroll = Fuzzy.scroll + 1 local spans = S.positions[idx]
end if spans then
render_results() for _, se in ipairs(spans) do
end local scol, ecol = se[1], se[2]
end, { buffer = Fuzzy.input_buf }) if ecol > scol then
vim.api.nvim_buf_set_extmark(S.buf_res, S.ns, i - 1, scol, {
vim.keymap.set('i', '<C-p>', function() end_col = ecol,
if Fuzzy.cursor > 1 then hl_group = 'FinderMatch',
Fuzzy.cursor = Fuzzy.cursor - 1
if Fuzzy.cursor <= Fuzzy.scroll then
Fuzzy.scroll = math.max(Fuzzy.scroll - 1, 0)
end
render_results()
end
end, { buffer = Fuzzy.input_buf })
vim.keymap.set('i', '<CR>', function()
local choice = Fuzzy.matches[Fuzzy.cursor]
if choice then
Fuzzy.close()
vim.cmd.edit(vim.fn.fnameescape(choice))
end
end, { buffer = Fuzzy.input_buf })
vim.keymap.set('i', '<Esc>', Fuzzy.close, { buffer = Fuzzy.input_buf })
vim.keymap.set('i', '<C-c>', Fuzzy.close, { buffer = Fuzzy.input_buf })
vim.keymap.set('n', '<Esc>', Fuzzy.close, { buffer = Fuzzy.input_buf })
vim.keymap.set('n', 'q', Fuzzy.close, { buffer = Fuzzy.input_buf })
vim.cmd.startinsert()
end
--------------------------------------------------------------------
-- 🟣 Ripgrep-based content search (scrolling + match highlighting)
--------------------------------------------------------------------
function Fuzzy.open_grep()
if Fuzzy.active then
Fuzzy.close()
end
Fuzzy.active = true
Fuzzy.input_buf, Fuzzy.result_buf, Fuzzy.input_win, Fuzzy.result_win = open_float('Grep: ')
Fuzzy.matches, Fuzzy.cursor, Fuzzy.scroll = {}, 1, 0
Fuzzy.page_size = 50
Fuzzy.ns_id = vim.api.nvim_create_namespace('FuzzyHighlight')
local function render_results(query)
local total = #Fuzzy.matches
if total == 0 then
vim.api.nvim_buf_set_lines(Fuzzy.result_buf, 0, -1, false, { '-- no matches --' })
return
end
local start_idx = Fuzzy.scroll + 1
local end_idx = math.min(start_idx + Fuzzy.page_size - 1, total)
local display = {}
for i = start_idx, end_idx do
display[#display + 1] = Fuzzy.matches[i]
end
vim.api.nvim_buf_set_lines(Fuzzy.result_buf, 0, -1, false, display)
vim.api.nvim_buf_clear_namespace(Fuzzy.result_buf, Fuzzy.ns_id, 0, -1)
-- highlight selection
local rel_cursor = math.min(Fuzzy.cursor - Fuzzy.scroll, #display)
vim.api.nvim_buf_set_extmark(Fuzzy.result_buf, Fuzzy.ns_id, rel_cursor - 1, 0, {
end_line = rel_cursor,
hl_group = 'Visual',
hl_eol = true,
})
-- highlight query matches
if query and query ~= '' then
local pattern = vim.pesc(query)
for i, line in ipairs(display) do
for s, e in line:gmatch('()' .. pattern .. '()') do
vim.api.nvim_buf_set_extmark(Fuzzy.result_buf, Fuzzy.ns_id, i - 1, s - 1, {
end_col = e - 1,
hl_group = 'Search',
}) })
end end
end end
end end
end end
local function run_grep(query) -- selection highlight
if query == '' then if total > 0 and #view > 0 then
vim.api.nvim_buf_set_lines(Fuzzy.result_buf, 0, -1, false, { '-- type to search --' }) vim.api.nvim_set_hl(0, 'FinderSelection', { link = 'CursorLine', default = true })
return local rel = clamp(S.select - S.scroll, 1, #view)
end vim.api.nvim_buf_set_extmark(S.buf_res, S.ns, rel - 1, 0, {
local handle = io.popen('rg --vimgrep --hidden --smart-case ' .. vim.fn.shellescape(query)) end_line = rel,
if not handle then hl_group = 'FinderSelection',
return hl_eol = true,
end })
local result = handle:read('*a')
handle:close()
Fuzzy.matches = vim.split(result, '\n', { trimempty = true })
Fuzzy.cursor, Fuzzy.scroll = 1, 0
render_results(query)
end end
vim.api.nvim_create_autocmd({ 'TextChangedI', 'TextChangedP' }, { vim.bo[S.buf_res].modifiable = false
buffer = Fuzzy.input_buf, L('render', { select = S.select, scroll = S.scroll, total = total, lines = #view })
end
---------------------------------------------------------------------
-- Querying / Filtering
---------------------------------------------------------------------
local function compute_positions_files(items, q)
-- matchfuzzypos returns {items, positions}. Handle via single return table.
local ok, res = pcall(vim.fn.matchfuzzypos, items, q)
if not ok or type(res) ~= 'table' then
return {}, {}
end
local out_items = res[1] or {}
local pos = res[2] or {}
-- sanitize items to strings and build per-index spans
local filtered, positions = {}, {}
for i, v in ipairs(out_items) do
filtered[i] = (type(v) == 'string') and v or tostring(v or '')
local cols = pos[i] or {}
local spans = {}
for _, c in ipairs(cols) do
local start0 = (c > 0) and (c - 1) or 0 -- to 0-based
spans[#spans + 1] = { start0, start0 + 1 }
end
positions[i] = spans
end
return filtered, positions
end
local function compute_positions_grep(lines, q)
if q == '' then
return lines, {}
end
local pat = vim.pesc(q)
local positions = {}
for i, line in ipairs(lines) do
local sidx = 1
local spans = {}
if type(line) ~= 'string' then
line = tostring(line or '')
end
while true do
local s, e = string.find(line, pat, sidx, true)
if not s then
break
end
spans[#spans + 1] = { s - 1, e } -- 0-based start, exclusive end
sidx = e + 1
if #spans > 64 then
break
end
end
positions[i] = (#spans > 0) and spans or nil
end
return lines, positions
end
---------------------------------------------------------------------
-- set_items
---------------------------------------------------------------------
local function set_items(list)
list = list or {}
if #list > M.config.max_items then
local tmp = {}
for i = 1, M.config.max_items do
tmp[i] = list[i]
end
list = tmp
end
-- sanitize to plain strings
for i, v in ipairs(list) do
if type(v) ~= 'string' then
list[i] = tostring(v or '')
end
end
S.items = list
S.filtered = list
S.positions = {}
S.select = 1
S.scroll = 0
render()
end
---------------------------------------------------------------------
-- set_query
---------------------------------------------------------------------
local function set_query(q)
local prev_val = S.filtered[S.select]
S.query = q
if S.mode == 'grep' then
local filtered, pos = compute_positions_grep(S.items, q)
S.filtered, S.positions = filtered, pos
else
if q == '' then
S.filtered = S.items
S.positions = {}
else
local filtered, pos = compute_positions_files(S.items, q)
S.filtered, S.positions = filtered, pos
end
end
-- sanitize filtered list
for i, v in ipairs(S.filtered) do
if type(v) ~= 'string' then
S.filtered[i] = tostring(v or '')
end
end
-- preserve previous pick if still present
local idx = 1
if prev_val then
for i, v in ipairs(S.filtered) do
if v == prev_val then
idx = i
break
end
end
end
S.select = clamp(idx, 1, #S.filtered)
ensure_visible()
render()
L('set_query', { query = q, filtered = #S.filtered })
end
---------------------------------------------------------------------
-- Move / Accept
---------------------------------------------------------------------
local function move_down()
if #S.filtered == 0 then
return
end
S.select = clamp(S.select + 1, 1, #S.filtered)
ensure_visible()
render()
end
local function move_up()
if #S.filtered == 0 then
return
end
S.select = clamp(S.select - 1, 1, #S.filtered)
ensure_visible()
render()
end
local function accept_selection_files()
local pick = S.filtered[S.select]
if not pick then
return
end
local file = pick
local edit_cb = function()
vim.cmd.edit(vim.fn.fnameescape(file))
end
M.close()
vim.schedule(edit_cb)
end
local function accept_selection_grep()
local pick = S.filtered[S.select]
if not pick then
return
end
local file, lnum = pick:match('^([^:]+):(%d+):')
if not file then
return
end
lnum = tonumber(lnum) or 1
local edit_cb = function()
vim.cmd.edit(vim.fn.fnameescape(file))
vim.api.nvim_win_set_cursor(0, { lnum, 0 })
end
M.close()
vim.schedule(edit_cb)
end
---------------------------------------------------------------------
-- Backends
---------------------------------------------------------------------
local function collect_files_async(cb)
local root = S.root
local c = S.cache[root]
if c and c.files and now_sec() - c.files.at < M.config.cache_ttl_sec then
cb(c.files.list)
return
end
local file_cmd = resolve_file_cmd()
if file_cmd then
local args = { file_cmd, '--type', 'f', '--hidden', '--follow', '--color', 'never' }
if file_cmd == 'fd' or file_cmd == 'fdfind' then
table.insert(args, '--strip-cwd-prefix')
end
vim.system(args, { text = true, cwd = root }, function(obj)
local list
if obj.code == 0 and obj.stdout then
list = vim.split(obj.stdout, '\n', { trimempty = true })
else
list = vim.fn.globpath(root, '**/*', false, true)
list = vim.tbl_filter(function(p)
return vim.fn.isdirectory(p) == 0
end, list)
end
if #list > M.config.max_items then
local tmp = {}
for i = 1, M.config.max_items do
tmp[i] = list[i]
end
list = tmp
end
S.cache[root] = S.cache[root] or {}
S.cache[root].files = { list = list, at = now_sec() }
vim.schedule(function()
cb(list)
end)
end)
else
local list = vim.fn.globpath(root, '**/*', false, true)
list = vim.tbl_filter(function(p)
return vim.fn.isdirectory(p) == 0
end, list)
if #list > M.config.max_items then
local tmp = {}
for i = 1, M.config.max_items do
tmp[i] = list[i]
end
list = tmp
end
S.cache[root] = S.cache[root] or {}
S.cache[root].files = { list = list, at = now_sec() }
cb(list)
end
end
local function grep_async(query, cb)
if query == '' then
cb({})
return
end
local rg = M.config.grep_cmd
if rg ~= 'rg' and not cmd_exists(rg) then
rg = 'rg'
end
if not cmd_exists(rg) then
cb({})
return
end
local args = { rg, '--vimgrep', '--hidden', '--smart-case', '--no-heading', '--', query }
vim.system(args, { text = true, cwd = S.root }, function(obj)
local list = {}
if obj.code == 0 and obj.stdout then
list = vim.split(obj.stdout, '\n', { trimempty = true })
end
if #list > M.config.max_items then
local tmp = {}
for i = 1, M.config.max_items do
tmp[i] = list[i]
end
list = tmp
end
vim.schedule(function()
cb(list)
end)
end)
end
---------------------------------------------------------------------
-- Layout
---------------------------------------------------------------------
local function open_layout(prompt)
S.buf_inp = vim.api.nvim_create_buf(false, true)
S.buf_res = vim.api.nvim_create_buf(false, true)
for _, b in ipairs({ S.buf_inp, S.buf_res }) do
vim.bo[b].buflisted = false
vim.bo[b].bufhidden = 'wipe'
vim.bo[b].swapfile = false
end
vim.bo[S.buf_inp].buftype = 'prompt'
vim.bo[S.buf_res].buftype = 'nofile'
vim.bo[S.buf_res].modifiable = false
vim.bo[S.buf_res].readonly = false
local width = math.floor(vim.o.columns * 0.8)
local height = math.min(math.floor(vim.o.lines * 0.5), M.config.page_size + 2)
local col = math.floor((vim.o.columns - width) / 2)
local row = math.floor((vim.o.lines - height) * 0.7)
S.win_inp = vim.api.nvim_open_win(S.buf_inp, true, {
relative = 'editor',
style = 'minimal',
border = 'rounded',
width = width,
height = 1,
col = col,
row = row,
focusable = true,
zindex = 200,
})
vim.fn.prompt_setprompt(S.buf_inp, prompt)
S.win_res = vim.api.nvim_open_win(S.buf_res, false, {
relative = 'editor',
style = 'minimal',
border = 'single',
width = width,
height = height - 2,
col = col,
row = row + 2,
focusable = false,
zindex = 199,
})
vim.wo[S.win_res].cursorline = false
vim.wo[S.win_res].cursorlineopt = 'line'
if S.aug then
pcall(vim.api.nvim_del_augroup_by_id, S.aug)
end
S.aug = vim.api.nvim_create_augroup('finder_session', { clear = true })
-- Close if focus enters a non-floating window that isn't ours
vim.api.nvim_create_autocmd('WinEnter', {
group = S.aug,
callback = function() callback = function()
local text = vim.fn.getline('.'):gsub('^Grep:%s*', '') if not S.active then
run_grep(text) return
end
local w = vim.api.nvim_get_current_win()
if w == S.win_inp or w == S.win_res then
return
end
local cfg = vim.api.nvim_win_get_config(w)
if cfg and cfg.relative == '' then
M.close()
end
end, end,
}) })
vim.keymap.set('i', '<C-n>', function() -- Close if prompt buffer hides or leaves
if Fuzzy.cursor < #Fuzzy.matches then vim.api.nvim_create_autocmd({ 'BufHidden', 'BufLeave' }, {
Fuzzy.cursor = Fuzzy.cursor + 1 group = S.aug,
if Fuzzy.cursor > Fuzzy.scroll + Fuzzy.page_size then buffer = S.buf_inp,
Fuzzy.scroll = Fuzzy.scroll + 1 callback = function()
if S.active then
M.close()
end end
local query = vim.fn.getline('.'):gsub('^Grep:%s*', '') end,
render_results(query) })
end
end, { buffer = Fuzzy.input_buf })
vim.keymap.set('i', '<C-p>', function() -- Re-render on resize to respect new viewport height
if Fuzzy.cursor > 1 then vim.api.nvim_create_autocmd('VimResized', {
Fuzzy.cursor = Fuzzy.cursor - 1 group = S.aug,
if Fuzzy.cursor <= Fuzzy.scroll then callback = function()
Fuzzy.scroll = math.max(Fuzzy.scroll - 1, 0) if S.active then
render()
end end
local query = vim.fn.getline('.'):gsub('^Grep:%s*', '') end,
render_results(query) })
end
end, { buffer = Fuzzy.input_buf })
vim.keymap.set('i', '<CR>', function()
local line = Fuzzy.matches[Fuzzy.cursor]
if line then
local parts = vim.split(line, ':')
local file, lnum = parts[1], tonumber(parts[2]) or 1
Fuzzy.close()
vim.cmd.edit(vim.fn.fnameescape(file))
vim.api.nvim_win_set_cursor(0, { lnum, 0 })
end
end, { buffer = Fuzzy.input_buf })
vim.keymap.set('i', '<Esc>', function()
Fuzzy.close()
end, { buffer = Fuzzy.input_buf })
vim.cmd.startinsert() vim.cmd.startinsert()
L('open_layout', { win_inp = S.win_inp, win_res = S.win_res })
end end
-------------------------------------------------------------------- local function close_layout()
-- 🧩 Commands & Keymaps for _, win in ipairs({ S.win_inp, S.win_res }) do
-------------------------------------------------------------------- if win and vim.api.nvim_win_is_valid(win) then
vim.api.nvim_create_user_command('FuzzyLive', function() pcall(vim.api.nvim_win_close, win, true)
Fuzzy.open() end
end, {}) end
vim.api.nvim_create_user_command('FuzzyGrep', function() for _, buf in ipairs({ S.buf_inp, S.buf_res }) do
Fuzzy.open_grep() if buf and vim.api.nvim_buf_is_valid(buf) then
end, {}) pcall(vim.api.nvim_buf_delete, buf, { force = true })
end
end
if S.aug then
pcall(vim.api.nvim_del_augroup_by_id, S.aug)
S.aug = nil
end
L('close_layout')
end
vim.keymap.set('n', '<leader>f', function() ---------------------------------------------------------------------
vim.cmd.FuzzyLive() -- Input handlers
end, { desc = 'Open fuzzy file finder' }) ---------------------------------------------------------------------
vim.keymap.set('n', '<leader>g', function() local function attach_handlers()
vim.cmd.FuzzyGrep() local opts = { buffer = S.buf_inp, nowait = true, silent = true, noremap = true }
end, { desc = 'Search file contents with ripgrep' }) vim.keymap.set('i', '<C-n>', move_down, opts)
vim.keymap.set('i', '<C-p>', move_up, opts)
vim.keymap.set('i', '<Down>', move_down, opts)
vim.keymap.set('i', '<Up>', move_up, opts)
vim.keymap.set('i', '<CR>', function()
if S.mode == 'grep' then
accept_selection_grep()
else
accept_selection_files()
end
end, opts)
vim.keymap.set('i', '<Esc>', function()
M.close()
end, opts)
vim.keymap.set('i', '<C-c>', function()
M.close()
end, opts)
return Fuzzy vim.api.nvim_create_autocmd({ 'TextChangedI', 'TextChangedP' }, {
group = S.aug,
buffer = S.buf_inp,
callback = function()
debounce(function()
local raw = vim.fn.getline('.')
local prompt = (S.mode == 'files') and '^Search:%s*' or '^Grep:%s*'
local q = raw:gsub(prompt, '')
if S.mode == 'grep' then
grep_async(q, function(list)
S.items = list
set_query(q)
end)
else
set_query(q)
end
end, M.config.debounce_ms)
end,
})
L('keymaps attached', opts)
end
---------------------------------------------------------------------
-- Public
---------------------------------------------------------------------
function M.files()
if S.active then
M.close()
end
S.active = true
S.mode = 'files'
S.root = project_root()
open_layout('Search: ')
attach_handlers()
collect_files_async(function(list)
set_items(list) -- use the helper
-- no need to call set_query('') here; set_items() renders initial list
end)
end
function M.grep()
if S.active then
M.close()
end
S.active = true
S.mode = 'grep'
S.root = project_root()
open_layout('Grep: ')
attach_handlers()
S.items = {}
set_query('')
end
function M.close()
if not S.active then
return
end
close_layout()
if S.timer then
S.timer:stop()
S.timer:close()
S.timer = nil
end
S.active = false
S.mode, S.root = nil, nil
S.items, S.filtered, S.positions = {}, {}, {}
S.query, S.select, S.scroll = '', 1, 0
L('session closed')
end
function M.setup(opts)
M.config = vim.tbl_deep_extend('force', M.config, opts or {})
if M.config.file_cmd and not cmd_exists(M.config.file_cmd) then
vim.notify(("finder: file_cmd '%s' not found"):format(M.config.file_cmd), vim.log.levels.WARN)
end
if M.config.grep_cmd and not cmd_exists(M.config.grep_cmd) then
vim.notify(
("finder: grep_cmd '%s' not found, will try 'rg'"):format(M.config.grep_cmd),
vim.log.levels.WARN
)
end
L('setup', M.config)
end
return M