Skip to content

Commit 726bf89

Browse files
committed
implementing snack picker
1 parent 14e0427 commit 726bf89

File tree

3 files changed

+122
-1
lines changed

3 files changed

+122
-1
lines changed

lua/obsidian/config.lua

+1
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ config.Picker = {
343343
telescope = "telescope.nvim",
344344
fzf_lua = "fzf-lua",
345345
mini = "mini.pick",
346+
snacks = "snacks.pick",
346347
}
347348

348349
---@class obsidian.config.PickerOpts

lua/obsidian/pickers/_snacks.lua

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
local snacks_picker = require "snacks.picker"
2+
local snacks = require "snacks"
3+
4+
5+
local Path = require "obsidian.path"
6+
local abc = require "obsidian.abc"
7+
local Picker = require "obsidian.pickers.picker"
8+
9+
---@param entry string
10+
---@return string
11+
local function clean_path(entry)
12+
if type(entry) == "string" then
13+
local path_end = assert(string.find(entry, ":", 1, true))
14+
return string.sub(entry, 1, path_end - 1)
15+
end
16+
vim.notify("entry: " .. table.concat(vim.tbl_keys(entry), ", "))
17+
return ""
18+
end
19+
20+
---@class obsidian.pickers.SnacksPicker : obsidian.Picker
21+
local SnacksPicker = abc.new_class({
22+
---@diagnostic disable-next-line: unused-local
23+
__tostring = function(self)
24+
return "SnacksPicker()"
25+
end,
26+
}, Picker)
27+
28+
SnacksPicker.find_files = function(self, opts)
29+
opts = opts and opts or {}
30+
31+
---@type obsidian.Path
32+
local dir = opts.dir and Path:new(opts.dir) or self.client.dir
33+
34+
local result = snacks_picker.pick("files", {
35+
cwd = tostring(dir),
36+
})
37+
38+
if result and opts.callback then
39+
local path = clean_path(result)
40+
opts.callback(tostring(dir / path))
41+
end
42+
end
43+
44+
SnacksPicker.grep = function(self, opts)
45+
opts = opts and opts or {}
46+
47+
---@type obsidian.Path
48+
local dir = opts.dir and Path:new(opts.dir) or self.client.dir
49+
50+
local result = snacks_picker.pick("grep", {
51+
cwd = tostring(dir),
52+
})
53+
54+
if result and opts.callback then
55+
local path = clean_path(result)
56+
opts.callback(tostring(dir / path))
57+
end
58+
end
59+
60+
SnacksPicker.pick = function(self, values, opts)
61+
62+
self.calling_bufnr = vim.api.nvim_get_current_buf()
63+
64+
local buf = opts.buf or vim.api.nvim_get_current_buf()
65+
66+
opts = opts and opts or {}
67+
68+
local entries = {}
69+
for _, value in ipairs(values) do
70+
if type(value) == "string" then
71+
table.insert(entries, {
72+
text = value,
73+
value = value,
74+
})
75+
elseif value.valid ~= false then
76+
local name = self:_make_display(value)
77+
table.insert(entries, {
78+
text = name,
79+
buf = buf,
80+
filename = value.filename,
81+
value = value.value,
82+
pos = { value.lnum, value.col },
83+
})
84+
end
85+
end
86+
87+
snacks_picker({
88+
tilte = opts.prompt_title,
89+
items = entries,
90+
layout = {
91+
preview = false
92+
},
93+
format = function(item, _)
94+
local ret = {}
95+
local a = snacks_picker.util.align
96+
ret[#ret + 1] = { a(item.text, 20) }
97+
return ret
98+
end,
99+
confirm = function(picker, item)
100+
picker:close()
101+
if item then
102+
if opts.callback then
103+
opts.callback(item.value)
104+
elseif item then
105+
vim.schedule(function()
106+
if item["buf"] then
107+
vim.api.nvim_set_current_buf(item["buf"])
108+
end
109+
vim.api.nvim_win_set_cursor(0, {item["pos"][1], 0})
110+
end)
111+
end
112+
end
113+
end,
114+
-- sort = require("snacks.picker.sort").idx(),
115+
})
116+
end
117+
118+
return SnacksPicker

lua/obsidian/pickers/init.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ M.get = function(client, picker_name)
1313
if picker_name then
1414
picker_name = string.lower(picker_name)
1515
else
16-
for _, name in ipairs { PickerName.telescope, PickerName.fzf_lua, PickerName.mini } do
16+
for _, name in ipairs { PickerName.telescope, PickerName.fzf_lua, PickerName.mini, PickerName.snacks } do
1717
local ok, res = pcall(M.get, client, name)
1818
if ok then
1919
return res
@@ -28,6 +28,8 @@ M.get = function(client, picker_name)
2828
return require("obsidian.pickers._mini").new(client)
2929
elseif picker_name == string.lower(PickerName.fzf_lua) then
3030
return require("obsidian.pickers._fzf").new(client)
31+
elseif picker_name == string.lower(PickerName.snacks) then
32+
return require("obsidian.pickers._snacks").new(client)
3133
elseif picker_name then
3234
error("not implemented for " .. picker_name)
3335
end

0 commit comments

Comments
 (0)