-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathinit.lua
655 lines (591 loc) · 20.2 KB
/
init.lua
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
-- try to do hot reload with lua
-- sometime it break your neovim:)
-- run that command and feel
-- @CMD lua _G.__is_dev=true
-- @CMD luafile %
--
-- reset state if you change default config
-- @CMD lua _G.__spectre_state = nil
if _G._require == nil then
if _G.__is_dev then
_G._require = require
_G.require = function(path)
if string.find(path, '^spectre[^_]*$') ~= nil then
package.loaded[path] = nil
end
return _G._require(path)
end
end
end
local api = vim.api
local config = require('spectre.config')
local state = require('spectre.state')
local state_utils = require('spectre.state_utils')
local utils = require('spectre.utils')
local ui = require('spectre.ui')
local log = require('spectre._log')
local async = require('plenary.async')
local scheduler = async.util.scheduler
local M = {}
M.setup = function(cfg)
state.user_config = vim.tbl_deep_extend('force', config, cfg or {})
for _, opt in pairs(state.user_config.default.find.options) do
state.options[opt] = true
end
end
M.open_visual = function(opts)
opts = opts or {}
if opts.select_word then
opts.search_text = vim.fn.expand('<cword>')
else
opts.search_text = utils.get_visual_selection()
end
M.open(opts)
end
M.open_file_search = function(opts)
opts = opts or {}
if opts.select_word then
opts.search_text = vim.fn.expand("<cword>")
else
opts.search_text = utils.get_visual_selection()
end
opts.path = vim.fn.fnameescape(vim.fn.expand("%:p:."))
if vim.loop.os_uname().sysname == "Windows_NT" then
opts.path = vim.fn.substitute(opts.path, "\\", "/", "g")
end
M.open(opts)
end
M.close = function()
if state.bufnr ~= nil then
local wins = vim.fn.win_findbuf(state.bufnr)
for _, win_id in pairs(wins) do
vim.api.nvim_win_close(win_id, true);
end
end
end
M.open = function(opts)
log.debug("Start")
if state.user_config == nil then
M.setup()
end
opts = vim.tbl_extend('force', {
cwd = nil,
is_insert_mode = state.user_config.is_insert_mode,
search_text = '',
replace_text = '',
path = '',
is_close = false, -- close an exists instance of spectre then open new
is_file = false
}, opts or {}) or {}
state.status_line = ''
opts.search_text = utils.trim(opts.search_text)
state.target_winid = api.nvim_get_current_win()
state.target_bufnr = api.nvim_get_current_buf()
if opts.is_close then
M.close()
end
local is_new = true
--check reopen panel by reuse bufnr
if state.bufnr ~= nil and not opts.is_close then
local wins = vim.fn.win_findbuf(state.bufnr)
if #wins >= 1 then
for _, win_id in pairs(wins) do
if vim.fn.win_gotoid(win_id) == 1 then
is_new = false
end
end
end
end
if state.bufnr == nil or is_new then
vim.cmd(state.user_config.open_cmd)
else
if state.query.path ~= nil
and #state.query.path > 1
and opts.path == ''
then
opts.path = state.query.path
end
end
vim.wo.foldenable = false
vim.bo.buftype = 'nofile'
vim.bo.buflisted = false
state.bufnr = api.nvim_get_current_buf();
vim.cmd(string.format("file %s/spectre", state.bufnr))
vim.bo.filetype = config.filetype
api.nvim_buf_clear_namespace(state.bufnr, config.namespace_status, 0, -1)
api.nvim_buf_clear_namespace(state.bufnr, config.namespace_result, 0, -1)
api.nvim_buf_set_lines(state.bufnr, 0, -1, false, {})
vim.api.nvim_buf_attach(state.bufnr, false, {
on_detach = M.stop,
})
ui.render_text_query(opts)
state.cwd = opts.cwd
M.change_view("reset")
ui.render_search_ui()
if opts.is_insert_mode == true then
vim.api.nvim_feedkeys('A', 'n', true)
end
M.mapping_buffer(state.bufnr)
if #opts.search_text > 0 then
M.search({
cwd = opts.cwd,
search_query = opts.search_text,
replace_query = opts.replace_text,
path = opts.path,
})
end
end
function M.mapping_buffer(bufnr)
_G.__spectre_fold = M.get_fold
vim.cmd [[augroup spectre_panel
au!
au InsertEnter <buffer> lua require"spectre".on_insert_enter()
au InsertLeave <buffer> lua require"spectre".on_search_change()
au BufLeave <buffer> lua require("spectre").on_leave()
au BufUnload <buffer> lua require("spectre").on_close()
augroup END ]]
vim.opt_local.wrap = false
vim.opt_local.foldexpr = "spectre#foldexpr()"
vim.opt_local.foldmethod = "expr"
local map_opt = { noremap = true, silent = _G.__is_dev == nil }
api.nvim_buf_set_keymap(bufnr, 'n', 'x', 'x<cmd>lua require("spectre").on_search_change()<CR>', map_opt)
api.nvim_buf_set_keymap(bufnr, 'n', 'p', "p<cmd>lua require('spectre').on_search_change()<cr>", map_opt)
api.nvim_buf_set_keymap(bufnr, 'v', 'p', "p<cmd>lua require('spectre').on_search_change()<cr>", map_opt)
api.nvim_buf_set_keymap(bufnr, 'v', 'P', "P<cmd>lua require('spectre').on_search_change()<cr>", map_opt)
api.nvim_buf_set_keymap(bufnr, 'n', 'd', '<nop>', map_opt)
api.nvim_buf_set_keymap(bufnr, 'v', 'd', '<esc><cmd>lua require("spectre").toggle_checked()<cr>', map_opt)
api.nvim_buf_set_keymap(bufnr, 'n', 'o', 'ji', map_opt) -- don't append line on can make the UI wrong
api.nvim_buf_set_keymap(bufnr, 'n', 'O', 'ki', map_opt)
api.nvim_buf_set_keymap(bufnr, 'n', '?', "<cmd>lua require('spectre').show_help()<cr>", map_opt)
for _, map in pairs(state.user_config.mapping) do
api.nvim_buf_set_keymap(bufnr, 'n', map.map, map.cmd, vim.tbl_deep_extend("force", map_opt, { desc = map.desc }))
end
vim.api.nvim_create_autocmd("BufWritePost", {
group = vim.api.nvim_create_augroup("SpectrePanelWrite", { clear = true }),
pattern = "*",
callback = require('spectre').on_write,
desc = "spectre write autocmd"
})
end
local function hl_match(opts)
if #opts.search_query > 0 then
api.nvim_buf_add_highlight(state.bufnr, config.namespace,
state.user_config.highlight.search, 2, 0, -1)
end
if #opts.replace_query > 0 then
api.nvim_buf_add_highlight(state.bufnr, config.namespace,
state.user_config.highlight.replace, 4, 0, -1)
end
end
local function can_edit_line()
local line = vim.fn.getpos('.')
if line[2] > config.lnum_UI then
return false
end
return true
end
M.on_insert_enter = function()
if can_edit_line() then return end
local key = api.nvim_replace_termcodes("<esc>", true, false, true)
api.nvim_feedkeys(key, "m", true)
print("You can't make changes in results.")
end
M.on_search_change = function()
if not can_edit_line() then return end
local lines = api.nvim_buf_get_lines(state.bufnr, 0, config.lnum_UI, false)
local query = {
replace_query = "",
search_query = "",
path = "",
}
for index, line in pairs(lines) do
if index <= 3 and #line > 0 then
query.search_query = query.search_query .. line
end
if index >= 5 and index < 7 and #line > 0 then
query.replace_query = query.replace_query .. line
end
if index >= 7 and index <= 9 and #line > 0 then
query.path = query.path .. line
end
end
local line = vim.fn.getpos('.')
-- check path to verify search in current file
if state.target_winid ~= nil then
local ok, bufnr = pcall(api.nvim_win_get_buf, state.target_winid)
if ok then
-- can't use api.nvim_buf_get_name it get a full path
local bufname = vim.fn.bufname(bufnr)
query.is_file = query.path == bufname
else
state.target_winid = nil
end
end
if line[2] >= 5 and line[2] < 7 then
M.async_replace(query)
else
M.search(query)
end
end
M.on_write = function()
if state.user_config.live_update == true then
M.search()
end
end
M.toggle_live_update = function()
state.user_config.live_update = not state.user_config.live_update
ui.render_header(state.user_config)
end
M.on_close = function()
M.stop()
vim.api.nvim_create_augroup("SpectrePanelWrite", { clear = true })
state.query_backup = vim.tbl_extend("force", state.query, {})
end
M.on_leave = function()
state.query_backup = vim.tbl_extend("force", state.query, {})
end
M.resume_last_search = function()
if not state.query_backup then
print('No previous search!')
return
end
ui.render_text_query({
replace_text = state.query_backup.replace_query,
search_text = state.query_backup.search_query,
path = state.query_backup.path
})
ui.render_search_ui()
M.search(state.query_backup)
end
M.async_replace = function(query)
-- clear old search result
api.nvim_buf_clear_namespace(state.bufnr, config.namespace_result, 0, -1)
state.async_id = vim.loop.hrtime()
async.void(function()
M.do_replace_text(query, state.async_id)
end)()
end
M.do_replace_text = function(opts, async_id)
state.query = opts or state.query
hl_match(state.query)
local count = 1
for _, item in pairs(state.total_item) do
if state.async_id ~= async_id then
return
end
ui.render_line(
state.bufnr,
config.namespace,
{
search_query = state.query.search_query,
replace_query = state.query.replace_query,
search_text = item.search_text,
lnum = item.display_lnum,
is_replace = true,
},
{
is_disable = item.disable,
padding_text = state.user_config.result_padding,
padding = #state.user_config.result_padding,
show_search = state.view.show_search,
show_replace = state.view.show_replace,
},
state.regex
)
count = count + 1
-- delay to next scheduler after 100 time
if count > 100 then
scheduler()
count = 0
end
end
end
M.change_view = function(reset)
if reset then
state.view.mode = ""
end
if state.view.mode == 'replace' then
state.view.mode = "search"
state.view.show_search = true
state.view.show_replace = false
elseif state.view.mode == 'both' then
state.view.mode = "replace"
state.view.show_search = false
state.view.show_replace = true
else
state.view.mode = "both"
state.view.show_search = true
state.view.show_replace = true
end
if not reset then
M.async_replace()
end
end
M.toggle_checked = function()
local startline = unpack(vim.api.nvim_buf_get_mark(0, '<'))
local endline = unpack(vim.api.nvim_buf_get_mark(0, '>'))
for i = startline, endline, 1 do
M.toggle_line(i)
end
end
M.toggle_line = function(line_visual)
if can_edit_line() then
-- delete line content
vim.cmd [[:normal! ^d$]]
return false
end
local lnum = line_visual or unpack(vim.api.nvim_win_get_cursor(0))
local item = state.total_item[lnum]
if item ~= nil and item.display_lnum == lnum - 1 then
item.disable = not item.disable
ui.render_line(
state.bufnr,
config.namespace,
{
search_query = state.query.search_query,
replace_query = state.query.replace_query,
search_text = item.search_text,
lnum = item.display_lnum,
is_replace = true
},
{
is_disable = item.disable,
padding_text = state.user_config.result_padding,
padding = #state.user_config.result_padding,
show_search = state.view.show_search,
show_replace = state.view.show_replace
},
state.regex
)
return
elseif not line_visual then
-- delete all item in 1 file
local line = vim.fn.getline(lnum)
local check = string.find(line, "([^%s]*%:%d*:%d*:)$")
if check then
check = state.total_item[lnum + 1]
if check == nil then return end
local disable = not check.disable
item = check
local index = lnum + 1
while item ~= nil and check.filename == item.filename do
item.disable = disable
ui.render_line(
state.bufnr,
config.namespace,
{
search_query = state.query.search_query,
replace_query = state.query.replace_query,
search_text = item.search_text,
lnum = item.display_lnum,
is_replace = true
},
{
is_disable = item.disable,
padding_text = state.user_config.result_padding,
padding = #state.user_config.result_padding,
show_search = state.view.show_search,
show_replace = state.view.show_replace
}, state.regex
)
index = index + 1
item = state.total_item[index]
end
end
end
end
M.search_handler = function()
local c_line = 0
local total = 0
local start_time = 0
local padding = #state.user_config.result_padding
local cfg = state.user_config or {}
local last_filename = ''
return {
on_start = function()
state.total_item = {}
state.is_running = true
state.status_line = "Start search"
c_line = config.line_result
total = 0
start_time = vim.loop.hrtime()
end,
on_result = function(item)
if not state.is_running then return end
item.replace_text = ''
if string.match(item.filename, '^%.%/') then
item.filename = item.filename:sub(3, #item.filename)
end
item.search_text = utils.truncate(utils.trim(item.text), 255)
if #state.query.replace_query > 1 then
item.replace_text = state.regex.replace_all(
state.query.search_query,
state.query.replace_query,
item.search_text
)
end
if last_filename ~= item.filename then
ui.render_filename(
state.bufnr,
config.namespace,
c_line,
item
)
c_line = c_line + 1
last_filename = item.filename
end
item.display_lnum = c_line
ui.render_line(
state.bufnr,
config.namespace,
{
search_query = state.query.search_query,
replace_query = state.query.replace_query,
search_text = item.search_text,
lnum = item.display_lnum,
is_replace = false
},
{
is_disable = item.disable,
padding_text = cfg.result_padding,
padding = padding,
show_search = state.view.show_search,
show_replace = state.view.show_replace
},
state.regex
)
c_line = c_line + 1
total = total + 1
state.status_line = "Item " .. total
state.total_item[c_line] = item
end,
on_error = function(error_msg)
api.nvim_buf_set_lines(state.bufnr, c_line, c_line + 1, false,
{ cfg.result_padding .. error_msg })
api.nvim_buf_add_highlight(state.bufnr, config.namespace,
cfg.highlight.border, c_line, 0, padding)
c_line = c_line + 1
state.finder_instance = nil
end,
on_finish = function()
if not state.is_running then return end
local end_time = (vim.loop.hrtime() - start_time) / 1E9
state.status_line = string.format("Total: %s match, time: %ss", total, end_time)
api.nvim_buf_set_lines(state.bufnr, c_line, c_line, false, {
cfg.line_sep,
})
api.nvim_buf_add_highlight(state.bufnr, config.namespace,
cfg.highlight.border, c_line, 0, -1)
state.vt.status_id = utils.write_virtual_text(
state.bufnr,
config.namespace_status,
config.line_result - 2,
{ { state.status_line, 'Question' } }
)
state.finder_instance = nil
state.is_running = false
end
}
end
M.stop = function()
state.is_running = false
log.debug('spectre stop')
if state.finder_instance ~= nil then
state.finder_instance:stop()
state.finder_instance = nil
end
end
M.search = function(opts)
M.stop()
opts = opts or state.query
local finder_creator = state_utils.get_finder_creator()
state.finder_instance = finder_creator:new(
state_utils.get_search_engine_config(),
M.search_handler()
)
if not opts.search_query or #opts.search_query < 2 then
return
end
state.query = opts
-- clear old search result
api.nvim_buf_clear_namespace(state.bufnr, config.namespace_result, 0, -1)
api.nvim_buf_set_lines(state.bufnr, config.line_result - 1, -1, false, {})
hl_match(opts)
local c_line = config.line_result
api.nvim_buf_set_lines(state.bufnr,
c_line - 1, c_line - 1,
false,
{ state.user_config.line_sep_start }
)
api.nvim_buf_add_highlight(state.bufnr, config.namespace,
state.user_config.highlight.border, c_line - 1, 0, -1)
state.total_item = {}
state.finder_instance:search({
cwd = state.cwd,
search_text = state.query.search_query,
path = state.query.path
})
M.init_regex()
end
M.init_regex = function()
local replace_config = state_utils.get_replace_engine_config()
if replace_config.cmd == 'oxi' then
state.regex = require('spectre.regex.rust')
else
state.regex = require('spectre.regex.vim')
end
state.regex.change_options(replace_config.options_value)
end
M.show_help = function()
ui.show_help()
end
M.change_engine_replace = function(engine_name)
if state.user_config.replace_engine[engine_name] then
state.user_config.default.replace.cmd = engine_name
M.init_regex()
vim.notify("change replace engine to: " .. engine_name)
ui.render_header(state.user_config)
M.search()
return
else
vim.notify(string.format("engine %s not found " .. engine_name))
end
end
M.change_options = function(key)
if state.options[key] == nil then
state.options[key] = false
end
state.options[key] = not state.options[key]
state.regex.change_options(state_utils.get_replace_engine_config().options_value)
if state.query.search_query ~= nil then
ui.render_search_ui()
M.search()
end
end
M.show_options = function()
local option_cmd = ui.show_options()
---@diagnostic disable-next-line: param-type-mismatch
vim.defer_fn(function()
local char = vim.fn.getchar() - 48
if option_cmd[char] then
M.change_options(option_cmd[char])
end
end, 200)
end
M.get_fold = function(lnum)
if lnum < config.lnum_UI then
return '0'
end
local line = vim.fn.getline(lnum)
local check = string.find(line, "([^%s]*%:%d*:%d*:)$")
if check then return '>1' end
local nextline = vim.fn.getline(lnum + 1)
local nextcheck = string.find(nextline, "([^%s]*%:%d*:%d*:)$")
if nextcheck then return '<1' end
local item = state.total_item[lnum]
if item ~= nil then
return '1'
end
return '0'
end
return M