feat: allow table as additional args in live grep and grep string (#2139)

This commit is contained in:
Gutyina Gergő
2022-10-24 08:44:13 +02:00
committed by GitHub
parent 286628d9f2
commit 5c7db4055d
3 changed files with 60 additions and 50 deletions

View File

@@ -772,28 +772,28 @@ builtin.live_grep({opts}) *telescope.builtin.live_grep()*
{opts} (table) options to pass to the picker {opts} (table) options to pass to the picker
Options: ~ Options: ~
{cwd} (string) root dir to search from {cwd} (string) root dir to search from
(default: cwd, use (default: cwd, use
utils.buffer_dir() to search utils.buffer_dir() to search
relative to open buffer) relative to open buffer)
{grep_open_files} (boolean) if true, restrict search to open {grep_open_files} (boolean) if true, restrict search to
files only, mutually exclusive open files only, mutually
with `search_dirs` exclusive with `search_dirs`
{search_dirs} (table) directory/directories/files to {search_dirs} (table) directory/directories/files to
search, mutually exclusive with search, mutually exclusive
`grep_open_files` with `grep_open_files`
{glob_pattern} (string|table) argument to be used with {glob_pattern} (string|table) argument to be used with
`--glob`, e.g. "*.toml", can use `--glob`, e.g. "*.toml", can
the opposite "!*.toml" use the opposite "!*.toml"
{type_filter} (string) argument to be used with {type_filter} (string) argument to be used with
`--type`, e.g. "rust", see `rg `--type`, e.g. "rust", see `rg
--type-list` --type-list`
{additional_args} (function) function(opts) which returns a {additional_args} (function|table) additional arguments to be
table of additional arguments to passed on. Can be fn(opts) ->
be passed on tbl
{max_results} (number) define a upper result value {max_results} (number) define a upper result value
{disable_coordinates} (boolean) don't show the line & row {disable_coordinates} (boolean) don't show the line & row
numbers (default: false) numbers (default: false)
builtin.grep_string({opts}) *telescope.builtin.grep_string()* builtin.grep_string({opts}) *telescope.builtin.grep_string()*
@@ -804,28 +804,30 @@ builtin.grep_string({opts}) *telescope.builtin.grep_string()*
{opts} (table) options to pass to the picker {opts} (table) options to pass to the picker
Options: ~ Options: ~
{cwd} (string) root dir to search from (default: {cwd} (string) root dir to search from
cwd, use utils.buffer_dir() to (default: cwd, use
search relative to open buffer) utils.buffer_dir() to search
{search} (string) the query to search relative to open buffer)
{grep_open_files} (boolean) if true, restrict search to open {search} (string) the query to search
files only, mutually exclusive with {grep_open_files} (boolean) if true, restrict search to
`search_dirs` open files only, mutually
{search_dirs} (table) directory/directories/files to exclusive with `search_dirs`
search, mutually exclusive with {search_dirs} (table) directory/directories/files to
`grep_open_files` search, mutually exclusive
{use_regex} (boolean) if true, special characters won't be with `grep_open_files`
escaped, allows for using regex {use_regex} (boolean) if true, special characters
(default: false) won't be escaped, allows for
{word_match} (string) can be set to `-w` to enable exact using regex (default: false)
word matches {word_match} (string) can be set to `-w` to enable
{additional_args} (function) function(opts) which returns a table exact word matches
of additional arguments to be passed {additional_args} (function|table) additional arguments to be
on passed on. Can be fn(opts) ->
{disable_coordinates} (boolean) don't show the line and row numbers tbl
(default: false) {disable_coordinates} (boolean) don't show the line and row
{only_sort_text} (boolean) only sort the text, not the file, numbers (default: false)
line or row (default: false) {only_sort_text} (boolean) only sort the text, not the
file, line or row (default:
false)
builtin.find_files({opts}) *telescope.builtin.find_files()* builtin.find_files({opts}) *telescope.builtin.find_files()*

View File

@@ -76,8 +76,12 @@ files.live_grep = function(opts)
end end
local additional_args = {} local additional_args = {}
if opts.additional_args ~= nil and type(opts.additional_args) == "function" then if opts.additional_args ~= nil then
additional_args = opts.additional_args(opts) if type(opts.additional_args) == "function" then
additional_args = opts.additional_args(opts)
elseif type(opts.additional_args) == "table" then
additional_args = opts.additional_args
end
end end
if opts.type_filter then if opts.type_filter then
@@ -134,8 +138,12 @@ files.grep_string = function(opts)
local search = opts.use_regex and word or escape_chars(word) 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 then
additional_args = opts.additional_args(opts) if type(opts.additional_args) == "function" then
additional_args = opts.additional_args(opts)
elseif type(opts.additional_args) == "table" then
additional_args = opts.additional_args
end
end end
if search == "" then if search == "" then

View File

@@ -52,7 +52,7 @@ end
---@field search_dirs table: directory/directories/files to search, mutually exclusive with `grep_open_files` ---@field search_dirs table: directory/directories/files to search, mutually exclusive with `grep_open_files`
---@field glob_pattern string|table: argument to be used with `--glob`, e.g. "*.toml", can use the opposite "!*.toml" ---@field glob_pattern string|table: argument to be used with `--glob`, e.g. "*.toml", can use the opposite "!*.toml"
---@field type_filter string: argument to be used with `--type`, e.g. "rust", see `rg --type-list` ---@field type_filter string: argument to be used with `--type`, e.g. "rust", see `rg --type-list`
---@field additional_args function: function(opts) which returns a table of additional arguments to be passed on ---@field additional_args function|table: additional arguments to be passed on. Can be fn(opts) -> tbl
---@field max_results number: define a upper result value ---@field max_results number: define a upper result value
---@field disable_coordinates boolean: don't show the line & row numbers (default: false) ---@field disable_coordinates boolean: don't show the line & row numbers (default: false)
builtin.live_grep = require_on_exported_call("telescope.builtin.__files").live_grep builtin.live_grep = require_on_exported_call("telescope.builtin.__files").live_grep
@@ -65,7 +65,7 @@ builtin.live_grep = require_on_exported_call("telescope.builtin.__files").live_g
---@field search_dirs table: directory/directories/files to search, mutually exclusive with `grep_open_files` ---@field search_dirs table: directory/directories/files to search, mutually exclusive with `grep_open_files`
---@field use_regex boolean: if true, special characters won't be escaped, allows for using regex (default: false) ---@field use_regex boolean: if true, special characters won't be escaped, allows for using regex (default: false)
---@field word_match string: can be set to `-w` to enable exact word matches ---@field word_match string: can be set to `-w` to enable exact word matches
---@field additional_args function: function(opts) which returns a table of additional arguments to be passed on ---@field additional_args function|table: additional arguments to be passed on. Can be fn(opts) -> tbl
---@field disable_coordinates boolean: don't show the line and row numbers (default: false) ---@field disable_coordinates boolean: don't show the line and row numbers (default: false)
---@field only_sort_text boolean: only sort the text, not the file, line or row (default: false) ---@field only_sort_text boolean: only sort the text, not the file, line or row (default: false)
builtin.grep_string = require_on_exported_call("telescope.builtin.__files").grep_string builtin.grep_string = require_on_exported_call("telescope.builtin.__files").grep_string