-
-
Notifications
You must be signed in to change notification settings - Fork 836
/
init.lua
1584 lines (1427 loc) · 54.5 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
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
---@tag telescope.actions
---@config { ["module"] = "telescope.actions" }
---@brief [[
--- These functions are useful for people creating their own mappings.
---
--- Actions can be either normal functions that expect the `prompt_bufnr` as
--- first argument (1) or they can be a custom telescope type called "action" (2).
---
--- (1) The `prompt_bufnr` of a normal function denotes the identifier of your
--- picker which can be used to access the picker state. In practice, users
--- most commonly access from both picker and global state via the following:
--- <code>
--- -- for utility functions
--- local action_state = require "telescope.actions.state"
---
--- local actions = {}
--- actions.do_stuff = function(prompt_bufnr)
--- local current_picker = action_state.get_current_picker(prompt_bufnr) -- picker state
--- local entry = action_state.get_selected_entry()
--- end
--- </code>
---
--- See |telescope.actions.state| for more information.
---
--- (2) To transform a module of functions into a module of "action"s, you need
--- to do the following:
--- <code>
--- local transform_mod = require("telescope.actions.mt").transform_mod
---
--- local mod = {}
--- mod.a1 = function(prompt_bufnr)
--- -- your code goes here
--- -- You can access the picker/global state as described above in (1).
--- end
---
--- mod.a2 = function(prompt_bufnr)
--- -- your code goes here
--- end
--- mod = transform_mod(mod)
---
--- -- Now the following is possible. This means that actions a2 will be executed
--- -- after action a1. You can chain as many actions as you want.
--- local action = mod.a1 + mod.a2
--- action(bufnr)
--- </code>
---
--- Another interesting thing to do is that these actions now have functions you
--- can call. These functions include `:replace(f)`, `:replace_if(f, c)`,
--- `replace_map(tbl)` and `enhance(tbl)`. More information on these functions
--- can be found in the `developers.md` and `lua/tests/automated/action_spec.lua`
--- file.
---@brief ]]
local a = vim.api
local conf = require("telescope.config").values
local state = require "telescope.state"
local utils = require "telescope.utils"
local popup = require "plenary.popup"
local p_scroller = require "telescope.pickers.scroller"
local action_state = require "telescope.actions.state"
local action_utils = require "telescope.actions.utils"
local action_set = require "telescope.actions.set"
local entry_display = require "telescope.pickers.entry_display"
local from_entry = require "telescope.from_entry"
local transform_mod = require("telescope.actions.mt").transform_mod
local resolver = require "telescope.config.resolve"
local actions = setmetatable({}, {
__index = function(_, k)
error("Key does not exist for 'telescope.actions': " .. tostring(k))
end,
})
local append_to_history = function(prompt_bufnr)
action_state
.get_current_history()
:append(action_state.get_current_line(), action_state.get_current_picker(prompt_bufnr))
end
--- Move the selection to the next entry
---@param prompt_bufnr number: The prompt bufnr
actions.move_selection_next = function(prompt_bufnr)
action_set.shift_selection(prompt_bufnr, 1)
end
--- Move the selection to the previous entry
---@param prompt_bufnr number: The prompt bufnr
actions.move_selection_previous = function(prompt_bufnr)
action_set.shift_selection(prompt_bufnr, -1)
end
--- Move the selection to the entry that has a worse score
---@param prompt_bufnr number: The prompt bufnr
actions.move_selection_worse = function(prompt_bufnr)
local picker = action_state.get_current_picker(prompt_bufnr)
action_set.shift_selection(prompt_bufnr, p_scroller.worse(picker.sorting_strategy))
end
--- Move the selection to the entry that has a better score
---@param prompt_bufnr number: The prompt bufnr
actions.move_selection_better = function(prompt_bufnr)
local picker = action_state.get_current_picker(prompt_bufnr)
action_set.shift_selection(prompt_bufnr, p_scroller.better(picker.sorting_strategy))
end
--- Move to the top of the picker
---@param prompt_bufnr number: The prompt bufnr
actions.move_to_top = function(prompt_bufnr)
local current_picker = action_state.get_current_picker(prompt_bufnr)
current_picker:set_selection(
p_scroller.top(current_picker.sorting_strategy, current_picker.max_results, current_picker.manager:num_results())
)
end
--- Move to the middle of the picker
---@param prompt_bufnr number: The prompt bufnr
actions.move_to_middle = function(prompt_bufnr)
local current_picker = action_state.get_current_picker(prompt_bufnr)
current_picker:set_selection(
p_scroller.middle(current_picker.sorting_strategy, current_picker.max_results, current_picker.manager:num_results())
)
end
--- Move to the bottom of the picker
---@param prompt_bufnr number: The prompt bufnr
actions.move_to_bottom = function(prompt_bufnr)
local current_picker = action_state.get_current_picker(prompt_bufnr)
current_picker:set_selection(
p_scroller.bottom(current_picker.sorting_strategy, current_picker.max_results, current_picker.manager:num_results())
)
end
--- Add current entry to multi select
---@param prompt_bufnr number: The prompt bufnr
actions.add_selection = function(prompt_bufnr)
local current_picker = action_state.get_current_picker(prompt_bufnr)
current_picker:add_selection(current_picker:get_selection_row())
end
--- Remove current entry from multi select
---@param prompt_bufnr number: The prompt bufnr
actions.remove_selection = function(prompt_bufnr)
local current_picker = action_state.get_current_picker(prompt_bufnr)
current_picker:remove_selection(current_picker:get_selection_row())
end
--- Toggle current entry status for multi select
---@param prompt_bufnr number: The prompt bufnr
actions.toggle_selection = function(prompt_bufnr)
local current_picker = action_state.get_current_picker(prompt_bufnr)
current_picker:toggle_selection(current_picker:get_selection_row())
end
--- Multi select all entries.
--- - Note: selected entries may include results not visible in the results pop up.
---@param prompt_bufnr number: The prompt bufnr
actions.select_all = function(prompt_bufnr)
local current_picker = action_state.get_current_picker(prompt_bufnr)
action_utils.map_entries(prompt_bufnr, function(entry, _, row)
if not current_picker._multi:is_selected(entry) then
current_picker._multi:add(entry)
if current_picker:can_select_row(row) then
local caret = current_picker:update_prefix(entry, row)
if current_picker._selection_entry == entry and current_picker._selection_row == row then
current_picker.highlighter:hi_selection(row, caret:match "(.*%S)")
end
current_picker.highlighter:hi_multiselect(row, current_picker._multi:is_selected(entry))
end
end
end)
current_picker:get_status_updater(current_picker.prompt_win, current_picker.prompt_bufnr)()
end
--- Drop all entries from the current multi selection.
---@param prompt_bufnr number: The prompt bufnr
actions.drop_all = function(prompt_bufnr)
local current_picker = action_state.get_current_picker(prompt_bufnr)
action_utils.map_entries(prompt_bufnr, function(entry, _, row)
current_picker._multi:drop(entry)
if current_picker:can_select_row(row) then
local caret = current_picker:update_prefix(entry, row)
if current_picker._selection_entry == entry and current_picker._selection_row == row then
current_picker.highlighter:hi_selection(row, caret:match "(.*%S)")
end
current_picker.highlighter:hi_multiselect(row, current_picker._multi:is_selected(entry))
end
end)
current_picker:get_status_updater(current_picker.prompt_win, current_picker.prompt_bufnr)()
end
--- Toggle multi selection for all entries.
--- - Note: toggled entries may include results not visible in the results pop up.
---@param prompt_bufnr number: The prompt bufnr
actions.toggle_all = function(prompt_bufnr)
local current_picker = action_state.get_current_picker(prompt_bufnr)
action_utils.map_entries(prompt_bufnr, function(entry, _, row)
current_picker._multi:toggle(entry)
if current_picker:can_select_row(row) then
local caret = current_picker:update_prefix(entry, row)
if current_picker._selection_entry == entry and current_picker._selection_row == row then
current_picker.highlighter:hi_selection(row, caret:match "(.*%S)")
end
current_picker.highlighter:hi_multiselect(row, current_picker._multi:is_selected(entry))
end
end)
current_picker:get_status_updater(current_picker.prompt_win, current_picker.prompt_bufnr)()
end
--- Scroll the preview window up
---@param prompt_bufnr number: The prompt bufnr
actions.preview_scrolling_up = function(prompt_bufnr)
action_set.scroll_previewer(prompt_bufnr, -1)
end
--- Scroll the preview window down
---@param prompt_bufnr number: The prompt bufnr
actions.preview_scrolling_down = function(prompt_bufnr)
action_set.scroll_previewer(prompt_bufnr, 1)
end
--- Scroll the preview window to the left
---@param prompt_bufnr number: The prompt bufnr
actions.preview_scrolling_left = function(prompt_bufnr)
action_set.scroll_horizontal_previewer(prompt_bufnr, -1)
end
--- Scroll the preview window to the right
---@param prompt_bufnr number: The prompt bufnr
actions.preview_scrolling_right = function(prompt_bufnr)
action_set.scroll_horizontal_previewer(prompt_bufnr, 1)
end
--- Scroll the results window up
---@param prompt_bufnr number: The prompt bufnr
actions.results_scrolling_up = function(prompt_bufnr)
action_set.scroll_results(prompt_bufnr, -1)
end
--- Scroll the results window down
---@param prompt_bufnr number: The prompt bufnr
actions.results_scrolling_down = function(prompt_bufnr)
action_set.scroll_results(prompt_bufnr, 1)
end
--- Scroll the results window to the left
---@param prompt_bufnr number: The prompt bufnr
actions.results_scrolling_left = function(prompt_bufnr)
action_set.scroll_horizontal_results(prompt_bufnr, -1)
end
--- Scroll the results window to the right
---@param prompt_bufnr number: The prompt bufnr
actions.results_scrolling_right = function(prompt_bufnr)
action_set.scroll_horizontal_results(prompt_bufnr, 1)
end
--- Center the cursor in the window, can be used after selecting a file to edit
--- You can just map `actions.select_default + actions.center`
---@param prompt_bufnr number: The prompt bufnr
actions.center = function(prompt_bufnr)
vim.cmd ":normal! zz"
end
--- Perform default action on selection, usually something like<br>
--- `:edit <selection>`
---
--- i.e. open the selection in the current buffer
---@param prompt_bufnr number: The prompt bufnr
actions.select_default = {
pre = append_to_history,
action = function(prompt_bufnr)
return action_set.select(prompt_bufnr, "default")
end,
}
--- Perform 'horizontal' action on selection, usually something like<br>
---`:new <selection>`
---
--- i.e. open the selection in a new horizontal split
---@param prompt_bufnr number: The prompt bufnr
actions.select_horizontal = {
pre = append_to_history,
action = function(prompt_bufnr)
return action_set.select(prompt_bufnr, "horizontal")
end,
}
--- Perform 'vertical' action on selection, usually something like<br>
---`:vnew <selection>`
---
--- i.e. open the selection in a new vertical split
---@param prompt_bufnr number: The prompt bufnr
actions.select_vertical = {
pre = append_to_history,
action = function(prompt_bufnr)
return action_set.select(prompt_bufnr, "vertical")
end,
}
--- Perform 'tab' action on selection, usually something like<br>
---`:tabedit <selection>`
---
--- i.e. open the selection in a new tab
---@param prompt_bufnr number: The prompt bufnr
actions.select_tab = {
pre = append_to_history,
action = function(prompt_bufnr)
return action_set.select(prompt_bufnr, "tab")
end,
}
--- Perform 'drop' action on selection, usually something like<br>
---`:drop <selection>`
---
--- i.e. open the selection in a window
---@param prompt_bufnr number: The prompt bufnr
actions.select_drop = {
pre = append_to_history,
action = function(prompt_bufnr)
return action_set.select(prompt_bufnr, "drop")
end,
}
--- Perform 'tab drop' action on selection, usually something like<br>
---`:tab drop <selection>`
---
--- i.e. open the selection in a new tab
---@param prompt_bufnr number: The prompt bufnr
actions.select_tab_drop = {
pre = append_to_history,
action = function(prompt_bufnr)
return action_set.select(prompt_bufnr, "tab drop")
end,
}
-- TODO: consider adding float!
-- https://github.com/nvim-telescope/telescope.nvim/issues/365
--- Perform file edit on selection, usually something like<br>
--- `:edit <selection>`
---@param prompt_bufnr number: The prompt bufnr
actions.file_edit = function(prompt_bufnr)
return action_set.edit(prompt_bufnr, "edit")
end
--- Perform file split on selection, usually something like<br>
--- `:new <selection>`
---@param prompt_bufnr number: The prompt bufnr
actions.file_split = function(prompt_bufnr)
return action_set.edit(prompt_bufnr, "new")
end
--- Perform file vsplit on selection, usually something like<br>
--- `:vnew <selection>`
---@param prompt_bufnr number: The prompt bufnr
actions.file_vsplit = function(prompt_bufnr)
return action_set.edit(prompt_bufnr, "vnew")
end
--- Perform file tab on selection, usually something like<br>
--- `:tabedit <selection>`
---@param prompt_bufnr number: The prompt bufnr
actions.file_tab = function(prompt_bufnr)
return action_set.edit(prompt_bufnr, "tabedit")
end
actions.close_pum = function(_)
if 0 ~= vim.fn.pumvisible() then
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<c-y>", true, true, true), "n", true)
end
end
--- Close the Telescope window, usually used within an action
---@param prompt_bufnr number: The prompt bufnr
actions.close = function(prompt_bufnr)
local picker = action_state.get_current_picker(prompt_bufnr)
local original_win_id = picker.original_win_id
local cursor_valid, original_cursor = pcall(a.nvim_win_get_cursor, original_win_id)
actions.close_pum(prompt_bufnr)
require("telescope.pickers").on_close_prompt(prompt_bufnr)
pcall(a.nvim_set_current_win, original_win_id)
if cursor_valid and a.nvim_get_mode().mode == "i" and picker._original_mode ~= "i" then
pcall(a.nvim_win_set_cursor, original_win_id, { original_cursor[1], original_cursor[2] + 1 })
end
end
--- Close the Telescope window, usually used within an action<br>
--- Deprecated and no longer needed, does the same as |telescope.actions.close|. Might be removed in the future
---@deprecated
---@param prompt_bufnr number: The prompt bufnr
actions._close = function(prompt_bufnr)
actions.close(prompt_bufnr)
end
local set_edit_line = function(prompt_bufnr, fname, prefix, postfix)
postfix = vim.F.if_nil(postfix, "")
postfix = a.nvim_replace_termcodes(postfix, true, false, true)
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection(fname)
return
end
actions.close(prompt_bufnr)
a.nvim_feedkeys(prefix .. selection.value .. postfix, "n", true)
end
--- Set a value in the command line and don't run it, making it editable.
---@param prompt_bufnr number: The prompt bufnr
actions.edit_command_line = function(prompt_bufnr)
set_edit_line(prompt_bufnr, "actions.edit_command_line", ":")
end
--- Set a value in the command line and run it
---@param prompt_bufnr number: The prompt bufnr
actions.set_command_line = function(prompt_bufnr)
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection "actions.set_command_line"
return
end
actions.close(prompt_bufnr)
vim.fn.histadd("cmd", selection.value)
vim.cmd(selection.value)
end
--- Set a value in the search line and don't search for it, making it editable.
---@param prompt_bufnr number: The prompt bufnr
actions.edit_search_line = function(prompt_bufnr)
set_edit_line(prompt_bufnr, "actions.edit_search_line", "/")
end
--- Set a value in the search line and search for it
---@param prompt_bufnr number: The prompt bufnr
actions.set_search_line = function(prompt_bufnr)
set_edit_line(prompt_bufnr, "actions.set_search_line", "/", "<CR>")
end
--- Edit a register
---@param prompt_bufnr number: The prompt bufnr
actions.edit_register = function(prompt_bufnr)
local selection = action_state.get_selected_entry()
local picker = action_state.get_current_picker(prompt_bufnr)
vim.fn.inputsave()
local updated_value = vim.fn.input("Edit [" .. selection.value .. "] ❯ ", selection.content)
vim.fn.inputrestore()
if updated_value ~= selection.content then
vim.fn.setreg(selection.value, updated_value)
selection.content = updated_value
end
-- update entry in results table
-- TODO: find way to redraw finder content
for _, v in pairs(picker.finder.results) do
if v == selection then
v.content = updated_value
end
end
end
--- Paste the selected register into the buffer
---
--- Note: only meant to be used inside builtin.registers
---@param prompt_bufnr number: The prompt bufnr
actions.paste_register = function(prompt_bufnr)
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection "actions.paste_register"
return
end
actions.close(prompt_bufnr)
-- ensure that the buffer can be written to
if vim.api.nvim_buf_get_option(vim.api.nvim_get_current_buf(), "modifiable") then
vim.api.nvim_paste(selection.content, true, -1)
end
end
--- Insert a symbol into the current buffer (while switching to normal mode)
---@param prompt_bufnr number: The prompt bufnr
actions.insert_symbol = function(prompt_bufnr)
local symbol = action_state.get_selected_entry().value[1]
actions.close(prompt_bufnr)
vim.schedule(function()
vim.api.nvim_put({ symbol }, "", true, true)
end)
end
--- Insert a symbol into the current buffer and keeping the insert mode.
---@param prompt_bufnr number: The prompt bufnr
actions.insert_symbol_i = function(prompt_bufnr)
local symbol = action_state.get_selected_entry().value[1]
actions.close(prompt_bufnr)
vim.schedule(function()
vim.cmd [[startinsert]]
vim.api.nvim_put({ symbol }, "", true, true)
end)
end
-- TODO: Think about how to do this.
actions.insert_value = function(prompt_bufnr)
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection "actions.insert_value"
return
end
vim.schedule(function()
actions.close(prompt_bufnr)
end)
return selection.value
end
--- Ask user to confirm an action
---@param prompt string: The prompt for confirmation
---@param default_value string: The default value of user input
---@param yes_values table: List of positive user confirmations ({"y", "yes"} by default)
---@return boolean: Whether user confirmed the prompt
local function ask_to_confirm(prompt, default_value, yes_values)
yes_values = yes_values or { "y", "yes" }
default_value = default_value or ""
local confirmation = vim.fn.input(prompt, default_value)
confirmation = string.lower(confirmation)
if string.len(confirmation) == 0 then
return false
end
for _, v in pairs(yes_values) do
if v == confirmation then
return true
end
end
return false
end
--- Create and checkout a new git branch if it doesn't already exist
---@param prompt_bufnr number: The prompt bufnr
actions.git_create_branch = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local new_branch = action_state.get_current_line()
if new_branch == "" then
utils.notify("actions.git_create_branch", {
msg = "Missing the new branch name",
level = "ERROR",
})
else
local confirmation = ask_to_confirm(string.format("Create new branch '%s'? [y/n]: ", new_branch))
if not confirmation then
utils.notify("actions.git_create_branch", {
msg = string.format("branch creation canceled: '%s'", new_branch),
level = "INFO",
})
return
end
actions.close(prompt_bufnr)
local _, ret, stderr = utils.get_os_command_output({ "git", "checkout", "-b", new_branch }, cwd)
if ret == 0 then
utils.notify("actions.git_create_branch", {
msg = string.format("Switched to a new branch: %s", new_branch),
level = "INFO",
})
else
utils.notify("actions.git_create_branch", {
msg = string.format(
"Error when creating new branch: '%s' Git returned '%s'",
new_branch,
table.concat(stderr, " ")
),
level = "INFO",
})
end
end
end
--- Applies an existing git stash
---@param prompt_bufnr number: The prompt bufnr
actions.git_apply_stash = function(prompt_bufnr)
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection "actions.git_apply_stash"
return
end
actions.close(prompt_bufnr)
local _, ret, stderr = utils.get_os_command_output { "git", "stash", "apply", "--index", selection.value }
if ret == 0 then
utils.notify("actions.git_apply_stash", {
msg = string.format("applied: '%s' ", selection.value),
level = "INFO",
})
else
utils.notify("actions.git_apply_stash", {
msg = string.format("Error when applying: %s. Git returned: '%s'", selection.value, table.concat(stderr, " ")),
level = "ERROR",
})
end
end
--- Checkout an existing git branch
---@param prompt_bufnr number: The prompt bufnr
actions.git_checkout = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection "actions.git_checkout"
return
end
actions.close(prompt_bufnr)
local _, ret, stderr = utils.get_os_command_output({ "git", "checkout", selection.value }, cwd)
if ret == 0 then
utils.notify("actions.git_checkout", {
msg = string.format("Checked out: %s", selection.value),
level = "INFO",
})
vim.cmd "checktime"
else
utils.notify("actions.git_checkout", {
msg = string.format(
"Error when checking out: %s. Git returned: '%s'",
selection.value,
table.concat(stderr, " ")
),
level = "ERROR",
})
end
end
--- Switch to git branch.<br>
--- If the branch already exists in local, switch to that.
--- If the branch is only in remote, create new branch tracking remote and switch to new one.
---@param prompt_bufnr number: The prompt bufnr
actions.git_switch_branch = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection "actions.git_switch_branch"
return
end
actions.close(prompt_bufnr)
local pattern = "^refs/remotes/%w+/"
local branch = selection.value
if string.match(selection.refname, pattern) then
branch = string.gsub(selection.refname, pattern, "")
end
local _, ret, stderr = utils.get_os_command_output({ "git", "switch", branch }, cwd)
if ret == 0 then
utils.notify("actions.git_switch_branch", {
msg = string.format("Switched to: '%s'", branch),
level = "INFO",
})
else
utils.notify("actions.git_switch_branch", {
msg = string.format(
"Error when switching to: %s. Git returned: '%s'",
selection.value,
table.concat(stderr, " ")
),
level = "ERROR",
})
end
end
--- Action to rename selected git branch
--- @param prompt_bufnr number: The prompt bufnr
actions.git_rename_branch = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection "actions.git_rename_branch"
return
end
-- Keeps the selected branch name for the input that asks for the new branch name
local new_branch = vim.fn.input("New branch name: ", selection.value)
if new_branch == "" then
utils.notify("actions.git_rename_branch", {
msg = "Missing the new branch name",
level = "ERROR",
})
else
actions.close(prompt_bufnr)
local _, ret, stderr = utils.get_os_command_output({ "git", "branch", "-m", selection.value, new_branch }, cwd)
if ret == 0 then
utils.notify("actions.git_rename_branch", {
msg = string.format("Renamed branch: '%s'", selection.value),
level = "INFO",
})
else
utils.notify("actions.git_rename_branch", {
msg = string.format(
"Error when renaming branch: %s. Git returned: '%s'",
selection.value,
table.concat(stderr, " ")
),
level = "ERROR",
})
end
end
end
local function make_git_branch_action(opts)
return function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection(opts.action_name)
return
end
local should_confirm = opts.should_confirm
if should_confirm then
local confirmation = ask_to_confirm(string.format(opts.confirmation_question, selection.value), "y")
if not confirmation then
utils.notify(opts.action_name, {
msg = "action canceled",
level = "INFO",
})
return
end
end
actions.close(prompt_bufnr)
local _, ret, stderr = utils.get_os_command_output(opts.command(selection.value), cwd)
if ret == 0 then
utils.notify(opts.action_name, {
msg = string.format(opts.success_message, selection.value),
level = "INFO",
})
else
utils.notify(opts.action_name, {
msg = string.format(opts.error_message, selection.value, table.concat(stderr, " ")),
level = "ERROR",
})
end
end
end
--- Tell git to track the currently selected remote branch in Telescope
---@param prompt_bufnr number: The prompt bufnr
actions.git_track_branch = make_git_branch_action {
should_confirm = false,
action_name = "actions.git_track_branch",
success_message = "Tracking branch: %s",
error_message = "Error when tracking branch: %s. Git returned: '%s'",
command = function(branch_name)
return { "git", "checkout", "--track", branch_name }
end,
}
--- Delete all currently selected branches
---@param prompt_bufnr number: The prompt bufnr
actions.git_delete_branch = function(prompt_bufnr)
local confirmation = ask_to_confirm("Do you really want to delete the selected branches? [Y/n] ", "y")
if not confirmation then
utils.notify("actions.git_delete_branch", {
msg = "action canceled",
level = "INFO",
})
return
end
local picker = action_state.get_current_picker(prompt_bufnr)
local action_name = "actions.git_delete_branch"
picker:delete_selection(function(selection)
local branch = selection.value
print("Deleting branch " .. branch)
local _, ret, stderr = utils.get_os_command_output({ "git", "branch", "-D", branch }, picker.cwd)
if ret == 0 then
utils.notify(action_name, {
msg = string.format("Deleted branch: %s", branch),
level = "INFO",
})
else
utils.notify(action_name, {
msg = string.format("Error when deleting branch: %s. Git returned: '%s'", branch, table.concat(stderr, " ")),
level = "ERROR",
})
end
return ret == 0
end)
end
--- Merge the currently selected branch
---@param prompt_bufnr number: The prompt bufnr
actions.git_merge_branch = make_git_branch_action {
should_confirm = true,
action_name = "actions.git_merge_branch",
confirmation_question = "Do you really wanna merge branch %s? [Y/n] ",
success_message = "Merged branch: %s",
error_message = "Error when merging branch: %s. Git returned: '%s'",
command = function(branch_name)
return { "git", "merge", branch_name }
end,
}
--- Rebase to selected git branch
---@param prompt_bufnr number: The prompt bufnr
actions.git_rebase_branch = make_git_branch_action {
should_confirm = true,
action_name = "actions.git_rebase_branch",
confirmation_question = "Do you really wanna rebase branch %s? [Y/n] ",
success_message = "Rebased branch: %s",
error_message = "Error when rebasing branch: %s. Git returned: '%s'",
command = function(branch_name)
return { "git", "rebase", branch_name }
end,
}
local git_reset_branch = function(prompt_bufnr, mode)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection "actions.git_reset_branch"
return
end
local confirmation =
ask_to_confirm("Do you really wanna " .. mode .. " reset to " .. selection.value .. "? [Y/n] ", "y")
if not confirmation then
utils.notify("actions.git_reset_branch", {
msg = "action canceled",
level = "INFO",
})
return
end
actions.close(prompt_bufnr)
local _, ret, stderr = utils.get_os_command_output({ "git", "reset", mode, selection.value }, cwd)
if ret == 0 then
utils.notify("actions.git_rebase_branch", {
msg = string.format("Reset to: '%s'", selection.value),
level = "INFO",
})
else
utils.notify("actions.git_rebase_branch", {
msg = string.format("Rest to: %s. Git returned: '%s'", selection.value, table.concat(stderr, " ")),
level = "ERROR",
})
end
end
--- Reset to selected git commit using mixed mode
---@param prompt_bufnr number: The prompt bufnr
actions.git_reset_mixed = function(prompt_bufnr)
git_reset_branch(prompt_bufnr, "--mixed")
end
--- Reset to selected git commit using soft mode
---@param prompt_bufnr number: The prompt bufnr
actions.git_reset_soft = function(prompt_bufnr)
git_reset_branch(prompt_bufnr, "--soft")
end
--- Reset to selected git commit using hard mode
---@param prompt_bufnr number: The prompt bufnr
actions.git_reset_hard = function(prompt_bufnr)
git_reset_branch(prompt_bufnr, "--hard")
end
--- Checkout a specific file for a given sha
---@param prompt_bufnr number: The prompt bufnr
actions.git_checkout_current_buffer = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection "actions.git_checkout_current_buffer"
return
end
actions.close(prompt_bufnr)
utils.get_os_command_output({ "git", "checkout", selection.value, "--", selection.current_file }, cwd)
vim.cmd "checktime"
end
--- Stage/unstage selected file
---@param prompt_bufnr number: The prompt bufnr
actions.git_staging_toggle = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry()
if selection == nil then
utils.__warn_no_selection "actions.git_staging_toggle"
return
end
if selection.status:sub(2) == " " then
utils.get_os_command_output({ "git", "restore", "--staged", selection.value }, cwd)
else
utils.get_os_command_output({ "git", "add", selection.value }, cwd)
end
end
local entry_to_qf = function(entry)
local text = entry.text
if not text then
if type(entry.value) == "table" then
text = entry.value.text
else
text = entry.value
end
end
return {
bufnr = entry.bufnr,
filename = from_entry.path(entry, false, false),
lnum = vim.F.if_nil(entry.lnum, 1),
col = vim.F.if_nil(entry.col, 1),
text = text,
type = entry.qf_type,
}
end
local send_selected_to_qf = function(prompt_bufnr, mode, target)
local picker = action_state.get_current_picker(prompt_bufnr)
local qf_entries = {}
for _, entry in ipairs(picker:get_multi_selection()) do
table.insert(qf_entries, entry_to_qf(entry))
end
local prompt = picker:_get_prompt()
actions.close(prompt_bufnr)
vim.api.nvim_exec_autocmds("QuickFixCmdPre", {})
if target == "loclist" then
vim.fn.setloclist(picker.original_win_id, qf_entries, mode)
else
local qf_title = string.format([[%s (%s)]], picker.prompt_title, prompt)
vim.fn.setqflist(qf_entries, mode)
vim.fn.setqflist({}, "a", { title = qf_title })
end
vim.api.nvim_exec_autocmds("QuickFixCmdPost", {})
end
local send_all_to_qf = function(prompt_bufnr, mode, target)
local picker = action_state.get_current_picker(prompt_bufnr)
local manager = picker.manager
local qf_entries = {}
for entry in manager:iter() do
table.insert(qf_entries, entry_to_qf(entry))
end
local prompt = picker:_get_prompt()
actions.close(prompt_bufnr)
vim.api.nvim_exec_autocmds("QuickFixCmdPre", {})
local qf_title = string.format([[%s (%s)]], picker.prompt_title, prompt)
if target == "loclist" then
vim.fn.setloclist(picker.original_win_id, qf_entries, mode)
vim.fn.setloclist(picker.original_win_id, {}, "a", { title = qf_title })
else
vim.fn.setqflist(qf_entries, mode)
vim.fn.setqflist({}, "a", { title = qf_title })
end
vim.api.nvim_exec_autocmds("QuickFixCmdPost", {})
end
--- Sends the selected entries to the quickfix list, replacing the previous entries.
---@param prompt_bufnr number: The prompt bufnr
actions.send_selected_to_qflist = {
pre = append_to_history,
action = function(prompt_bufnr)
send_selected_to_qf(prompt_bufnr, " ")
end,
}
--- Adds the selected entries to the quickfix list, keeping the previous entries.
---@param prompt_bufnr number: The prompt bufnr
actions.add_selected_to_qflist = {
pre = append_to_history,
action = function(prompt_bufnr)
send_selected_to_qf(prompt_bufnr, "a")
end,
}
--- Sends all entries to the quickfix list, replacing the previous entries.
---@param prompt_bufnr number: The prompt bufnr
actions.send_to_qflist = {
pre = append_to_history,
action = function(prompt_bufnr)
send_all_to_qf(prompt_bufnr, " ")
end,
}
--- Adds all entries to the quickfix list, keeping the previous entries.
---@param prompt_bufnr number: The prompt bufnr
actions.add_to_qflist = {
pre = append_to_history,
action = function(prompt_bufnr)
send_all_to_qf(prompt_bufnr, "a")
end,
}
--- Sends the selected entries to the location list, replacing the previous entries.
---@param prompt_bufnr number: The prompt bufnr
actions.send_selected_to_loclist = {