@@ -5,158 +5,155 @@ local abc = require "obsidian.abc"
55local Picker = require " obsidian.pickers.picker"
66
77local function debug_once (msg , ...)
8- -- vim.notify(msg .. vim.inspect(...))
8+ -- vim.notify(msg .. vim.inspect(...))
99end
1010
1111--- @param mapping table
1212--- @return table
1313local function notes_mappings (mapping )
14- if type (mapping ) == " table" then
15- opts = { win = { input = { keys = {} } }, actions = {} };
16- for k , v in pairs (mapping ) do
17- local name = string.gsub (v .desc , " " , " _" )
18- opts .win .input .keys = {
19- [k ] = { name , mode = { " n" , " i" }, desc = v .desc }
20- }
21- opts .actions [name ] = function (picker , item )
22- debug_once (" mappings :" , item )
23- picker :close ()
24- vim .schedule (function ()
25- v .callback (item .value or item ._path )
26- end )
27- end
28- end
29- return opts
14+ if type (mapping ) == " table" then
15+ local opts = { win = { input = { keys = {} } }, actions = {} }
16+ for k , v in pairs (mapping ) do
17+ local name = string.gsub (v .desc , " " , " _" )
18+ opts .win .input .keys = {
19+ [k ] = { name , mode = { " n" , " i" }, desc = v .desc },
20+ }
21+ opts .actions [name ] = function (picker , item )
22+ debug_once (" mappings :" , item )
23+ picker :close ()
24+ vim .schedule (function ()
25+ v .callback (item .value or item ._path )
26+ end )
27+ end
3028 end
31- return {}
29+ return opts
30+ end
31+ return {}
3232end
3333
3434--- @class obsidian.pickers.SnacksPicker : obsidian.Picker
3535local SnacksPicker = abc .new_class ({
36- --- @diagnostic disable-next-line : unused-local
37- __tostring = function (self )
38- return " SnacksPicker()"
39- end ,
36+ --- @diagnostic disable-next-line : unused-local
37+ __tostring = function (self )
38+ return " SnacksPicker()"
39+ end ,
4040}, Picker )
4141
4242--- @param opts obsidian.PickerFindOpts |? Options.
4343SnacksPicker .find_files = function (self , opts )
44- opts = opts or {}
45-
46- --- @type obsidian.Path
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 ))
51-
52- local pick_opts = vim .tbl_extend (" force" , map or {}, {
53- source = " files" ,
54- title = opts .prompt_title ,
55- cwd = tostring (dir ),
56- confirm = function (picker , item , action )
57- picker :close ()
58- if item then
59- if opts .callback then
60- debug_once (" find files callback: " , item )
61- opts .callback (item ._path )
62- else
63- debug_once (" find files jump: " , item )
64- snacks_picker .actions .jump (picker , item , action )
65- end
66- end
67- end ,
68- })
69- local t = snacks_picker .pick (pick_opts )
44+ opts = opts or {}
45+
46+ --- @type obsidian.Path
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" , {}, notes_mappings (opts .selection_mappings ))
50+
51+ local pick_opts = vim .tbl_extend (" force" , map or {}, {
52+ source = " files" ,
53+ title = opts .prompt_title ,
54+ cwd = tostring (dir ),
55+ confirm = function (picker , item , action )
56+ picker :close ()
57+ if item then
58+ if opts .callback then
59+ debug_once (" find files callback: " , item )
60+ opts .callback (item ._path )
61+ else
62+ debug_once (" find files jump: " , item )
63+ snacks_picker .actions .jump (picker , item , action )
64+ end
65+ end
66+ end ,
67+ })
68+ snacks_picker .pick (pick_opts )
7069end
7170
7271--- @param opts obsidian.PickerGrepOpts |? Options.
7372SnacksPicker .grep = function (self , opts )
74- opts = opts or {}
75-
76- debug_once (" grep opts : " , opts )
77-
78- --- @type obsidian.Path
79- local dir = opts .dir .filename and Path :new (opts .dir .filename ) or self .client .dir
80-
81- local map = vim .tbl_deep_extend (" force" , {},
82- notes_mappings (opts .selection_mappings ))
83-
84- local pick_opts = vim .tbl_extend (" force" , map or {}, {
85- source = " grep" ,
86- title = opts .prompt_title ,
87- cwd = tostring (dir ),
88- confirm = function (picker , item , action )
89- picker :close ()
90- if item then
91- if opts .callback then
92- debug_once (" grep callback: " , item )
93- opts .callback (item ._path or item .filename )
94- else
95- debug_once (" grep jump: " , item )
96- snacks_picker .actions .jump (picker , item , action )
97- end
98- end
99- end ,
100- })
101- snacks_picker .pick (pick_opts )
73+ opts = opts or {}
74+
75+ debug_once (" grep opts : " , opts )
76+
77+ --- @type obsidian.Path
78+ local dir = opts .dir .filename and Path :new (opts .dir .filename ) or self .client .dir
79+
80+ local map = vim .tbl_deep_extend (" force" , {}, notes_mappings (opts .selection_mappings ))
81+
82+ local pick_opts = vim .tbl_extend (" force" , map or {}, {
83+ source = " grep" ,
84+ title = opts .prompt_title ,
85+ cwd = tostring (dir ),
86+ confirm = function (picker , item , action )
87+ picker :close ()
88+ if item then
89+ if opts .callback then
90+ debug_once (" grep callback: " , item )
91+ opts .callback (item ._path or item .filename )
92+ else
93+ debug_once (" grep jump: " , item )
94+ snacks_picker .actions .jump (picker , item , action )
95+ end
96+ end
97+ end ,
98+ })
99+ snacks_picker .pick (pick_opts )
102100end
103101
104102--- @param values string[] | obsidian.PickerEntry[]
105103--- @param opts obsidian.PickerPickOpts |? Options.
106104--- @diagnostic disable-next-line : unused-local
107105SnacksPicker .pick = function (self , values , opts )
108- self .calling_bufnr = vim .api .nvim_get_current_buf ()
109-
110- opts = opts or {}
111-
112- debug_once (" pick opts: " , opts )
113-
114- local buf = opts .buf or vim .api .nvim_get_current_buf ()
115-
116- local entries = {}
117- for _ , value in ipairs (values ) do
118- if type (value ) == " string" then
119- table.insert (entries , {
120- text = value ,
121- value = value ,
122- })
123- elseif value .valid ~= false then
124- local name = self :_make_display (value )
125- table.insert (entries , {
126- text = name ,
127- buf = buf ,
128- filename = value .filename ,
129- value = value .value ,
130- pos = { value .lnum , value .col or 0 },
131- })
132- end
106+ self .calling_bufnr = vim .api .nvim_get_current_buf ()
107+
108+ opts = opts or {}
109+
110+ debug_once (" pick opts: " , opts )
111+
112+ local buf = opts .buf or vim .api .nvim_get_current_buf ()
113+
114+ local entries = {}
115+ for _ , value in ipairs (values ) do
116+ if type (value ) == " string" then
117+ table.insert (entries , {
118+ text = value ,
119+ value = value ,
120+ })
121+ elseif value .valid ~= false then
122+ local name = self :_make_display (value )
123+ table.insert (entries , {
124+ text = name ,
125+ buf = buf ,
126+ filename = value .filename ,
127+ value = value .value ,
128+ pos = { value .lnum , value .col or 0 },
129+ })
133130 end
131+ end
132+
133+ local map = vim .tbl_deep_extend (" force" , {}, notes_mappings (opts .selection_mappings ))
134+
135+ local pick_opts = vim .tbl_extend (" force" , map or {}, {
136+ tilte = opts .prompt_title ,
137+ items = entries ,
138+ layout = {
139+ preview = false ,
140+ },
141+ format = " text" ,
142+ confirm = function (picker , item , action )
143+ picker :close ()
144+ if item then
145+ if opts .callback then
146+ debug_once (" pick callback: " , item )
147+ opts .callback (item .value )
148+ else
149+ debug_once (" pick jump: " , item )
150+ snacks_picker .actions .jump (picker , item , action )
151+ end
152+ end
153+ end ,
154+ })
134155
135- local map = vim .tbl_deep_extend (" force" , {},
136- notes_mappings (opts .selection_mappings ))
137-
138- local pick_opts = vim .tbl_extend (" force" , map or {}, {
139- tilte = opts .prompt_title ,
140- items = entries ,
141- layout = {
142- preview = false
143- },
144- format = " text" ,
145- confirm = function (picker , item , action )
146- picker :close ()
147- if item then
148- if opts .callback then
149- debug_once (" pick callback: " , item )
150- opts .callback (item .value )
151- else
152- debug_once (" pick jump: " , item )
153- snacks_picker .actions .jump (picker , item , action )
154- end
155- end
156- end ,
157- })
158-
159- local entry = snacks_picker .pick (pick_opts )
156+ snacks_picker .pick (pick_opts )
160157end
161158
162159return SnacksPicker
0 commit comments