Skip to content

Commit 62c546a

Browse files
committed
refactor(pickers): improve snacks picker implementation and error handling
- Remove unused debug helper functions - Add proper type annotations for picker options - Improve action mapping logic with notes_mappings - Fix path handling in file and grep operations - Add debug logging for better troubleshooting - Enhance callback handling for picker actions
1 parent 1e1c912 commit 62c546a

File tree

1 file changed

+48
-71
lines changed

1 file changed

+48
-71
lines changed

lua/obsidian/pickers/_snacks.lua

+48-71
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,29 @@
11
local snacks_picker = require "snacks.picker"
2-
local snacks = require "snacks"
3-
42

53
local Path = require "obsidian.path"
64
local abc = require "obsidian.abc"
75
local Picker = require "obsidian.pickers.picker"
86

9-
10-
function print_table(t, indent)
11-
indent = indent or 0
12-
local padding = string.rep(" ", indent)
13-
14-
for key, value in pairs(t) do
15-
if type(value) == "table" then
16-
print(padding .. tostring(key) .. " = {")
17-
print_table(value, indent + 1)
18-
print(padding .. "}")
19-
else
20-
print(padding .. tostring(key) .. " = " .. tostring(value))
21-
end
22-
end
23-
end
24-
25-
function table_to_string(t, indent)
26-
if type(t) ~= "table" then return tostring(t) end
27-
28-
indent = indent or 0
29-
local padding = string.rep(" ", indent)
30-
local parts = {}
31-
32-
for k, v in pairs(t) do
33-
local key = type(k) == "number" and "[" .. k .. "]" or k
34-
local value
35-
if type(v) == "table" then
36-
value = "{\n" .. table_to_string(v, indent + 1) .. padding .. "}"
37-
elseif type(v) == "string" then
38-
value = string.format("%q", v)
39-
else
40-
value = tostring(v)
41-
end
42-
parts[#parts + 1] = padding .. key .. " = " .. value
43-
end
44-
45-
return table.concat(parts, ",\n") .. "\n"
7+
local function debug_once(msg, ...)
8+
-- vim.notify(msg .. vim.inspect(...))
469
end
4710

48-
---@param entry string
49-
---@return string
50-
local function clean_path(entry)
51-
if type(entry) == "string" then
52-
local path_end = assert(string.find(entry, ":", 1, true))
53-
return string.sub(entry, 1, path_end - 1)
54-
end
55-
vim.notify("entry: " .. table.concat(vim.tbl_keys(entry), ", "))
56-
return ""
57-
end
58-
59-
local function map_actions(action)
60-
if type(action) == "table" then
11+
---@param mapping table
12+
---@return table
13+
local function notes_mappings(mapping)
14+
if type(mapping) == "table" then
6115
opts = { win = { input = { keys = {} } }, actions = {} };
62-
for k, v in pairs(action) do
16+
for k, v in pairs(mapping) do
6317
local name = string.gsub(v.desc, " ", "_")
6418
opts.win.input.keys = {
6519
[k] = { name, mode = { "n", "i" }, desc = v.desc }
6620
}
6721
opts.actions[name] = function(picker, item)
68-
vim.notify("action item: " .. table_to_string(item))
69-
v.callback({args: item.text})
22+
debug_once("mappings :", item)
23+
picker:close()
24+
vim.schedule(function()
25+
v.callback(item.value or item._path)
26+
end)
7027
end
7128
end
7229
return opts
@@ -82,46 +39,60 @@ local SnacksPicker = abc.new_class({
8239
end,
8340
}, Picker)
8441

42+
---@param opts obsidian.PickerFindOpts|? Options.
8543
SnacksPicker.find_files = function(self, opts)
8644
opts = opts or {}
8745

8846
---@type obsidian.Path
89-
local dir = opts.dir and Path:new(opts.dir) or self.client.dir
47+
local dir = opts.dir.filename and Path:new(opts.dir.filename) or self.client.dir
48+
49+
local map = vim.tbl_deep_extend("force", {},
50+
notes_mappings(opts.selection_mappings))
9051

9152
local pick_opts = vim.tbl_extend("force", map or {}, {
9253
source = "files",
9354
title = opts.prompt_title,
94-
cwd = opts.dir.filename,
55+
cwd = tostring(dir),
9556
confirm = function(picker, item, action)
9657
picker:close()
9758
if item then
9859
if opts.callback then
60+
debug_once("find files callback: ", item)
9961
opts.callback(item._path)
10062
else
63+
debug_once("find files jump: ", item)
10164
snacks_picker.actions.jump(picker, item, action)
10265
end
10366
end
10467
end,
10568
})
106-
snacks_picker.pick(pick_opts)
69+
local t = snacks_picker.pick(pick_opts)
10770
end
10871

109-
SnacksPicker.grep = function(self, opts, action)
72+
---@param opts obsidian.PickerGrepOpts|? Options.
73+
SnacksPicker.grep = function(self, opts)
11074
opts = opts or {}
11175

76+
debug_once("grep opts : ", opts)
77+
11278
---@type obsidian.Path
11379
local dir = opts.dir and Path:new(opts.dir) or self.client.dir
11480

81+
local map = vim.tbl_deep_extend("force", {},
82+
notes_mappings(opts.selection_mappings))
83+
11584
local pick_opts = vim.tbl_extend("force", map or {}, {
11685
source = "grep",
11786
title = opts.prompt_title,
118-
cwd = opts.dir.filename,
87+
cwd = dir,
11988
confirm = function(picker, item, action)
12089
picker:close()
12190
if item then
12291
if opts.callback then
123-
opts.callback(item._path)
92+
debug_once("grep callback: ", item)
93+
opts.callback(item._path or item.filename)
12494
else
95+
debug_once("grep jump: ", item)
12596
snacks_picker.actions.jump(picker, item, action)
12697
end
12798
end
@@ -130,16 +101,17 @@ SnacksPicker.grep = function(self, opts, action)
130101
snacks_picker.pick(pick_opts)
131102
end
132103

104+
---@param values string[]|obsidian.PickerEntry[]
105+
---@param opts obsidian.PickerPickOpts|? Options.
106+
---@diagnostic disable-next-line: unused-local
133107
SnacksPicker.pick = function(self, values, opts)
134108
self.calling_bufnr = vim.api.nvim_get_current_buf()
135109

136110
opts = opts or {}
137111

138-
local buf = opts.buf or vim.api.nvim_get_current_buf()
112+
debug_once("pick opts: ", opts)
139113

140-
-- local map = vim.tbl_deep_extend("force", {},
141-
-- map_actions(opts.selection_mappings),
142-
-- map_actions(opts.query_mappings))
114+
local buf = opts.buf or vim.api.nvim_get_current_buf()
143115

144116
local entries = {}
145117
for _, value in ipairs(values) do
@@ -155,25 +127,30 @@ SnacksPicker.pick = function(self, values, opts)
155127
buf = buf,
156128
filename = value.filename,
157129
value = value.value,
158-
pos = { value.lnum, value.col },
130+
pos = { value.lnum, value.col or 0 },
159131
})
160132
end
161133
end
162134

135+
local map = vim.tbl_deep_extend("force", {},
136+
notes_mappings(opts.selection_mappings))
137+
163138
local pick_opts = vim.tbl_extend("force", map or {}, {
164139
tilte = opts.prompt_title,
165140
items = entries,
166141
layout = {
167142
preview = false
168143
},
169144
format = "text",
170-
confirm = function(picker, item)
145+
confirm = function(picker, item, action)
171146
picker:close()
172-
if item and opts.callback then
173-
if type(item) == "string" then
174-
opts.callback(item)
175-
else
147+
if item then
148+
if opts.callback then
149+
debug_once("pick callback: ", item)
176150
opts.callback(item.value)
151+
else
152+
debug_once("pick jump: ", item)
153+
snacks_picker.actions.jump(picker, item, action)
177154
end
178155
end
179156
end,

0 commit comments

Comments
 (0)