-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcommands.lua
More file actions
2457 lines (2221 loc) · 73.6 KB
/
Copy pathcommands.lua
File metadata and controls
2457 lines (2221 loc) · 73.6 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
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local M = {}
local actions = require('diffs.actions')
local content = require('diffs.content')
local diff_parser = require('diffs.diffargs')
local diffopt = require('diffs.diffopt')
local diffspec = require('diffs.spec')
local difftastic = require('diffs.difftastic')
local generated = require('diffs.generated')
local git = require('diffs.git')
local hunk_model = require('diffs.hunks')
local lists = require('diffs.lists')
local log = require('diffs.log')
local rails = require('diffs.rails')
local render = require('diffs.render')
local review = require('diffs.review')
local runtime = require('diffs.runtime')
local split = require('diffs.split')
local dbg = log.dbg
local notify = log.notify
local review_split_group = vim.api.nvim_create_augroup('diffs_review_split', { clear = false })
--- Paint difftastic structural intra spans onto a generated unified buffer and
--- emit the formatting-only notice when difft saw no structural change.
---@param diff_buf integer
---@param diff_lines string[]
---@param diff_spec diffs.DiffSpec?
---@param lhs table<integer, table[]>
---@param rhs table<integer, table[]>
local function paint_difft_unified(diff_buf, diff_lines, diff_spec, lhs, rhs)
difftastic.apply_unified(
diff_buf,
lhs,
rhs,
diff_lines,
diff_spec,
rails.width_for_buffer(diff_buf)
)
if not difftastic.has_changes(lhs, rhs) then
notify('difftastic: no structural changes (formatting only)', vim.log.levels.INFO)
end
end
--- Apply difftastic to a generated unified buffer built from two content
--- arrays. No-op when difftastic is disabled/unavailable or content is missing.
---@param diff_buf integer
---@param diff_lines string[]
---@param diff_spec diffs.DiffSpec?
---@param old_lines string[]?
---@param new_lines string[]?
---@param relpath string
local function apply_difft_unified(diff_buf, diff_lines, diff_spec, old_lines, new_lines, relpath)
if not difftastic.available() or not old_lines or not new_lines then
return
end
local lhs, rhs = difftastic.span_maps_for_content(old_lines, new_lines, relpath)
if lhs and rhs then
paint_difft_unified(diff_buf, diff_lines, diff_spec, lhs, rhs)
end
end
---@class diffs.HunkKeymap
---@field mode string
---@field lhs string
---@field callback function
---@type table<integer, diffs.HunkKeymap[]>
local hunk_keymaps = {}
---@type table<integer, integer>
local hunk_keymap_autocmds = {}
---@alias diffs.ReviewMapLayout "unified"|"stacked"
---@class diffs.ReviewSplitState
---@field left_buf integer
---@field right_buf integer
---@field left_win integer
---@field right_win integer
---@field review diffs.ReviewSpec
---@field repo_root string
---@field display string
---@field review_lines string[]
---@field list_opts table?
---@field map_layout diffs.ReviewMapLayout
---@field selected_file string
---@field selected_key string?
---@field selected_diff_spec diffs.DiffSpec
---@field autocmds integer[]
---@type table<integer, diffs.ReviewSplitState>
local review_split_states = {}
---@param bufnr integer
---@param mode string
---@param lhs string
---@return table?
local function get_buffer_keymap(bufnr, mode, lhs)
for _, keymap in ipairs(vim.api.nvim_buf_get_keymap(bufnr, mode)) do
if keymap.lhs == lhs then
return keymap
end
end
return nil
end
---@param bufnr integer
local function clear_hunk_keymaps(bufnr)
local registered = hunk_keymaps[bufnr]
if not registered then
return
end
for _, keymap in ipairs(registered) do
local current = get_buffer_keymap(bufnr, keymap.mode, keymap.lhs)
if current and current.callback == keymap.callback then
pcall(vim.keymap.del, keymap.mode, keymap.lhs, { buffer = bufnr })
end
end
hunk_keymaps[bufnr] = nil
end
---@param bufnr integer
local function ensure_hunk_keymap_cleanup(bufnr)
if hunk_keymap_autocmds[bufnr] then
return
end
hunk_keymap_autocmds[bufnr] = vim.api.nvim_create_autocmd('BufWipeout', {
buffer = bufnr,
once = true,
callback = function()
hunk_keymaps[bufnr] = nil
hunk_keymap_autocmds[bufnr] = nil
end,
})
end
---@param bufnr integer
---@param mode string
---@param lhs string
---@param callback function
---@param desc string
local function set_hunk_keymap(bufnr, mode, lhs, callback, desc)
if get_buffer_keymap(bufnr, mode, lhs) then
return
end
vim.keymap.set(mode, lhs, callback, { buffer = bufnr, desc = desc })
hunk_keymaps[bufnr] = hunk_keymaps[bufnr] or {}
hunk_keymaps[bufnr][#hunk_keymaps[bufnr] + 1] = {
mode = mode,
lhs = lhs,
callback = callback,
}
end
---@param bufnr integer
---@return integer, integer
local function visual_range(bufnr)
local start_line = vim.api.nvim_buf_get_mark(bufnr, '<')[1]
local finish_line = vim.api.nvim_buf_get_mark(bufnr, '>')[1]
return math.min(start_line, finish_line), math.max(start_line, finish_line)
end
---@return integer?
function M.find_diffs_window()
local tabpage = vim.api.nvim_get_current_tabpage()
for _, win in ipairs(vim.api.nvim_tabpage_list_wins(tabpage)) do
if vim.api.nvim_win_is_valid(win) then
local buf = vim.api.nvim_win_get_buf(win)
local name = vim.api.nvim_buf_get_name(buf)
if name:match('^diffs://') then
return win
end
end
end
return nil
end
---@param bufnr integer
---@return integer?
local function first_window_for_buffer(bufnr)
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
return win
end
end
return nil
end
---@param bufnr integer
function M.setup_diff_buf(bufnr)
vim.diagnostic.enable(false, { bufnr = bufnr })
if not get_buffer_keymap(bufnr, 'n', 'q') then
vim.keymap.set('n', 'q', '<cmd>close<CR>', { buffer = bufnr })
end
local has_hunks, parsed_hunks = generated.raw_hunks(bufnr)
if not has_hunks then
clear_hunk_keymaps(bufnr)
return
end
clear_hunk_keymaps(bufnr)
local can_put = false
local can_obtain = false
for _, hunk in ipairs(type(parsed_hunks) == 'table' and parsed_hunks or {}) do
can_put = can_put or hunk.can_put == true
can_obtain = can_obtain or hunk.can_obtain == true
end
set_hunk_keymap(bufnr, 'n', ']c', function()
hunk_model.goto_next(bufnr)
end, 'Next diff hunk')
set_hunk_keymap(bufnr, 'n', '[c', function()
hunk_model.goto_prev(bufnr)
end, 'Previous diff hunk')
set_hunk_keymap(bufnr, 'n', '<CR>', function()
hunk_model.open_source(bufnr)
end, 'Open source file')
if can_obtain then
set_hunk_keymap(bufnr, 'n', 'do', function()
if actions.obtain_hunk(bufnr) then
M.read_buffer(bufnr)
end
end, 'Unstage diff hunk')
set_hunk_keymap(bufnr, 'x', 'do', function()
local range_start, range_finish = visual_range(bufnr)
if actions.obtain_range(bufnr, range_start, range_finish) then
M.read_buffer(bufnr)
end
end, 'Unstage selected diff lines')
end
if can_put then
set_hunk_keymap(bufnr, 'n', 'dp', function()
if actions.put_hunk(bufnr) then
M.read_buffer(bufnr)
end
end, 'Stage diff hunk')
set_hunk_keymap(bufnr, 'x', 'dp', function()
local range_start, range_finish = visual_range(bufnr)
if actions.put_range(bufnr, range_start, range_finish) then
M.read_buffer(bufnr)
end
end, 'Stage selected diff lines')
end
ensure_hunk_keymap_cleanup(bufnr)
end
---@param diff_lines string[]
---@param hunk_position { hunk_header: string, offset: integer }
---@return integer?
function M.find_hunk_line(diff_lines, hunk_position)
for i, line in ipairs(diff_lines) do
if line == hunk_position.hunk_header then
return i + hunk_position.offset
end
end
return nil
end
---@param lines string[]
---@return string[]
function M.filter_combined_diffs(lines)
local result = {}
local skip = false
for _, line in ipairs(lines) do
if line:match('^diff %-%-cc ') then
skip = true
elseif line:match('^diff %-%-git ') then
skip = false
end
if not skip then
table.insert(result, line)
end
end
return result
end
---@param diff_spec diffs.DiffSpec
---@return string
local function diff_buffer_label(diff_spec)
diff_spec = diffspec.new(diff_spec)
local left = diff_spec.left
local right = diff_spec.right
if
left.kind == diffspec.endpoint_kind.index and right.kind == diffspec.endpoint_kind.worktree
then
return 'unstaged'
end
if
left.kind == diffspec.endpoint_kind.tree
and left.rev == 'HEAD'
and right.kind == diffspec.endpoint_kind.index
then
return 'staged'
end
if left.kind == diffspec.endpoint_kind.tree and right.kind == diffspec.endpoint_kind.worktree then
return left.rev
end
if
left.kind == diffspec.endpoint_kind.stage and right.kind == diffspec.endpoint_kind.worktree
then
return 'stage' .. left.stage
end
return diffspec.label(diff_spec)
end
---@param bufnr integer
---@param diff_spec diffs.DiffSpec
local function set_diff_spec_var(bufnr, diff_spec)
generated.set_spec(bufnr, diff_spec)
end
---@param bufnr integer
---@param diff_lines string[]
---@param diff_spec diffs.DiffSpec
local function set_diff_hunks_var(bufnr, diff_lines, diff_spec)
generated.set_hunks_from_lines(bufnr, diff_lines, diff_spec)
end
---@param bufnr integer
---@param info diffs.RailInfo?
local function set_diff_rails_var(bufnr, info)
if info then
vim.api.nvim_buf_set_var(bufnr, 'diffs_rail_width', info.prefix_width)
vim.api.nvim_buf_set_var(bufnr, 'diffs_rail_separator_width', info.separator_width)
if info.style == 'single' or info.style == 'dual' then
vim.api.nvim_buf_set_var(bufnr, 'diffs_rail_style', info.style)
else
pcall(vim.api.nvim_buf_del_var, bufnr, 'diffs_rail_style')
end
else
pcall(vim.api.nvim_buf_del_var, bufnr, 'diffs_rail_width')
pcall(vim.api.nvim_buf_del_var, bufnr, 'diffs_rail_separator_width')
pcall(vim.api.nvim_buf_del_var, bufnr, 'diffs_rail_style')
end
end
---@param bufnr integer
local function clear_diff_hunks_var(bufnr)
generated.clear_hunks(bufnr)
end
---@param bufnr integer
---@return diffs.DiffSpec?, string?
local function get_diff_spec_var(bufnr)
return generated.spec(bufnr)
end
---@param bufnr integer
---@return diffs.GeneratedBufferSource?, string?
local function get_source_var(bufnr)
return generated.source(bufnr)
end
---@param bufnr integer
local function set_generated_diff_buffer_options(bufnr)
vim.api.nvim_set_option_value('buftype', 'nowrite', { buf = bufnr })
vim.api.nvim_set_option_value('bufhidden', 'delete', { buf = bufnr })
vim.api.nvim_set_option_value('swapfile', false, { buf = bufnr })
vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr })
end
---@param bufnr integer
local function set_generated_diff_buffer_filetype(bufnr)
vim.api.nvim_set_option_value('filetype', 'diff', { buf = bufnr })
end
---@param win integer
local function clear_generated_diff_window_bindings(win)
if not vim.api.nvim_win_is_valid(win) then
return
end
vim.api.nvim_set_option_value('scrollbind', false, { win = win })
vim.api.nvim_set_option_value('cursorbind', false, { win = win })
end
---@class diffs.GeneratedDiffBufferOpts
---@field name string
---@field lines string[]
---@field repo_root? string
---@field diff_spec? diffs.DiffSpec
---@field source? diffs.GeneratedBufferSource
---@field vars? table<string, any>
---@field rail_style? diffs.RailStyle
---@param opts diffs.GeneratedDiffBufferOpts
---@return integer
local function create_generated_diff_buffer(opts)
local bufnr = vim.api.nvim_create_buf(false, true)
local display_lines, rail_info = rails.annotate(opts.lines, {
rail_separator = runtime.get_view_config().rail_separator,
rail_style = opts.rail_style,
})
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, display_lines)
vim.api.nvim_buf_set_name(bufnr, opts.name)
set_diff_rails_var(bufnr, rail_info)
if opts.diff_spec then
set_diff_spec_var(bufnr, opts.diff_spec)
set_diff_hunks_var(bufnr, opts.lines, opts.diff_spec)
end
if opts.repo_root then
generated.set_repo_root(bufnr, opts.repo_root)
end
if opts.source then
generated.set_source(bufnr, opts.source)
end
for name, value in pairs(opts.vars or {}) do
if value ~= nil then
vim.api.nvim_buf_set_var(bufnr, name, value)
end
end
set_generated_diff_buffer_options(bufnr)
set_generated_diff_buffer_filetype(bufnr)
return bufnr
end
---@param bufnr integer
---@param vertical? boolean
---@param replace_win? integer
---@return boolean
local function show_generated_diff_buffer(bufnr, vertical, replace_win)
local replace_requested = type(replace_win) == 'number'
if replace_requested and not vim.api.nvim_win_is_valid(replace_win) then
notify('replacement diff window is no longer valid', vim.log.levels.WARN)
return false
end
local existing_win = replace_requested and replace_win or M.find_diffs_window()
if existing_win then
vim.api.nvim_set_current_win(existing_win)
split.release_pair_window_options(existing_win)
clear_generated_diff_window_bindings(existing_win)
vim.api.nvim_win_set_buf(existing_win, bufnr)
else
vim.cmd(vertical and 'vsplit' or 'split')
clear_generated_diff_window_bindings(vim.api.nvim_get_current_win())
vim.api.nvim_win_set_buf(0, bufnr)
end
return true
end
---@param bufnr integer
---@param diff_lines string[]
---@param diff_spec? diffs.DiffSpec
---@param opts? { rail_style?: diffs.RailStyle }
local function replace_generated_diff_buffer_lines(bufnr, diff_lines, diff_spec, opts)
opts = opts or {}
difftastic.clear_active(bufnr)
local display_lines, rail_info = rails.annotate(diff_lines, {
rail_separator = runtime.get_view_config().rail_separator,
rail_style = opts.rail_style or rails.style_for_buffer(bufnr),
})
vim.api.nvim_set_option_value('modifiable', true, { buf = bufnr })
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, display_lines)
set_diff_rails_var(bufnr, rail_info)
if diff_spec then
set_diff_hunks_var(bufnr, diff_lines, diff_spec)
else
clear_diff_hunks_var(bufnr)
end
set_generated_diff_buffer_options(bufnr)
set_generated_diff_buffer_filetype(bufnr)
runtime.refresh(bufnr)
end
---@param bufnr integer
local function attach_generated_diff_buffer(bufnr)
M.setup_diff_buf(bufnr)
vim.schedule(function()
runtime.attach(bufnr)
end)
end
---@param raw_lines string[]
---@param repo_root string
---@return string[]
local function replace_combined_diffs(raw_lines, repo_root)
local unmerged_files = {}
for _, line in ipairs(raw_lines) do
local cc_file = line:match('^diff %-%-cc (.+)$')
if cc_file then
table.insert(unmerged_files, cc_file)
end
end
local result = M.filter_combined_diffs(raw_lines)
for _, filename in ipairs(unmerged_files) do
local filepath = repo_root .. '/' .. filename
local old_lines = git.get_file_content(':2', filepath) or {}
local new_lines = git.get_file_content(':3', filepath) or {}
local diff_lines = render.unified_lines(old_lines, new_lines, filename, filename)
for _, dl in ipairs(diff_lines) do
table.insert(result, dl)
end
end
return result
end
---@class diffs.ReviewDepsOpts
---@field rail_style? diffs.RailStyle
---@field review_layout? diffs.ReviewMapLayout
---@field replace_win? integer
---@param layout? string
---@return diffs.ReviewMapLayout
local function normalize_review_map_layout(layout)
return layout == 'stacked' and 'stacked' or 'unified'
end
---@param rail_style? diffs.RailStyle
---@return diffs.ReviewMapLayout
local function review_layout_for_rail_style(rail_style)
return rail_style == 'single' and 'stacked' or 'unified'
end
---@param bufnr integer
---@param display string
---@param layout diffs.ReviewMapLayout
local function setup_review_map_buffer(bufnr, display, layout)
vim.b[bufnr].diffs_review = { display = display, layout = layout }
if not get_buffer_keymap(bufnr, 'n', 'gs') then
vim.keymap.set(
'n',
'gs',
'<Plug>(diffs-review-toggle-layout)',
{ buffer = bufnr, remap = true }
)
end
end
---@param opts? diffs.ReviewDepsOpts
---@return diffs.ReviewDeps
local function review_deps(opts)
opts = opts or {}
return {
create_generated_diff_buffer = create_generated_diff_buffer,
show_generated_diff_buffer = function(bufnr, vertical)
return show_generated_diff_buffer(bufnr, vertical, opts.replace_win)
end,
attach_generated_diff_buffer = attach_generated_diff_buffer,
replace_combined_diffs = replace_combined_diffs,
rail_style = opts.rail_style,
review_layout = opts.review_layout or review_layout_for_rail_style(opts.rail_style),
setup_review_map_buffer = setup_review_map_buffer,
}
end
---@param repo_root string
---@param section "staged"|"unstaged"
---@return string[]
local function render_section_source(repo_root, section)
local cmd = {
'git',
'-C',
repo_root,
'diff',
'--no-ext-diff',
'--no-color',
'--src-prefix=a/',
'--dst-prefix=b/',
}
vim.list_extend(cmd, diffopt.git_flags())
if section == 'staged' then
table.insert(cmd, '--cached')
end
local diff_lines = vim.fn.systemlist(cmd)
if vim.v.shell_error ~= 0 then
diff_lines = {}
end
return replace_combined_diffs(diff_lines, repo_root)
end
---@param source diffs.GeneratedBufferSource
---@return string[]?, diffs.DiffSpec?, string?, table?
local function render_source(source)
if source.kind == 'file' then
local diff_lines, read_err = render.file(source.spec, source.repo_root, {
empty_on_missing = true,
})
if not diff_lines then
return nil, nil, read_err, nil
end
return diff_lines, source.spec, diffspec.label(source.spec), nil
end
if source.kind == 'files' then
local old_lines = git.get_working_content(source.left_path)
if not old_lines then
return nil, nil, source.left_name .. ': file not readable', nil
end
local new_lines = git.get_working_content(source.right_path)
if not new_lines then
return nil, nil, source.right_name .. ': file not readable', nil
end
return render.unified_lines(old_lines, new_lines, source.left_name, source.right_name),
nil,
'files:' .. source.left_name .. ' -> ' .. source.right_name,
nil
end
if source.kind == 'file_pair' then
local abs_path = source.repo_root .. '/' .. source.path
local old_abs_path = source.repo_root .. '/' .. source.old_path
local old_lines, new_lines
if source.edge == 'staged' then
old_lines = git.get_file_content('HEAD', old_abs_path) or {}
new_lines = git.get_index_content(abs_path) or {}
else
old_lines = git.get_index_content(old_abs_path)
if not old_lines then
old_lines = git.get_file_content('HEAD', old_abs_path) or {}
end
new_lines = git.get_working_content(abs_path) or {}
end
return render.unified_lines(old_lines, new_lines, source.old_path, source.path),
nil,
source.edge .. ':' .. source.path,
nil
end
if source.kind == 'section' then
return render_section_source(source.repo_root, source.section),
nil,
source.section .. ':all',
nil
end
if source.kind == 'review' then
local review_lines, review_err, list_opts = review.reload_source(source, review_deps())
if not review_lines then
return nil, nil, review_err, nil
end
return review_lines, nil, 'review', list_opts
end
local abs_path = source.repo_root .. '/' .. source.path
local old_lines = git.get_file_content(':2', abs_path) or {}
local new_lines = git.get_file_content(':3', abs_path) or {}
return render.unified_lines(old_lines, new_lines, source.path, source.path),
nil,
'unmerged:' .. source.path,
nil
end
---@param diff_label string
---@param rel_path string
---@param old_rel_path string
---@return string
local function file_pair_label(diff_label, rel_path, old_rel_path)
return diff_label .. ' rename/copy ' .. old_rel_path .. ' -> ' .. rel_path
end
---@param spec? diffs.ReviewSpec
---@param opts? diffs.ReviewDepsOpts
---@return integer?
function M.review(spec, opts)
return review.open(spec, review_deps(opts))
end
---@param buf integer
---@param lnum integer
---@return string?
function M.review_file_at_line(buf, lnum)
return review.file_at_line(buf, lnum)
end
local layout_options = {
'++layout=unified',
'++layout=stacked',
'++layout=split',
}
local untracked_options = {
'++nountracked',
}
---@param layout? "unified"|"stacked"|"split"
---@return diffs.RailStyle
local function rail_style_for_layout(layout)
if layout == 'stacked' then
return 'single'
end
return 'dual'
end
local function warn_vertical_split_ignored()
notify(
'++layout=split ignores the :vertical modifier; the split layout manages its own windows',
vim.log.levels.WARN
)
end
-- Always-available object literals offered in completion. Merge-stage objects
-- (`:1:%`/`:2:%`/`:3:%`) are valid only during a conflict, so they are accepted
-- by the parser but not advertised here.
local diff_objects = {
':',
':%',
':0:%',
'@:%',
}
local command_names = {
Diff = true,
}
---@param value string
---@param prefix string
---@return boolean
local function starts_with(value, prefix)
return value:find(prefix, 1, true) == 1
end
---@param candidates string[]
---@param arglead string
---@return string[]
local function prefix_matches(candidates, arglead)
local matches = {}
for _, candidate in ipairs(candidates) do
if starts_with(candidate, arglead) then
matches[#matches + 1] = candidate
end
end
return matches
end
---@param arglead string
---@return string[]
local function complete_diff_object(arglead)
local matches = prefix_matches(diff_objects, arglead)
for _, ref in ipairs(review.complete(arglead)) do
if not ref:find('..', 1, true) then
matches[#matches + 1] = ref
end
end
return matches
end
---@param arglead string
---@param cmdline? string
---@param cursorpos? integer
---@return string[] # completed argument tokens before the cursor, excluding the command name
local function command_arg_tokens(arglead, cmdline, cursorpos)
local before = cmdline or ''
if type(cursorpos) == 'number' and cursorpos > 0 then
before = before:sub(1, cursorpos)
end
if arglead ~= '' and before:sub(-#arglead) == arglead then
before = before:sub(1, #before - #arglead)
end
local tokens = vim.split(vim.trim(before), '%s+', { trimempty = true })
local command_index = 0
for i = #tokens, 1, -1 do
if command_names[tokens[i]] then
command_index = i
break
end
end
if command_index == 0 and #tokens > 0 then
command_index = 1
end
local args = {}
for i = command_index + 1, #tokens do
args[#args + 1] = tokens[i]
end
return args
end
---@param args string[]
---@param from? integer # first argument index to scan (defaults to 1)
---@return { has_layout: boolean, has_untracked: boolean, has_value: boolean }
local function args_context(args, from)
local has_layout = false
local has_untracked = false
local has_value = false
for i = from or 1, #args do
local token = args[i]
if token:match('^%+%+layout=') then
has_layout = true
elseif token == '++nountracked' then
has_untracked = true
elseif not token:match('^%+%+') then
has_value = true
end
end
return {
has_layout = has_layout,
has_untracked = has_untracked,
has_value = has_value,
}
end
---@param arglead string
---@param cmdline? string
---@param cursorpos? integer
---@return { has_layout: boolean, has_untracked: boolean, has_value: boolean }
local function completion_context(arglead, cmdline, cursorpos)
return args_context(command_arg_tokens(arglead, cmdline, cursorpos))
end
---@param arglead string
---@param cmdline? string
---@param cursorpos? integer
---@return string[]
local function complete_diff_args(arglead, cmdline, cursorpos)
local context = completion_context(arglead, cmdline, cursorpos)
if context.has_value then
return {}
end
if arglead:match('^%+%+') then
if context.has_layout then
return {}
end
return prefix_matches(layout_options, arglead)
end
local matches = {}
if arglead == '' and not context.has_layout then
vim.list_extend(matches, layout_options)
end
vim.list_extend(matches, complete_diff_object(arglead))
return matches
end
---@param arglead string
---@param context { has_layout: boolean, has_untracked: boolean, has_value: boolean }
---@return string[]
local function complete_review_args(arglead, context)
if context.has_value then
return {}
end
if arglead:match('^%+%+') then
local matches = {}
if not context.has_layout then
vim.list_extend(matches, prefix_matches(layout_options, arglead))
end
if not context.has_untracked then
vim.list_extend(matches, prefix_matches(untracked_options, arglead))
end
vim.list_extend(matches, review.complete(arglead))
return matches
end
local matches = {}
if arglead == '' and not context.has_layout then
vim.list_extend(matches, layout_options)
end
if arglead == '' and not context.has_untracked then
vim.list_extend(matches, untracked_options)
end
vim.list_extend(matches, review.complete(arglead))
return matches
end
local files_layout_options = {
'++layout=unified',
'++layout=stacked',
}
---@param arglead string
---@return string[]
local function complete_files_args(arglead)
if arglead:match('^%+%+') then
return prefix_matches(files_layout_options, arglead)
end
return vim.fn.getcompletion(arglead, 'file')
end
---@param arglead string
---@param cmdline? string
---@param cursorpos? integer
---@return string[]
local function complete_diff_command(arglead, cmdline, cursorpos)
local args = command_arg_tokens(arglead, cmdline, cursorpos)
if args[1] == 'review' then
return complete_review_args(arglead, args_context(args, 2))
end
if args[1] == 'files' then
return complete_files_args(arglead)
end
local matches = {}
if #args == 0 and not arglead:match('^%+%+') then
if starts_with('review', arglead) then
matches[#matches + 1] = 'review'
end
if starts_with('files', arglead) then
matches[#matches + 1] = 'files'
end
end
vim.list_extend(matches, complete_diff_args(arglead, cmdline, cursorpos))
return matches
end
---@class diffs.OpenReviewSplitOpts
---@field selection? diffs.GeneratedFileSelection
---@field replace_win? integer
---@field map_layout? diffs.ReviewMapLayout
---@type fun(spec?: diffs.ReviewSpec, opts?: diffs.OpenReviewSplitOpts): integer?
local open_review_split
---@param args? string
---@param vertical? boolean
---@param opts? { warn_vertical_split?: boolean }
---@return integer?
function M.review_command(args, vertical, opts)
opts = opts or {}
local parsed, err = review.parse_command_args(args)
if not parsed then
notify(err, vim.log.levels.ERROR)
return nil
end
if parsed.layout == 'split' then
if opts.warn_vertical_split then
warn_vertical_split_ignored()
end
return open_review_split(parsed.spec)
end
parsed.spec.vertical = vertical or false
local bufnr = M.review(parsed.spec, {
rail_style = rail_style_for_layout(parsed.layout),
review_layout = normalize_review_map_layout(parsed.layout),
})
return bufnr
end
--- Primary `:Diff` command handler. Routes `:Diff review ...` to the review
--- surface and everything else to the current-file diff, threading the
--- `:vertical` modifier through to generated layouts.
---@param args? string
---@param vertical? boolean
---@return integer?
function M.diff_command(args, vertical)
vertical = vertical or false
if args then
local sub, remainder = args:match('^%s*(%S+)%s*(.*)$')
if sub == 'review' then
return M.review_command(
remainder ~= '' and remainder or nil,
vertical,
{ warn_vertical_split = vertical }
)
end
if sub == 'files' then
return M.diff_files_command(remainder ~= '' and remainder or nil, vertical)
end
end
return M.diff(args, vertical, { warn_vertical_split = vertical })
end
---@param args? string
---@param vertical? boolean
---@return integer?
function M.diff_files_command(args, vertical)
local parsed, err = diff_parser.parse_files(args)
if not parsed then
notify(err, vim.log.levels.ERROR)
return nil
end
local layout = parsed.layout
if layout == 'split' then
notify(
'split layout is not supported for :Diff files; use nvim -d or :diffsplit for side-by-side',
vim.log.levels.ERROR
)
return nil
end
return M.diff_files(parsed.left, parsed.right, {
layout = layout,
vertical = vertical,
})
end
---@class diffs.DiffFilesViewOpts
---@field layout "unified"|"stacked"|"split"
---@field vertical? boolean
---@param left string