-- finder.lua — Minimal, hardened async fuzzy finder + grep for Neovim ≥ 0.11 local M = {} --------------------------------------------------------------------- -- Config --------------------------------------------------------------------- M.config = { file_cmd = nil, -- "fd" | "fdfind" | nil (auto-detect) grep_cmd = 'rg', -- ripgrep binary page_size = 60, -- soft cap; real viewport height is measured debounce_ms = 80, instant_items = 1500, -- <= this count + cached => 0 ms debounce fast_items = 4000, -- <= this count => debounce_ms/2 cache_ttl_sec = 20, max_items = 5000, -- safety cap for massive outputs debug = false, follow_symlinks = true, -- pass -L to fd -- Exclusion patterns (used by fd/rg in addition to their gitignore handling) exclude_patterns = { 'node_modules', 'dist', 'build', '.git' }, -- Optional on-disk filelist cache at $ROOT/.devflow/finder_cache.json use_disk_cache = false, } --------------------------------------------------------------------- -- State --------------------------------------------------------------------- local S = { active = false, mode = nil, -- "files" | "grep" root = nil, cache = {}, timer = nil, ns = vim.api.nvim_create_namespace('finder_ns'), aug = nil, gen = 0, -- session generation id -- async jobs job_files = nil, job_rg = nil, -- UI win_inp = nil, buf_inp = nil, win_res = nil, buf_res = nil, -- Data query = '', items = {}, -- full set (files or grep lines) 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) user_moved = false, -- has user navigated during this session? -- meta files_cached = false, } --------------------------------------------------------------------- -- Utils --------------------------------------------------------------------- local function L(msg, data) if not M.config.debug then return end local s = '[finder] ' .. msg if data ~= nil then s = s .. ' ' .. vim.inspect(data) end vim.schedule(function() pcall(vim.notify, s) end) end local function now_sec() return os.time() end local function clamp(v, lo, hi) return (v < lo) and lo or ((v > hi) and hi or v) end local function cmd_exists(bin) return vim.fn.executable(bin) == 1 end -- single reusable timer to avoid churn local function debounce(fn, ms) if not S.timer then S.timer = vim.loop.new_timer() end S.timer:stop() S.timer:start(ms, 0, function() S.timer:stop() vim.schedule(fn) end) end local function ensure_cache_dir(root) local dir = root .. '/.devflow' local ok = vim.loop.fs_stat(dir) if not ok then pcall(vim.loop.fs_mkdir, dir, 448) -- 0700 end return dir end local function load_cache(root) if not M.config.use_disk_cache then return nil end local file = ensure_cache_dir(root) .. '/finder_cache.json' local f = io.open(file, 'r') if not f then return nil end local content = f:read('*a') f:close() local ok, data = pcall(vim.json.decode, content) if not ok or type(data) ~= 'table' or type(data.files) ~= 'table' then return nil end if now_sec() - (data.timestamp or 0) > M.config.cache_ttl_sec then pcall(os.remove, file) return nil end return data.files end local function save_cache(root, files) if not M.config.use_disk_cache then return end local file = ensure_cache_dir(root) .. '/finder_cache.json' local payload = { timestamp = now_sec(), files = files } local ok, json = pcall(vim.json.encode, payload) if not ok then return end local f = io.open(file, 'w') if not f then return end f:write(json) f:close() 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.fn.getcwd(0, 0) end local function normalize_rel(root, p) if not p or p == '' then return '' end local abs = vim.fs.normalize(vim.fn.fnamemodify(p, ':p')) local root_norm = vim.fs.normalize(root) if abs == root_norm then return '' end if abs:find(root_norm, 1, true) == 1 then local ch = abs:sub(#root_norm + 1, #root_norm + 1) if ch == '/' or ch == '\\' then return abs:sub(#root_norm + 2) end end return (p:gsub('^%./', '')) end local function to_abs_path(root, rel) if not rel or rel == '' then return root end return vim.fs.normalize(root .. '/' .. rel) end local function to_display_path(abs) return vim.fn.fnamemodify(abs, ':.') end local function page_rows() if S.win_res and vim.api.nvim_win_is_valid(S.win_res) then return math.max(1, vim.api.nvim_win_get_height(S.win_res)) end return math.max(1, math.min(M.config.page_size, vim.o.lines)) end local function effective_debounce_ms() if S.mode == 'files' then local n = #S.items if S.files_cached and n > 0 and n <= (M.config.instant_items or 1500) then return 0 elseif n > 0 and n <= (M.config.fast_items or 4000) then return math.max(0, math.floor((M.config.debounce_ms or 80) / 2)) end end return M.config.debounce_ms or 80 end --------------------------------------------------------------------- -- Render --------------------------------------------------------------------- local function ensure_visible() local page = page_rows() local sel = clamp(S.select, 1, math.max(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 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 view[#view + 1] = tostring(S.filtered[i] or '') end end vim.bo[S.buf_res].modifiable = true vim.api.nvim_buf_set_lines(S.buf_res, 0, -1, false, view) vim.api.nvim_buf_clear_namespace(S.buf_res, S.ns, 0, -1) -- match highlights vim.api.nvim_set_hl(0, 'FinderMatch', { link = 'Search', default = true }) for i = 1, #view do local idx = S.scroll + i local spans = S.positions[idx] if spans then for _, se in ipairs(spans) do local scol, ecol = se[1], se[2] if ecol > scol then pcall(vim.api.nvim_buf_set_extmark, S.buf_res, S.ns, i - 1, scol, { end_col = ecol, hl_group = 'FinderMatch', }) end end end end -- selection highlight if total > 0 then vim.api.nvim_set_hl(0, 'FinderSelection', { link = 'CursorLine', default = true }) local rel = clamp(S.select - S.scroll, 1, #view) pcall(vim.api.nvim_buf_set_extmark, S.buf_res, S.ns, rel - 1, 0, { end_line = rel, hl_group = 'FinderSelection', hl_eol = true, }) end vim.bo[S.buf_res].modifiable = false L('render', { select = S.select, scroll = S.scroll, total = total }) end --------------------------------------------------------------------- -- Filtering --------------------------------------------------------------------- local function compute_positions_files(items, q) 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 {} local filtered, positions = {}, {} for i, v in ipairs(out_items) do filtered[i] = tostring(v or '') local cols = pos[i] or {} table.sort(cols) local spans = {} local start_col, last_col = nil, nil for _, c in ipairs(cols) do if not start_col then start_col, last_col = c, c elseif c == last_col + 1 then last_col = c else table.insert(spans, { start_col, last_col + 1 }) start_col, last_col = c, c end end if start_col then table.insert(spans, { start_col, last_col + 1 }) end positions[i] = (#spans > 0) and spans or nil end return filtered, positions end local function compute_positions_grep(lines, q) if q == '' then return lines, {} end -- Match rg's --smart-case: case-insensitive unless query has uppercase local case_insensitive = not q:match('[A-Z]') local qmatch = case_insensitive and q:lower() or q local positions = {} for i, line in ipairs(lines) do local lmatch = case_insensitive and tostring(line or ''):lower() or tostring(line or '') local spans = {} local sidx = 1 while true do local s, e = lmatch:find(qmatch, sidx, true) if not s then break end table.insert(spans, { s - 1, e }) 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 / set_query --------------------------------------------------------------------- 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 for i, v in ipairs(list) do list[i] = tostring(v or '') end table.sort(list) S.items = list S.filtered = list S.positions = {} S.select = 1 S.scroll = 0 render() end 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 if not S.user_moved then S.select = 1 else local idx = nil 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 = idx or 1 end ensure_visible() render() L('set_query', { query = q, filtered = #S.filtered }) end --------------------------------------------------------------------- -- Navigation --------------------------------------------------------------------- local function move_down() if #S.filtered == 0 then return end S.user_moved = true 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.user_moved = true S.select = clamp(S.select - 1, 1, #S.filtered) ensure_visible() render() end local function page_down() if #S.filtered == 0 then return end S.user_moved = true S.select = clamp(S.select + page_rows(), 1, #S.filtered) ensure_visible() render() end local function page_up() if #S.filtered == 0 then return end S.user_moved = true S.select = clamp(S.select - page_rows(), 1, #S.filtered) ensure_visible() render() end --------------------------------------------------------------------- -- Accept selection --------------------------------------------------------------------- local function parse_vimgrep(line) if type(line) ~= 'string' then return nil end local file, lnum, col = line:match('^(.-):(%d+):(%d+):') if not file then return nil end return file, tonumber(lnum) or 1, tonumber(col) or 1 end local function accept_selection() local pick = S.filtered[S.select] if type(pick) ~= 'string' or pick == '' or pick:match('^%s*%(') then return -- ignore "(no matches)" placeholder end if S.mode == 'grep' then local file, lnum, col = parse_vimgrep(pick) if not file then return end local abs = to_abs_path(S.root, file) local path = to_display_path(abs) M.close() vim.schedule(function() vim.cmd.edit(vim.fn.fnameescape(path)) pcall(vim.api.nvim_win_set_cursor, 0, { lnum, col - 1 }) end) else local abs = to_abs_path(S.root, pick) local path = to_display_path(abs) M.close() vim.schedule(function() vim.cmd.edit(vim.fn.fnameescape(path)) end) end end --------------------------------------------------------------------- -- Async backends --------------------------------------------------------------------- local function cancel_job(job) if not job then return end pcall(function() job:kill(15) end) end local function collect_files_async(cb) local root = S.root local gen = S.gen local disk = load_cache(root) if disk and type(disk) == 'table' then S.files_cached = true cb(disk) return end S.files_cached = false local file_cmd = M.config.file_cmd if not file_cmd then if cmd_exists('fd') then file_cmd = 'fd' elseif cmd_exists('fdfind') then file_cmd = 'fdfind' end end if file_cmd and cmd_exists(file_cmd) then local args = { file_cmd, '--type', 'f', '--hidden', '--color', 'never' } if file_cmd == 'fd' or file_cmd == 'fdfind' then table.insert(args, '--strip-cwd-prefix') if M.config.follow_symlinks then table.insert(args, '-L') end end for _, ex in ipairs(M.config.exclude_patterns or {}) do table.insert(args, '--exclude') table.insert(args, ex) end cancel_job(S.job_files) S.job_files = vim.system(args, { text = true, cwd = root }, function(obj) if not (S.active and gen == S.gen) then return end local list = {} if obj.stdout then for p in obj.stdout:gmatch('[^\n]+') do local rel = normalize_rel(root, p) if rel ~= '' then list[#list + 1] = rel end end 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 save_cache(root, list) vim.schedule(function() if S.active and gen == S.gen then cb(list) end end) end) return end -- Fallback: git ls-files if cmd_exists('git') then cancel_job(S.job_files) S.job_files = vim.system( { 'git', 'ls-files', '-co', '--exclude-standard' }, { text = true, cwd = root }, function(obj) if not (S.active and gen == S.gen) then return end local list = {} if obj.stdout then for p in obj.stdout:gmatch('[^\n]+') do local rel = normalize_rel(root, p) if rel ~= '' then list[#list + 1] = rel end end 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 save_cache(root, list) vim.schedule(function() if S.active and gen == S.gen then cb(list) end end) end ) return end L('no file lister available') cb({}) end local function grep_async(query, cb) if query == '' then cb({}) return end local gen = S.gen local rg = M.config.grep_cmd if not cmd_exists(rg) then L('grep_cmd not found', rg) cb({}) return end local root = S.root local args = { rg, '--vimgrep', '--hidden', '--smart-case', '--no-heading', '--no-config', '--color', 'never', '--path-separator', '/', } -- Add excludes before the pattern for _, p in ipairs(M.config.exclude_patterns or {}) do table.insert(args, '--glob') table.insert(args, '!' .. p) end table.insert(args, '--') table.insert(args, query) cancel_job(S.job_rg) S.job_rg = vim.system(args, { text = true, cwd = root }, function(obj) if not (S.active and gen == S.gen) then return end local list = {} if obj.stdout and #obj.stdout > 0 then for line in obj.stdout:gmatch('[^\n]+') do list[#list + 1] = line end 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() if S.active and gen == S.gen then cb(list) end 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 if b and vim.api.nvim_buf_is_valid(b) then vim.bo[b].buflisted = false vim.bo[b].bufhidden = 'wipe' vim.bo[b].swapfile = false end end vim.bo[S.buf_inp].buftype = 'prompt' vim.bo[S.buf_res].buftype = 'nofile' vim.bo[S.buf_res].modifiable = 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.4) 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 or '') S.win_res = vim.api.nvim_open_win(S.buf_res, false, { relative = 'editor', style = 'minimal', border = 'rounded', width = width, height = math.max(1, height - 2), col = col, row = row + 3, focusable = false, zindex = 199, }) 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 }) vim.api.nvim_create_autocmd('WinEnter', { group = S.aug, callback = function() if not S.active then 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, }) vim.api.nvim_create_autocmd({ 'BufHidden', 'BufLeave' }, { group = S.aug, buffer = S.buf_inp, callback = function() if S.active then M.close() end end, }) vim.api.nvim_create_autocmd('VimResized', { group = S.aug, callback = function() if S.active then render() end end, }) vim.cmd.startinsert() L('open_layout') end local function close_layout() for _, win in ipairs({ S.win_inp, S.win_res }) do if win and vim.api.nvim_win_is_valid(win) then pcall(vim.api.nvim_win_close, win, true) end end for _, buf in ipairs({ S.buf_inp, S.buf_res }) do if buf and vim.api.nvim_buf_is_valid(buf) then 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 end --------------------------------------------------------------------- -- Input handlers --------------------------------------------------------------------- local function attach_handlers() local opts = { buffer = S.buf_inp, nowait = true, silent = true, noremap = true } vim.keymap.set('i', '', move_down, opts) vim.keymap.set('i', '', move_up, opts) vim.keymap.set('i', '', move_down, opts) vim.keymap.set('i', '', move_up, opts) vim.keymap.set('i', '', page_down, opts) vim.keymap.set('i', '', page_up, opts) vim.keymap.set('i', '', accept_selection, opts) vim.keymap.set('i', '', M.close, opts) vim.keymap.set('i', '', M.close, opts) vim.api.nvim_create_autocmd({ 'TextChangedI', 'TextChangedP' }, { group = S.aug, buffer = S.buf_inp, callback = function() local delay = effective_debounce_ms() debounce(function() if not (S.active and S.buf_inp and vim.api.nvim_buf_is_valid(S.buf_inp)) then return end local raw = vim.fn.getline('.') or '' local prompt_len = S.mode == 'files' and 8 or 6 -- "Search: " or "Grep: " local q = raw:sub(prompt_len + 1) if S.mode == 'grep' then grep_async(q, function(list) if not S.active then return end S.items = list or {} set_query(q) end) else set_query(q) end end, delay) end, }) L('handlers attached') end --------------------------------------------------------------------- -- Public API --------------------------------------------------------------------- function M.files() if S.active then M.close() end S.active = true S.gen = S.gen + 1 S.user_moved = false S.mode = 'files' S.root = project_root() open_layout('Search: ') attach_handlers() collect_files_async(function(list) if not S.active then return end set_items(list) end) end function M.grep() if S.active then M.close() end S.active = true S.gen = S.gen + 1 S.user_moved = false 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 if S.timer then pcall(function() S.timer:stop() S.timer:close() end) S.timer = nil end cancel_job(S.job_rg) S.job_rg = nil cancel_job(S.job_files) S.job_files = nil close_layout() S.active = false S.mode, S.root = nil, nil S.items, S.filtered, S.positions = {}, {}, {} S.query, S.select, S.scroll = '', 1, 0 S.user_moved = false S.files_cached = false L('closed') end function M.clear_cache() local root = project_root() local file = root .. '/.devflow/finder_cache.json' local ok = pcall(os.remove, file) vim.notify(ok and 'Finder cache cleared' or 'No cache to clear') end function M.diagnose() local root = project_root() local lines = { '=== Finder Diagnostics ===' } local fd_cmd = M.config.file_cmd or (cmd_exists('fd') and 'fd') or (cmd_exists('fdfind') and 'fdfind') or nil table.insert(lines, 'fd command: ' .. tostring(fd_cmd)) table.insert( lines, 'rg command: ' .. tostring(M.config.grep_cmd) .. ' (exists: ' .. tostring(cmd_exists(M.config.grep_cmd)) .. ')' ) local cache_file = root .. '/.devflow/finder_cache.json' local cache_stat = vim.loop.fs_stat(cache_file) if cache_stat then table.insert(lines, 'Cache file: ' .. cache_file) table.insert(lines, 'Cache size: ' .. cache_stat.size .. ' bytes') local cached = load_cache(root) if cached then table.insert(lines, 'Cache valid: true (' .. #cached .. ' files)') else table.insert(lines, 'Cache valid: false (expired or corrupt)') end else table.insert(lines, 'Cache: none') end table.insert(lines, 'Exclude patterns: ' .. vim.inspect(M.config.exclude_patterns)) table.insert(lines, 'Follow symlinks: ' .. tostring(M.config.follow_symlinks)) vim.notify(table.concat(lines, '\n'), vim.log.levels.INFO) end function M.setup(opts) M.config = vim.tbl_deep_extend('force', M.config, opts or {}) local fd_cmd = M.config.file_cmd if fd_cmd and not cmd_exists(fd_cmd) then vim.notify('[finder] file_cmd "' .. fd_cmd .. '" not found', vim.log.levels.WARN) end if M.config.grep_cmd and not cmd_exists(M.config.grep_cmd) then vim.notify('[finder] grep_cmd "' .. M.config.grep_cmd .. '" not found', vim.log.levels.WARN) end L('setup complete', M.config) end return M