-
In Telescope, I've bound some keys to toggle between -- switch to live_grep
["<m-r>"] = function(prompt_bufnr)
local picker = state.get_current_picker(prompt_bufnr)
local prompt = picker:_get_prompt()
actions.close(prompt_bufnr)
builtin.live_grep({ cwd = picker.cwd, default_text = prompt, debounce = 100 })
end Is it possible to do this in snacks.picker? To make it like what I have here, I would need:
|
Beta Was this translation helpful? Give feedback.
Answered by
synic
Jan 16, 2025
Replies: 2 comments 3 replies
-
This is how I did this, again, not sure if I'm doing this correctly, but hey, it works. sources = {
files = {
actions = {
switch_to_grep = function(picker, _)
local snacks = require("snacks")
local pattern = picker.input.filter.pattern or picker.input.filter.search
local cwd = picker.input.filter.cwd
picker:close()
---@diagnostic disable-next-line: missing-fields
snacks.picker.grep({ cwd = cwd, search = pattern })
end,
},
win = {
input = {
keys = {
["<a-r>"] = { "switch_to_grep", desc = "Switch to grep", mode = { "i", "n" } },
},
},
},
},
grep = {
actions = {
switch_to_files = function(picker, _)
local snacks = require("snacks")
local pattern = picker.input.filter.search or picker.input.filter.pattern
local cwd = picker.input.filter.cwd
picker:close()
---@diagnostic disable-next-line: missing-fields
snacks.picker.files({ cwd = cwd, pattern = pattern })
end,
},
win = {
input = {
keys = {
["<a-r>"] = { "switch_to_files", desc = "Switch to files", mode = { "i", "n" } },
},
},
},
},
} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
synic
-
@synic I modified this a little to support this for other pickers as well. M.switch_to_grep = nil
M.grep_alt_picker = nil
---@param picker snacks.Picker
local function switch_to_grep(picker, _)
local picker_type = picker.opts.source
local allowed_pickers = { "files", "buffers", "recent", "smart", "grep" }
if not vim.tbl_contains(allowed_pickers, picker_type) then
Snacks.notify.warn("Switching to grep is not supported for `" .. picker_type .. "`", { title = "Snacks Picker" })
return
end
if picker_type == "grep" then
M.switch_to_grep = false
M.grep_alt_picker = M.grep_alt_picker or "files"
else
M.switch_to_grep = true
if picker_type == "recent_files" then
picker_type = "recent"
end
M.grep_alt_picker = picker_type
end
local snacks = require("snacks")
local cwd = picker.input.filter.cwd
picker:close()
if M.switch_to_grep then
local pattern = picker.input.filter.pattern or ""
---@diagnostic disable-next-line: missing-fields
snacks.picker.grep({ cwd = cwd, search = pattern })
else
local pattern = picker.input.filter.search or ""
---@diagnostic disable-next-line: missing-fields
snacks.picker.pick(M.grep_alt_picker, { cwd = cwd, pattern = pattern })
end
end And then you map it to supported pickers {
smart = {
actions = {
switch_grep_files = function(picker, _)
switch_to_grep(picker, _)
end,
},
win = {
input = {
keys = {
["<c-k>"] = { "switch_grep_files", desc = "Switch to grep", mode = { "i", "n" } },
},
},
},
},
recent = {
actions = {
switch_grep_files = function(picker, _)
switch_to_grep(picker, _)
end,
},
win = {
input = {
keys = {
["<c-k>"] = { "switch_grep_files", desc = "Switch to grep", mode = { "i", "n" } },
},
},
},
},
buffers = {
actions = {
switch_grep_files = function(picker, _)
switch_to_grep(picker, _)
end,
},
win = {
input = {
keys = {
["<c-k>"] = { "switch_grep_files", desc = "Switch to grep", mode = { "i", "n" } },
},
},
},
},
files = {
-- live = true,
actions = {
switch_grep_files = function(picker, _)
switch_to_grep(picker, _)
end,
cd_up = function(picker, _)
cd_up(picker, _)
end,
},
win = {
input = {
keys = {
["<c-k>"] = { "switch_grep_files", desc = "Switch to grep", mode = { "i", "n" } },
["<c-u>"] = { "cd_up", desc = "cd_up", mode = { "i", "n" } },
},
},
},
},
grep = {
actions = {
switch_grep_files = function(picker, _)
switch_to_grep(picker, _)
end,
cd_up = function(picker, _)
cd_up(picker, _)
end,
},
win = {
input = {
keys = {
["<c-k>"] = { "switch_grep_files", desc = "Switch to grep", mode = { "i", "n" } },
["<c-u>"] = { "cd_up", desc = "cd_up", mode = { "i", "n" } },
},
},
},
},
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is how I did this, again, not sure if I'm doing this correctly, but hey, it works.