feat(grep_string): invert_match for search="", removing empty lines (#2040)

This commit is contained in:
Simon Hauser
2022-07-06 20:38:51 +02:00
committed by GitHub
parent b44e657986
commit 30f2a367f2
2 changed files with 46 additions and 21 deletions

View File

@@ -126,43 +126,43 @@ end
files.grep_string = function(opts) files.grep_string = function(opts)
-- TODO: This should probably check your visual selection as well, if you've got one -- TODO: This should probably check your visual selection as well, if you've got one
local vimgrep_arguments = opts.vimgrep_arguments or conf.vimgrep_arguments
local search_dirs = opts.search_dirs
local word = opts.search or vim.fn.expand "<cword>"
local search = opts.use_regex and word or escape_chars(word)
local word_match = opts.word_match
local grep_open_files = opts.grep_open_files
opts.entry_maker = opts.entry_maker or make_entry.gen_from_vimgrep(opts)
opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) or vim.loop.cwd() opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) or vim.loop.cwd()
local vimgrep_arguments = vim.F.if_nil(opts.vimgrep_arguments, conf.vimgrep_arguments)
local title_word = word:gsub("\n", "\\n") local word = vim.F.if_nil(opts.search, vim.fn.expand "<cword>")
local search = opts.use_regex and word or escape_chars(word)
local additional_args = {} local additional_args = {}
if opts.additional_args ~= nil and type(opts.additional_args) == "function" then if opts.additional_args ~= nil and type(opts.additional_args) == "function" then
additional_args = opts.additional_args(opts) additional_args = opts.additional_args(opts)
end end
if search == "" then
search = { "-v", "--", "^[[:space:]]*$" }
opts.__inverted = true
else
search = { "--", search }
end
local args = flatten { local args = flatten {
vimgrep_arguments, vimgrep_arguments,
additional_args, additional_args,
word_match, opts.word_match,
"--",
search, search,
} }
if grep_open_files then if opts.grep_open_files then
for _, file in ipairs(get_open_filelist(grep_open_files, opts.cwd)) do for _, file in ipairs(get_open_filelist(opts.grep_open_files, opts.cwd)) do
table.insert(args, file) table.insert(args, file)
end end
elseif search_dirs then elseif opts.search_dirs then
for _, path in ipairs(search_dirs) do for _, path in ipairs(opts.search_dirs) do
table.insert(args, vim.fn.expand(path)) table.insert(args, vim.fn.expand(path))
end end
end end
opts.entry_maker = opts.entry_maker or make_entry.gen_from_vimgrep(opts)
pickers.new(opts, { pickers.new(opts, {
prompt_title = "Find Word (" .. title_word .. ")", prompt_title = "Find Word (" .. word:gsub("\n", "\\n") .. ")",
finder = finders.new_oneshot_job(args, opts), finder = finders.new_oneshot_job(args, opts),
previewer = conf.grep_previewer(opts), previewer = conf.grep_previewer(opts),
sorter = conf.generic_sorter(opts), sorter = conf.generic_sorter(opts),

View File

@@ -205,7 +205,7 @@ do
} }
-- Gets called only once to parse everything out for the vimgrep, after that looks up directly. -- Gets called only once to parse everything out for the vimgrep, after that looks up directly.
local parse = function(t) local parse_with_col = function(t)
local _, _, filename, lnum, col, text = string.find(t.value, [[(..-):(%d+):(%d+):(.*)]]) local _, _, filename, lnum, col, text = string.find(t.value, [[(..-):(%d+):(%d+):(.*)]])
local ok local ok
@@ -227,11 +227,32 @@ do
return { filename, lnum, col, text } return { filename, lnum, col, text }
end end
function make_entry.gen_from_vimgrep(opts) local parse_without_col = function(t)
local mt_vimgrep_entry local _, _, filename, lnum, text = string.find(t.value, [[(..-):(%d+):(.*)]])
local ok
ok, lnum = pcall(tonumber, lnum)
if not ok then
lnum = nil
end
t.filename = filename
t.lnum = lnum
t.col = nil
t.text = text
return { filename, lnum, nil, text }
end
function make_entry.gen_from_vimgrep(opts)
opts = opts or {} opts = opts or {}
local mt_vimgrep_entry
local parse = parse_with_col
if opts.__inverted == true then
parse = parse_without_col
end
local disable_devicons = opts.disable_devicons local disable_devicons = opts.disable_devicons
local disable_coordinates = opts.disable_coordinates local disable_coordinates = opts.disable_coordinates
local only_sort_text = opts.only_sort_text local only_sort_text = opts.only_sort_text
@@ -279,7 +300,11 @@ do
local coordinates = "" local coordinates = ""
if not disable_coordinates then if not disable_coordinates then
if entry.col then
coordinates = string.format("%s:%s:", entry.lnum, entry.col) coordinates = string.format("%s:%s:", entry.lnum, entry.col)
else
coordinates = string.format("%s:", entry.lnum)
end
end end
local display, hl_group = utils.transform_devicons( local display, hl_group = utils.transform_devicons(