Update matcha nvim plugin

This commit is contained in:
Talia 2025-08-29 11:22:57 +02:00
parent df7f3c715a
commit 497c440216
2 changed files with 52 additions and 45 deletions

View file

@ -6,44 +6,21 @@ local function nextcol(state)
end end
end end
local function unquote(args)
local result = {}
local term
if args[1]:find([=[^["']]=]) then
term = args[1]:sub(1, 1) .. "$"
elseif args[1]:find("^%[=*%[") then
local num = args[1]:find("[", 2, true) - 2
term = "]" .. string.rep("=", num) .. "]$"
else
return args
end
for _, arg in ipairs(args) do
table.insert(result, arg)
if term and arg:find(term) then
result = {table.concat(result, " "):sub(#term, -#term)}
term = nil
end
end
return result
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(args) local function findLocations(pattern, first_glob, ...)
local files if #args>1 then local files if first_glob then
files = vim.fs.find(function(file) files = vim.fn.glob(first_glob, false, true)
for _, pattern in ipairs(args), args, 1 do if ... then
if vim.fs.basename(file):find(pattern) then for _, glob in ipairs { ... } do
return true for _, file in ipairs(vim.fn.glob(glob, false, true)) do
-- Assume overhead of table.insert is outweighed by all the file ops anyway
table.insert(files, file)
end
end end
end end
return false
end, {
limit=math.huge;
type="file";
})
else else
files = vim.fn.argv() files = vim.fn.argv()
end end
@ -54,7 +31,7 @@ local function findLocations(args)
local lnum = 0 local lnum = 0
for line in io.lines(file) do for line in io.lines(file) do
lnum = lnum + 1 lnum = lnum + 1
for from, to in colmatch(line, args[1]) do for from, to in colmatch(line, pattern) do
table.insert(locations, { table.insert(locations, {
filename = file; filename = file;
lnum = lnum; lnum = lnum;
@ -69,18 +46,46 @@ local function findLocations(args)
return locations return locations
end end
vim.api.nvim_create_user_command("Matcha", function(params) local parse_args do
local locations = findLocations(unquote(params.fargs)) if pcall(require, "arrr") then
if not locations[1] then return print("Nothing found") end local arrr = require "arrr"
vim.fn.setqflist(locations)
vim.cmd("copen") parse_args = arrr {
vim.cmd("crewind") { "Use location list instead of quickfix list", "--location-list", "-l" };
end, {nargs="+"}) { "Appends to the current quicklist", "--append", "-a" };
{ "Replaces the current quicklist", "--replace", "-r" };
}
else
function parse_args(args)
return args
end
end
end
vim.api.nvim_create_user_command("Matcha", function(params)
local args = parse_args(params.fargs)
local locations = findLocations(unpack(args))
vim.api.nvim_create_user_command("MatchaLocal", function(params)
local locations = findLocations(unquote(params.fargs))
if not locations[1] then return print("Nothing found") end if not locations[1] then return print("Nothing found") end
vim.fn.setloclist(0, locations)
local action if args.append then
action = "a"
elseif args.replace then
action = "u"
else
action = " "
end
local what = { title = string.format("Matcha: `%s`", args[1]), items = locations }
if args.locationlist then
vim.fn.setloclist(0, {}, action, what)
vim.cmd("lopen") vim.cmd("lopen")
vim.cmd("lrewind") vim.cmd("lrewind")
else
vim.fn.setqflist({}, action, what)
vim.cmd("copen")
vim.cmd("crewind")
end
end, {nargs="+"}) end, {nargs="+"})

2
vimrc
View file

@ -16,4 +16,6 @@ set foldlevelstart=99
set foldmethod=expr set foldmethod=expr
set foldexpr=nvim_treesitter#foldexpr() set foldexpr=nvim_treesitter#foldexpr()
cnoreabbrev matcha Matcha -l
nnoremap g# :b #<CR> nnoremap g# :b #<CR>