-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnavigate.lua
More file actions
605 lines (522 loc) · 15.7 KB
/
Copy pathnavigate.lua
File metadata and controls
605 lines (522 loc) · 15.7 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
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
local M = {}
local render = require("jumpy.render")
local session = require("jumpy.session")
local utils = require("jumpy.utils")
--- Guard cursor-based actions so invoking them from the sidebar or another
--- special buffer gives a clear hint instead of a confusing "no hunks".
local function guard_source()
local ok, reason = utils.check_source_buffer()
if not ok then
vim.notify(reason, vim.log.levels.WARN)
end
return ok
end
local MSG_NO_HUNKS = "jumpy: no hunks"
local MSG_NO_HUNK_UNDER_CURSOR = "jumpy: no hunk under cursor"
local offset_table = {}
local undo_history = {}
local syncing_undo = {}
local function copy_offsets(bufnr)
return vim.deepcopy(offset_table[bufnr] or {})
end
local function restore_offsets(bufnr, offsets)
offset_table[bufnr] = vim.deepcopy(offsets or {})
end
local function same_lines(a, b)
return vim.deep_equal(a, b)
end
--- Hunk indices an accept step applied: present in the pre-accept snapshot but
--- gone from the post-accept one (they were written into the buffer + cleared).
local function applied_hunk_idxs(entry)
local before = (entry.before_state and entry.before_state.hunks) or {}
local after = (entry.after_state and entry.after_state.hunks) or {}
local idxs = {}
for idx, hunk in pairs(before) do
if hunk and not after[idx] then
idxs[#idxs + 1] = idx
end
end
return idxs
end
--- Keep the session log in step with native undo/redo: undoing an accept puts
--- those hunks back to "pending" (the change is no longer applied); redoing it
--- marks them "accepted" again.
local function reconcile_session(bufnr, entry, status)
for _, idx in ipairs(applied_hunk_idxs(entry)) do
session.mark_hunk(bufnr, idx, status)
end
end
local function sync_undo_state(bufnr)
if syncing_undo[bufnr] or not vim.api.nvim_buf_is_valid(bufnr) then
return
end
local history = undo_history[bufnr]
if not history then
return
end
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
for i = #history, 1, -1 do
local entry = history[i]
if same_lines(lines, entry.before_lines) then
syncing_undo[bufnr] = true
render.restore(bufnr, entry.before_state)
restore_offsets(bufnr, entry.before_offsets)
syncing_undo[bufnr] = nil
reconcile_session(bufnr, entry, "pending")
M._refresh_quickfix()
return
end
if same_lines(lines, entry.after_lines) then
syncing_undo[bufnr] = true
render.restore(bufnr, entry.after_state)
restore_offsets(bufnr, entry.after_offsets)
syncing_undo[bufnr] = nil
reconcile_session(bufnr, entry, "accepted")
M._refresh_quickfix()
return
end
end
end
M._sync_undo_state = sync_undo_state
local function record_undo_state(bufnr, before_state, before_offsets, before_lines)
local history = undo_history[bufnr] or {}
table.insert(history, {
before_state = before_state,
before_offsets = before_offsets,
before_lines = before_lines,
after_state = render.snapshot(bufnr),
after_offsets = copy_offsets(bufnr),
after_lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false),
})
undo_history[bufnr] = history
end
function M._clear_undo_history(bufnr)
undo_history[bufnr] = nil
offset_table[bufnr] = nil
syncing_undo[bufnr] = nil
end
vim.api.nvim_create_autocmd("TextChanged", {
callback = function(args)
sync_undo_state(args.buf)
end,
})
vim.api.nvim_create_autocmd("BufWipeout", {
callback = function(args)
M._clear_undo_history(args.buf)
end,
})
local function center_cursor()
vim.cmd("normal! zz")
end
local function set_cursor_centered(line)
local line_count = vim.api.nvim_buf_line_count(0)
local target_line = math.min(math.max(line, 1), line_count)
vim.api.nvim_win_set_cursor(0, { target_line, 0 })
center_cursor()
end
local function current_hunk_line(bufnr, idx, hunk)
local line = hunk.old_start + M._get_offset(bufnr, idx)
local line_count = vim.api.nvim_buf_line_count(bufnr)
return math.min(math.max(line, 1), line_count)
end
local function get_active_hunks(bufnr)
local state = render.get_state(bufnr)
if not state then
return {}
end
local active = {}
for idx, hunk in pairs(state.hunks) do
if hunk then
table.insert(active, { idx = idx, hunk = hunk })
end
end
table.sort(active, function(a, b)
return a.hunk.old_start < b.hunk.old_start
end)
return active
end
function M.hunk_at_cursor()
local bufnr = vim.api.nvim_get_current_buf()
local cursor_line = vim.api.nvim_win_get_cursor(0)[1]
local active = get_active_hunks(bufnr)
for _, entry in ipairs(active) do
local hunk = entry.hunk
local hunk_start = current_hunk_line(bufnr, entry.idx, hunk)
local hunk_end = hunk_start + math.max(hunk.old_count, 1) - 1
if cursor_line >= hunk_start and cursor_line <= hunk_end then
return entry.idx
end
end
return nil
end
function M.next_hunk()
if not guard_source() then
return
end
local bufnr = vim.api.nvim_get_current_buf()
local cursor_line = vim.api.nvim_win_get_cursor(0)[1]
local active = get_active_hunks(bufnr)
if #active == 0 then
vim.notify(MSG_NO_HUNKS, vim.log.levels.INFO)
return
end
for _, entry in ipairs(active) do
local hunk_line = current_hunk_line(bufnr, entry.idx, entry.hunk)
if hunk_line > cursor_line then
set_cursor_centered(hunk_line)
return
end
end
set_cursor_centered(current_hunk_line(bufnr, active[1].idx, active[1].hunk))
end
function M.prev_hunk()
if not guard_source() then
return
end
local bufnr = vim.api.nvim_get_current_buf()
local cursor_line = vim.api.nvim_win_get_cursor(0)[1]
local active = get_active_hunks(bufnr)
if #active == 0 then
vim.notify(MSG_NO_HUNKS, vim.log.levels.INFO)
return
end
for i = #active, 1, -1 do
local hunk_line = current_hunk_line(bufnr, active[i].idx, active[i].hunk)
if hunk_line < cursor_line then
set_cursor_centered(hunk_line)
return
end
end
set_cursor_centered(current_hunk_line(bufnr, active[#active].idx, active[#active].hunk))
end
function M.accept_hunk(bufnr, hunk_idx)
local state = render.get_state(bufnr)
if not state then
return false
end
local hunk = state.hunks[hunk_idx]
if not hunk then
return false
end
local offset = M._get_offset(bufnr, hunk_idx)
local before_state = render.snapshot(bufnr)
local before_offsets = copy_offsets(bufnr)
local before_lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local start_line = hunk.old_start - 1 + offset
local end_line = start_line + hunk.old_count
syncing_undo[bufnr] = true
vim.api.nvim_buf_set_lines(bufnr, start_line, end_line, false, hunk.added_lines)
local delta = #hunk.added_lines - hunk.old_count
M._apply_offset(bufnr, hunk_idx, delta)
render.clear_hunk(bufnr, hunk_idx)
if not render.get_state(bufnr) then
offset_table[bufnr] = nil
end
record_undo_state(bufnr, before_state, before_offsets, before_lines)
syncing_undo[bufnr] = nil
session.mark_hunk(bufnr, hunk_idx, "accepted")
M._refresh_quickfix()
return true
end
function M.accept()
if not guard_source() then
return
end
local bufnr = vim.api.nvim_get_current_buf()
local hunk_idx = M.hunk_at_cursor()
if not hunk_idx then
vim.notify(MSG_NO_HUNK_UNDER_CURSOR, vim.log.levels.WARN)
return
end
M.accept_hunk(bufnr, hunk_idx)
M._advance_to_next(bufnr)
end
function M.reject_hunk(bufnr, hunk_idx)
local state = render.get_state(bufnr)
if not state or not state.hunks[hunk_idx] then
return false
end
render.clear_hunk(bufnr, hunk_idx)
session.mark_hunk(bufnr, hunk_idx, "rejected")
M._refresh_quickfix()
return true
end
function M.reject()
if not guard_source() then
return
end
local bufnr = vim.api.nvim_get_current_buf()
local hunk_idx = M.hunk_at_cursor()
if not hunk_idx then
vim.notify(MSG_NO_HUNK_UNDER_CURSOR, vim.log.levels.WARN)
return
end
M.reject_hunk(bufnr, hunk_idx)
M._advance_to_next(bufnr)
end
function M.accept_all()
if not guard_source() then
return
end
local bufnr = vim.api.nvim_get_current_buf()
local active = get_active_hunks(bufnr)
if #active == 0 then
vim.notify(MSG_NO_HUNKS, vim.log.levels.INFO)
return
end
local reversed = {}
for i = #active, 1, -1 do
table.insert(reversed, active[i])
end
local before_state = render.snapshot(bufnr)
local before_offsets = copy_offsets(bufnr)
local before_lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
syncing_undo[bufnr] = true
for _, entry in ipairs(reversed) do
local hunk = entry.hunk
local start_line = hunk.old_start - 1
local end_line = start_line + hunk.old_count
vim.api.nvim_buf_set_lines(bufnr, start_line, end_line, false, hunk.added_lines)
end
render.clear(bufnr)
offset_table[bufnr] = nil
record_undo_state(bufnr, before_state, before_offsets, before_lines)
syncing_undo[bufnr] = nil
session.mark_all(bufnr, "accepted")
M._refresh_quickfix()
vim.notify("jumpy: all hunks accepted", vim.log.levels.INFO)
end
function M.reject_all()
if not guard_source() then
return
end
local bufnr = vim.api.nvim_get_current_buf()
render.clear(bufnr)
session.mark_all(bufnr, "rejected")
M._refresh_quickfix()
vim.notify("jumpy: all hunks rejected", vim.log.levels.INFO)
end
function M.replace_hunk(bufnr, hunk_idx, new_lines)
render.update_hunk_lines(bufnr, hunk_idx, new_lines)
local preview = new_lines[1] and ("+ " .. new_lines[1]) or "(change)"
session.update_hunk_preview(bufnr, hunk_idx, preview, new_lines)
M._refresh_quickfix()
end
function M.add_hunks_to_quickfix()
local states = render.get_all_states()
local items = M._transform_hunks_to_quickfix(states)
if #items == 0 then
vim.notify(MSG_NO_HUNKS, vim.log.levels.INFO)
return
end
vim.fn.setqflist({}, " ", { title = "jumpy:hunks", items = items })
vim.cmd("copen")
M._setup_quickfix_keymaps()
end
function M._refresh_quickfix(index)
local quickfix = vim.fn.getqflist({ title = 0, idx = 0 })
if quickfix.title ~= "jumpy:hunks" then
return
end
local items = M._transform_hunks_to_quickfix(render.get_all_states())
if #items == 0 then
vim.fn.setqflist({}, "r", { title = "jumpy:hunks", items = {} })
vim.cmd("cclose")
return
end
local target_index = math.min(index or quickfix.idx, #items)
vim.fn.setqflist({}, "r", {
title = "jumpy:hunks",
items = items,
idx = math.max(target_index, 1),
})
end
function M._transform_hunks_to_quickfix(states)
local items = {}
for bufnr, state in pairs(states) do
for hunk_idx, hunk in pairs(state.hunks) do
if hunk then
local text
if #hunk.added_lines > 0 then
text = "+ " .. hunk.added_lines[1]
else
text = "- " .. hunk.removed_lines[1]
end
table.insert(items, {
bufnr = bufnr,
lnum = current_hunk_line(bufnr, hunk_idx, hunk),
col = 1,
text = text,
user_data = { bufnr = bufnr, hunk_idx = hunk_idx },
})
end
end
end
table.sort(items, function(a, b)
local a_name = vim.api.nvim_buf_get_name(a.bufnr)
local b_name = vim.api.nvim_buf_get_name(b.bufnr)
if a_name ~= b_name then
return a_name < b_name
end
if a.lnum ~= b.lnum then
return a.lnum < b.lnum
end
return a.user_data.hunk_idx < b.user_data.hunk_idx
end)
return items
end
local function current_quickfix_hunk()
local quickfix = vim.fn.getqflist({ title = 0, items = 0, idx = 0 })
if quickfix.title ~= "jumpy:hunks" then
return nil
end
local item = quickfix.items[quickfix.idx]
local user_data = item and item.user_data
if type(user_data) ~= "table" then
return nil
end
return user_data.bufnr, user_data.hunk_idx, quickfix.idx
end
local function advance_quickfix(index)
local quickfix = vim.fn.getqflist({ title = 0, size = 0 })
if quickfix.size == 0 then
return
end
vim.cmd("cc " .. math.min(index, quickfix.size))
end
function M.accept_quickfix_hunk()
local bufnr, hunk_idx, index = current_quickfix_hunk()
if not bufnr or not M.accept_hunk(bufnr, hunk_idx) then
M._refresh_quickfix(index)
vim.notify("jumpy: quickfix hunk is no longer pending", vim.log.levels.WARN)
return
end
M._refresh_quickfix(index)
advance_quickfix(index)
end
function M.reject_quickfix_hunk()
local bufnr, hunk_idx, index = current_quickfix_hunk()
if not bufnr or not M.reject_hunk(bufnr, hunk_idx) then
M._refresh_quickfix(index)
vim.notify("jumpy: quickfix hunk is no longer pending", vim.log.levels.WARN)
return
end
M._refresh_quickfix(index)
advance_quickfix(index)
end
function M._setup_quickfix_keymaps()
local quickfix_bufnr = vim.fn.getqflist({ qfbufnr = 0 }).qfbufnr
if quickfix_bufnr == 0 then
return
end
vim.keymap.set("n", "a", M.accept_quickfix_hunk, {
buffer = quickfix_bufnr,
silent = true,
desc = "Accept Jumpy hunk",
})
vim.keymap.set("n", "x", M.reject_quickfix_hunk, {
buffer = quickfix_bufnr,
silent = true,
desc = "Reject Jumpy hunk",
})
end
function M._get_offset(bufnr, hunk_idx)
if not offset_table[bufnr] then
offset_table[bufnr] = {}
end
return offset_table[bufnr][hunk_idx] or 0
end
function M._apply_offset(bufnr, accepted_idx, delta)
if not offset_table[bufnr] then
offset_table[bufnr] = {}
end
local state = render.get_state(bufnr)
if not state then
return
end
for idx, hunk in pairs(state.hunks) do
if hunk and idx > accepted_idx then
offset_table[bufnr][idx] = (offset_table[bufnr][idx] or 0) + delta
end
end
end
local function get_all_active_hunks()
local all = {}
for bufnr, state in pairs(render.get_all_states()) do
for idx, hunk in pairs(state.hunks) do
if hunk then
table.insert(all, { bufnr = bufnr, idx = idx, hunk = hunk })
end
end
end
table.sort(all, function(a, b)
local na = vim.api.nvim_buf_get_name(a.bufnr)
local nb = vim.api.nvim_buf_get_name(b.bufnr)
if na ~= nb then
return na < nb
end
return a.hunk.old_start < b.hunk.old_start
end)
return all
end
local function jump_to(bufnr, line)
local cur_win = vim.api.nvim_get_current_win()
local win_cfg = vim.api.nvim_win_get_config(cur_win)
if win_cfg.relative ~= "" then
for _, win in ipairs(vim.api.nvim_list_wins()) do
if vim.api.nvim_win_get_config(win).relative == "" then
vim.api.nvim_set_current_win(win)
break
end
end
end
vim.cmd("normal! m'")
vim.api.nvim_win_set_buf(0, bufnr)
set_cursor_centered(line)
end
function M.first_hunk_any_buf()
local all = get_all_active_hunks()
if #all == 0 then
vim.notify(MSG_NO_HUNKS, vim.log.levels.INFO)
return
end
local entry = all[1]
jump_to(entry.bufnr, entry.hunk.old_start)
end
--- Jump to a specific pending hunk (used by the session sidebar). Returns
--- whether the hunk still exists in the live render state.
function M.jump_to_hunk(bufnr, hunk_idx)
if not vim.api.nvim_buf_is_valid(bufnr) then
return false
end
local state = render.get_state(bufnr)
if not state or not state.hunks[hunk_idx] then
return false
end
local line = current_hunk_line(bufnr, hunk_idx, state.hunks[hunk_idx])
jump_to(bufnr, line)
return true
end
function M._advance_to_next(bufnr)
local active = get_active_hunks(bufnr)
if #active > 0 then
local cursor_line = vim.api.nvim_win_get_cursor(0)[1]
for _, entry in ipairs(active) do
local hunk_line = current_hunk_line(bufnr, entry.idx, entry.hunk)
if hunk_line >= cursor_line then
set_cursor_centered(hunk_line)
return
end
end
set_cursor_centered(current_hunk_line(bufnr, active[1].idx, active[1].hunk))
else
offset_table[bufnr] = nil
local all = get_all_active_hunks()
if #all > 0 then
jump_to(all[1].bufnr, all[1].hunk.old_start)
else
vim.cmd("normal! \\<C-o>")
end
end
end
return M