Filter out directories in matcha

This commit is contained in:
Talia 2025-09-03 10:54:28 +02:00
parent 497c440216
commit 678fa8827e

View file

@ -6,34 +6,39 @@ local function nextcol(state)
end end
end end
local function isfile(path)
return vim.fn.isdirectory(path) == 0
end
local function colmatch(str, pattern) local function colmatch(str, pattern)
return nextcol, {str=str,pattern=pattern,last=0} return nextcol, {str=str,pattern=pattern,last=0}
end end
local function findLocations(pattern, first_glob, ...) local function findLocations(pattern, first_glob, ...)
local files if first_glob then local paths if first_glob then
files = vim.fn.glob(first_glob, false, true) paths = vim.fn.glob(first_glob, false, true)
if ... then if ... then
for _, glob in ipairs { ... } do for _, glob in ipairs { ... } do
for _, file in ipairs(vim.fn.glob(glob, false, true)) do for _, path in ipairs(vim.fn.glob(glob, false, true)) do
-- Assume overhead of table.insert is outweighed by all the file ops anyway -- Assume overhead of table.insert is outweighed by all the file ops anyway
table.insert(files, file) table.insert(paths, path)
end end
end end
end end
else else
files = vim.fn.argv() paths = vim.fn.argv()
end end
local locations = {} local locations = {}
for _, file in ipairs(files) do for _, path in ipairs(paths) do
local lnum = 0 local lnum = 0
for line in io.lines(file) do if isfile(path) then
for line in io.lines(path) do
lnum = lnum + 1 lnum = lnum + 1
for from, to in colmatch(line, pattern) do for from, to in colmatch(line, pattern) do
table.insert(locations, { table.insert(locations, {
filename = file; filename = path;
lnum = lnum; lnum = lnum;
col = from; col = from;
end_col = to; end_col = to;
@ -42,6 +47,7 @@ local function findLocations(pattern, first_glob, ...)
end end
end end
end end
end
return locations return locations
end end