forked from sudo-tee/opencode.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_picker.lua
More file actions
173 lines (145 loc) · 4.06 KB
/
file_picker.lua
File metadata and controls
173 lines (145 loc) · 4.06 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
local M = {}
local picker = require('opencode.ui.picker')
local function format_file(path)
-- when path is something like: file.extension dir1/dir2 -> format to dir1/dir2/file.extension
local file_match, path_match = path:match('^(.-)\t(.-)$')
if file_match and path_match then
path = path_match .. '/' .. file_match
end
return {
name = vim.fn.fnamemodify(path, ':t'),
path = path,
}
end
local function telescope_ui(callback, path)
local builtin = require('telescope.builtin')
local actions = require('telescope.actions')
local action_state = require('telescope.actions.state')
local opts = {
attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(function()
local picker = action_state.get_current_picker(prompt_bufnr)
local multi = picker and picker:get_multi_selection() or {}
if multi and #multi > 0 then
actions.close(prompt_bufnr)
for _, entry in ipairs(multi) do
if entry and entry.value and callback then
callback(entry.value)
end
end
return
end
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
if selection and callback then
callback(selection.value)
end
end)
return true
end,
}
if path then
opts.cwd = path
end
builtin.find_files(opts)
end
local function fzf_ui(callback, path)
local fzf_lua = require('fzf-lua')
local opts = {
actions = {
['default'] = function(selected)
if not selected or #selected == 0 then
return
end
for _, sel in ipairs(selected) do
local file = fzf_lua.path.entry_to_file(sel)
if file and file.path and callback then
callback(file.path)
end
end
end,
},
}
if path then
opts.cwd = path
end
fzf_lua.files(opts)
end
local function mini_pick_ui(callback, path)
local mini_pick = require('mini.pick')
local opts = {
source = {
choose = function(selected)
if selected and callback then
callback(selected)
end
return false
end,
},
}
if path then
opts.source.cwd = path
end
mini_pick.builtin.files(nil, opts)
end
local function snacks_picker_ui(callback, path)
local Snacks = require('snacks')
local origin_win = vim.api.nvim_get_current_win()
local origin_mode = vim.fn.mode()
local origin_pos = vim.api.nvim_win_get_cursor(origin_win)
local confirmed = false
local opts = {
confirm = function(picker_obj)
local items = picker_obj:selected({ fallback = true })
confirmed = true
picker_obj:close()
if items and callback then
for _, it in ipairs(items) do
if it and it.file then
callback(it.file)
end
end
end
end,
on_close = function(obj)
-- snacks doesn't seem to restore window / mode / cursor position when you
-- cancel the picker. if we pick a file, we're already handling that case elsewhere
if confirmed or not vim.api.nvim_win_is_valid(origin_win) then
return
end
vim.api.nvim_set_current_win(origin_win)
if origin_mode:match('i') then
vim.cmd('startinsert')
end
vim.api.nvim_win_set_cursor(origin_win, origin_pos)
end,
}
if path then
opts.cwd = path
end
Snacks.picker.files(opts)
end
function M.pick(callback, path)
local picker_type = picker.get_best_picker()
if not picker_type then
return
end
local wrapped_callback = function(selected_file)
local file_name = format_file(selected_file)
callback(file_name)
end
vim.schedule(function()
if picker_type == 'telescope' then
telescope_ui(wrapped_callback, path)
elseif picker_type == 'fzf' then
fzf_ui(wrapped_callback, path)
elseif picker_type == 'mini.pick' then
mini_pick_ui(wrapped_callback, path)
elseif picker_type == 'snacks' then
snacks_picker_ui(wrapped_callback, path)
else
callback(nil)
end
end)
end
return M