Skip to content

Commit

Permalink
feat(picker): added opts.rtp (bool) to find/grep over files in the …
Browse files Browse the repository at this point in the history
…rtp. See #680
  • Loading branch information
folke committed Jan 23, 2025
1 parent 3fb1110 commit 9d5d3bd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lua/snacks/picker/config/sources.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ M.diagnostics_buffer = {
---@field follow? boolean follow symlinks
---@field exclude? string[] exclude patterns
---@field args? string[] additional arguments
---@field rtp? boolean search in runtimepath
M.files = {
finder = "files",
format = "file",
Expand Down Expand Up @@ -221,6 +222,7 @@ M.git_diff = {
---@field need_search? boolean require a search pattern
---@field exclude? string[] exclude patterns
---@field args? string[] additional arguments
---@field rtp? boolean search in runtimepath
M.grep = {
finder = "grep",
format = "file",
Expand Down
12 changes: 9 additions & 3 deletions lua/snacks/picker/source/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@ local function get_cmd(opts, filter)
end

-- dirs
if opts.dirs and #opts.dirs > 0 then
local dirs = vim.tbl_map(vim.fs.normalize, opts.dirs) ---@type string[]
local dirs = opts.dirs or {}
if opts.rtp then
vim.list_extend(dirs, Snacks.picker.util.rtp())
end
if #dirs > 0 then
dirs = vim.tbl_map(vim.fs.normalize, dirs) ---@type string[]
if is_fd and not pattern then
args[#args + 1] = "."
end
Expand All @@ -103,7 +107,9 @@ end
---@param opts snacks.picker.files.Config
---@type snacks.picker.finder
function M.files(opts, ctx)
local cwd = not (opts.dirs and #opts.dirs > 0) and vim.fs.normalize(opts and opts.cwd or uv.cwd() or ".") or nil
local cwd = not (opts.rtp or (opts.dirs and #opts.dirs > 0))
and vim.fs.normalize(opts and opts.cwd or uv.cwd() or ".")
or nil
local cmd, args = get_cmd(opts, ctx.filter)
return require("snacks.picker.source.proc").proc({
opts,
Expand Down
8 changes: 5 additions & 3 deletions lua/snacks/picker/source/grep.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ local function get_cmd(opts, filter)
paths[#paths + 1] = name
end
end
elseif opts.dirs and #opts.dirs > 0 then
paths = opts.dirs or {}
end
vim.list_extend(paths, opts.dirs or {})
if opts.rtp then
vim.list_extend(paths, Snacks.picker.util.rtp())
end

-- dirs
Expand All @@ -104,7 +106,7 @@ function M.grep(opts, ctx)
if opts.need_search ~= false and ctx.filter.search == "" then
return function() end
end
local absolute = (opts.dirs and #opts.dirs > 0) or opts.buffers
local absolute = (opts.dirs and #opts.dirs > 0) or opts.buffers or opts.rtp
local cwd = not absolute and vim.fs.normalize(opts and opts.cwd or uv.cwd() or ".") or nil
local cmd, args = get_cmd(opts, ctx.filter)
return require("snacks.picker.source.proc").proc({
Expand Down
10 changes: 10 additions & 0 deletions lua/snacks/picker/util/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ function M.title(str)
)
end

function M.rtp()
local ret = {} ---@type string[]
vim.list_extend(ret, vim.api.nvim_get_runtime_file("", true))
if package.loaded.lazy then
local extra = require("lazy.core.util").get_unloaded_rtp("")
vim.list_extend(ret, extra)
end
return ret
end

---@param str string
---@return string text, string[] args
function M.parse(str)
Expand Down

0 comments on commit 9d5d3bd

Please sign in to comment.