v3
This commit is contained in:
parent
62ec735553
commit
36817d1e18
@ -1,8 +1,7 @@
|
|||||||
require('plugins.filetree')
|
require('plugins.filetree')
|
||||||
require('plugins.finder').setup({
|
require('plugins.finder').setup({
|
||||||
file_cmd = 'fdfind',
|
exclude_patterns = { 'node_modules', 'dist', 'build', '.git', '.cache', '.turbo' },
|
||||||
grep_cmd = 'rg',
|
use_disk_cache = true, -- optional
|
||||||
debug = false,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
vim.keymap.set('n', '<leader>f', function()
|
vim.keymap.set('n', '<leader>f', function()
|
||||||
|
|||||||
@ -1,6 +1,4 @@
|
|||||||
-- finder.lua — Minimal async fuzzy finder + grep for Neovim ≥ 0.11
|
-- finder.lua — Minimal, hardened 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 = {}
|
local M = {}
|
||||||
|
|
||||||
@ -15,6 +13,12 @@ M.config = {
|
|||||||
cache_ttl_sec = 20,
|
cache_ttl_sec = 20,
|
||||||
max_items = 5000, -- safety cap for massive outputs
|
max_items = 5000, -- safety cap for massive outputs
|
||||||
debug = false,
|
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,
|
active = false,
|
||||||
mode = nil, -- "files" | "grep"
|
mode = nil, -- "files" | "grep"
|
||||||
root = nil,
|
root = nil,
|
||||||
is_git = false,
|
|
||||||
|
|
||||||
cache = {},
|
cache = {},
|
||||||
timer = nil,
|
timer = nil,
|
||||||
@ -49,6 +52,7 @@ local S = {
|
|||||||
positions = {}, -- positions[i] = { {scol, ecol}, ... } for filtered[i]
|
positions = {}, -- positions[i] = { {scol, ecol}, ... } for filtered[i]
|
||||||
select = 1, -- absolute index in filtered (1-based)
|
select = 1, -- absolute index in filtered (1-based)
|
||||||
scroll = 0, -- top index (0-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)
|
s = s .. ' ' .. vim.inspect(data)
|
||||||
end
|
end
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
vim.notify(s)
|
pcall(vim.notify, s)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -94,61 +98,106 @@ local function debounce(fn, ms)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function is_windows()
|
local function read_gitignore(root)
|
||||||
local sys = vim.loop.os_uname().sysname
|
local p = root .. '/.gitignore'
|
||||||
return sys == 'Windows_NT'
|
local lines = {}
|
||||||
end
|
local f = io.open(p, 'r')
|
||||||
|
if not f then
|
||||||
local function is_abs_path(p)
|
return {}
|
||||||
if is_windows() then
|
|
||||||
-- C:\... or \\server\share...
|
|
||||||
return p:match('^%a:[/\\]') or p:match('^[/\\][/\\]')
|
|
||||||
else
|
|
||||||
return p:sub(1, 1) == '/'
|
|
||||||
end
|
end
|
||||||
end
|
for line in f:lines() do
|
||||||
|
local l = vim.trim(line)
|
||||||
local function joinpath(a, b)
|
if #l > 0 and not l:match('^#') and not l:match('^!') then
|
||||||
return vim.fs.normalize(vim.fs.joinpath(a, b))
|
-- normalize directory suffix to plain token for our excludes
|
||||||
end
|
l = l:gsub('/+$', '')
|
||||||
|
table.insert(lines, l)
|
||||||
local function to_abs_in_root(root, p)
|
end
|
||||||
if is_abs_path(p) then
|
|
||||||
return p
|
|
||||||
end
|
end
|
||||||
local joined = joinpath(root, p)
|
f:close()
|
||||||
local rp = vim.loop.fs_realpath(joined)
|
return lines
|
||||||
return rp or joined
|
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
|
end
|
||||||
|
|
||||||
local function project_root()
|
local function project_root()
|
||||||
local obj = vim.system({ 'git', 'rev-parse', '--show-toplevel' }, { text = true }):wait()
|
local obj = vim.system({ 'git', 'rev-parse', '--show-toplevel' }, { text = true }):wait()
|
||||||
if obj.code == 0 and obj.stdout and obj.stdout ~= '' then
|
if obj.code == 0 and obj.stdout and obj.stdout ~= '' then
|
||||||
S.is_git = true
|
|
||||||
return vim.trim(obj.stdout)
|
return vim.trim(obj.stdout)
|
||||||
end
|
end
|
||||||
S.is_git = false
|
|
||||||
-- Respect Neovim cwd, not process cwd
|
|
||||||
return vim.fn.getcwd(0, 0)
|
return vim.fn.getcwd(0, 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function resolve_file_cmd()
|
local function normalize_rel(root, p)
|
||||||
if M.config.file_cmd then
|
if not p or p == '' then
|
||||||
return M.config.file_cmd
|
return ''
|
||||||
end
|
end
|
||||||
if cmd_exists('fd') then
|
-- strip leading ./ and leading root if any
|
||||||
return 'fd'
|
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
|
end
|
||||||
if cmd_exists('fdfind') then
|
return p
|
||||||
return 'fdfind'
|
end
|
||||||
|
|
||||||
|
local function to_abs_path(root, rel)
|
||||||
|
if not rel or rel == '' then
|
||||||
|
return root
|
||||||
end
|
end
|
||||||
return nil
|
return vim.fs.normalize(root .. '/' .. rel)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function page_rows()
|
local function page_rows()
|
||||||
if S.win_res and vim.api.nvim_win_is_valid(S.win_res) then
|
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, vim.api.nvim_win_get_height(S.win_res))
|
||||||
return math.max(1, h)
|
|
||||||
end
|
end
|
||||||
return math.max(1, math.min(M.config.page_size, vim.o.lines))
|
return math.max(1, math.min(M.config.page_size, vim.o.lines))
|
||||||
end
|
end
|
||||||
@ -173,7 +222,7 @@ end
|
|||||||
-- Render
|
-- Render
|
||||||
---------------------------------------------------------------------
|
---------------------------------------------------------------------
|
||||||
local function 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
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -187,13 +236,8 @@ local function render()
|
|||||||
local start_idx = S.scroll + 1
|
local start_idx = S.scroll + 1
|
||||||
local end_idx = math.min(start_idx + page_rows() - 1, total)
|
local end_idx = math.min(start_idx + page_rows() - 1, total)
|
||||||
for i = start_idx, end_idx do
|
for i = start_idx, end_idx do
|
||||||
view[#view + 1] = S.filtered[i]
|
local line = S.filtered[i]
|
||||||
end
|
view[#view + 1] = type(line) == 'string' and line or tostring(line or '')
|
||||||
end
|
|
||||||
|
|
||||||
for i = 1, #view do
|
|
||||||
if type(view[i]) ~= 'string' then
|
|
||||||
view[i] = tostring(view[i] or '')
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -239,6 +283,7 @@ end
|
|||||||
-- Querying / Filtering
|
-- Querying / Filtering
|
||||||
---------------------------------------------------------------------
|
---------------------------------------------------------------------
|
||||||
local function compute_positions_files(items, q)
|
local function compute_positions_files(items, q)
|
||||||
|
-- matchfuzzypos returns {items, positions}
|
||||||
local ok, res = pcall(vim.fn.matchfuzzypos, items, q)
|
local ok, res = pcall(vim.fn.matchfuzzypos, items, q)
|
||||||
if not ok or type(res) ~= 'table' then
|
if not ok or type(res) ~= 'table' then
|
||||||
return {}, {}
|
return {}, {}
|
||||||
@ -252,7 +297,7 @@ local function compute_positions_files(items, q)
|
|||||||
local cols = pos[i] or {}
|
local cols = pos[i] or {}
|
||||||
local spans = {}
|
local spans = {}
|
||||||
for _, c in ipairs(cols) do
|
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 }
|
spans[#spans + 1] = { start0, start0 + 1 }
|
||||||
end
|
end
|
||||||
positions[i] = spans
|
positions[i] = spans
|
||||||
@ -264,16 +309,14 @@ local function compute_positions_grep(lines, q)
|
|||||||
if q == '' then
|
if q == '' then
|
||||||
return lines, {}
|
return lines, {}
|
||||||
end
|
end
|
||||||
local pat = vim.pesc(q)
|
local qlow = q:lower()
|
||||||
local positions = {}
|
local positions = {}
|
||||||
for i, line in ipairs(lines) do
|
for i, line in ipairs(lines) do
|
||||||
local sidx = 1
|
local sidx = 1
|
||||||
local spans = {}
|
local spans = {}
|
||||||
if type(line) ~= 'string' then
|
local llow = (type(line) == 'string' and line or tostring(line or '')):lower()
|
||||||
line = tostring(line or '')
|
|
||||||
end
|
|
||||||
while true do
|
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
|
if not s then
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
@ -300,6 +343,8 @@ local function set_items(list)
|
|||||||
end
|
end
|
||||||
list = tmp
|
list = tmp
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- sanitize to strings
|
||||||
for i, v in ipairs(list) do
|
for i, v in ipairs(list) do
|
||||||
if type(v) ~= 'string' then
|
if type(v) ~= 'string' then
|
||||||
list[i] = tostring(v or '')
|
list[i] = tostring(v or '')
|
||||||
@ -334,26 +379,34 @@ local function set_query(q)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- sanitize filtered list
|
||||||
for i, v in ipairs(S.filtered) do
|
for i, v in ipairs(S.filtered) do
|
||||||
if type(v) ~= 'string' then
|
if type(v) ~= 'string' then
|
||||||
S.filtered[i] = tostring(v or '')
|
S.filtered[i] = tostring(v or '')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- preserve previous pick if still present
|
-- selection policy:
|
||||||
local idx = 1
|
-- if user hasn't moved, always reset to first on query changes;
|
||||||
if prev_val then
|
-- if user moved, preserve previous pick when present, else first.
|
||||||
for i, v in ipairs(S.filtered) do
|
if not S.user_moved then
|
||||||
if v == prev_val then
|
S.select = 1
|
||||||
idx = i
|
else
|
||||||
break
|
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
|
||||||
end
|
end
|
||||||
|
S.select = idx or 1
|
||||||
end
|
end
|
||||||
S.select = clamp(idx, 1, #S.filtered)
|
|
||||||
ensure_visible()
|
ensure_visible()
|
||||||
render()
|
render()
|
||||||
L('set_query', { query = q, filtered = #S.filtered })
|
L('set_query', { query = q, filtered = #S.filtered, user_moved = S.user_moved })
|
||||||
end
|
end
|
||||||
|
|
||||||
---------------------------------------------------------------------
|
---------------------------------------------------------------------
|
||||||
@ -363,6 +416,7 @@ local function move_down()
|
|||||||
if #S.filtered == 0 then
|
if #S.filtered == 0 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
S.user_moved = true
|
||||||
S.select = clamp(S.select + 1, 1, #S.filtered)
|
S.select = clamp(S.select + 1, 1, #S.filtered)
|
||||||
ensure_visible()
|
ensure_visible()
|
||||||
render()
|
render()
|
||||||
@ -372,27 +426,17 @@ local function move_up()
|
|||||||
if #S.filtered == 0 then
|
if #S.filtered == 0 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
S.user_moved = true
|
||||||
S.select = clamp(S.select - 1, 1, #S.filtered)
|
S.select = clamp(S.select - 1, 1, #S.filtered)
|
||||||
ensure_visible()
|
ensure_visible()
|
||||||
render()
|
render()
|
||||||
end
|
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)
|
local function parse_vimgrep(line)
|
||||||
-- Robust against Windows drive letters and extra colons in path.
|
|
||||||
-- Greedy file capture up to last ":<lnum>:"
|
-- Greedy file capture up to last ":<lnum>:"
|
||||||
|
if type(line) ~= 'string' then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
local file, lnum = line:match('^(.*):(%d+):')
|
local file, lnum = line:match('^(.*):(%d+):')
|
||||||
if not file then
|
if not file then
|
||||||
return nil
|
return nil
|
||||||
@ -400,101 +444,98 @@ local function parse_vimgrep(line)
|
|||||||
return file, tonumber(lnum) or 1
|
return file, tonumber(lnum) or 1
|
||||||
end
|
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 function accept_selection_grep()
|
||||||
local pick = S.filtered[S.select]
|
local pick = S.filtered[S.select]
|
||||||
if not pick then
|
if type(pick) ~= 'string' or pick == '' then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local file, lnum = parse_vimgrep(pick)
|
local file, lnum = parse_vimgrep(pick)
|
||||||
if not file then
|
if not file then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
file = to_abs_in_root(S.root, file)
|
local abs = to_abs_path(S.root, file)
|
||||||
lnum = tonumber(lnum) or 1
|
local ln = 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
|
|
||||||
M.close()
|
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
|
end
|
||||||
|
|
||||||
---------------------------------------------------------------------
|
---------------------------------------------------------------------
|
||||||
-- Backends
|
-- Backends
|
||||||
---------------------------------------------------------------------
|
---------------------------------------------------------------------
|
||||||
local function cancel_job(j)
|
local function cancel_job(job)
|
||||||
if not j then
|
if not job then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
pcall(function()
|
pcall(function()
|
||||||
j:kill(15)
|
job:kill(15)
|
||||||
end) -- SIGTERM if available
|
end) -- SIGTERM if available
|
||||||
end
|
end
|
||||||
|
|
||||||
local function collect_files_async(cb)
|
local function collect_files_async(cb)
|
||||||
local gen = S.gen
|
|
||||||
local root = S.root
|
local root = S.root
|
||||||
local c = S.cache[root]
|
local gen = S.gen
|
||||||
if c and c.files and now_sec() - c.files.at < M.config.cache_ttl_sec then
|
|
||||||
cb(c.files.list)
|
local disk = load_cache(root)
|
||||||
|
if disk and type(disk) == 'table' then
|
||||||
|
cb(disk)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Prefer fd/fdfind, then git ls-files, then blocking glob fallback.
|
local file_cmd = M.config.file_cmd
|
||||||
local file_cmd = resolve_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
|
if file_cmd then
|
||||||
local args =
|
local args = { file_cmd, '--type', 'f', '--hidden', '--color', 'never' }
|
||||||
{ file_cmd, '--type', 'f', '--hidden', '--follow', '--color', 'never', '--exclude', '.git' }
|
-- best-effort: avoid leading ./ in output
|
||||||
if file_cmd == 'fd' or file_cmd == 'fdfind' then
|
if file_cmd == 'fd' or file_cmd == 'fdfind' then
|
||||||
table.insert(args, '--strip-cwd-prefix')
|
table.insert(args, '--strip-cwd-prefix')
|
||||||
end
|
end
|
||||||
|
for _, ex in ipairs(excludes) do
|
||||||
|
table.insert(args, '--exclude')
|
||||||
|
table.insert(args, ex)
|
||||||
|
end
|
||||||
cancel_job(S.job_files)
|
cancel_job(S.job_files)
|
||||||
S.job_files = vim.system(args, { text = true, cwd = root }, function(obj)
|
S.job_files = vim.system(args, { text = true, cwd = root }, function(obj)
|
||||||
if not (S.active and gen == S.gen) then
|
if not (S.active and gen == S.gen) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local list
|
local list = {}
|
||||||
if obj.code == 0 and obj.stdout then
|
if obj.code == 0 and obj.stdout then
|
||||||
local raw = vim.split(obj.stdout, '\n', { trimempty = true })
|
local raw = vim.split(obj.stdout, '\n', { trimempty = true })
|
||||||
list = {}
|
for _, p in ipairs(raw) do
|
||||||
for i = 1, #raw do
|
list[#list + 1] = normalize_rel(root, p)
|
||||||
list[i] = to_abs_in_root(root, raw[i])
|
|
||||||
end
|
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
|
end
|
||||||
if #list > M.config.max_items then
|
if #list > M.config.max_items then
|
||||||
local tmp = {}
|
local tmp = {}
|
||||||
@ -503,8 +544,7 @@ local function collect_files_async(cb)
|
|||||||
end
|
end
|
||||||
list = tmp
|
list = tmp
|
||||||
end
|
end
|
||||||
S.cache[root] = S.cache[root] or {}
|
save_cache(root, list)
|
||||||
S.cache[root].files = { list = list, at = now_sec() }
|
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
if S.active and gen == S.gen then
|
if S.active and gen == S.gen then
|
||||||
cb(list)
|
cb(list)
|
||||||
@ -514,37 +554,82 @@ local function collect_files_async(cb)
|
|||||||
return
|
return
|
||||||
end
|
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)
|
local list = vim.fn.globpath(root, '**/*', false, true)
|
||||||
list = vim.tbl_filter(function(p)
|
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)
|
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 = {}
|
local tmp = {}
|
||||||
for i = 1, M.config.max_items do
|
for i = 1, M.config.max_items do
|
||||||
tmp[i] = list[i]
|
tmp[i] = rel[i]
|
||||||
end
|
end
|
||||||
list = tmp
|
rel = tmp
|
||||||
end
|
end
|
||||||
S.cache[root] = S.cache[root] or {}
|
save_cache(root, rel)
|
||||||
S.cache[root].files = { list = list, at = now_sec() }
|
cb(rel)
|
||||||
cb(list)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function grep_async(query, cb)
|
local function grep_async(query, cb)
|
||||||
local gen = S.gen
|
|
||||||
if query == '' then
|
if query == '' then
|
||||||
cb({})
|
cb({})
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
local gen = S.gen
|
||||||
local rg = M.config.grep_cmd
|
local rg = M.config.grep_cmd
|
||||||
if rg ~= 'rg' and not cmd_exists(rg) then
|
|
||||||
rg = 'rg'
|
|
||||||
end
|
|
||||||
if not cmd_exists(rg) then
|
if not cmd_exists(rg) then
|
||||||
cb({})
|
cb({})
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local root = S.root
|
||||||
local args = {
|
local args = {
|
||||||
rg,
|
rg,
|
||||||
'--vimgrep',
|
'--vimgrep',
|
||||||
@ -559,8 +644,22 @@ local function grep_async(query, cb)
|
|||||||
'--',
|
'--',
|
||||||
query,
|
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)
|
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
|
if not (S.active and gen == S.gen) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@ -591,14 +690,20 @@ local function open_layout(prompt)
|
|||||||
S.buf_res = 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
|
for _, b in ipairs({ S.buf_inp, S.buf_res }) do
|
||||||
vim.bo[b].buflisted = false
|
if b and vim.api.nvim_buf_is_valid(b) then
|
||||||
vim.bo[b].bufhidden = 'wipe'
|
vim.bo[b].buflisted = false
|
||||||
vim.bo[b].swapfile = 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
|
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 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 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,
|
focusable = true,
|
||||||
zindex = 200,
|
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, {
|
S.win_res = vim.api.nvim_open_win(S.buf_res, false, {
|
||||||
relative = 'editor',
|
relative = 'editor',
|
||||||
@ -629,14 +736,17 @@ local function open_layout(prompt)
|
|||||||
focusable = false,
|
focusable = false,
|
||||||
zindex = 199,
|
zindex = 199,
|
||||||
})
|
})
|
||||||
vim.wo[S.win_res].cursorline = false
|
if S.win_res and vim.api.nvim_win_is_valid(S.win_res) then
|
||||||
vim.wo[S.win_res].cursorlineopt = 'line'
|
vim.wo[S.win_res].cursorline = false
|
||||||
|
vim.wo[S.win_res].cursorlineopt = 'line'
|
||||||
|
end
|
||||||
|
|
||||||
if S.aug then
|
if S.aug then
|
||||||
pcall(vim.api.nvim_del_augroup_by_id, S.aug)
|
pcall(vim.api.nvim_del_augroup_by_id, S.aug)
|
||||||
end
|
end
|
||||||
S.aug = vim.api.nvim_create_augroup('finder_session', { clear = true })
|
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', {
|
vim.api.nvim_create_autocmd('WinEnter', {
|
||||||
group = S.aug,
|
group = S.aug,
|
||||||
callback = function()
|
callback = function()
|
||||||
@ -654,16 +764,20 @@ local function open_layout(prompt)
|
|||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
vim.api.nvim_create_autocmd({ 'BufHidden', 'BufLeave' }, {
|
-- Close if prompt buffer hides or leaves
|
||||||
group = S.aug,
|
if S.buf_inp and vim.api.nvim_buf_is_valid(S.buf_inp) then
|
||||||
buffer = S.buf_inp,
|
vim.api.nvim_create_autocmd({ 'BufHidden', 'BufLeave' }, {
|
||||||
callback = function()
|
group = S.aug,
|
||||||
if S.active then
|
buffer = S.buf_inp,
|
||||||
M.close()
|
callback = function()
|
||||||
end
|
if S.active then
|
||||||
end,
|
M.close()
|
||||||
})
|
end
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Re-render on resize to respect new viewport height
|
||||||
vim.api.nvim_create_autocmd('VimResized', {
|
vim.api.nvim_create_autocmd('VimResized', {
|
||||||
group = S.aug,
|
group = S.aug,
|
||||||
callback = function()
|
callback = function()
|
||||||
@ -718,32 +832,34 @@ local function attach_handlers()
|
|||||||
M.close()
|
M.close()
|
||||||
end, opts)
|
end, opts)
|
||||||
|
|
||||||
vim.api.nvim_create_autocmd({ 'TextChangedI', 'TextChangedP' }, {
|
if S.buf_inp and vim.api.nvim_buf_is_valid(S.buf_inp) then
|
||||||
group = S.aug,
|
vim.api.nvim_create_autocmd({ 'TextChangedI', 'TextChangedP' }, {
|
||||||
buffer = S.buf_inp,
|
group = S.aug,
|
||||||
callback = function()
|
buffer = S.buf_inp,
|
||||||
debounce(function()
|
callback = function()
|
||||||
if not (S.active and vim.api.nvim_buf_is_valid(S.buf_inp)) then
|
debounce(function()
|
||||||
return
|
if not (S.active and S.buf_inp and vim.api.nvim_buf_is_valid(S.buf_inp)) then
|
||||||
end
|
return
|
||||||
-- Prompt buffers usually return only the user input, but strip prompt defensively.
|
end
|
||||||
local raw = vim.fn.getline('.')
|
local raw = vim.fn.getline('.')
|
||||||
local prompt = (S.mode == 'files') and '^Search:%s*' or '^Grep:%s*'
|
raw = type(raw) == 'string' and raw or ''
|
||||||
local q = raw:gsub(prompt, '')
|
local prompt_pat = (S.mode == 'files') and '^Search:%s*' or '^Grep:%s*'
|
||||||
if S.mode == 'grep' then
|
local q = raw:gsub(prompt_pat, '')
|
||||||
grep_async(q, function(list)
|
if S.mode == 'grep' then
|
||||||
if not S.active then
|
grep_async(q, function(list)
|
||||||
return
|
if not S.active then
|
||||||
end
|
return
|
||||||
S.items = list
|
end
|
||||||
|
S.items = list or {}
|
||||||
|
set_query(q)
|
||||||
|
end)
|
||||||
|
else
|
||||||
set_query(q)
|
set_query(q)
|
||||||
end)
|
end
|
||||||
else
|
end, M.config.debounce_ms)
|
||||||
set_query(q)
|
end,
|
||||||
end
|
})
|
||||||
end, M.config.debounce_ms)
|
end
|
||||||
end,
|
|
||||||
})
|
|
||||||
|
|
||||||
L('keymaps attached', opts)
|
L('keymaps attached', opts)
|
||||||
end
|
end
|
||||||
@ -757,6 +873,7 @@ function M.files()
|
|||||||
end
|
end
|
||||||
S.active = true
|
S.active = true
|
||||||
S.gen = S.gen + 1
|
S.gen = S.gen + 1
|
||||||
|
S.user_moved = false
|
||||||
S.mode = 'files'
|
S.mode = 'files'
|
||||||
S.root = project_root()
|
S.root = project_root()
|
||||||
open_layout('Search: ')
|
open_layout('Search: ')
|
||||||
@ -765,7 +882,7 @@ function M.files()
|
|||||||
if not S.active then
|
if not S.active then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
set_items(list)
|
set_items(list) -- render initial list (relative paths)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -775,6 +892,7 @@ function M.grep()
|
|||||||
end
|
end
|
||||||
S.active = true
|
S.active = true
|
||||||
S.gen = S.gen + 1
|
S.gen = S.gen + 1
|
||||||
|
S.user_moved = false
|
||||||
S.mode = 'grep'
|
S.mode = 'grep'
|
||||||
S.root = project_root()
|
S.root = project_root()
|
||||||
open_layout('Grep: ')
|
open_layout('Grep: ')
|
||||||
@ -803,16 +921,22 @@ function M.close()
|
|||||||
S.mode, S.root = nil, nil
|
S.mode, S.root = nil, nil
|
||||||
S.items, S.filtered, S.positions = {}, {}, {}
|
S.items, S.filtered, S.positions = {}, {}, {}
|
||||||
S.query, S.select, S.scroll = '', 1, 0
|
S.query, S.select, S.scroll = '', 1, 0
|
||||||
|
S.user_moved = false
|
||||||
L('session closed')
|
L('session closed')
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.setup(opts)
|
function M.setup(opts)
|
||||||
M.config = vim.tbl_deep_extend('force', M.config, opts or {})
|
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
|
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
|
end
|
||||||
if M.config.grep_cmd and not cmd_exists(M.config.grep_cmd) then
|
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),
|
("finder: grep_cmd '%s' not found, will try 'rg'"):format(M.config.grep_cmd),
|
||||||
vim.log.levels.WARN
|
vim.log.levels.WARN
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user