Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow table as additional args in live grep and grep string #2139

Merged
merged 4 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 46 additions & 44 deletions doc/telescope.txt
Original file line number Diff line number Diff line change
Expand Up @@ -772,28 +772,28 @@ builtin.live_grep({opts}) *telescope.builtin.live_grep()*
{opts} (table) options to pass to the picker

Options: ~
{cwd} (string) root dir to search from
(default: cwd, use
utils.buffer_dir() to search
relative to open buffer)
{grep_open_files} (boolean) if true, restrict search to open
files only, mutually exclusive
with `search_dirs`
{search_dirs} (table) directory/directories/files to
search, mutually exclusive with
`grep_open_files`
{glob_pattern} (string|table) argument to be used with
`--glob`, e.g. "*.toml", can use
the opposite "!*.toml"
{type_filter} (string) argument to be used with
`--type`, e.g. "rust", see `rg
--type-list`
{additional_args} (function) function(opts) which returns a
table of additional arguments to
be passed on
{max_results} (number) define a upper result value
{disable_coordinates} (boolean) don't show the line & row
numbers (default: false)
{cwd} (string) root dir to search from
(default: cwd, use
utils.buffer_dir() to search
relative to open buffer)
{grep_open_files} (boolean) if true, restrict search to
open files only, mutually
exclusive with `search_dirs`
{search_dirs} (table) directory/directories/files to
search, mutually exclusive
with `grep_open_files`
{glob_pattern} (string|table) argument to be used with
`--glob`, e.g. "*.toml", can
use the opposite "!*.toml"
{type_filter} (string) argument to be used with
`--type`, e.g. "rust", see `rg
--type-list`
{additional_args} (function|table) additional arguments to be
passed on. Can be fn(opts) ->
tbl
{max_results} (number) define a upper result value
{disable_coordinates} (boolean) don't show the line & row
numbers (default: false)


builtin.grep_string({opts}) *telescope.builtin.grep_string()*
Expand All @@ -804,28 +804,30 @@ builtin.grep_string({opts}) *telescope.builtin.grep_string()*
{opts} (table) options to pass to the picker

Options: ~
{cwd} (string) root dir to search from (default:
cwd, use utils.buffer_dir() to
search relative to open buffer)
{search} (string) the query to search
{grep_open_files} (boolean) if true, restrict search to open
files only, mutually exclusive with
`search_dirs`
{search_dirs} (table) directory/directories/files to
search, mutually exclusive with
`grep_open_files`
{use_regex} (boolean) if true, special characters won't be
escaped, allows for using regex
(default: false)
{word_match} (string) can be set to `-w` to enable exact
word matches
{additional_args} (function) function(opts) which returns a table
of additional arguments to be passed
on
{disable_coordinates} (boolean) don't show the line and row numbers
(default: false)
{only_sort_text} (boolean) only sort the text, not the file,
line or row (default: false)
{cwd} (string) root dir to search from
(default: cwd, use
utils.buffer_dir() to search
relative to open buffer)
{search} (string) the query to search
{grep_open_files} (boolean) if true, restrict search to
open files only, mutually
exclusive with `search_dirs`
{search_dirs} (table) directory/directories/files to
search, mutually exclusive
with `grep_open_files`
{use_regex} (boolean) if true, special characters
won't be escaped, allows for
using regex (default: false)
{word_match} (string) can be set to `-w` to enable
exact word matches
{additional_args} (function|table) additional arguments to be
passed on. Can be fn(opts) ->
tbl
{disable_coordinates} (boolean) don't show the line and row
numbers (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()*
Expand Down
16 changes: 12 additions & 4 deletions lua/telescope/builtin/__files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ files.live_grep = function(opts)
end

local additional_args = {}
if opts.additional_args ~= nil and type(opts.additional_args) == "function" then
additional_args = opts.additional_args(opts)
if opts.additional_args ~= nil then
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

if opts.type_filter then
Expand Down Expand Up @@ -134,8 +138,12 @@ files.grep_string = function(opts)
local search = opts.use_regex and word or escape_chars(word)

local additional_args = {}
if opts.additional_args ~= nil and type(opts.additional_args) == "function" then
additional_args = opts.additional_args(opts)
if opts.additional_args ~= nil then
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

if search == "" then
Expand Down
4 changes: 2 additions & 2 deletions lua/telescope/builtin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ end
---@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 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 disable_coordinates boolean: don't show the line & row numbers (default: false)
builtin.live_grep = require_on_exported_call("telescope.builtin.__files").live_grep
Expand All @@ -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 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 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 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
Expand Down