-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlists.lua
More file actions
975 lines (887 loc) · 25.8 KB
/
Copy pathlists.lua
File metadata and controls
975 lines (887 loc) · 25.8 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
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
local M = {}
local generated = require('diffs.generated')
local hunk_model = require('diffs.hunks')
local generated_list_autocmds = {}
local generated_list_state = {}
local quickfix_keymap_autocmd
local quickfix_enter_desc = 'Open quickfix item'
local group = vim.api.nvim_create_augroup('diffs_generated_lists', { clear = false })
---@class diffs.GeneratedListOptions
---@field title? string
---@field loclist_title? string
---@field diff_spec? diffs.DiffSpec
---@field metadata_for_line? fun(lnum: integer, file: string): table?
---@field sections? table[]
---@field store_hunks? boolean
---@field quickfix? boolean
---@field is_skipped? fun(entry: table): boolean
---@param bufnr integer
---@param name string
---@return any
local function get_buf_var(bufnr, name)
return generated.get_var(bufnr, name)
end
---@param bufnr integer
---@return diffs.DiffSpec?
local function buffer_diff_spec(bufnr)
return get_buf_var(bufnr, 'diffs_spec')
end
---@param bufnr integer
---@return integer[]
local function windows_for_buffer(bufnr)
local wins = {}
for _, win in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
if vim.api.nvim_win_is_valid(win) and vim.api.nvim_win_get_buf(win) == bufnr then
wins[#wins + 1] = win
end
end
return wins
end
---@param win integer
---@return integer?
local function win_number(win)
local nr = vim.fn.win_id2win(win)
if nr > 0 then
return nr
end
return nil
end
---@param hunk diffs.DiffHunk
---@return string
local function hunk_file(hunk)
return hunk.file or hunk.path or '(unknown)'
end
---@param hunk diffs.DiffHunk
---@return integer
local function file_lnum(hunk)
return hunk.file_header_range and hunk.file_header_range.start or hunk.buffer_range.start
end
---@param line string
---@return string?
local function diff_file(line)
local old_path, new_path = line:match('^diff %-%-git %a/(.-) %a/(.+)$')
return new_path or old_path
end
---@param hunk diffs.DiffHunk
---@return integer, integer
local function hunk_stats(hunk)
local adds = 0
local dels = 0
for _, line in ipairs(hunk.lines or {}) do
if line.kind == 'add' then
adds = adds + 1
elseif line.kind == 'delete' then
dels = dels + 1
end
end
return adds, dels
end
---@param entries table[]
---@param by_file table<string, table>
---@param file string
---@param lnum integer
---@param meta? table
---@return table
local function ensure_file_entry(entries, by_file, file, lnum, meta)
meta = meta or {}
local key = meta.key or file
local entry = by_file[key]
if entry then
entry.lnum = math.min(entry.lnum, lnum)
entry.skipped = entry.skipped or meta.skipped == true
return entry
end
entry = {
key = key,
file = file,
lnum = lnum,
adds = 0,
dels = 0,
hunks = {},
section = meta.section,
section_label = meta.section_label,
diff_spec = meta.diff_spec,
skipped = meta.skipped == true,
}
by_file[key] = entry
entries[#entries + 1] = entry
return entry
end
---@param opts? table
---@param lnum integer
---@param file string
---@return table
local function metadata_for_line(opts, lnum, file)
local fn = opts and opts.metadata_for_line
if type(fn) ~= 'function' then
return {}
end
local meta = fn(lnum, file)
if type(meta) == 'table' then
return meta
end
return {}
end
---@param entry table
---@return string
local function entry_display_file(entry)
local file = entry.skipped and (entry.file .. ' (skipped)') or entry.file
if type(entry.section_label) == 'string' and entry.section_label ~= '' then
return ('[%s] %s'):format(entry.section_label, file)
end
return file
end
---@param hunk diffs.DiffHunk?
---@return string?
local function hunk_key(hunk)
if type(hunk) ~= 'table' then
return nil
end
return hunk.generated_key
end
---@param hunks diffs.DiffHunk[]
---@param diff_lines? string[]
---@param opts? table
---@return table[]
local function file_entries(hunks, diff_lines, opts)
local by_file = {}
local entries = {}
for lnum, line in ipairs(diff_lines or {}) do
local file = diff_file(line)
if file then
ensure_file_entry(entries, by_file, file, lnum, metadata_for_line(opts, lnum, file))
end
end
for _, hunk in ipairs(hunks) do
local file = hunk_file(hunk)
local meta = metadata_for_line(opts, hunk.buffer_range.start, file)
local entry = ensure_file_entry(entries, by_file, file, file_lnum(hunk), meta)
local adds, dels = hunk_stats(hunk)
entry.adds = entry.adds + adds
entry.dels = entry.dels + dels
entry.hunks[#entry.hunks + 1] = hunk
hunk.generated_key = entry.key
hunk.section = entry.section
hunk.section_label = entry.section_label
if not hunk.diff_spec and entry.diff_spec then
hunk_model.decorate_actionability({ hunk }, entry.diff_spec)
end
end
return entries
end
---@param entries table[]
---@param opts? table
local function apply_skipped(entries, opts)
local is_skipped = opts and opts.is_skipped
if type(is_skipped) ~= 'function' then
return
end
for _, entry in ipairs(entries) do
entry.skipped = is_skipped(entry) == true
end
end
---@param entries table[]
local function format_file_text(entries)
local max_fname = 0
local max_add, max_del = 0, 0
for _, entry in ipairs(entries) do
entry.display_file = entry_display_file(entry)
max_fname = math.max(max_fname, #entry.display_file)
if entry.adds > 0 then
max_add = math.max(max_add, #tostring(entry.adds) + 1)
end
if entry.dels > 0 then
max_del = math.max(max_del, #tostring(entry.dels) + 1)
end
end
for _, entry in ipairs(entries) do
local padded = entry.display_file .. string.rep(' ', max_fname - #entry.display_file)
local parts = { padded }
if max_add > 0 then
parts[#parts + 1] = entry.adds > 0 and string.format('%' .. max_add .. 's', '+' .. entry.adds)
or string.rep(' ', max_add)
end
if max_del > 0 then
parts[#parts + 1] = entry.dels > 0 and string.format('%' .. max_del .. 's', '-' .. entry.dels)
or string.rep(' ', max_del)
end
entry.text = table.concat(parts, ' '):gsub('%s+$', '')
end
end
---@param bufnr integer
---@param entries table[]
---@return table[]
local function quickfix_items(bufnr, entries)
format_file_text(entries)
local items = {}
for _, entry in ipairs(entries) do
items[#items + 1] = {
bufnr = bufnr,
lnum = entry.lnum,
col = 1,
text = entry.text,
user_data = {
diffs = {
kind = 'file',
file = entry.file,
key = entry.key,
section = entry.section,
section_label = entry.section_label,
diff_spec = entry.diff_spec,
skipped = entry.skipped == true,
},
},
}
end
return items
end
---@param entries table[]
---@param lnum integer
---@param sections? table[]
---@return table?
local function entry_at_lnum(entries, lnum, sections)
if #entries == 0 then
return nil
end
local section = nil
for _, candidate in ipairs(sections or {}) do
if lnum >= candidate.start and lnum <= candidate.finish then
section = candidate
break
end
end
local selected = nil
for _, entry in ipairs(entries) do
if section and entry.section ~= section.id then
goto continue
end
if lnum >= entry.lnum then
selected = entry
else
break
end
::continue::
end
if selected then
return selected
end
if section then
for _, entry in ipairs(entries) do
if entry.section == section.id then
return entry
end
end
end
return entries[1]
end
---@param hunks diffs.DiffHunk[]
---@param entries table[]
---@param lnum integer
---@return string?
local function file_at_lnum(hunks, entries, lnum)
local entry = entry_at_lnum(entries or file_entries(hunks), lnum)
return entry and entry.file or nil
end
---@param bufnr integer
---@param hunks diffs.DiffHunk[]
---@param entry table?
---@return table[]
local function loclist_items(bufnr, hunks, entry)
local file_hunk_count = {}
local key = entry and entry.key or nil
local file = entry and entry.file or nil
local items = {}
for _, hunk in ipairs(hunks) do
local hfile = hunk_file(hunk)
local hkey = hunk_key(hunk)
if (key and hkey == key) or (not key and (not file or hfile == file)) then
local count_key = hkey or hfile
file_hunk_count[count_key] = (file_hunk_count[count_key] or 0) + 1
local display = hunk.section_label and ('[' .. hunk.section_label .. '] ' .. hfile) or hfile
items[#items + 1] = {
bufnr = bufnr,
lnum = hunk.buffer_range.start,
col = 1,
text = ('%s (hunk %d) %s'):format(display, file_hunk_count[count_key], hunk.header),
user_data = {
diffs = {
kind = 'hunk',
file = hfile,
key = hkey,
section = hunk.section,
section_label = hunk.section_label,
diff_spec = hunk.diff_spec,
hunk = hunk.index,
},
},
}
end
end
return items
end
---@param hunks diffs.DiffHunk[]
---@param lnum integer
---@return diffs.DiffHunk?
local function hunk_at_lnum(hunks, lnum)
for _, hunk in ipairs(hunks) do
if lnum >= hunk.buffer_range.start and lnum <= hunk.buffer_range.finish then
return hunk
end
end
return nil
end
---@param hunks diffs.DiffHunk[]
---@param index integer?
---@return diffs.DiffHunk?
local function hunk_by_index(hunks, index)
if type(index) ~= 'number' then
return nil
end
for _, hunk in ipairs(hunks) do
if hunk.index == index then
return hunk
end
end
return nil
end
---@param hunks diffs.DiffHunk[]
---@param target diffs.DiffHunk?
---@return integer?
local function file_hunk_index(hunks, target)
if not target then
return nil
end
local count = 0
local file = hunk_file(target)
local key = hunk_key(target)
for _, hunk in ipairs(hunks) do
if (key and hunk_key(hunk) == key) or (not key and hunk_file(hunk) == file) then
count = count + 1
end
if hunk == target then
return count
end
end
return nil
end
---@param state table
---@param entry table
---@return diffs.GeneratedFileSelection
local function selection_from_entry(state, entry)
local hunk = entry.hunks[1]
return {
bufnr = 0,
file = entry.file,
key = (hunk and hunk_key(hunk)) or entry.key,
section = (hunk and hunk.section) or entry.section,
section_label = (hunk and hunk.section_label) or entry.section_label,
diff_spec = (hunk and hunk.diff_spec) or entry.diff_spec,
lnum = entry.lnum,
hunk = hunk,
hunk_index = file_hunk_index(state.hunks, hunk),
added = entry.adds,
removed = entry.dels,
skipped = entry.skipped == true,
}
end
---@param item table?
---@return table?
local function item_diffs_data(item)
if type(item) ~= 'table' or type(item.user_data) ~= 'table' then
return nil
end
local diffs = item.user_data.diffs
if type(diffs) == 'table' then
return diffs
end
return nil
end
---@param item table?
---@return table?, string?
local function selection_from_item(item)
local data = item_diffs_data(item)
if not data or (data.kind ~= 'file' and data.kind ~= 'hunk') then
return nil, 'quickfix item is not a generated diff file'
end
if type(item.bufnr) ~= 'number' or item.bufnr <= 0 then
return nil, 'quickfix item is missing a generated buffer'
end
if type(data.file) ~= 'string' or data.file == '' then
return nil, 'quickfix item is missing a generated diff file'
end
local state = generated_list_state[item.bufnr]
local hunk = state and hunk_by_index(state.hunks, data.hunk) or nil
local key = data.key or hunk_key(hunk) or data.file
return {
bufnr = item.bufnr,
file = data.file,
key = key,
section = data.section,
section_label = data.section_label,
diff_spec = data.diff_spec,
lnum = item.lnum,
hunk = hunk,
hunk_index = state and file_hunk_index(state.hunks, hunk) or nil,
skipped = data.skipped == true,
},
nil
end
---@param title string
---@param items table[]
local function set_quickfix(title, items)
M.ensure_quickfix_keymap()
vim.fn.setqflist({}, ' ', {
title = title,
items = items,
quickfixtextfunc = 'v:lua._diffs_qftf',
})
end
---@param win integer
---@param title string
---@param items table[]
local function set_loclist(win, title, items)
local nr = win_number(win)
if not nr then
return
end
M.ensure_quickfix_keymap()
vim.fn.setloclist(nr, {}, ' ', {
title = title,
items = items,
quickfixtextfunc = 'v:lua._diffs_qftf',
})
end
---@param diff_lines string[]
---@param opts? diffs.GeneratedListOptions
---@return { hunks: diffs.DiffHunk[], entries: table[], sections: table[]?, loclist_title: string }
local function list_state(diff_lines, opts)
opts = opts or {}
local diff_spec = opts.diff_spec
local hunks = hunk_model.parse(diff_lines, diff_spec)
local entries = file_entries(hunks, diff_lines, opts)
apply_skipped(entries, opts)
local title = opts.title or 'diffs'
return {
hunks = hunks,
entries = entries,
sections = opts.sections,
loclist_title = opts.loclist_title or (title .. ' hunks'),
}
end
---@param bufnr integer
---@param win integer
---@param force? boolean
local function refresh_loclist_for_window(bufnr, win, force)
local state = generated_list_state[bufnr]
if not state or not vim.api.nvim_win_is_valid(win) then
return
end
if vim.api.nvim_win_get_buf(win) ~= bufnr then
return
end
local cursor = vim.api.nvim_win_get_cursor(win)[1]
local entry = entry_at_lnum(state.entries, cursor, state.sections)
local key = entry and entry.key or nil
local cache = state.win_files or {}
state.win_files = cache
if not force and cache[win] == key then
return
end
cache[win] = key
set_loclist(win, state.loclist_title, loclist_items(bufnr, state.hunks, entry))
end
---@param bufnr integer
local function refresh_current_loclist(bufnr)
local win = vim.api.nvim_get_current_win()
refresh_loclist_for_window(bufnr, win, false)
end
---@param bufnr integer
local function ensure_generated_autocmds(bufnr)
if generated_list_autocmds[bufnr] then
return
end
local ids = {}
ids[#ids + 1] = vim.api.nvim_create_autocmd({ 'BufEnter', 'CursorMoved' }, {
group = group,
buffer = bufnr,
callback = function(args)
refresh_current_loclist(args.buf)
end,
})
ids[#ids + 1] = vim.api.nvim_create_autocmd('BufWipeout', {
group = group,
buffer = bufnr,
once = true,
callback = function(args)
generated_list_state[args.buf] = nil
generated_list_autocmds[args.buf] = nil
end,
})
generated_list_autocmds[bufnr] = ids
end
---@param item table?
local function sync_split_item(item)
if type(item) ~= 'table' or type(item.bufnr) ~= 'number' then
return
end
local source = get_buf_var(item.bufnr, 'diffs_source')
if type(source) ~= 'table' or source.kind ~= 'split_endpoint' then
return
end
require('diffs.split').sync_cursor_to_hunk(item.bufnr)
end
---@param info table
---@return table[]
local function quickfix_text_items(info)
if info.quickfix == 1 then
return vim.fn.getqflist({ id = info.id, items = 0 }).items
end
return vim.fn.getloclist(info.winid or 0, { id = info.id, items = 0 }).items
end
---@return table?, boolean
local function current_quickfix_item()
local info = vim.fn.getwininfo(vim.api.nvim_get_current_win())[1]
if type(info) ~= 'table' or info.quickfix ~= 1 then
return nil, false
end
local is_loclist = info.loclist == 1
local row = vim.api.nvim_win_get_cursor(0)[1]
local items = is_loclist and vim.fn.getloclist(0, { items = 0 }).items
or vim.fn.getqflist({ items = 0 }).items
return items[row], is_loclist
end
---@param bufnr integer
---@param explicit_lnum integer?
---@return integer
local function selection_lnum(bufnr, explicit_lnum)
if type(explicit_lnum) == 'number' then
return explicit_lnum
end
if vim.api.nvim_get_current_buf() == bufnr then
return vim.api.nvim_win_get_cursor(0)[1]
end
local win = windows_for_buffer(bufnr)[1]
if win then
return vim.api.nvim_win_get_cursor(win)[1]
end
return 1
end
local function jump_current_quickfix_item()
local item, is_loclist = current_quickfix_item()
local row = vim.api.nvim_win_get_cursor(0)[1]
local command = is_loclist and 'll' or 'cc'
local jumped = false
if type(item) == 'table' and type(item.bufnr) == 'number' then
local item_win = windows_for_buffer(item.bufnr)[1]
if item_win then
local lnum = math.max(1, math.min(item.lnum or 1, vim.api.nvim_buf_line_count(item.bufnr)))
local col = math.max(0, (item.col or 1) - 1)
vim.api.nvim_buf_set_var(item.bufnr, 'diffs_generated_jump_in_progress', true)
local ok, err = pcall(function()
vim.api.nvim_win_set_cursor(item_win, { lnum, col })
vim.api.nvim_set_current_win(item_win)
end)
pcall(vim.api.nvim_buf_del_var, item.bufnr, 'diffs_generated_jump_in_progress')
if not ok then
error(err, 0)
end
jumped = true
end
end
if not jumped then
vim.cmd(command .. ' ' .. row)
end
sync_split_item(item)
end
---@class diffs.GeneratedFileSelectionOpts
---@field bufnr? integer
---@field lnum? integer
---@field item? table
---@class diffs.GeneratedFileSelection
---@field bufnr integer
---@field file string
---@field key? string
---@field section? string
---@field section_label? string
---@field diff_spec? diffs.DiffSpec
---@field lnum integer?
---@field hunk? diffs.DiffHunk
---@field hunk_index? integer
---@field added? integer
---@field removed? integer
---@field skipped? boolean
---@param opts? diffs.GeneratedFileSelectionOpts
---@return diffs.GeneratedFileSelection?, string?
function M.selected_generated_file(opts)
opts = opts or {}
if opts.item then
return selection_from_item(opts.item)
end
if not opts.bufnr and not opts.lnum then
local item = current_quickfix_item()
if item then
return selection_from_item(item)
end
end
local bufnr = opts.bufnr or vim.api.nvim_get_current_buf()
local state = generated_list_state[bufnr]
if not state then
return nil, 'current buffer has no generated diff file index'
end
local lnum = selection_lnum(bufnr, opts.lnum)
local entry = entry_at_lnum(state.entries, lnum, state.sections)
if not entry then
return nil, 'no generated diff file selected'
end
local hunk = hunk_at_lnum(state.hunks, lnum)
return {
bufnr = bufnr,
file = entry.file,
key = (hunk and hunk_key(hunk)) or entry.key,
section = (hunk and hunk.section) or entry.section,
section_label = (hunk and hunk.section_label) or entry.section_label,
diff_spec = (hunk and hunk.diff_spec) or entry.diff_spec,
lnum = lnum,
hunk = hunk,
hunk_index = file_hunk_index(state.hunks, hunk),
skipped = entry.skipped == true,
},
nil
end
---@param bufnr integer
---@return table?
local function enter_keymap(bufnr)
for _, keymap in ipairs(vim.api.nvim_buf_get_keymap(bufnr, 'n')) do
if keymap.lhs == '<CR>' then
return keymap
end
end
return nil
end
---@param bufnr integer
local function set_quickfix_keymap(bufnr)
local existing = enter_keymap(bufnr)
if existing and existing.desc ~= quickfix_enter_desc then
return
end
vim.keymap.set('n', '<CR>', jump_current_quickfix_item, {
buffer = bufnr,
desc = quickfix_enter_desc,
})
end
function M.ensure_quickfix_keymap()
if quickfix_keymap_autocmd then
return
end
quickfix_keymap_autocmd = vim.api.nvim_create_autocmd('FileType', {
group = group,
pattern = 'qf',
callback = function(args)
set_quickfix_keymap(args.buf)
end,
})
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
if
vim.api.nvim_buf_is_valid(bufnr)
and vim.api.nvim_get_option_value('filetype', { buf = bufnr }) == 'qf'
then
set_quickfix_keymap(bufnr)
end
end
end
-- selene: allow(global_usage)
function _G._diffs_qftf(info)
local items = quickfix_text_items(info)
local max_lnum = 0
for i = info.start_idx, info.end_idx do
local e = items[i]
if e and e.lnum > 0 then
max_lnum = math.max(max_lnum, #tostring(e.lnum))
end
end
local lnum_fmt = '%' .. math.max(max_lnum, 1) .. 'd'
local lines = {}
for i = info.start_idx, info.end_idx do
local e = items[i] or {}
local text = e.text or ''
if max_lnum > 0 and e.lnum and e.lnum > 0 then
lines[#lines + 1] = ('%s %s'):format(lnum_fmt:format(e.lnum), text)
else
lines[#lines + 1] = text
end
end
return lines
end
---@param bufnr integer
---@param diff_lines string[]
---@param opts? diffs.GeneratedListOptions
function M.set_for_unified_buffer(bufnr, diff_lines, opts)
opts = opts or {}
local title = opts.title or 'diffs'
opts.diff_spec = opts.diff_spec or buffer_diff_spec(bufnr)
local state = list_state(diff_lines, opts)
if opts.store_hunks then
generated.set_hunks(bufnr, state.hunks)
end
generated_list_state[bufnr] = {
hunks = state.hunks,
entries = state.entries,
sections = state.sections,
loclist_title = state.loclist_title,
win_files = {},
}
ensure_generated_autocmds(bufnr)
local want_quickfix = opts.quickfix
if want_quickfix == nil then
want_quickfix = #state.entries > 1
end
if want_quickfix then
set_quickfix(title, quickfix_items(bufnr, state.entries))
end
for _, win in ipairs(windows_for_buffer(bufnr)) do
refresh_loclist_for_window(bufnr, win, true)
end
end
---@param diff_lines string[]
---@param opts? diffs.GeneratedListOptions
---@return diffs.GeneratedFileSelection?
function M.first_generated_file(diff_lines, opts)
local state = list_state(diff_lines, opts)
local entry = state.entries[1]
if not entry then
return nil
end
return selection_from_entry(state, entry)
end
---@param diff_lines string[]
---@param opts? diffs.GeneratedListOptions
---@return diffs.GeneratedFileSelection[]
function M.generated_files(diff_lines, opts)
local state = list_state(diff_lines, opts)
local files = {}
for _, entry in ipairs(state.entries) do
files[#files + 1] = selection_from_entry(state, entry)
end
return files
end
---@param bufnr integer
---@param diff_lines string[]
---@param opts? diffs.GeneratedListOptions
function M.set_loclist_for_buffer(bufnr, diff_lines, opts)
opts = opts or {}
local state = list_state(diff_lines, {
diff_spec = opts.diff_spec or buffer_diff_spec(bufnr),
title = opts.title,
loclist_title = opts.loclist_title,
})
local title = state.loclist_title
local entry = state.entries[1]
for _, win in ipairs(windows_for_buffer(bufnr)) do
set_loclist(win, title, loclist_items(bufnr, state.hunks, entry))
end
end
---@param hunk diffs.DiffHunk
---@param anchors integer[]?
---@return integer
local function split_anchor_lnum(hunk, anchors)
return (anchors and anchors[hunk.index]) or 1
end
---@param hunk diffs.DiffHunk
---@param side "left"|"right"
---@param left_buf integer
---@param right_buf integer
---@return integer
local function split_target_buf(hunk, side, left_buf, right_buf)
local range = side == 'left' and hunk.old_range or hunk.new_range
if range.count > 0 then
return side == 'left' and left_buf or right_buf
end
return side == 'left' and right_buf or left_buf
end
---@param left_buf integer
---@param right_buf integer
---@param hunks diffs.DiffHunk[]
---@param anchors integer[]?
---@param side "left"|"right"
---@return table[]
local function split_loclist_items(left_buf, right_buf, hunks, anchors, side)
local items = {}
for i, hunk in ipairs(hunks) do
items[#items + 1] = {
bufnr = split_target_buf(hunk, side, left_buf, right_buf),
lnum = split_anchor_lnum(hunk, anchors),
col = 1,
text = ('%s (hunk %d) %s'):format(hunk_file(hunk), i, hunk.header),
user_data = {
diffs = {
kind = 'split_hunk',
file = hunk_file(hunk),
hunk = i,
side = side,
},
},
}
end
return items
end
---@param opts { left_buf: integer, right_buf: integer, hunks: diffs.DiffHunk[], anchors: integer[]? }
---@return table[]
local function split_quickfix_items(opts)
local entries = file_entries(opts.hunks)
format_file_text(entries)
local items = {}
for _, entry in ipairs(entries) do
local first_hunk = entry.hunks[1]
local lnum = entry.lnum
local target_buf = opts.right_buf
if first_hunk then
lnum = split_anchor_lnum(first_hunk, opts.anchors)
target_buf = split_target_buf(first_hunk, 'right', opts.left_buf, opts.right_buf)
end
items[#items + 1] = {
bufnr = target_buf,
lnum = lnum,
col = 1,
text = entry.text,
user_data = {
diffs = {
kind = 'file',
file = entry.file,
},
},
}
end
return items
end
---@param opts { title: string, loclist_title?: string, left_buf: integer, right_buf: integer, left_win?: integer, right_win?: integer, hunks: diffs.DiffHunk[], anchors?: integer[], quickfix?: boolean }
function M.set_for_split_pair(opts)
local title = opts.title
local loclist_title = opts.loclist_title or (title .. ' hunks')
local quickfix_entries = split_quickfix_items(opts)
if opts.quickfix ~= false and #quickfix_entries > 1 then
set_quickfix(title, quickfix_entries)
end
for _, win in ipairs(opts.left_win and { opts.left_win } or windows_for_buffer(opts.left_buf)) do
set_loclist(
win,
loclist_title,
split_loclist_items(opts.left_buf, opts.right_buf, opts.hunks, opts.anchors, 'left')
)
end
for _, win in ipairs(opts.right_win and { opts.right_win } or windows_for_buffer(opts.right_buf)) do
set_loclist(
win,
loclist_title,
split_loclist_items(opts.left_buf, opts.right_buf, opts.hunks, opts.anchors, 'right')
)
end
end
M._test = {
file_at_lnum = file_at_lnum,
file_entries = file_entries,
hunk_at_lnum = hunk_at_lnum,
loclist_items = loclist_items,
quickfix_items = quickfix_items,
selected_generated_file = M.selected_generated_file,
}
return M