-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathfzy.lua
More file actions
542 lines (454 loc) · 12.9 KB
/
Copy pathfzy.lua
File metadata and controls
542 lines (454 loc) · 12.9 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
local M = {}
local selection_ns = vim.api.nvim_create_namespace("builtin_fuzzy_select_selection")
local match_ns = vim.api.nvim_create_namespace("builtin_fuzzy_select_matches")
local count_ns = vim.api.nvim_create_namespace("builtin_fuzzy_select_count")
local function stopinsert()
if vim.api.nvim_get_mode().mode == "i" then
vim.cmd("stopinsert")
end
end
local function select_opts(title)
local columns = vim.o.columns
local lines = vim.o.lines
local width = math.floor(columns * 0.9)
local height = math.floor(lines * 0.8)
local row = math.floor((lines - height) * 0.5)
local col = math.floor((columns - width) * 0.5)
local result_height = math.max(1, height - 2)
return {
input = {
relative = "editor",
style = "minimal",
row = row,
col = col,
width = width,
height = 1,
focusable = true,
border = { "╭", "─", "╮", "│", "┤", "─", "├", "│" },
title = title,
title_pos = "center",
},
result = {
relative = "editor",
style = "minimal",
row = row + 2,
col = col,
width = width,
height = result_height,
focusable = true,
border = { "├", "─", "┤", "│", "╯", "─", "╰", "│" },
},
}
end
local function set_scratch_options(buf)
vim.bo[buf].buftype = "nofile"
vim.bo[buf].bufhidden = "wipe"
vim.bo[buf].buflisted = false
vim.bo[buf].swapfile = false
end
local function set_lines(buf, lines)
if not buf or not vim.api.nvim_buf_is_valid(buf) then
return
end
vim.bo[buf].modifiable = true
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
vim.bo[buf].modifiable = false
vim.bo[buf].modified = false
end
local function set_range_highlight(buf, ns, hl_group, row, start_col, end_col)
if end_col <= start_col then
return
end
vim.api.nvim_buf_set_extmark(buf, ns, row, start_col, {
end_col = end_col,
hl_group = hl_group,
priority = 100,
})
end
local function close_window(state)
if state.closed then
return
end
stopinsert()
state.closed = true
if state.group then
pcall(vim.api.nvim_del_augroup_by_id, state.group)
end
if state.input_win and vim.api.nvim_win_is_valid(state.input_win) then
pcall(vim.api.nvim_win_close, state.input_win, true)
end
if state.result_win and vim.api.nvim_win_is_valid(state.result_win) then
pcall(vim.api.nvim_win_close, state.result_win, true)
end
if state.input_buf and vim.api.nvim_buf_is_valid(state.input_buf) then
pcall(vim.api.nvim_buf_delete, state.input_buf, { force = true })
end
if state.result_buf and vim.api.nvim_buf_is_valid(state.result_buf) then
pcall(vim.api.nvim_buf_delete, state.result_buf, { force = true })
end
end
local function state_has_window(state, win)
return (state.input_win and vim.api.nvim_win_is_valid(state.input_win) and win == state.input_win)
or (state.result_win and vim.api.nvim_win_is_valid(state.result_win) and win == state.result_win)
end
local function close_on_focus_lost(state)
vim.schedule(function()
if state.closed then
return
end
if not state_has_window(state, vim.api.nvim_get_current_win()) then
close_window(state)
end
end)
end
local function open_file(filename, open)
if not filename or filename == "" then
return
end
vim.cmd((open or "edit") .. " " .. vim.fn.fnameescape(filename))
end
local function render(state)
if state.closed then
return
end
if not state.input_buf or not vim.api.nvim_buf_is_valid(state.input_buf) then
return
end
local query = state.query or ""
local matches = {}
local positions = {}
if query == "" then
matches = vim.list_slice(state.items, 1, state.max_results)
for _ = 1, #matches do
table.insert(positions, {})
end
else
local fuzzy = vim.fn.matchfuzzypos(state.items, query)
matches = vim.list_slice(fuzzy[1], 1, state.max_results)
positions = vim.list_slice(fuzzy[2], 1, state.max_results)
end
state.matches = matches
state.positions = positions
if #matches > 0 then
state.index = math.max(1, math.min(state.index, #matches))
else
state.index = 1
end
vim.api.nvim_buf_clear_namespace(state.input_buf, count_ns, 0, -1)
vim.api.nvim_buf_set_extmark(state.input_buf, count_ns, 0, 0, {
virt_text = { { (" %d/%d"):format(#matches, #state.items), "Comment" } },
virt_text_pos = "eol",
hl_mode = "combine",
priority = 100,
})
if not state.result_buf or not vim.api.nvim_buf_is_valid(state.result_buf) then
return
end
local lines = {}
for _, item in ipairs(matches) do
table.insert(lines, item)
end
set_lines(state.result_buf, lines)
vim.api.nvim_buf_clear_namespace(state.result_buf, selection_ns, 0, -1)
vim.api.nvim_buf_clear_namespace(state.result_buf, match_ns, 0, -1)
for i, pos_list in ipairs(positions) do
for _, pos in ipairs(pos_list or {}) do
local col = pos
set_range_highlight(state.result_buf, match_ns, "Search", i - 1, col, col + 1)
end
end
if state.result_win and vim.api.nvim_win_is_valid(state.result_win) and #lines > 0 then
pcall(vim.api.nvim_win_set_cursor, state.result_win, { math.min(state.index, #lines), 0 })
end
end
local function current_query(state)
if not state.input_buf or not vim.api.nvim_buf_is_valid(state.input_buf) then
return ""
end
return vim.api.nvim_buf_get_lines(state.input_buf, 0, 1, false)[1] or ""
end
local function refresh_query(state)
local query = current_query(state)
if query == state.query then
return
end
state.query = query
state.index = 1
render(state)
end
local function accept(state)
local choice = state.matches and state.matches[state.index]
stopinsert()
close_window(state)
if choice and choice ~= "" and state.on_choice then
vim.schedule(function()
state.on_choice(choice)
end)
end
end
local function move(state, delta)
local count = #(state.matches or {})
if count == 0 then
return
end
state.index = state.index + delta
if state.index < 1 then
state.index = count
elseif state.index > count then
state.index = 1
end
render(state)
end
local function run_lines(lines, opts)
opts = opts or {}
lines = lines or {}
stopinsert()
local title = opts.title or "Select"
local win_opts = select_opts(title)
local state = {
items = lines,
matches = {},
query = opts.query or "",
index = 1,
max_results = math.max(1, win_opts.result.height),
on_choice = opts.on_choice,
closed = false,
}
state.input_buf = vim.api.nvim_create_buf(false, true)
state.result_buf = vim.api.nvim_create_buf(false, true)
set_scratch_options(state.input_buf)
set_scratch_options(state.result_buf)
vim.bo[state.result_buf].modifiable = false
state.input_win = vim.api.nvim_open_win(state.input_buf, true, win_opts.input)
state.result_win = vim.api.nvim_open_win(state.result_buf, false, win_opts.result)
vim.wo[state.input_win].number = false
vim.wo[state.input_win].relativenumber = false
vim.wo[state.result_win].number = false
vim.wo[state.result_win].relativenumber = false
vim.wo[state.result_win].wrap = false
vim.wo[state.result_win].cursorline = true
vim.api.nvim_buf_set_lines(state.input_buf, 0, -1, false, { state.query })
vim.api.nvim_win_set_cursor(state.input_win, { 1, #state.query })
local function focus_input(insert)
if state.input_win and vim.api.nvim_win_is_valid(state.input_win) then
vim.api.nvim_set_current_win(state.input_win)
if insert then
vim.cmd("startinsert!")
end
end
end
local function map(buf, mode, lhs, rhs)
vim.keymap.set(mode, lhs, rhs, {
buffer = buf,
nowait = true,
silent = true,
})
end
local function map_result(lhs, rhs)
map(state.result_buf, "n", lhs, rhs)
end
local function map_both(mode, lhs, rhs)
vim.keymap.set(mode, lhs, rhs, {
buffer = state.input_buf,
nowait = true,
silent = true,
})
vim.keymap.set("n", lhs, rhs, {
buffer = state.result_buf,
nowait = true,
silent = true,
})
end
map_both({ "i", "n" }, "<CR>", function()
accept(state)
end)
map_both({ "i", "n" }, "<Esc>", function()
close_window(state)
end)
map_both("n", "q", function()
close_window(state)
end)
map_both({ "i", "n" }, "<C-c>", function()
close_window(state)
end)
map_both("n", "j", function()
move(state, 1)
end)
map_both("n", "k", function()
move(state, -1)
end)
map_both({ "i", "n" }, "<Down>", function()
move(state, 1)
end)
map_both({ "i", "n" }, "<Up>", function()
move(state, -1)
end)
map_both({ "i", "n" }, "<C-n>", function()
move(state, 1)
end)
map_both({ "i", "n" }, "<C-p>", function()
move(state, -1)
end)
map_result("i", function()
focus_input(true)
end)
map_result("/", function()
focus_input(true)
end)
state.group = vim.api.nvim_create_augroup("kide-fzy-" .. state.input_buf, { clear = true })
vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, {
group = state.group,
buffer = state.input_buf,
callback = function()
refresh_query(state)
end,
})
vim.api.nvim_create_autocmd("BufWipeout", {
group = state.group,
buffer = state.input_buf,
callback = function()
close_window(state)
end,
})
vim.api.nvim_create_autocmd("BufWipeout", {
group = state.group,
buffer = state.result_buf,
callback = function()
close_window(state)
end,
})
vim.api.nvim_create_autocmd("WinLeave", {
group = state.group,
callback = function()
close_on_focus_lost(state)
end,
})
render(state)
focus_input(true)
end
function M.select(lines, opts)
run_lines(lines, opts or {})
end
local function file_source()
if vim.fn.executable("fd") == 1 then
return "fd --type f --hidden --follow --exclude .git"
end
if vim.fn.executable("rg") == 1 then
return "rg --files --hidden --glob '!.git/**'"
end
return "find . -type f -not -path '*/.git/*' | sed 's#^\\./##'"
end
local function run_source(source_cmd, opts)
opts = opts or {}
local lines = vim.fn.systemlist(source_cmd)
if vim.v.shell_error ~= 0 then
vim.notify("source command 执行失败: " .. source_cmd, vim.log.levels.ERROR)
return
end
run_lines(lines, opts)
end
function M.files(opts)
opts = opts or {}
run_source(file_source(), {
title = "Files",
query = opts.query,
empty_message = "没有文件",
on_choice = function(choice)
open_file(choice, opts.open)
end,
})
end
local function buffer_lines()
local lines = {}
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
if vim.bo[bufnr].buflisted then
local name = vim.api.nvim_buf_get_name(bufnr)
if name == "" then
name = "[No Name]"
else
name = vim.fn.fnamemodify(name, ":~:.")
end
local modified = vim.bo[bufnr].modified and "+" or " "
table.insert(lines, ("%d\t%s\t%s"):format(bufnr, modified, name))
end
end
return lines
end
function M.buffers(opts)
opts = opts or {}
run_lines(buffer_lines(), {
title = "Buffers",
query = opts.query,
empty_message = "没有 listed buffer",
on_choice = function(choice)
local bufnr = tonumber(choice:match("^(%d+)\t"))
if bufnr and vim.api.nvim_buf_is_valid(bufnr) then
vim.api.nvim_set_current_buf(bufnr)
end
end,
})
end
local function oldfile_lines()
local lines = {}
local seen = {}
for _, filename in ipairs(vim.v.oldfiles) do
if not seen[filename] and vim.fn.filereadable(filename) == 1 then
seen[filename] = true
table.insert(lines, vim.fn.fnamemodify(filename, ":~:."))
end
end
return lines
end
function M.oldfiles(opts)
opts = opts or {}
run_lines(oldfile_lines(), {
title = "Oldfiles",
query = opts.query,
empty_message = "没有 oldfiles",
on_choice = function(choice)
open_file(choice, opts.open)
end,
})
end
local function qflist_lines()
local lines = {}
local path = require("kide.path")
for idx, item in ipairs(vim.fn.getqflist()) do
if item.valid == 1 then
local filename = item.filename
if (not filename or filename == "") and item.bufnr and item.bufnr > 0 then
filename = vim.api.nvim_buf_get_name(item.bufnr)
end
if filename and filename ~= "" then
local text = item.text or ""
text = text:gsub("%s+", " ")
table.insert(
lines,
("%d\t%s:%d:%d\t%s"):format(
idx,
path.shorten(vim.fn.fnamemodify(filename, ":~:."), 40),
item.lnum or 1,
item.col or 1,
text
)
)
end
end
end
return lines
end
function M.quickfix(opts)
opts = opts or {}
run_lines(qflist_lines(), {
title = "Quickfix",
query = opts.query,
empty_message = "quickfix 为空",
on_choice = function(choice)
local idx = tonumber(choice:match("^(%d+)\t"))
if idx then
vim.cmd("cc " .. idx)
end
end,
})
end
return M