Skip to content

Commit dc8e505

Browse files
committed
refactor: move set_popup_keymaps to popup module
1 parent 984de35 commit dc8e505

File tree

9 files changed

+105
-104
lines changed

9 files changed

+105
-104
lines changed

lua/gitlab/actions/comment.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,14 @@ M.create_comment_layout = function(opts)
232232
local unlinked = opts.unlinked or false
233233

234234
---Keybinding for focus on draft section
235-
state.set_popup_keymaps(M.draft_popup, function()
235+
popup.set_popup_keymaps(M.draft_popup, function()
236236
local text = u.get_buffer_text(M.comment_popup.bufnr)
237237
confirm_create_comment(text, range, unlinked, opts.discussion_id)
238238
vim.api.nvim_set_current_win(M.current_win)
239239
end, miscellaneous.toggle_bool, popup.non_editable_popup_opts)
240240

241241
---Keybinding for focus on text section
242-
state.set_popup_keymaps(M.comment_popup, function(text)
242+
popup.set_popup_keymaps(M.comment_popup, function(text)
243243
confirm_create_comment(text, range, unlinked, opts.discussion_id)
244244
vim.api.nvim_set_current_win(M.current_win)
245245
end, miscellaneous.attach_file, popup.editable_popup_opts)

lua/gitlab/actions/create_mr.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,12 @@ M.open_confirmation_popup = function(mr)
278278
action_before_exit = true,
279279
}
280280

281-
state.set_popup_keymaps(description_popup, M.create_mr, miscellaneous.attach_file, popup_opts)
282-
state.set_popup_keymaps(title_popup, M.create_mr, nil, popup_opts)
283-
state.set_popup_keymaps(target_popup, M.create_mr, M.select_new_target, popup_opts)
284-
state.set_popup_keymaps(delete_branch_popup, M.create_mr, miscellaneous.toggle_bool, popup_opts)
285-
state.set_popup_keymaps(squash_popup, M.create_mr, miscellaneous.toggle_bool, popup_opts)
286-
state.set_popup_keymaps(forked_project_id_popup, M.create_mr, nil, popup_opts)
281+
popup.set_popup_keymaps(description_popup, M.create_mr, miscellaneous.attach_file, popup_opts)
282+
popup.set_popup_keymaps(title_popup, M.create_mr, nil, popup_opts)
283+
popup.set_popup_keymaps(target_popup, M.create_mr, M.select_new_target, popup_opts)
284+
popup.set_popup_keymaps(delete_branch_popup, M.create_mr, miscellaneous.toggle_bool, popup_opts)
285+
popup.set_popup_keymaps(squash_popup, M.create_mr, miscellaneous.toggle_bool, popup_opts)
286+
popup.set_popup_keymaps(forked_project_id_popup, M.create_mr, nil, popup_opts)
287287
popup.set_cycle_popups_keymaps(popups)
288288

289289
vim.api.nvim_set_current_buf(M.description_bufnr)

lua/gitlab/actions/discussions/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,15 +312,15 @@ M.edit_comment = function(tree, unlinked)
312312

313313
-- Draft notes module handles edits for draft notes
314314
if M.is_draft_note(tree) then
315-
state.set_popup_keymaps(
315+
popup.set_popup_keymaps(
316316
edit_popup,
317317
draft_notes.confirm_edit_draft_note(note_node.id, unlinked),
318318
nil,
319319
popup.editable_popup_opts
320320
)
321321
else
322322
local comment = require("gitlab.actions.comment")
323-
state.set_popup_keymaps(
323+
popup.set_popup_keymaps(
324324
edit_popup,
325325
comment.confirm_edit_comment(tostring(root_node.id), tonumber(note_node.root_note_id or note_node.id), unlinked),
326326
nil,

lua/gitlab/actions/help.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ M.open = function()
3030

3131
help_popup:mount()
3232

33-
state.set_popup_keymaps(help_popup, "Help", nil)
33+
popup.set_popup_keymaps(help_popup, "Help", nil)
3434
local currentBuffer = vim.api.nvim_get_current_buf()
3535
vim.api.nvim_buf_set_lines(currentBuffer, 0, #help_content_lines, false, help_content_lines)
3636
u.switch_can_edit_buf(currentBuffer, false)

lua/gitlab/actions/merge.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ M.merge = function(opts)
3333
local squash_message_popup = create_squash_message_popup()
3434
popup.set_up_autocommands(squash_message_popup, nil, vim.api.nvim_get_current_win())
3535
squash_message_popup:mount()
36-
state.set_popup_keymaps(squash_message_popup, function(text)
36+
popup.set_popup_keymaps(squash_message_popup, function(text)
3737
M.confirm_merge(merge_body, text)
3838
end, nil, popup.editable_popup_opts)
3939
else

lua/gitlab/actions/pipeline.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ M.open = function()
9393
end
9494

9595
pipeline_popup.border:set_text("top", "Pipeline Status", "center")
96-
state.set_popup_keymaps(pipeline_popup, M.retrigger, M.see_logs)
96+
popup.set_popup_keymaps(pipeline_popup, M.retrigger, M.see_logs)
9797
u.switch_can_edit_buf(bufnr, false)
9898
end)
9999
end

lua/gitlab/actions/summary.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,19 @@ M.summary = function()
6060
M.color_details(info_popup.bufnr) -- Color values in details popup
6161
end
6262

63-
state.set_popup_keymaps(
63+
popup.set_popup_keymaps(
6464
description_popup,
6565
M.edit_summary,
6666
miscellaneous.attach_file,
6767
{ cb = exit, action_before_close = true, action_before_exit = true, save_to_temp_register = true }
6868
)
69-
state.set_popup_keymaps(
69+
popup.set_popup_keymaps(
7070
title_popup,
7171
M.edit_summary,
7272
nil,
7373
{ cb = exit, action_before_close = true, action_before_exit = true }
7474
)
75-
state.set_popup_keymaps(
75+
popup.set_popup_keymaps(
7676
info_popup,
7777
M.edit_summary,
7878
nil,

lua/gitlab/popup.lua

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,95 @@ local u = require("gitlab.utils")
22

33
local 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

lua/gitlab/state.lua

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -433,94 +433,6 @@ M.setPluginConfiguration = function()
433433
return true
434434
end
435435

436-
local function exit(popup, opts)
437-
if opts.action_before_exit and opts.cb ~= nil then
438-
opts.cb()
439-
popup:unmount()
440-
else
441-
popup:unmount()
442-
if opts.cb ~= nil then
443-
opts.cb()
444-
end
445-
end
446-
end
447-
448-
-- These keymaps are buffer specific and are set dynamically when popups mount
449-
M.set_popup_keymaps = function(popup, action, linewise_action, opts)
450-
if M.settings.keymaps.disable_all or M.settings.keymaps.popup.disable_all then
451-
return
452-
end
453-
454-
if opts == nil then
455-
opts = {}
456-
end
457-
if action ~= "Help" and M.settings.keymaps.help then -- Don't show help on the help popup
458-
vim.keymap.set("n", M.settings.keymaps.help, function()
459-
local help = require("gitlab.actions.help")
460-
help.open()
461-
end, { buffer = popup.bufnr, desc = "Open help", nowait = M.settings.keymaps.help_nowait })
462-
end
463-
if action ~= nil and M.settings.keymaps.popup.perform_action then
464-
vim.keymap.set("n", M.settings.keymaps.popup.perform_action, function()
465-
local text = u.get_buffer_text(popup.bufnr)
466-
if opts.action_before_close then
467-
action(text, popup.bufnr)
468-
exit(popup, opts)
469-
else
470-
exit(popup, opts)
471-
action(text, popup.bufnr)
472-
end
473-
end, { buffer = popup.bufnr, desc = "Perform action", nowait = M.settings.keymaps.popup.perform_action_nowait })
474-
end
475-
476-
if linewise_action ~= nil and M.settings.keymaps.popup.perform_action then
477-
vim.keymap.set("n", M.settings.keymaps.popup.perform_linewise_action, function()
478-
local bufnr = vim.api.nvim_get_current_buf()
479-
local linnr = vim.api.nvim_win_get_cursor(0)[1]
480-
local text = u.get_line_content(bufnr, linnr)
481-
linewise_action(text)
482-
end, {
483-
buffer = popup.bufnr,
484-
desc = "Perform linewise action",
485-
nowait = M.settings.keymaps.popup.perform_linewise_action_nowait,
486-
})
487-
end
488-
489-
if M.settings.keymaps.popup.discard_changes then
490-
vim.keymap.set("n", M.settings.keymaps.popup.discard_changes, function()
491-
local temp_registers = M.settings.popup.temp_registers
492-
M.settings.popup.temp_registers = {}
493-
vim.cmd("quit!")
494-
M.settings.popup.temp_registers = temp_registers
495-
end, {
496-
buffer = popup.bufnr,
497-
desc = "Quit discarding changes",
498-
nowait = M.settings.keymaps.popup.discard_changes_nowait,
499-
})
500-
end
501-
502-
if opts.save_to_temp_register then
503-
vim.api.nvim_create_autocmd("BufWinLeave", {
504-
buffer = popup.bufnr,
505-
callback = function()
506-
local text = u.get_buffer_text(popup.bufnr)
507-
for _, register in ipairs(M.settings.popup.temp_registers) do
508-
vim.fn.setreg(register, text)
509-
end
510-
end,
511-
})
512-
end
513-
514-
if opts.action_before_exit then
515-
vim.api.nvim_create_autocmd("BufWinLeave", {
516-
buffer = popup.bufnr,
517-
callback = function()
518-
exit(popup, opts)
519-
end,
520-
})
521-
end
522-
end
523-
524436
-- Dependencies
525437
-- These tables are passed to the async.sequence function, which calls them in sequence
526438
-- before calling an action. They are used to set global state that's required

0 commit comments

Comments
 (0)