@@ -2,6 +2,95 @@ local u = require("gitlab.utils")
22
33local M = {}
44
5+ local function exit (popup , opts )
6+ if opts .action_before_exit and opts .cb ~= nil then
7+ opts .cb ()
8+ popup :unmount ()
9+ else
10+ popup :unmount ()
11+ if opts .cb ~= nil then
12+ opts .cb ()
13+ end
14+ end
15+ end
16+
17+ -- These keymaps are buffer specific and are set dynamically when popups mount
18+ M .set_popup_keymaps = function (popup , action , linewise_action , opts )
19+ local settings = require (" gitlab.state" ).settings
20+ if settings .keymaps .disable_all or settings .keymaps .popup .disable_all then
21+ return
22+ end
23+
24+ if opts == nil then
25+ opts = {}
26+ end
27+ if action ~= " Help" and settings .keymaps .help then -- Don't show help on the help popup
28+ vim .keymap .set (" n" , settings .keymaps .help , function ()
29+ local help = require (" gitlab.actions.help" )
30+ help .open ()
31+ end , { buffer = popup .bufnr , desc = " Open help" , nowait = settings .keymaps .help_nowait })
32+ end
33+ if action ~= nil and settings .keymaps .popup .perform_action then
34+ vim .keymap .set (" n" , settings .keymaps .popup .perform_action , function ()
35+ local text = u .get_buffer_text (popup .bufnr )
36+ if opts .action_before_close then
37+ action (text , popup .bufnr )
38+ exit (popup , opts )
39+ else
40+ exit (popup , opts )
41+ action (text , popup .bufnr )
42+ end
43+ end , { buffer = popup .bufnr , desc = " Perform action" , nowait = settings .keymaps .popup .perform_action_nowait })
44+ end
45+
46+ if linewise_action ~= nil and settings .keymaps .popup .perform_action then
47+ vim .keymap .set (" n" , settings .keymaps .popup .perform_linewise_action , function ()
48+ local bufnr = vim .api .nvim_get_current_buf ()
49+ local linnr = vim .api .nvim_win_get_cursor (0 )[1 ]
50+ local text = u .get_line_content (bufnr , linnr )
51+ linewise_action (text )
52+ end , {
53+ buffer = popup .bufnr ,
54+ desc = " Perform linewise action" ,
55+ nowait = settings .keymaps .popup .perform_linewise_action_nowait ,
56+ })
57+ end
58+
59+ if settings .keymaps .popup .discard_changes then
60+ vim .keymap .set (" n" , settings .keymaps .popup .discard_changes , function ()
61+ local temp_registers = settings .popup .temp_registers
62+ settings .popup .temp_registers = {}
63+ vim .cmd (" quit!" )
64+ settings .popup .temp_registers = temp_registers
65+ end , {
66+ buffer = popup .bufnr ,
67+ desc = " Quit discarding changes" ,
68+ nowait = settings .keymaps .popup .discard_changes_nowait ,
69+ })
70+ end
71+
72+ if opts .save_to_temp_register then
73+ vim .api .nvim_create_autocmd (" BufWinLeave" , {
74+ buffer = popup .bufnr ,
75+ callback = function ()
76+ local text = u .get_buffer_text (popup .bufnr )
77+ for _ , register in ipairs (settings .popup .temp_registers ) do
78+ vim .fn .setreg (register , text )
79+ end
80+ end ,
81+ })
82+ end
83+
84+ if opts .action_before_exit then
85+ vim .api .nvim_create_autocmd (" BufWinLeave" , {
86+ buffer = popup .bufnr ,
87+ callback = function ()
88+ exit (popup , opts )
89+ end ,
90+ })
91+ end
92+ end
93+
594--- Setup autocommands for the popup
695--- @param popup NuiPopup
796--- @param layout NuiLayout | nil
0 commit comments