1
1
local snacks_picker = require " snacks.picker"
2
- local snacks = require " snacks"
3
-
4
2
5
3
local Path = require " obsidian.path"
6
4
local abc = require " obsidian.abc"
7
5
local Picker = require " obsidian.pickers.picker"
8
6
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(...))
46
9
end
47
10
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
61
15
opts = { win = { input = { keys = {} } }, actions = {} };
62
- for k , v in pairs (action ) do
16
+ for k , v in pairs (mapping ) do
63
17
local name = string.gsub (v .desc , " " , " _" )
64
18
opts .win .input .keys = {
65
19
[k ] = { name , mode = { " n" , " i" }, desc = v .desc }
66
20
}
67
21
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 )
70
27
end
71
28
end
72
29
return opts
@@ -82,46 +39,60 @@ local SnacksPicker = abc.new_class({
82
39
end ,
83
40
}, Picker )
84
41
42
+ --- @param opts obsidian.PickerFindOpts |? Options.
85
43
SnacksPicker .find_files = function (self , opts )
86
44
opts = opts or {}
87
45
88
46
--- @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 ))
90
51
91
52
local pick_opts = vim .tbl_extend (" force" , map or {}, {
92
53
source = " files" ,
93
54
title = opts .prompt_title ,
94
- cwd = opts . dir . filename ,
55
+ cwd = tostring ( dir ) ,
95
56
confirm = function (picker , item , action )
96
57
picker :close ()
97
58
if item then
98
59
if opts .callback then
60
+ debug_once (" find files callback: " , item )
99
61
opts .callback (item ._path )
100
62
else
63
+ debug_once (" find files jump: " , item )
101
64
snacks_picker .actions .jump (picker , item , action )
102
65
end
103
66
end
104
67
end ,
105
68
})
106
- snacks_picker .pick (pick_opts )
69
+ local t = snacks_picker .pick (pick_opts )
107
70
end
108
71
109
- SnacksPicker .grep = function (self , opts , action )
72
+ --- @param opts obsidian.PickerGrepOpts |? Options.
73
+ SnacksPicker .grep = function (self , opts )
110
74
opts = opts or {}
111
75
76
+ debug_once (" grep opts : " , opts )
77
+
112
78
--- @type obsidian.Path
113
79
local dir = opts .dir and Path :new (opts .dir ) or self .client .dir
114
80
81
+ local map = vim .tbl_deep_extend (" force" , {},
82
+ notes_mappings (opts .selection_mappings ))
83
+
115
84
local pick_opts = vim .tbl_extend (" force" , map or {}, {
116
85
source = " grep" ,
117
86
title = opts .prompt_title ,
118
- cwd = opts . dir . filename ,
87
+ cwd = dir ,
119
88
confirm = function (picker , item , action )
120
89
picker :close ()
121
90
if item then
122
91
if opts .callback then
123
- opts .callback (item ._path )
92
+ debug_once (" grep callback: " , item )
93
+ opts .callback (item ._path or item .filename )
124
94
else
95
+ debug_once (" grep jump: " , item )
125
96
snacks_picker .actions .jump (picker , item , action )
126
97
end
127
98
end
@@ -130,16 +101,17 @@ SnacksPicker.grep = function(self, opts, action)
130
101
snacks_picker .pick (pick_opts )
131
102
end
132
103
104
+ --- @param values string[] | obsidian.PickerEntry[]
105
+ --- @param opts obsidian.PickerPickOpts |? Options.
106
+ --- @diagnostic disable-next-line : unused-local
133
107
SnacksPicker .pick = function (self , values , opts )
134
108
self .calling_bufnr = vim .api .nvim_get_current_buf ()
135
109
136
110
opts = opts or {}
137
111
138
- local buf = opts . buf or vim . api . nvim_get_current_buf ( )
112
+ debug_once ( " pick opts: " , opts )
139
113
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 ()
143
115
144
116
local entries = {}
145
117
for _ , value in ipairs (values ) do
@@ -155,25 +127,30 @@ SnacksPicker.pick = function(self, values, opts)
155
127
buf = buf ,
156
128
filename = value .filename ,
157
129
value = value .value ,
158
- pos = { value .lnum , value .col },
130
+ pos = { value .lnum , value .col or 0 },
159
131
})
160
132
end
161
133
end
162
134
135
+ local map = vim .tbl_deep_extend (" force" , {},
136
+ notes_mappings (opts .selection_mappings ))
137
+
163
138
local pick_opts = vim .tbl_extend (" force" , map or {}, {
164
139
tilte = opts .prompt_title ,
165
140
items = entries ,
166
141
layout = {
167
142
preview = false
168
143
},
169
144
format = " text" ,
170
- confirm = function (picker , item )
145
+ confirm = function (picker , item , action )
171
146
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 )
176
150
opts .callback (item .value )
151
+ else
152
+ debug_once (" pick jump: " , item )
153
+ snacks_picker .actions .jump (picker , item , action )
177
154
end
178
155
end
179
156
end ,
0 commit comments