From 73e3a4c2b8c3dd531f7d70c178d008e8a92a0959 Mon Sep 17 00:00:00 2001 From: Tomas Mirchev Date: Sun, 26 Oct 2025 07:52:32 +0200 Subject: [PATCH 1/4] v1 --- lua/modules/navigation.lua | 14 +- lua/plugins/finder.lua | 925 ++++++++++++++++++++++++++----------- 2 files changed, 660 insertions(+), 279 deletions(-) diff --git a/lua/modules/navigation.lua b/lua/modules/navigation.lua index e291a6f..8d629eb 100644 --- a/lua/modules/navigation.lua +++ b/lua/modules/navigation.lua @@ -1,2 +1,14 @@ -require('plugins.finder') require('plugins.filetree') +require('plugins.finder').setup({ + file_cmd = 'fdfind', + grep_cmd = 'rg', + debug = false, +}) + +vim.keymap.set('n', 'f', function() + require('plugins.finder').files() +end) + +vim.keymap.set('n', 'g', function() + require('plugins.finder').grep() +end) diff --git a/lua/plugins/finder.lua b/lua/plugins/finder.lua index 1c12870..4a047bc 100644 --- a/lua/plugins/finder.lua +++ b/lua/plugins/finder.lua @@ -1,323 +1,692 @@ --- Minimal fuzzy finder + content search for Neovim 0.11+ --- Optional: `fdfind` or `fd` for file listing, and `rg` (ripgrep) for text search. +-- finder.lua — Minimal async fuzzy finder + grep for Neovim ≥ 0.11 +-- 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() - local handle = io.popen('fdfind --type f 2>/dev/null || fd --type f 2>/dev/null') - if handle then - local result = handle:read('*a') - handle:close() - if result and result ~= '' then - return vim.split(result, '\n', { trimempty = true }) - end - end - return vim.fn.globpath('.', '**/*', false, true) -end +--------------------------------------------------------------------- +-- State +--------------------------------------------------------------------- +local S = { + active = false, + mode = nil, -- "files" | "grep" + root = nil, --- Create floating input + result windows -local function open_float(prompt) - local input_buf = vim.api.nvim_create_buf(false, true) - local result_buf = vim.api.nvim_create_buf(false, true) + cache = {}, + timer = nil, + ns = vim.api.nvim_create_namespace('finder_ns'), + aug = nil, - -- mark both buffers as scratch/unlisted - for _, b in ipairs({ input_buf, result_buf }) do - vim.bo[b].bufhidden = 'wipe' - vim.bo[b].buflisted = false - vim.bo[b].swapfile = false - end - vim.bo[input_buf].buftype = 'prompt' - vim.bo[result_buf].buftype = 'nofile' + -- UI + win_inp = nil, + buf_inp = nil, + win_res = nil, + buf_res = nil, - local width = math.floor(vim.o.columns * 0.7) - local height = 20 - local row = math.floor((vim.o.lines - height) / 2) - local col = math.floor((vim.o.columns - width) / 2) + -- 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) +} - local input_win = vim.api.nvim_open_win(input_buf, true, { - relative = 'editor', - row = row, - col = col, - width = width, - 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 +--------------------------------------------------------------------- +-- Utils +--------------------------------------------------------------------- +local function L(msg, data) + if not M.config.debug then return end - if not self.ns_id then - self.ns_id = vim.api.nvim_create_namespace('FuzzyHighlight') - end - 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 + local s = '[finder] ' .. msg + if data ~= nil then + s = s .. ' ' .. vim.inspect(data) end + vim.schedule(function() + vim.notify(s) + end) end --------------------------------------------------------------------- --- 🔴 Close all floating windows --------------------------------------------------------------------- -function Fuzzy.close() - local wins = { Fuzzy.input_win, Fuzzy.result_win } - for _, win in ipairs(wins) do - if win and vim.api.nvim_win_is_valid(win) then - vim.api.nvim_win_close(win, true) - end - end - Fuzzy.active = false +local function now_sec() + return vim.loop.now() / 1000 +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 --------------------------------------------------------------------- --- 🟢 File finder --------------------------------------------------------------------- -function Fuzzy.open() - if Fuzzy.active then - Fuzzy.close() +local function debounce(fn, ms) + if S.timer then + S.timer:stop() + S.timer:close() + S.timer = nil end - Fuzzy.active = true - - Fuzzy.files = get_file_list() - Fuzzy.matches = Fuzzy.files - Fuzzy.cursor = 1 - Fuzzy.scroll = 0 - 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 + S.timer = vim.loop.new_timer() + S.timer:start(ms, 0, function() + if S.timer then + S.timer:stop() + S.timer:close() + S.timer = nil end - local start_idx = Fuzzy.scroll + 1 - local end_idx = math.min(start_idx + Fuzzy.page_size - 1, total) - local display = {} + vim.schedule(fn) + end) +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 - display[#display + 1] = Fuzzy.matches[i] + view[#view + 1] = S.filtered[i] end - vim.api.nvim_buf_set_lines(Fuzzy.result_buf, 0, -1, false, display) - Fuzzy:highlight_selection() end - local function update_results(text) - if text == '' then - Fuzzy.matches = Fuzzy.files - else - Fuzzy.matches = vim.fn.matchfuzzy(Fuzzy.files, text) + -- sanitize all lines to strings to avoid E5108 errors + for i = 1, #view do + if type(view[i]) ~= 'string' then + view[i] = tostring(view[i] or '') end - Fuzzy.cursor, Fuzzy.scroll = 1, 0 - render_results() end - vim.api.nvim_create_autocmd({ 'TextChangedI', 'TextChangedP' }, { - buffer = Fuzzy.input_buf, - callback = function() - local text = vim.fn.getline('.'):gsub('^Search:%s*', '') - update_results(text) - end, - }) + vim.bo[S.buf_res].modifiable = true + vim.bo[S.buf_res].readonly = false + 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) - vim.keymap.set('i', '', function() - if Fuzzy.cursor < #Fuzzy.matches then - Fuzzy.cursor = Fuzzy.cursor + 1 - if Fuzzy.cursor > Fuzzy.scroll + Fuzzy.page_size then - Fuzzy.scroll = Fuzzy.scroll + 1 - end - render_results() - end - end, { buffer = Fuzzy.input_buf }) - - vim.keymap.set('i', '', function() - if Fuzzy.cursor > 1 then - 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', '', 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', '', Fuzzy.close, { buffer = Fuzzy.input_buf }) - vim.keymap.set('i', '', Fuzzy.close, { buffer = Fuzzy.input_buf }) - vim.keymap.set('n', '', 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', + -- match highlights (visible window only) + vim.api.nvim_set_hl(0, 'FinderMatch', { link = 'Search', default = true }) + for i, _ in ipairs(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 + vim.api.nvim_buf_set_extmark(S.buf_res, S.ns, i - 1, scol, { + end_col = ecol, + hl_group = 'FinderMatch', }) end end end end - local function run_grep(query) - if query == '' then - vim.api.nvim_buf_set_lines(Fuzzy.result_buf, 0, -1, false, { '-- type to search --' }) - return - end - local handle = io.popen('rg --vimgrep --hidden --smart-case ' .. vim.fn.shellescape(query)) - if not handle then - return - 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) + -- selection highlight + if total > 0 and #view > 0 then + vim.api.nvim_set_hl(0, 'FinderSelection', { link = 'CursorLine', default = true }) + local rel = clamp(S.select - S.scroll, 1, #view) + 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.api.nvim_create_autocmd({ 'TextChangedI', 'TextChangedP' }, { - buffer = Fuzzy.input_buf, + vim.bo[S.buf_res].modifiable = false + 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() - local text = vim.fn.getline('.'):gsub('^Grep:%s*', '') - run_grep(text) + 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.keymap.set('i', '', function() - if Fuzzy.cursor < #Fuzzy.matches then - Fuzzy.cursor = Fuzzy.cursor + 1 - if Fuzzy.cursor > Fuzzy.scroll + Fuzzy.page_size then - Fuzzy.scroll = Fuzzy.scroll + 1 + -- Close if prompt buffer hides or leaves + vim.api.nvim_create_autocmd({ 'BufHidden', 'BufLeave' }, { + group = S.aug, + buffer = S.buf_inp, + callback = function() + if S.active then + M.close() end - local query = vim.fn.getline('.'):gsub('^Grep:%s*', '') - render_results(query) - end - end, { buffer = Fuzzy.input_buf }) + end, + }) - vim.keymap.set('i', '', function() - if Fuzzy.cursor > 1 then - Fuzzy.cursor = Fuzzy.cursor - 1 - if Fuzzy.cursor <= Fuzzy.scroll then - Fuzzy.scroll = math.max(Fuzzy.scroll - 1, 0) + -- Re-render on resize to respect new viewport height + vim.api.nvim_create_autocmd('VimResized', { + group = S.aug, + callback = function() + if S.active then + render() end - local query = vim.fn.getline('.'):gsub('^Grep:%s*', '') - render_results(query) - end - end, { buffer = Fuzzy.input_buf }) - - vim.keymap.set('i', '', 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', '', function() - Fuzzy.close() - end, { buffer = Fuzzy.input_buf }) + end, + }) vim.cmd.startinsert() + L('open_layout', { win_inp = S.win_inp, win_res = S.win_res }) end --------------------------------------------------------------------- --- 🧩 Commands & Keymaps --------------------------------------------------------------------- -vim.api.nvim_create_user_command('FuzzyLive', function() - Fuzzy.open() -end, {}) -vim.api.nvim_create_user_command('FuzzyGrep', function() - Fuzzy.open_grep() -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 + L('close_layout') +end -vim.keymap.set('n', 'f', function() - vim.cmd.FuzzyLive() -end, { desc = 'Open fuzzy file finder' }) -vim.keymap.set('n', 'g', function() - vim.cmd.FuzzyGrep() -end, { desc = 'Search file contents with ripgrep' }) +--------------------------------------------------------------------- +-- 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', '', function() + if S.mode == 'grep' then + accept_selection_grep() + else + accept_selection_files() + end + end, opts) + vim.keymap.set('i', '', function() + M.close() + end, opts) + vim.keymap.set('i', '', 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 -- 2.45.2 From 62ec73555368274942facbbf5a355c55a9b952f0 Mon Sep 17 00:00:00 2001 From: Tomas Mirchev Date: Sun, 26 Oct 2025 08:06:20 +0200 Subject: [PATCH 2/4] v2 --- lua/plugins/finder.lua | 229 ++++++++++++++++++++++++++++++++--------- 1 file changed, 180 insertions(+), 49 deletions(-) diff --git a/lua/plugins/finder.lua b/lua/plugins/finder.lua index 4a047bc..8af2437 100644 --- a/lua/plugins/finder.lua +++ b/lua/plugins/finder.lua @@ -1,5 +1,6 @@ -- 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. local M = {} @@ -23,11 +24,17 @@ local S = { active = false, mode = nil, -- "files" | "grep" root = nil, + is_git = false, 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, @@ -87,12 +94,42 @@ 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) == '/' + 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 + end + local joined = joinpath(root, p) + local rp = vim.loop.fs_realpath(joined) + return rp or joined +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 - return vim.loop.cwd() + S.is_git = false + -- Respect Neovim cwd, not process cwd + return vim.fn.getcwd(0, 0) end local function resolve_file_cmd() @@ -110,9 +147,10 @@ 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) + local h = vim.api.nvim_win_get_height(S.win_res) + return math.max(1, h) end - return math.min(M.config.page_size, vim.o.lines) + return math.max(1, math.min(M.config.page_size, vim.o.lines)) end --------------------------------------------------------------------- @@ -135,7 +173,7 @@ end -- Render --------------------------------------------------------------------- local function render() - if not (S.buf_res and vim.api.nvim_buf_is_valid(S.buf_res)) then + if not (S.active and S.buf_res and vim.api.nvim_buf_is_valid(S.buf_res)) then return end @@ -153,7 +191,6 @@ local function render() end end - -- sanitize all lines to strings to avoid E5108 errors for i = 1, #view do if type(view[i]) ~= 'string' then view[i] = tostring(view[i] or '') @@ -167,7 +204,7 @@ local function render() -- match highlights (visible window only) vim.api.nvim_set_hl(0, 'FinderMatch', { link = 'Search', default = true }) - for i, _ in ipairs(view) do + for i = 1, #view do local idx = S.scroll + i local spans = S.positions[idx] if spans then @@ -202,7 +239,6 @@ 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 {}, {} @@ -210,14 +246,13 @@ local function compute_positions_files(items, q) 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 + local start0 = (c > 0) and (c - 1) or 0 spans[#spans + 1] = { start0, start0 + 1 } end positions[i] = spans @@ -265,8 +300,6 @@ local function set_items(list) 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 '') @@ -286,8 +319,8 @@ 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 @@ -301,7 +334,6 @@ 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 '') @@ -350,7 +382,7 @@ local function accept_selection_files() if not pick then return end - local file = pick + local file = to_abs_in_root(S.root, pick) local edit_cb = function() vim.cmd.edit(vim.fn.fnameescape(file)) end @@ -358,19 +390,32 @@ local function accept_selection_files() 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 "::" + local file, lnum = line:match('^(.*):(%d+):') + if not file then + return nil + end + return file, tonumber(lnum) or 1 +end + local function accept_selection_grep() local pick = S.filtered[S.select] if not pick then return end - local file, lnum = pick:match('^([^:]+):(%d+):') + 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)) - vim.api.nvim_win_set_cursor(0, { lnum, 0 }) + if vim.api.nvim_get_current_buf() > 0 then + pcall(vim.api.nvim_win_set_cursor, 0, { lnum, 0 }) + end end M.close() vim.schedule(edit_cb) @@ -379,7 +424,17 @@ end --------------------------------------------------------------------- -- Backends --------------------------------------------------------------------- +local function cancel_job(j) + if not j then + return + end + pcall(function() + j: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 @@ -387,21 +442,59 @@ local function collect_files_async(cb) return end + -- Prefer fd/fdfind, then git ls-files, then blocking glob fallback. local file_cmd = resolve_file_cmd() if file_cmd then - local args = { file_cmd, '--type', 'f', '--hidden', '--follow', '--color', 'never' } + local args = + { file_cmd, '--type', 'f', '--hidden', '--follow', '--color', 'never', '--exclude', '.git' } 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) + 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.code == 0 and obj.stdout then - list = vim.split(obj.stdout, '\n', { trimempty = true }) + local raw = vim.split(obj.stdout, '\n', { trimempty = true }) + list = {} + for i = 1, #raw do + list[i] = to_abs_in_root(root, raw[i]) + end else - list = vim.fn.globpath(root, '**/*', false, true) - list = vim.tbl_filter(function(p) - return vim.fn.isdirectory(p) == 0 - end, list) + 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 = {} @@ -413,28 +506,33 @@ local function collect_files_async(cb) S.cache[root] = S.cache[root] or {} S.cache[root].files = { list = list, at = now_sec() } vim.schedule(function() - cb(list) + if S.active and gen == S.gen then + cb(list) + end 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) + return end + + -- Last-resort blocking fallback (no fd, not git, or git failed) + 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 local function grep_async(query, cb) + local gen = S.gen if query == '' then cb({}) return @@ -447,8 +545,25 @@ local function grep_async(query, cb) cb({}) return end - local args = { rg, '--vimgrep', '--hidden', '--smart-case', '--no-heading', '--', query } - vim.system(args, { text = true, cwd = S.root }, function(obj) + local args = { + rg, + '--vimgrep', + '--hidden', + '--smart-case', + '--no-heading', + '--no-config', + '--color', + 'never', + '--path-separator', + '/', + '--', + query, + } + cancel_job(S.job_rg) + S.job_rg = vim.system(args, { text = true, cwd = S.root }, function(obj) + if not (S.active and gen == S.gen) then + return + end local list = {} if obj.code == 0 and obj.stdout then list = vim.split(obj.stdout, '\n', { trimempty = true }) @@ -461,7 +576,9 @@ local function grep_async(query, cb) list = tmp end vim.schedule(function() - cb(list) + if S.active and gen == S.gen then + cb(list) + end end) end) end @@ -506,7 +623,7 @@ local function open_layout(prompt) style = 'minimal', border = 'single', width = width, - height = height - 2, + height = math.max(1, height - 2), col = col, row = row + 2, focusable = false, @@ -520,7 +637,6 @@ local function open_layout(prompt) 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() @@ -538,7 +654,6 @@ local function open_layout(prompt) end, }) - -- Close if prompt buffer hides or leaves vim.api.nvim_create_autocmd({ 'BufHidden', 'BufLeave' }, { group = S.aug, buffer = S.buf_inp, @@ -549,7 +664,6 @@ local function open_layout(prompt) end, }) - -- Re-render on resize to respect new viewport height vim.api.nvim_create_autocmd('VimResized', { group = S.aug, callback = function() @@ -609,11 +723,18 @@ local function attach_handlers() 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 set_query(q) end) @@ -635,13 +756,16 @@ function M.files() M.close() end S.active = true + S.gen = S.gen + 1 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 + if not S.active then + return + end + set_items(list) end) end @@ -650,6 +774,7 @@ function M.grep() M.close() end S.active = true + S.gen = S.gen + 1 S.mode = 'grep' S.root = project_root() open_layout('Grep: ') @@ -662,12 +787,18 @@ function M.close() if not S.active then return end - close_layout() + -- stop timers and jobs first if S.timer then S.timer:stop() S.timer:close() 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 = {}, {}, {} -- 2.45.2 From 36817d1e18d9e3df5e3973dd49e60cab9b766014 Mon Sep 17 00:00:00 2001 From: Tomas Mirchev Date: Sun, 26 Oct 2025 08:09:28 +0200 Subject: [PATCH 3/4] v3 --- lua/modules/navigation.lua | 5 +- lua/plugins/finder.lua | 520 +++++++++++++++++++++++-------------- 2 files changed, 324 insertions(+), 201 deletions(-) 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 ) -- 2.45.2 From 095c7b92a20ce9a3120d13a7654704d2fb9ef897 Mon Sep 17 00:00:00 2001 From: Tomas Mirchev Date: Sun, 26 Oct 2025 08:32:01 +0200 Subject: [PATCH 4/4] v4 --- lua/modules/navigation.lua | 2 +- lua/plugins/finder.lua | 182 ++++++++++++++++++------------------- 2 files changed, 92 insertions(+), 92 deletions(-) diff --git a/lua/modules/navigation.lua b/lua/modules/navigation.lua index 1116597..66d5d28 100644 --- a/lua/modules/navigation.lua +++ b/lua/modules/navigation.lua @@ -1,6 +1,6 @@ require('plugins.filetree') require('plugins.finder').setup({ - exclude_patterns = { 'node_modules', 'dist', 'build', '.git', '.cache', '.turbo' }, + exclude_patterns = { 'node_modules', 'dist', 'build', '.git', '.cache', '.turbo', '*-lock.json' }, use_disk_cache = true, -- optional }) diff --git a/lua/plugins/finder.lua b/lua/plugins/finder.lua index 1500417..6fdb487 100644 --- a/lua/plugins/finder.lua +++ b/lua/plugins/finder.lua @@ -10,6 +10,8 @@ M.config = { 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, @@ -53,6 +55,9 @@ local S = { 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, } --------------------------------------------------------------------- @@ -81,34 +86,28 @@ local function cmd_exists(bin) return vim.fn.executable(bin) == 1 end +-- single reusable timer to avoid churn; caller controls ms local function debounce(fn, ms) - if S.timer then - S.timer:stop() - S.timer:close() - S.timer = nil + if not S.timer then + S.timer = vim.loop.new_timer() end - S.timer = vim.loop.new_timer() + S.timer:stop() S.timer:start(ms, 0, function() - if S.timer then - S.timer:stop() - S.timer:close() - S.timer = nil - end + S.timer:stop() vim.schedule(fn) end) end local function read_gitignore(root) local p = root .. '/.gitignore' - local lines = {} local f = io.open(p, 'r') if not f then return {} end + local lines = {} 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 @@ -142,6 +141,7 @@ local function load_cache(root) return nil end if now_sec() - (data.timestamp or 0) > M.config.cache_ttl_sec then + pcall(os.remove, file) -- proactively clear stale return nil end return data.files @@ -173,19 +173,24 @@ local function project_root() return vim.fn.getcwd(0, 0) end +-- Robust relative-to-root local function normalize_rel(root, p) if not p or p == '' then return '' end - -- strip leading ./ and leading root if any - p = p:gsub('^%./', '') + local abs = vim.fs.normalize(vim.fn.fnamemodify(p, ':p')) 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 + if abs == root_norm then + return '' end - return p + 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 + -- strip leading ./ as last resort + return (p:gsub('^%./', '')) end local function to_abs_path(root, rel) @@ -195,6 +200,11 @@ local function to_abs_path(root, rel) return vim.fs.normalize(root .. '/' .. rel) end +local function to_display_path(abs) + -- relative to current working dir if possible, else absolute + 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)) @@ -202,6 +212,18 @@ local function page_rows() 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 helpers --------------------------------------------------------------------- @@ -237,7 +259,7 @@ local function render() local end_idx = math.min(start_idx + page_rows() - 1, total) for i = start_idx, end_idx do local line = S.filtered[i] - view[#view + 1] = type(line) == 'string' and line or tostring(line or '') + view[#view + 1] = tostring(line or '') end end @@ -283,7 +305,6 @@ 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 {}, {} @@ -293,14 +314,26 @@ local function compute_positions_files(items, q) local filtered, positions = {}, {} for i, v in ipairs(out_items) do - filtered[i] = (type(v) == 'string') and v or tostring(v or '') + 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 - local start0 = (c > 0) and (c - 1) or 0 -- to 0-based - spans[#spans + 1] = { start0, start0 + 1 } + local c0 = c + if not start_col then + start_col, last_col = c0, c0 + elseif c0 == last_col + 1 then + last_col = c0 + else + table.insert(spans, { start_col, last_col + 1 }) -- end exclusive + start_col, last_col = c0, c0 + end end - positions[i] = spans + 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 @@ -312,15 +345,15 @@ local function compute_positions_grep(lines, q) local qlow = q:lower() local positions = {} for i, line in ipairs(lines) do - local sidx = 1 + local llow = tostring(line or ''):lower() local spans = {} - local llow = (type(line) == 'string' and line or tostring(line or '')):lower() + local sidx = 1 while true do - local s, e = string.find(llow, qlow, sidx, true) + local s, e = llow:find(qlow, sidx, true) if not s then break end - spans[#spans + 1] = { s - 1, e } -- 0-based start, exclusive end + table.insert(spans, { s - 1, e }) sidx = e + 1 if #spans > 64 then break @@ -346,9 +379,7 @@ local function set_items(list) -- sanitize to strings for i, v in ipairs(list) do - if type(v) ~= 'string' then - list[i] = tostring(v or '') - end + list[i] = tostring(v or '') end S.items = list @@ -379,16 +410,7 @@ 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 - -- 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 @@ -432,12 +454,12 @@ local function move_up() render() end +-- Correct parser for rg --vimgrep (file:line:col:match) local function parse_vimgrep(line) - -- Greedy file capture up to last "::" if type(line) ~= 'string' then return nil end - local file, lnum = line:match('^(.*):(%d+):') + local file, lnum = line:match('^(.-):(%d+):%d+:') if not file then return nil end @@ -450,9 +472,10 @@ local function accept_selection_files() return end 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(abs)) + vim.cmd.edit(vim.fn.fnameescape(path)) end) end @@ -466,12 +489,12 @@ local function accept_selection_grep() return end local abs = to_abs_path(S.root, file) + local path = to_display_path(abs) local ln = tonumber(lnum) or 1 M.close() vim.schedule(function() - vim.cmd.edit(vim.fn.fnameescape(abs)) - if pcall(vim.api.nvim_win_set_cursor, 0, { ln, 0 }) then - end + vim.cmd.edit(vim.fn.fnameescape(path)) + pcall(vim.api.nvim_win_set_cursor, 0, { ln, 0 }) end) end @@ -493,9 +516,11 @@ local function collect_files_async(cb) 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 @@ -517,7 +542,6 @@ local function collect_files_async(cb) if file_cmd then 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 @@ -531,7 +555,7 @@ local function collect_files_async(cb) return end local list = {} - if obj.code == 0 and obj.stdout then + if obj.stdout then local raw = vim.split(obj.stdout, '\n', { trimempty = true }) for _, p in ipairs(raw) do list[#list + 1] = normalize_rel(root, p) @@ -565,7 +589,7 @@ local function collect_files_async(cb) return end local list = {} - if o2.code == 0 and o2.stdout then + if o2.stdout then for p in o2.stdout:gmatch('([^%z]+)') do list[#list + 1] = normalize_rel(root, p) end @@ -588,33 +612,8 @@ local function collect_files_async(cb) return end - -- Last resort: blocking glob with simple exclusion checks - local list = vim.fn.globpath(root, '**/*', false, true) - list = vim.tbl_filter(function(p) - 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) - - 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] = rel[i] - end - rel = tmp - end - save_cache(root, rel) - cb(rel) + -- Last resort omitted: blocking glob removed to keep async-only behavior + cb({}) end local function grep_async(query, cb) @@ -645,17 +644,13 @@ 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) + table.insert(args, 2, '--glob') + table.insert(args, 3, '!' .. 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) + for _, p in ipairs(read_gitignore(root)) do + table.insert(args, 2, '--glob') + table.insert(args, 3, '!' .. p) end cancel_job(S.job_rg) @@ -664,7 +659,8 @@ local function grep_async(query, cb) return end local list = {} - if obj.code == 0 and obj.stdout then + -- Accept output even if exit code != 0 (no matches or partial errors) + if obj.stdout and #obj.stdout > 0 then list = vim.split(obj.stdout, '\n', { trimempty = true }) end if #list > M.config.max_items then @@ -837,6 +833,7 @@ local function attach_handlers() 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 @@ -856,7 +853,7 @@ local function attach_handlers() else set_query(q) end - end, M.config.debounce_ms) + end, delay) end, }) end @@ -882,7 +879,7 @@ function M.files() if not S.active then return end - set_items(list) -- render initial list (relative paths) + set_items(list) -- render initial list (relative to root) end) end @@ -907,8 +904,10 @@ function M.close() end -- stop timers and jobs first if S.timer then - S.timer:stop() - S.timer:close() + pcall(function() + S.timer:stop() + S.timer:close() + end) S.timer = nil end cancel_job(S.job_rg) @@ -922,6 +921,7 @@ function M.close() S.items, S.filtered, S.positions = {}, {}, {} S.query, S.select, S.scroll = '', 1, 0 S.user_moved = false + S.files_cached = false L('session closed') end -- 2.45.2