-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnacks.lua
More file actions
74 lines (63 loc) · 1.81 KB
/
snacks.lua
File metadata and controls
74 lines (63 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
local ext = require("peekstack.extensions")
local notify = require("peekstack.util.notify")
local M = {}
---@param item table
---@param opts? table
local function push_from_snacks(item, opts)
if not item then
return
end
local filename = item.file
if filename and item.cwd and vim.fn.fnamemodify(filename, ":p") ~= filename then
filename = item.cwd .. "/" .. filename
end
-- snacks pos is {1-based line, 0-based col}; push_entry expects 1-based col
local col = item.pos and (item.pos[2] + 1) or nil
ext.push_entry({
filename = filename,
lnum = item.pos and item.pos[1],
col = col,
}, opts)
end
---@param snacks_picker string
---@param provider string
---@param opts? table
local function open_picker(snacks_picker, provider, opts)
opts = opts or {}
local ok, snacks = pcall(require, "snacks.picker")
if not ok then
notify.warn("snacks.nvim not available")
return
end
local fn = snacks[snacks_picker]
if not fn then
notify.warn("snacks.picker." .. snacks_picker .. " not found")
return
end
local push_opts = { provider = provider, mode = opts.mode }
fn(vim.tbl_extend("force", opts, {
confirm = function(picker, item)
picker:close()
push_from_snacks(item, push_opts)
end,
}))
end
function M.push_file(opts)
open_picker("files", "extension.file", opts)
end
function M.push_grep(opts)
open_picker("grep", "extension.grep", opts)
end
function M.push_lsp_references(opts)
open_picker("lsp_references", "extension.lsp_references", opts)
end
--- Generic action for use in custom snacks picker configurations.
---@param picker table
---@param item table
---@param opts? { provider?: string, mode?: string }
local function push_action(picker, item, opts)
picker:close()
push_from_snacks(item, opts)
end
M.actions = { push = push_action }
return M