diff --git a/lua/modules/navigation.lua b/lua/modules/navigation.lua index 8d629eb..1116597 100644 --- a/lua/modules/navigation.lua +++ b/lua/modules/navigation.lua @@ -1,8 +1,7 @@ require('plugins.filetree') require('plugins.finder').setup({ - file_cmd = 'fdfind', - grep_cmd = 'rg', - debug = false, + exclude_patterns = { 'node_modules', 'dist', 'build', '.git', '.cache', '.turbo' }, + use_disk_cache = true, -- optional }) vim.keymap.set('n', 'f', function() diff --git a/lua/plugins/finder.lua b/lua/plugins/finder.lua index 8af2437..1500417 100644 --- a/lua/plugins/finder.lua +++ b/lua/plugins/finder.lua @@ -1,6 +1,4 @@ --- finder.lua — Minimal async fuzzy finder + grep for Neovim ≥ 0.11 --- Non-blocking, debounced, stable selection, correct scrolling, match highlights. --- Hardened for races, Windows paths, and cwd/root mismatches. +-- finder.lua — Minimal, hardened async fuzzy finder + grep for Neovim ≥ 0.11 local M = {} @@ -15,6 +13,12 @@ M.config = { cache_ttl_sec = 20, max_items = 5000, -- safety cap for massive outputs debug = false, + + -- Exclusion controls (added to tool flags and used in glob fallback) + exclude_patterns = { 'node_modules', 'dist', 'build', '.git' }, + + -- Optional on-disk filelist cache at $ROOT/.devflow/finder_cache.json + use_disk_cache = false, } --------------------------------------------------------------------- @@ -24,7 +28,6 @@ local S = { active = false, mode = nil, -- "files" | "grep" root = nil, - is_git = false, cache = {}, timer = nil, @@ -49,6 +52,7 @@ local S = { 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? } --------------------------------------------------------------------- @@ -63,7 +67,7 @@ local function L(msg, data) s = s .. ' ' .. vim.inspect(data) end vim.schedule(function() - vim.notify(s) + pcall(vim.notify, s) end) end @@ -94,61 +98,106 @@ local function debounce(fn, ms) end) end -local function is_windows() - local sys = vim.loop.os_uname().sysname - return sys == 'Windows_NT' -end - -local function is_abs_path(p) - if is_windows() then - -- C:\... or \\server\share... - return p:match('^%a:[/\\]') or p:match('^[/\\][/\\]') - else - return p:sub(1, 1) == '/' +local function read_gitignore(root) + local p = root .. '/.gitignore' + local lines = {} + local f = io.open(p, 'r') + if not f then + return {} end -end - -local function joinpath(a, b) - return vim.fs.normalize(vim.fs.joinpath(a, b)) -end - -local function to_abs_in_root(root, p) - if is_abs_path(p) then - return p + for line in f:lines() do + local l = vim.trim(line) + if #l > 0 and not l:match('^#') and not l:match('^!') then + -- normalize directory suffix to plain token for our excludes + l = l:gsub('/+$', '') + table.insert(lines, l) + end end - local joined = joinpath(root, p) - local rp = vim.loop.fs_realpath(joined) - return rp or joined + f:close() + return lines +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) + end -- 0700 + 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 + 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 - S.is_git = true return vim.trim(obj.stdout) end - S.is_git = false - -- Respect Neovim cwd, not process cwd return vim.fn.getcwd(0, 0) end -local function resolve_file_cmd() - if M.config.file_cmd then - return M.config.file_cmd +local function normalize_rel(root, p) + if not p or p == '' then + return '' end - if cmd_exists('fd') then - return 'fd' + -- strip leading ./ and leading root if any + p = p:gsub('^%./', '') + local root_norm = vim.fs.normalize(root) + local abs = vim.fs.normalize(p) + if abs:find(root_norm, 1, true) == 1 then + local rel = abs:sub(#root_norm + 2) + return rel ~= '' and rel or abs end - if cmd_exists('fdfind') then - return 'fdfind' + return p +end + +local function to_abs_path(root, rel) + if not rel or rel == '' then + return root end - return nil + return vim.fs.normalize(root .. '/' .. rel) end local function page_rows() if S.win_res and vim.api.nvim_win_is_valid(S.win_res) then - local h = vim.api.nvim_win_get_height(S.win_res) - return math.max(1, h) + 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 @@ -173,7 +222,7 @@ end -- Render --------------------------------------------------------------------- local function render() - if not (S.active and S.buf_res and vim.api.nvim_buf_is_valid(S.buf_res)) then + if not (S.buf_res and vim.api.nvim_buf_is_valid(S.buf_res)) then return end @@ -187,13 +236,8 @@ local function render() 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] = S.filtered[i] - end - end - - for i = 1, #view do - if type(view[i]) ~= 'string' then - view[i] = tostring(view[i] or '') + local line = S.filtered[i] + view[#view + 1] = type(line) == 'string' and line or tostring(line or '') end end @@ -239,6 +283,7 @@ end -- Querying / Filtering --------------------------------------------------------------------- local function compute_positions_files(items, q) + -- matchfuzzypos returns {items, positions} local ok, res = pcall(vim.fn.matchfuzzypos, items, q) if not ok or type(res) ~= 'table' then return {}, {} @@ -252,7 +297,7 @@ local function compute_positions_files(items, q) local cols = pos[i] or {} local spans = {} for _, c in ipairs(cols) do - local start0 = (c > 0) and (c - 1) or 0 + local start0 = (c > 0) and (c - 1) or 0 -- to 0-based spans[#spans + 1] = { start0, start0 + 1 } end positions[i] = spans @@ -264,16 +309,14 @@ local function compute_positions_grep(lines, q) if q == '' then return lines, {} end - local pat = vim.pesc(q) + local qlow = q:lower() local positions = {} for i, line in ipairs(lines) do local sidx = 1 local spans = {} - if type(line) ~= 'string' then - line = tostring(line or '') - end + local llow = (type(line) == 'string' and line or tostring(line or '')):lower() while true do - local s, e = string.find(line, pat, sidx, true) + local s, e = string.find(llow, qlow, sidx, true) if not s then break end @@ -300,6 +343,8 @@ local function set_items(list) end list = tmp end + + -- sanitize to strings for i, v in ipairs(list) do if type(v) ~= 'string' then list[i] = tostring(v or '') @@ -334,26 +379,34 @@ local function set_query(q) 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 + -- selection policy: + -- if user hasn't moved, always reset to first on query changes; + -- if user moved, preserve previous pick when present, else first. + 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 - S.select = clamp(idx, 1, #S.filtered) + ensure_visible() render() - L('set_query', { query = q, filtered = #S.filtered }) + L('set_query', { query = q, filtered = #S.filtered, user_moved = S.user_moved }) end --------------------------------------------------------------------- @@ -363,6 +416,7 @@ 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() @@ -372,27 +426,17 @@ 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 accept_selection_files() - local pick = S.filtered[S.select] - if not pick then - return - end - local file = to_abs_in_root(S.root, pick) - local edit_cb = function() - vim.cmd.edit(vim.fn.fnameescape(file)) - end - M.close() - vim.schedule(edit_cb) -end - local function parse_vimgrep(line) - -- Robust against Windows drive letters and extra colons in path. -- Greedy file capture up to last "::" + if type(line) ~= 'string' then + return nil + end local file, lnum = line:match('^(.*):(%d+):') if not file then return nil @@ -400,101 +444,98 @@ local function parse_vimgrep(line) return file, tonumber(lnum) or 1 end +local function accept_selection_files() + local pick = S.filtered[S.select] + if type(pick) ~= 'string' or pick == '' then + return + end + local abs = to_abs_path(S.root, pick) + M.close() + vim.schedule(function() + vim.cmd.edit(vim.fn.fnameescape(abs)) + end) +end + local function accept_selection_grep() local pick = S.filtered[S.select] - if not pick then + if type(pick) ~= 'string' or pick == '' then return end local file, lnum = parse_vimgrep(pick) if not file then return end - file = to_abs_in_root(S.root, file) - lnum = tonumber(lnum) or 1 - local edit_cb = function() - vim.cmd.edit(vim.fn.fnameescape(file)) - if vim.api.nvim_get_current_buf() > 0 then - pcall(vim.api.nvim_win_set_cursor, 0, { lnum, 0 }) - end - end + local abs = to_abs_path(S.root, file) + local ln = tonumber(lnum) or 1 M.close() - vim.schedule(edit_cb) + vim.schedule(function() + vim.cmd.edit(vim.fn.fnameescape(abs)) + if pcall(vim.api.nvim_win_set_cursor, 0, { ln, 0 }) then + end + end) end --------------------------------------------------------------------- -- Backends --------------------------------------------------------------------- -local function cancel_job(j) - if not j then +local function cancel_job(job) + if not job then return end pcall(function() - j:kill(15) + job:kill(15) end) -- SIGTERM if available end local function collect_files_async(cb) - local gen = S.gen 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) + local gen = S.gen + + local disk = load_cache(root) + if disk and type(disk) == 'table' then + cb(disk) return end - -- Prefer fd/fdfind, then git ls-files, then blocking glob fallback. - local file_cmd = resolve_file_cmd() + 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 + + local excludes = {} + for _, p in ipairs(M.config.exclude_patterns or {}) do + table.insert(excludes, p) + end + local gi = read_gitignore(root) + for _, p in ipairs(gi) do + table.insert(excludes, p) + end + if file_cmd then - local args = - { file_cmd, '--type', 'f', '--hidden', '--follow', '--color', 'never', '--exclude', '.git' } + local args = { file_cmd, '--type', 'f', '--hidden', '--color', 'never' } + -- best-effort: avoid leading ./ in output if file_cmd == 'fd' or file_cmd == 'fdfind' then table.insert(args, '--strip-cwd-prefix') end + for _, ex in ipairs(excludes) 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 + local list = {} if obj.code == 0 and obj.stdout then local raw = vim.split(obj.stdout, '\n', { trimempty = true }) - list = {} - for i = 1, #raw do - list[i] = to_abs_in_root(root, raw[i]) + for _, p in ipairs(raw) do + list[#list + 1] = normalize_rel(root, p) end - else - list = {} - end - if #list == 0 and S.is_git and cmd_exists('git') then - -- fallback to git ls-files, still async - local gargs = { 'git', 'ls-files', '-co', '--exclude-standard', '-z' } - cancel_job(S.job_files) - S.job_files = vim.system(gargs, { text = true, cwd = root }, function(o2) - if not (S.active and gen == S.gen) then - return - end - local glist = {} - if o2.code == 0 and o2.stdout then - for p in o2.stdout:gmatch('([^%z]+)') do - glist[#glist + 1] = to_abs_in_root(root, p) - end - end - if #glist > M.config.max_items then - local tmp = {} - for i = 1, M.config.max_items do - tmp[i] = glist[i] - end - glist = tmp - end - S.cache[root] = S.cache[root] or {} - S.cache[root].files = { list = glist, at = now_sec() } - vim.schedule(function() - if S.active and gen == S.gen then - cb(glist) - end - end) - end) - return end if #list > M.config.max_items then local tmp = {} @@ -503,8 +544,7 @@ local function collect_files_async(cb) end list = tmp end - S.cache[root] = S.cache[root] or {} - S.cache[root].files = { list = list, at = now_sec() } + save_cache(root, list) vim.schedule(function() if S.active and gen == S.gen then cb(list) @@ -514,37 +554,82 @@ local function collect_files_async(cb) return end - -- Last-resort blocking fallback (no fd, not git, or git failed) + -- Try git ls-files as async fallback + if cmd_exists('git') then + cancel_job(S.job_files) + S.job_files = vim.system( + { 'git', 'ls-files', '-co', '--exclude-standard', '-z' }, + { text = true, cwd = root }, + function(o2) + if not (S.active and gen == S.gen) then + return + end + local list = {} + if o2.code == 0 and o2.stdout then + for p in o2.stdout:gmatch('([^%z]+)') do + list[#list + 1] = normalize_rel(root, p) + 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 + + -- Last resort: blocking glob with simple exclusion checks local list = vim.fn.globpath(root, '**/*', false, true) list = vim.tbl_filter(function(p) - return vim.fn.isdirectory(p) == 0 + if vim.fn.isdirectory(p) == 1 then + return false + end + for _, ex in ipairs(excludes) do + if p:find('/' .. ex .. '/', 1, true) or p:find('/' .. ex .. '$', 1, true) then + return false + end + end + return true end, list) - if #list > M.config.max_items then + + local rel = {} + for i = 1, #list do + rel[i] = normalize_rel(root, list[i]) + end + if #rel > M.config.max_items then local tmp = {} for i = 1, M.config.max_items do - tmp[i] = list[i] + tmp[i] = rel[i] end - list = tmp + rel = tmp end - S.cache[root] = S.cache[root] or {} - S.cache[root].files = { list = list, at = now_sec() } - cb(list) + save_cache(root, rel) + cb(rel) end local function grep_async(query, cb) - local gen = S.gen if query == '' then cb({}) return end + local gen = S.gen 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 root = S.root local args = { rg, '--vimgrep', @@ -559,8 +644,22 @@ local function grep_async(query, cb) '--', query, } + -- Apply excludes as negative globs + local excludes = {} + for _, p in ipairs(M.config.exclude_patterns or {}) do + table.insert(excludes, p) + end + local gi = read_gitignore(root) + for _, p in ipairs(gi) do + table.insert(excludes, p) + end + for _, ex in ipairs(excludes) do + table.insert(args, 2, '--glob') -- insert before '--' + table.insert(args, 3, '!' .. ex) + end + cancel_job(S.job_rg) - S.job_rg = vim.system(args, { text = true, cwd = S.root }, function(obj) + S.job_rg = vim.system(args, { text = true, cwd = root }, function(obj) if not (S.active and gen == S.gen) then return end @@ -591,14 +690,20 @@ local function open_layout(prompt) 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 + 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 + if S.buf_inp and vim.api.nvim_buf_is_valid(S.buf_inp) then + vim.bo[S.buf_inp].buftype = 'prompt' + end + if S.buf_res and vim.api.nvim_buf_is_valid(S.buf_res) then + vim.bo[S.buf_res].buftype = 'nofile' + vim.bo[S.buf_res].modifiable = false + vim.bo[S.buf_res].readonly = 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) @@ -616,7 +721,9 @@ local function open_layout(prompt) focusable = true, zindex = 200, }) - vim.fn.prompt_setprompt(S.buf_inp, prompt) + if S.buf_inp and vim.api.nvim_buf_is_valid(S.buf_inp) then + vim.fn.prompt_setprompt(S.buf_inp, prompt or '') + end S.win_res = vim.api.nvim_open_win(S.buf_res, false, { relative = 'editor', @@ -629,14 +736,17 @@ local function open_layout(prompt) focusable = false, zindex = 199, }) - vim.wo[S.win_res].cursorline = false - vim.wo[S.win_res].cursorlineopt = 'line' + if S.win_res and vim.api.nvim_win_is_valid(S.win_res) then + vim.wo[S.win_res].cursorline = false + vim.wo[S.win_res].cursorlineopt = 'line' + end 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() @@ -654,16 +764,20 @@ local function open_layout(prompt) 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, - }) + -- Close if prompt buffer hides or leaves + if S.buf_inp and vim.api.nvim_buf_is_valid(S.buf_inp) then + vim.api.nvim_create_autocmd({ 'BufHidden', 'BufLeave' }, { + group = S.aug, + buffer = S.buf_inp, + callback = function() + if S.active then + M.close() + end + end, + }) + end + -- Re-render on resize to respect new viewport height vim.api.nvim_create_autocmd('VimResized', { group = S.aug, callback = function() @@ -718,32 +832,34 @@ local function attach_handlers() M.close() end, opts) - vim.api.nvim_create_autocmd({ 'TextChangedI', 'TextChangedP' }, { - group = S.aug, - buffer = S.buf_inp, - callback = function() - debounce(function() - if not (S.active and vim.api.nvim_buf_is_valid(S.buf_inp)) then - return - end - -- Prompt buffers usually return only the user input, but strip prompt defensively. - 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) - if not S.active then - return - end - S.items = list + if S.buf_inp and vim.api.nvim_buf_is_valid(S.buf_inp) then + vim.api.nvim_create_autocmd({ 'TextChangedI', 'TextChangedP' }, { + group = S.aug, + buffer = S.buf_inp, + callback = function() + 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('.') + raw = type(raw) == 'string' and raw or '' + local prompt_pat = (S.mode == 'files') and '^Search:%s*' or '^Grep:%s*' + local q = raw:gsub(prompt_pat, '') + 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) - else - set_query(q) - end - end, M.config.debounce_ms) - end, - }) + end + end, M.config.debounce_ms) + end, + }) + end L('keymaps attached', opts) end @@ -757,6 +873,7 @@ function M.files() end S.active = true S.gen = S.gen + 1 + S.user_moved = false S.mode = 'files' S.root = project_root() open_layout('Search: ') @@ -765,7 +882,7 @@ function M.files() if not S.active then return end - set_items(list) + set_items(list) -- render initial list (relative paths) end) end @@ -775,6 +892,7 @@ function M.grep() end S.active = true S.gen = S.gen + 1 + S.user_moved = false S.mode = 'grep' S.root = project_root() open_layout('Grep: ') @@ -803,16 +921,22 @@ function M.close() S.mode, S.root = nil, nil S.items, S.filtered, S.positions = {}, {}, {} S.query, S.select, S.scroll = '', 1, 0 + S.user_moved = false 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) + pcall( + 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( + pcall( + vim.notify, ("finder: grep_cmd '%s' not found, will try 'rg'"):format(M.config.grep_cmd), vim.log.levels.WARN )