Add argument quoting feature to matcha

This commit is contained in:
Talia 2024-06-12 09:09:11 +02:00
parent b17a9e949b
commit 499b3dce36

View file

@ -6,6 +6,27 @@ 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
@ -49,13 +70,13 @@ local function findLocations(args)
end end
vim.api.nvim_create_user_command("Matcha", function(params) vim.api.nvim_create_user_command("Matcha", function(params)
vim.fn.setqflist(findLocations(params.fargs)) vim.fn.setqflist(findLocations(unquote(params.fargs)))
vim.cmd("copen") vim.cmd("copen")
vim.cmd("crewind") vim.cmd("crewind")
end, {nargs="+"}) end, {nargs="+"})
vim.api.nvim_create_user_command("MatchaLocal", function(params) vim.api.nvim_create_user_command("MatchaLocal", function(params)
vim.fn.setloclist(0, findLocations(params.fargs)) vim.fn.setloclist(0, findLocations(unquote(params.fargs)))
vim.cmd("lopen") vim.cmd("lopen")
vim.cmd("lrewind") vim.cmd("lrewind")
end, {nargs="+"}) end, {nargs="+"})