fix: rg invert and files-with-matches res line parsing (#2208)

This commit is contained in:
Simon Hauser
2022-10-25 21:22:17 +02:00
committed by GitHub
parent 5c7db4055d
commit 9cf465894a
2 changed files with 49 additions and 10 deletions

View File

@@ -59,6 +59,31 @@ local get_open_filelist = function(grep_open_files, cwd)
return filelist return filelist
end end
local opts_contain_invert = function(args)
local invert = false
local files_with_matches = false
for _, v in ipairs(args) do
if v == "--invert-match" then
invert = true
elseif v == "--files-with-matches" or v == "--files-without-match" then
files_with_matches = true
end
if #v >= 2 and v:sub(1, 1) == "-" and v:sub(2, 2) ~= "-" then
for i = 2, #v do
local vi = v:sub(i, i)
if vi == "v" then
invert = true
elseif vi == "l" then
files_with_matches = true
end
end
end
end
return invert, files_with_matches
end
-- Special keys: -- Special keys:
-- opts.search_dirs -- list of directory to search in -- opts.search_dirs -- list of directory to search in
-- opts.grep_open_files -- boolean to restrict search to open files -- opts.grep_open_files -- boolean to restrict search to open files
@@ -96,9 +121,10 @@ files.live_grep = function(opts)
end end
end end
local live_grepper = finders.new_job(function(prompt) local args = flatten { vimgrep_arguments, additional_args }
-- TODO: Probably could add some options for smart case and whatever else rg offers. opts.__inverted, opts.__matches = opts_contain_invert(args)
local live_grepper = finders.new_job(function(prompt)
if not prompt or prompt == "" then if not prompt or prompt == "" then
return nil return nil
end end
@@ -111,7 +137,7 @@ files.live_grep = function(opts)
search_list = search_dirs search_list = search_dirs
end end
return flatten { vimgrep_arguments, additional_args, "--", prompt, search_list } return flatten { args, "--", prompt, search_list }
end, opts.entry_maker or make_entry.gen_from_vimgrep(opts), opts.max_results, opts.cwd) end, opts.entry_maker or make_entry.gen_from_vimgrep(opts), opts.max_results, opts.cwd)
pickers pickers
@@ -148,7 +174,6 @@ files.grep_string = function(opts)
if search == "" then if search == "" then
search = { "-v", "--", "^[[:space:]]*$" } search = { "-v", "--", "^[[:space:]]*$" }
opts.__inverted = true
else else
search = { "--", search } search = { "--", search }
end end
@@ -159,6 +184,7 @@ files.grep_string = function(opts)
opts.word_match, opts.word_match,
search, search,
} }
opts.__inverted, opts.__matches = opts_contain_invert(args)
if opts.grep_open_files then if opts.grep_open_files then
for _, file in ipairs(get_open_filelist(opts.grep_open_files, opts.cwd)) do for _, file in ipairs(get_open_filelist(opts.grep_open_files, opts.cwd)) do

View File

@@ -244,12 +244,23 @@ do
return { filename, lnum, nil, text } return { filename, lnum, nil, text }
end end
local parse_only_filename = function(t)
t.filename = t.value
t.lnum = nil
t.col = nil
t.text = ""
return { t.filename, nil, nil, "" }
end
function make_entry.gen_from_vimgrep(opts) function make_entry.gen_from_vimgrep(opts)
opts = opts or {} opts = opts or {}
local mt_vimgrep_entry local mt_vimgrep_entry
local parse = parse_with_col local parse = parse_with_col
if opts.__inverted == true then if opts.__matches == true then
parse = parse_only_filename
elseif opts.__inverted == true then
parse = parse_without_col parse = parse_without_col
end end
@@ -290,7 +301,7 @@ do
end end
end end
local display_string = "%s:%s%s" local display_string = "%s%s%s"
mt_vimgrep_entry = { mt_vimgrep_entry = {
cwd = vim.fn.expand(opts.cwd or vim.loop.cwd()), cwd = vim.fn.expand(opts.cwd or vim.loop.cwd()),
@@ -300,10 +311,12 @@ do
local coordinates = "" local coordinates = ""
if not disable_coordinates then if not disable_coordinates then
if entry.lnum then
if entry.col then if entry.col then
coordinates = string.format("%s:%s:", entry.lnum, entry.col) coordinates = string.format(":%s:%s:", entry.lnum, entry.col)
else else
coordinates = string.format("%s:", entry.lnum) coordinates = string.format(":%s:", entry.lnum)
end
end end
end end