From da263bff89e5ffd3df4151ff3f7164e0fb9ec785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Fri, 15 Nov 2024 07:55:32 +0100 Subject: [PATCH 01/21] fix: remove unnecessary empty lines in help --- lua/gitlab/actions/help.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/gitlab/actions/help.lua b/lua/gitlab/actions/help.lua index d76c7b5e..09b67210 100644 --- a/lua/gitlab/actions/help.lua +++ b/lua/gitlab/actions/help.lua @@ -18,7 +18,7 @@ M.open = function() end, {}) local longest_line = u.get_longest_string(help_content_lines) local help_popup = - Popup(u.create_popup_state("Help", state.settings.popup.help, longest_line + 3, #help_content_lines + 3, 60)) + Popup(u.create_popup_state("Help", state.settings.popup.help, longest_line + 3, #help_content_lines, 60)) help_popup:on(event.BufLeave, function() help_popup:unmount() end) From c403e7cb05b4fdc240f295e5beb42c03647880bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Fri, 15 Nov 2024 07:58:50 +0100 Subject: [PATCH 02/21] fix: remove irrelevant fields from settings Pipeline and help popups are only customizable through the general `settings.popup` field. --- doc/gitlab.nvim.txt | 1 - lua/gitlab/state.lua | 2 -- 2 files changed, 3 deletions(-) diff --git a/doc/gitlab.nvim.txt b/doc/gitlab.nvim.txt index 523afa20..20a008a6 100644 --- a/doc/gitlab.nvim.txt +++ b/doc/gitlab.nvim.txt @@ -242,7 +242,6 @@ you call this function with no values the defaults will be used: comment = nil, -- Individual popup overrides, e.g. { width = "60%", height = "80%", border = "single", opacity = 0.85 }, edit = nil, note = nil, - pipeline = nil, reply = nil, squash_message = nil, temp_registers = {}, -- List of registers for backing up popup content (see `:h gitlab.nvim.temp-registers`) diff --git a/lua/gitlab/state.lua b/lua/gitlab/state.lua index 22278c67..026af31d 100644 --- a/lua/gitlab/state.lua +++ b/lua/gitlab/state.lua @@ -139,8 +139,6 @@ M.settings = { edit = nil, comment = nil, note = nil, - help = nil, - pipeline = nil, reply = nil, squash_message = nil, temp_registers = {}, From b398df827d788678c38e0e1e6536576facb2890a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Fri, 15 Nov 2024 08:01:21 +0100 Subject: [PATCH 03/21] style: use the same ordering in docs and in state --- lua/gitlab/state.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/gitlab/state.lua b/lua/gitlab/state.lua index 026af31d..862a274b 100644 --- a/lua/gitlab/state.lua +++ b/lua/gitlab/state.lua @@ -136,8 +136,8 @@ M.settings = { height = "60%", border = "rounded", opacity = 1.0, - edit = nil, comment = nil, + edit = nil, note = nil, reply = nil, squash_message = nil, From 55dea15652a14974a242a02b1751ab0deef18cc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Fri, 15 Nov 2024 21:13:42 +0100 Subject: [PATCH 04/21] fix: re-enable popup customization --- doc/gitlab.nvim.txt | 6 ++++-- lua/gitlab/actions/comment.lua | 13 ++++++++----- lua/gitlab/actions/create_mr.lua | 17 +++++++++-------- lua/gitlab/actions/summary.lua | 11 ++++++----- lua/gitlab/state.lua | 2 ++ lua/gitlab/utils/init.lua | 15 +++++++-------- 6 files changed, 36 insertions(+), 28 deletions(-) diff --git a/doc/gitlab.nvim.txt b/doc/gitlab.nvim.txt index 20a008a6..f9f93667 100644 --- a/doc/gitlab.nvim.txt +++ b/doc/gitlab.nvim.txt @@ -235,8 +235,8 @@ you call this function with no values the defaults will be used: }, }, popup = { -- The popup for comment creation, editing, and replying - width = "40%", - height = "60%", + width = "40%", -- Can be a percentage (string or decimal, "40%" = 0.4) of editor screen width, or an integer (number of columns) + height = "60%", -- Can be a percentage (string or decimal, "60%" = 0.6) of editor screen width, or an integer (number of rows) border = "rounded", -- One of "rounded", "single", "double", "solid" opacity = 1.0, -- From 0.0 (fully transparent) to 1.0 (fully opaque) comment = nil, -- Individual popup overrides, e.g. { width = "60%", height = "80%", border = "single", opacity = 0.85 }, @@ -244,6 +244,8 @@ you call this function with no values the defaults will be used: note = nil, reply = nil, squash_message = nil, + create_mr = { width = "95%", height = "95%" }, + summary = { width = "95%", height = "95%" }, temp_registers = {}, -- List of registers for backing up popup content (see `:h gitlab.nvim.temp-registers`) }, discussion_tree = { -- The discussion tree that holds all comments diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index fbc3ad01..3265a512 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -197,12 +197,15 @@ M.create_comment_layout = function(opts) end end - local title = opts.discussion_id and "Reply" or "Comment" - local settings = opts.discussion_id ~= nil and state.settings.popup.reply or state.settings.popup.comment + local title = opts.discussion_id and "Reply" or opts.unlinked and "Note" or "Comment" + + local popup = state.settings.popup + local overrides = opts.discussion_id ~= nil and popup.reply or opts.unlinked and popup.note or popup.comment + local settings = u.merge(popup, overrides or {}) M.current_win = vim.api.nvim_get_current_win() M.comment_popup = Popup(u.create_popup_state(title, settings)) - M.draft_popup = Popup(u.create_box_popup_state("Draft", false)) + M.draft_popup = Popup(u.create_box_popup_state("Draft", false, settings)) M.start_line, M.end_line = u.get_visual_selection_boundaries() local internal_layout = Layout.Box({ @@ -214,8 +217,8 @@ M.create_comment_layout = function(opts) position = "50%", relative = "editor", size = { - width = "50%", - height = "55%", + width = settings.width, + height = settings.height, }, }, internal_layout) diff --git a/lua/gitlab/actions/create_mr.lua b/lua/gitlab/actions/create_mr.lua index 2e366946..2617cc44 100644 --- a/lua/gitlab/actions/create_mr.lua +++ b/lua/gitlab/actions/create_mr.lua @@ -328,19 +328,20 @@ M.create_mr = function() end M.create_layout = function() - local title_popup = Popup(u.create_box_popup_state("Title", false)) + local settings = u.merge(state.settings.popup, state.settings.popup.create_mr or {}) + local title_popup = Popup(u.create_box_popup_state("Title", false, settings)) M.title_bufnr = title_popup.bufnr - local description_popup = Popup(u.create_box_popup_state("Description", true)) + local description_popup = Popup(u.create_box_popup_state("Description", true, settings)) M.description_bufnr = description_popup.bufnr - local target_branch_popup = Popup(u.create_box_popup_state("Target branch", false)) + local target_branch_popup = Popup(u.create_box_popup_state("Target branch", false, settings)) M.target_bufnr = target_branch_popup.bufnr local delete_title = vim.o.columns > 110 and "Delete source branch" or "Delete source" - local delete_branch_popup = Popup(u.create_box_popup_state(delete_title, false)) + local delete_branch_popup = Popup(u.create_box_popup_state(delete_title, false, settings)) M.delete_branch_bufnr = delete_branch_popup.bufnr local squash_title = vim.o.columns > 110 and "Squash commits" or "Squash" - local squash_popup = Popup(u.create_box_popup_state(squash_title, false)) + local squash_popup = Popup(u.create_box_popup_state(squash_title, false, settings)) M.squash_bufnr = squash_popup.bufnr - local forked_project_id_popup = Popup(u.create_box_popup_state("Forked Project ID", false)) + local forked_project_id_popup = Popup(u.create_box_popup_state("Forked Project ID", false, settings)) M.forked_project_id_bufnr = forked_project_id_popup.bufnr local boxes = {} @@ -363,8 +364,8 @@ M.create_layout = function() position = "50%", relative = "editor", size = { - width = "95%", - height = "95%", + width = settings.width, + height = settings.height, }, }, internal_layout) diff --git a/lua/gitlab/actions/summary.lua b/lua/gitlab/actions/summary.lua index 6d034be1..70793d91 100644 --- a/lua/gitlab/actions/summary.lua +++ b/lua/gitlab/actions/summary.lua @@ -166,15 +166,16 @@ M.edit_summary = function() end M.create_layout = function(info_lines) - local title_popup = Popup(u.create_box_popup_state(nil, false)) + local settings = u.merge(state.settings.popup, state.settings.popup.summary or {}) + local title_popup = Popup(u.create_box_popup_state(nil, false, settings)) M.title_bufnr = title_popup.bufnr - local description_popup = Popup(u.create_box_popup_state("Description", true)) + local description_popup = Popup(u.create_box_popup_state("Description", true, settings)) M.description_bufnr = description_popup.bufnr local details_popup local internal_layout if state.settings.info.enabled then - details_popup = Popup(u.create_box_popup_state("Details", false)) + details_popup = Popup(u.create_box_popup_state("Details", false, settings)) if state.settings.info.horizontal then local longest_line = u.get_longest_string(info_lines) internal_layout = Layout.Box({ @@ -202,8 +203,8 @@ M.create_layout = function(info_lines) position = "50%", relative = "editor", size = { - width = "95%", - height = "95%", + width = settings.width, + height = settings.height, }, }, internal_layout) diff --git a/lua/gitlab/state.lua b/lua/gitlab/state.lua index 862a274b..b5e37124 100644 --- a/lua/gitlab/state.lua +++ b/lua/gitlab/state.lua @@ -141,6 +141,8 @@ M.settings = { note = nil, reply = nil, squash_message = nil, + create_mr = { width = "95%", height = "95%" }, + summary = { width = "95%", height = "95%" }, temp_registers = {}, }, discussion_tree = { diff --git a/lua/gitlab/utils/init.lua b/lua/gitlab/utils/init.lua index aa71136b..73be671c 100644 --- a/lua/gitlab/utils/init.lua +++ b/lua/gitlab/utils/init.lua @@ -487,8 +487,7 @@ end ---@param height number? Override default height ---@return table M.create_popup_state = function(title, settings, width, height, zindex) - local default_settings = require("gitlab.state").settings.popup - local user_settings = settings or {} + settings = M.merge(require("gitlab.state").settings.popup, settings or {}) local view_opts = { buf_options = { filetype = "markdown", @@ -498,17 +497,17 @@ M.create_popup_state = function(title, settings, width, height, zindex) focusable = true, zindex = zindex or 50, border = { - style = user_settings.border or default_settings.border, + style = settings.border, text = { top = title, }, }, position = "50%", size = { - width = user_settings.width or width or default_settings.width, - height = user_settings.height or height or default_settings.height, + width = width or settings.width, + height = height or settings.height, }, - opacity = user_settings.opacity or default_settings.opacity, + opacity = settings.opacity, } return view_opts @@ -517,9 +516,9 @@ end ---Create view_opts for Box popups used inside popup Layouts ---@param title string|nil The string to appear on top of the popup ---@param enter boolean Whether the pop should be focused after creation +---@param settings table User defined popup settings ---@return table -M.create_box_popup_state = function(title, enter) - local settings = require("gitlab.state").settings.popup +M.create_box_popup_state = function(title, enter, settings) return { buf_options = { filetype = "markdown", From 0199fbc428992caa01461513a8e59453619cd70e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Sat, 16 Nov 2024 07:29:05 +0100 Subject: [PATCH 05/21] fix: limit popup size to screen size --- lua/gitlab/utils/init.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/gitlab/utils/init.lua b/lua/gitlab/utils/init.lua index 73be671c..5476053e 100644 --- a/lua/gitlab/utils/init.lua +++ b/lua/gitlab/utils/init.lua @@ -504,8 +504,8 @@ M.create_popup_state = function(title, settings, width, height, zindex) }, position = "50%", size = { - width = width or settings.width, - height = height or settings.height, + width = width and math.min(width, vim.o.columns - 2) or settings.width, + height = height and math.min(height, vim.o.lines - 3) or settings.height, }, opacity = settings.opacity, } From 910705c8f4a3cee3cb290649186198ed9e3f0247 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Sat, 16 Nov 2024 22:17:25 +0100 Subject: [PATCH 06/21] feat: make comment popups resizable --- lua/gitlab/actions/comment.lua | 2 ++ lua/gitlab/utils/popup.lua | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 lua/gitlab/utils/popup.lua diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index 3265a512..8a501802 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -7,6 +7,7 @@ local diffview_lib = require("diffview.lib") local state = require("gitlab.state") local job = require("gitlab.job") local u = require("gitlab.utils") +local popup_utils = require("gitlab.utils.popup") local git = require("gitlab.git") local discussions = require("gitlab.actions.discussions") local draft_notes = require("gitlab.actions.draft_notes") @@ -223,6 +224,7 @@ M.create_comment_layout = function(opts) }, internal_layout) miscellaneous.set_cycle_popups_keymaps({ M.comment_popup, M.draft_popup }) + popup_utils.set_up_autocommands(M.comment_popup, layout) local range = opts.ranged and { start_line = M.start_line, end_line = M.end_line } or nil local unlinked = opts.unlinked or false diff --git a/lua/gitlab/utils/popup.lua b/lua/gitlab/utils/popup.lua new file mode 100644 index 00000000..b249cb7d --- /dev/null +++ b/lua/gitlab/utils/popup.lua @@ -0,0 +1,17 @@ +local M = {} + +--- Setup autocommands for the popup +--- @param popup NuiPopup +--- @param layout NuiLayout|nil +M.set_up_autocommands = function(popup, layout) + -- Make the popup/layout resizable + popup:on("VimResized", function() + if layout ~= nil then + layout:update() + else + popup:update_layout() + end + end) +end + +return M From 4f0f51d30da1026037543b892daf9686a234cbd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Sat, 16 Nov 2024 22:21:15 +0100 Subject: [PATCH 07/21] feat: make edit_comment popup resizable --- lua/gitlab/actions/discussions/init.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lua/gitlab/actions/discussions/init.lua b/lua/gitlab/actions/discussions/init.lua index aaa4d48f..b067171e 100644 --- a/lua/gitlab/actions/discussions/init.lua +++ b/lua/gitlab/actions/discussions/init.lua @@ -7,6 +7,7 @@ local Popup = require("nui.popup") local NuiTree = require("nui.tree") local job = require("gitlab.job") local u = require("gitlab.utils") +local popup_utils = require("gitlab.utils.popup") local state = require("gitlab.state") local reviewer = require("gitlab.reviewer") local common = require("gitlab.actions.common") @@ -293,6 +294,8 @@ M.edit_comment = function(tree, unlinked) return end + popup_utils.set_up_autocommands(edit_popup) + edit_popup:mount() -- Gather all lines from immediate children that aren't note nodes From d80d615ad43709fd29281d5abf2e96b917066392 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Sun, 17 Nov 2024 00:31:54 +0100 Subject: [PATCH 08/21] feat: make help popup resizable --- lua/gitlab/actions/help.lua | 10 ++++++++-- lua/gitlab/utils/popup.lua | 7 +++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lua/gitlab/actions/help.lua b/lua/gitlab/actions/help.lua index 09b67210..617e831a 100644 --- a/lua/gitlab/actions/help.lua +++ b/lua/gitlab/actions/help.lua @@ -1,6 +1,7 @@ local M = {} local u = require("gitlab.utils") +local popup_utils = require("gitlab.utils.popup") local event = require("nui.utils.autocmd").event local state = require("gitlab.state") local List = require("gitlab.utils.list") @@ -16,12 +17,17 @@ M.open = function() end return agg end, {}) + local longest_line = u.get_longest_string(help_content_lines) - local help_popup = - Popup(u.create_popup_state("Help", state.settings.popup.help, longest_line + 3, #help_content_lines, 60)) + local opts = {"Help", state.settings.popup.help, longest_line + 3, #help_content_lines, 60} + local help_popup = Popup(u.create_popup_state(unpack(opts))) + help_popup:on(event.BufLeave, function() help_popup:unmount() end) + + popup_utils.set_up_autocommands(help_popup, nil, opts) + help_popup:mount() state.set_popup_keymaps(help_popup, "Help", nil) diff --git a/lua/gitlab/utils/popup.lua b/lua/gitlab/utils/popup.lua index b249cb7d..31da656c 100644 --- a/lua/gitlab/utils/popup.lua +++ b/lua/gitlab/utils/popup.lua @@ -1,15 +1,18 @@ +local u = require("gitlab.utils") + local M = {} --- Setup autocommands for the popup --- @param popup NuiPopup --- @param layout NuiLayout|nil -M.set_up_autocommands = function(popup, layout) +--- @param opts table|nil Table with options for updating the popup +M.set_up_autocommands = function(popup, layout, opts) -- Make the popup/layout resizable popup:on("VimResized", function() if layout ~= nil then layout:update() else - popup:update_layout() + popup:update_layout(opts and u.create_popup_state(unpack(opts))) end end) end From 12a26e889566c5c5e18b692a4a15dac76d26b53d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Sun, 17 Nov 2024 00:35:11 +0100 Subject: [PATCH 09/21] feat: make create_mr popup resizable The `description_popup` state is created with `u.create_popup_state` instead of `u.create_box_popup_state` because otherwise the resizing doesn't work for some reason. --- lua/gitlab/actions/create_mr.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lua/gitlab/actions/create_mr.lua b/lua/gitlab/actions/create_mr.lua index 2617cc44..13f26e78 100644 --- a/lua/gitlab/actions/create_mr.lua +++ b/lua/gitlab/actions/create_mr.lua @@ -5,6 +5,7 @@ local Input = require("nui.input") local Popup = require("nui.popup") local job = require("gitlab.job") local u = require("gitlab.utils") +local popup_utils = require("gitlab.utils.popup") local git = require("gitlab.git") local state = require("gitlab.state") local common = require("gitlab.actions.common") @@ -331,7 +332,7 @@ M.create_layout = function() local settings = u.merge(state.settings.popup, state.settings.popup.create_mr or {}) local title_popup = Popup(u.create_box_popup_state("Title", false, settings)) M.title_bufnr = title_popup.bufnr - local description_popup = Popup(u.create_box_popup_state("Description", true, settings)) + local description_popup = Popup(u.create_popup_state("Description", settings)) M.description_bufnr = description_popup.bufnr local target_branch_popup = Popup(u.create_box_popup_state("Target branch", false, settings)) M.target_bufnr = target_branch_popup.bufnr @@ -369,6 +370,8 @@ M.create_layout = function() }, }, internal_layout) + popup_utils.set_up_autocommands(description_popup, layout) + layout:mount() return layout, From 72f243116154fdf1e0eff8232009c1c06022a596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Sun, 17 Nov 2024 00:39:34 +0100 Subject: [PATCH 10/21] feat: make summary popup resizable The `description_popup` state is created with `u.create_popup_state` instead of `u.create_box_popup_state` because otherwise the resizing doesn't work for some reason. --- lua/gitlab/actions/summary.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lua/gitlab/actions/summary.lua b/lua/gitlab/actions/summary.lua index 70793d91..396cb5ea 100644 --- a/lua/gitlab/actions/summary.lua +++ b/lua/gitlab/actions/summary.lua @@ -7,6 +7,7 @@ local git = require("gitlab.git") local job = require("gitlab.job") local common = require("gitlab.actions.common") local u = require("gitlab.utils") +local popup_utils = require("gitlab.utils.popup") local List = require("gitlab.utils.list") local state = require("gitlab.state") local miscellaneous = require("gitlab.actions.miscellaneous") @@ -169,7 +170,7 @@ M.create_layout = function(info_lines) local settings = u.merge(state.settings.popup, state.settings.popup.summary or {}) local title_popup = Popup(u.create_box_popup_state(nil, false, settings)) M.title_bufnr = title_popup.bufnr - local description_popup = Popup(u.create_box_popup_state("Description", true, settings)) + local description_popup = Popup(u.create_popup_state("Description", settings)) M.description_bufnr = description_popup.bufnr local details_popup @@ -208,6 +209,8 @@ M.create_layout = function(info_lines) }, }, internal_layout) + popup_utils.set_up_autocommands(description_popup, layout) + layout:mount() return layout, title_popup, description_popup, details_popup end From 11873613ef8f8cff58821c92b4156dd5108c63fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Sun, 17 Nov 2024 01:15:00 +0100 Subject: [PATCH 11/21] feat: focus last active window after closing popup --- lua/gitlab/actions/comment.lua | 10 +--------- lua/gitlab/actions/create_mr.lua | 2 +- lua/gitlab/actions/discussions/init.lua | 2 +- lua/gitlab/actions/help.lua | 2 +- lua/gitlab/actions/summary.lua | 2 +- lua/gitlab/utils/popup.lua | 12 +++++++++++- 6 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index 8a501802..a8bc8a27 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -224,7 +224,7 @@ M.create_comment_layout = function(opts) }, internal_layout) miscellaneous.set_cycle_popups_keymaps({ M.comment_popup, M.draft_popup }) - popup_utils.set_up_autocommands(M.comment_popup, layout) + popup_utils.set_up_autocommands(M.comment_popup, layout, M.current_win) local range = opts.ranged and { start_line = M.start_line, end_line = M.end_line } or nil local unlinked = opts.unlinked or false @@ -247,14 +247,6 @@ M.create_comment_layout = function(opts) vim.api.nvim_buf_set_lines(M.draft_popup.bufnr, 0, -1, false, { u.bool_to_string(draft_mode) }) end) - --Send back to previous window on close - vim.api.nvim_create_autocmd("BufHidden", { - buffer = M.draft_popup.bufnr, - callback = function() - vim.api.nvim_set_current_win(M.current_win) - end, - }) - return layout end diff --git a/lua/gitlab/actions/create_mr.lua b/lua/gitlab/actions/create_mr.lua index 13f26e78..61794baa 100644 --- a/lua/gitlab/actions/create_mr.lua +++ b/lua/gitlab/actions/create_mr.lua @@ -370,7 +370,7 @@ M.create_layout = function() }, }, internal_layout) - popup_utils.set_up_autocommands(description_popup, layout) + popup_utils.set_up_autocommands(description_popup, layout, vim.api.nvim_get_current_win()) layout:mount() diff --git a/lua/gitlab/actions/discussions/init.lua b/lua/gitlab/actions/discussions/init.lua index b067171e..df0a8efa 100644 --- a/lua/gitlab/actions/discussions/init.lua +++ b/lua/gitlab/actions/discussions/init.lua @@ -294,7 +294,7 @@ M.edit_comment = function(tree, unlinked) return end - popup_utils.set_up_autocommands(edit_popup) + popup_utils.set_up_autocommands(edit_popup, nil, vim.api.nvim_get_current_win()) edit_popup:mount() diff --git a/lua/gitlab/actions/help.lua b/lua/gitlab/actions/help.lua index 617e831a..a866cfe9 100644 --- a/lua/gitlab/actions/help.lua +++ b/lua/gitlab/actions/help.lua @@ -26,7 +26,7 @@ M.open = function() help_popup:unmount() end) - popup_utils.set_up_autocommands(help_popup, nil, opts) + popup_utils.set_up_autocommands(help_popup, nil, vim.api.nvim_get_current_win(), opts) help_popup:mount() diff --git a/lua/gitlab/actions/summary.lua b/lua/gitlab/actions/summary.lua index 396cb5ea..91ebcc6b 100644 --- a/lua/gitlab/actions/summary.lua +++ b/lua/gitlab/actions/summary.lua @@ -209,7 +209,7 @@ M.create_layout = function(info_lines) }, }, internal_layout) - popup_utils.set_up_autocommands(description_popup, layout) + popup_utils.set_up_autocommands(description_popup, layout, vim.api.nvim_get_current_win()) layout:mount() return layout, title_popup, description_popup, details_popup diff --git a/lua/gitlab/utils/popup.lua b/lua/gitlab/utils/popup.lua index 31da656c..dedcbf05 100644 --- a/lua/gitlab/utils/popup.lua +++ b/lua/gitlab/utils/popup.lua @@ -5,8 +5,9 @@ local M = {} --- Setup autocommands for the popup --- @param popup NuiPopup --- @param layout NuiLayout|nil +--- @param previous_window number|nil Number of window active before the popup was opened --- @param opts table|nil Table with options for updating the popup -M.set_up_autocommands = function(popup, layout, opts) +M.set_up_autocommands = function(popup, layout, previous_window, opts) -- Make the popup/layout resizable popup:on("VimResized", function() if layout ~= nil then @@ -15,6 +16,15 @@ M.set_up_autocommands = function(popup, layout, opts) popup:update_layout(opts and u.create_popup_state(unpack(opts))) end end) + + -- After closing the popup, refocus the previously active window + if previous_window ~= nil then + popup:on("BufHidden", function() + vim.schedule(function() + vim.api.nvim_set_current_win(previous_window) + end) + end) + end end return M From 2484eca6dc69f5ae54f7a118801e25be9ab702e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Sun, 17 Nov 2024 01:25:05 +0100 Subject: [PATCH 12/21] refactor: move popup-related stuff to utils.popup --- lua/gitlab/actions/comment.lua | 6 +-- lua/gitlab/actions/create_mr.lua | 2 +- lua/gitlab/actions/discussions/init.lua | 5 +- lua/gitlab/actions/merge.lua | 4 +- lua/gitlab/actions/miscellaneous.lua | 64 ------------------------- lua/gitlab/actions/summary.lua | 2 +- lua/gitlab/utils/popup.lua | 64 +++++++++++++++++++++++++ 7 files changed, 73 insertions(+), 74 deletions(-) diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index a8bc8a27..5f899279 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -223,7 +223,7 @@ M.create_comment_layout = function(opts) }, }, internal_layout) - miscellaneous.set_cycle_popups_keymaps({ M.comment_popup, M.draft_popup }) + popup_utils.set_cycle_popups_keymaps({ M.comment_popup, M.draft_popup }) popup_utils.set_up_autocommands(M.comment_popup, layout, M.current_win) local range = opts.ranged and { start_line = M.start_line, end_line = M.end_line } or nil @@ -234,13 +234,13 @@ M.create_comment_layout = function(opts) local text = u.get_buffer_text(M.comment_popup.bufnr) confirm_create_comment(text, range, unlinked, opts.discussion_id) vim.api.nvim_set_current_win(M.current_win) - end, miscellaneous.toggle_bool, miscellaneous.non_editable_popup_opts) + end, miscellaneous.toggle_bool, popup_utils.non_editable_popup_opts) ---Keybinding for focus on text section state.set_popup_keymaps(M.comment_popup, function(text) confirm_create_comment(text, range, unlinked, opts.discussion_id) vim.api.nvim_set_current_win(M.current_win) - end, miscellaneous.attach_file, miscellaneous.editable_popup_opts) + end, miscellaneous.attach_file, popup_utils.editable_popup_opts) vim.schedule(function() local draft_mode = state.settings.discussion_tree.draft_mode diff --git a/lua/gitlab/actions/create_mr.lua b/lua/gitlab/actions/create_mr.lua index 61794baa..9dd6fe6c 100644 --- a/lua/gitlab/actions/create_mr.lua +++ b/lua/gitlab/actions/create_mr.lua @@ -284,7 +284,7 @@ M.open_confirmation_popup = function(mr) state.set_popup_keymaps(delete_branch_popup, M.create_mr, miscellaneous.toggle_bool, popup_opts) state.set_popup_keymaps(squash_popup, M.create_mr, miscellaneous.toggle_bool, popup_opts) state.set_popup_keymaps(forked_project_id_popup, M.create_mr, nil, popup_opts) - miscellaneous.set_cycle_popups_keymaps(popups) + popup_utils.set_cycle_popups_keymaps(popups) vim.api.nvim_set_current_buf(M.description_bufnr) end) diff --git a/lua/gitlab/actions/discussions/init.lua b/lua/gitlab/actions/discussions/init.lua index df0a8efa..d7c8582b 100644 --- a/lua/gitlab/actions/discussions/init.lua +++ b/lua/gitlab/actions/discussions/init.lua @@ -13,7 +13,6 @@ local reviewer = require("gitlab.reviewer") local common = require("gitlab.actions.common") local List = require("gitlab.utils.list") local tree_utils = require("gitlab.actions.discussions.tree") -local miscellaneous = require("gitlab.actions.miscellaneous") local discussions_tree = require("gitlab.actions.discussions.tree") local draft_notes = require("gitlab.actions.draft_notes") local diffview_lib = require("diffview.lib") @@ -317,7 +316,7 @@ M.edit_comment = function(tree, unlinked) edit_popup, draft_notes.confirm_edit_draft_note(note_node.id, unlinked), nil, - miscellaneous.editable_popup_opts + popup_utils.editable_popup_opts ) else local comment = require("gitlab.actions.comment") @@ -325,7 +324,7 @@ M.edit_comment = function(tree, unlinked) edit_popup, comment.confirm_edit_comment(tostring(root_node.id), tonumber(note_node.root_note_id or note_node.id), unlinked), nil, - miscellaneous.editable_popup_opts + popup_utils.editable_popup_opts ) end end diff --git a/lua/gitlab/actions/merge.lua b/lua/gitlab/actions/merge.lua index 81d23846..d3d48d55 100644 --- a/lua/gitlab/actions/merge.lua +++ b/lua/gitlab/actions/merge.lua @@ -1,9 +1,9 @@ local u = require("gitlab.utils") +local popup_utils = require("gitlab.utils.popup") local Popup = require("nui.popup") local state = require("gitlab.state") local job = require("gitlab.job") local reviewer = require("gitlab.reviewer") -local miscellaneous = require("gitlab.actions.miscellaneous") local M = {} @@ -34,7 +34,7 @@ M.merge = function(opts) squash_message_popup:mount() state.set_popup_keymaps(squash_message_popup, function(text) M.confirm_merge(merge_body, text) - end, nil, miscellaneous.editable_popup_opts) + end, nil, popup_utils.editable_popup_opts) else M.confirm_merge(merge_body) end diff --git a/lua/gitlab/actions/miscellaneous.lua b/lua/gitlab/actions/miscellaneous.lua index df080665..368c198b 100644 --- a/lua/gitlab/actions/miscellaneous.lua +++ b/lua/gitlab/actions/miscellaneous.lua @@ -34,70 +34,6 @@ M.attach_file = function() end) end -M.editable_popup_opts = { - action_before_close = true, - action_before_exit = false, - save_to_temp_register = true, -} - -M.non_editable_popup_opts = { - action_before_close = true, - action_before_exit = false, - save_to_temp_register = false, -} - --- Get the index of the next popup when cycling forward -local function next_index(i, n, count) - count = count > 0 and count or 1 - for _ = 1, count do - if i < n then - i = i + 1 - elseif i == n then - i = 1 - end - end - return i -end - ----Get the index of the previous popup when cycling backward ----@param i integer The current index ----@param n integer The total number of popups ----@param count integer The count used with the keymap (replaced with 1 if no count was given) -local function prev_index(i, n, count) - count = count > 0 and count or 1 - for _ = 1, count do - if i > 1 then - i = i - 1 - elseif i == 1 then - i = n - end - end - return i -end - ----Setup keymaps for cycling popups. The keymap accepts count. ----@param popups table Table of Popups -M.set_cycle_popups_keymaps = function(popups) - local keymaps = require("gitlab.state").settings.keymaps - if keymaps.disable_all or keymaps.popup.disable_all then - return - end - - local number_of_popups = #popups - for i, popup in ipairs(popups) do - if keymaps.popup.next_field then - popup:map("n", keymaps.popup.next_field, function() - vim.api.nvim_set_current_win(popups[next_index(i, number_of_popups, vim.v.count)].winid) - end, { desc = "Go to next field (accepts count)", nowait = keymaps.popup.next_field_nowait }) - end - if keymaps.popup.prev_field then - popup:map("n", keymaps.popup.prev_field, function() - vim.api.nvim_set_current_win(popups[prev_index(i, number_of_popups, vim.v.count)].winid) - end, { desc = "Go to previous field (accepts count)", nowait = keymaps.popup.prev_field_nowait }) - end - end -end - ---Toggle the value in a "Boolean buffer" M.toggle_bool = function() local bufnr = vim.api.nvim_get_current_buf() diff --git a/lua/gitlab/actions/summary.lua b/lua/gitlab/actions/summary.lua index 91ebcc6b..560c4ba6 100644 --- a/lua/gitlab/actions/summary.lua +++ b/lua/gitlab/actions/summary.lua @@ -78,7 +78,7 @@ M.summary = function() nil, { cb = exit, action_before_close = true, action_before_exit = true } ) - miscellaneous.set_cycle_popups_keymaps(popups) + popup_utils.set_cycle_popups_keymaps(popups) vim.api.nvim_set_current_buf(description_popup.bufnr) end) diff --git a/lua/gitlab/utils/popup.lua b/lua/gitlab/utils/popup.lua index dedcbf05..60ddb404 100644 --- a/lua/gitlab/utils/popup.lua +++ b/lua/gitlab/utils/popup.lua @@ -27,4 +27,68 @@ M.set_up_autocommands = function(popup, layout, previous_window, opts) end end +M.editable_popup_opts = { + action_before_close = true, + action_before_exit = false, + save_to_temp_register = true, +} + +M.non_editable_popup_opts = { + action_before_close = true, + action_before_exit = false, + save_to_temp_register = false, +} + +-- Get the index of the next popup when cycling forward +local function next_index(i, n, count) + count = count > 0 and count or 1 + for _ = 1, count do + if i < n then + i = i + 1 + elseif i == n then + i = 1 + end + end + return i +end + +---Get the index of the previous popup when cycling backward +---@param i integer The current index +---@param n integer The total number of popups +---@param count integer The count used with the keymap (replaced with 1 if no count was given) +local function prev_index(i, n, count) + count = count > 0 and count or 1 + for _ = 1, count do + if i > 1 then + i = i - 1 + elseif i == 1 then + i = n + end + end + return i +end + +---Setup keymaps for cycling popups. The keymap accepts count. +---@param popups table Table of Popups +M.set_cycle_popups_keymaps = function(popups) + local keymaps = require("gitlab.state").settings.keymaps + if keymaps.disable_all or keymaps.popup.disable_all then + return + end + + local number_of_popups = #popups + for i, popup in ipairs(popups) do + if keymaps.popup.next_field then + popup:map("n", keymaps.popup.next_field, function() + vim.api.nvim_set_current_win(popups[next_index(i, number_of_popups, vim.v.count)].winid) + end, { desc = "Go to next field (accepts count)", nowait = keymaps.popup.next_field_nowait }) + end + if keymaps.popup.prev_field then + popup:map("n", keymaps.popup.prev_field, function() + vim.api.nvim_set_current_win(popups[prev_index(i, number_of_popups, vim.v.count)].winid) + end, { desc = "Go to previous field (accepts count)", nowait = keymaps.popup.prev_field_nowait }) + end + end +end + return M From c3279b504f8dab744215e4a6b5f0e1fbe65033f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Sun, 17 Nov 2024 01:29:42 +0100 Subject: [PATCH 13/21] feat: make squash_message_popup resizable --- lua/gitlab/actions/merge.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lua/gitlab/actions/merge.lua b/lua/gitlab/actions/merge.lua index d3d48d55..ac830445 100644 --- a/lua/gitlab/actions/merge.lua +++ b/lua/gitlab/actions/merge.lua @@ -31,6 +31,7 @@ M.merge = function(opts) if merge_body.squash then local squash_message_popup = create_squash_message_popup() + popup_utils.set_up_autocommands(squash_message_popup, nil, vim.api.nvim_get_current_win()) squash_message_popup:mount() state.set_popup_keymaps(squash_message_popup, function(text) M.confirm_merge(merge_body, text) From 911cf8846acfa3cb107648d1d40db6f952116f45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Sun, 17 Nov 2024 01:35:01 +0100 Subject: [PATCH 14/21] feat: make pipeline popup resizable --- lua/gitlab/actions/pipeline.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lua/gitlab/actions/pipeline.lua b/lua/gitlab/actions/pipeline.lua index bddc1949..2029bd2d 100644 --- a/lua/gitlab/actions/pipeline.lua +++ b/lua/gitlab/actions/pipeline.lua @@ -5,6 +5,7 @@ local Popup = require("nui.popup") local state = require("gitlab.state") local job = require("gitlab.job") local u = require("gitlab.utils") +local popup_utils = require("gitlab.utils.popup") local M = { pipeline_jobs = nil, latest_pipeline = nil, @@ -41,6 +42,7 @@ M.open = function() local pipeline_popup = Popup(u.create_popup_state("Loading Pipeline...", state.settings.popup.pipeline, width, height, 60)) + popup_utils.set_up_autocommands(pipeline_popup, nil, vim.api.nvim_get_current_win()) M.pipeline_popup = pipeline_popup pipeline_popup:mount() From b5f12c9164eb278e96423ea297d4467975cb59fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Sun, 17 Nov 2024 01:37:09 +0100 Subject: [PATCH 15/21] fix: increase zindex of help popup This makes sure it's shown on top of the pipeline popup. --- lua/gitlab/actions/help.lua | 2 +- lua/gitlab/utils/init.lua | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/gitlab/actions/help.lua b/lua/gitlab/actions/help.lua index a866cfe9..5d79b592 100644 --- a/lua/gitlab/actions/help.lua +++ b/lua/gitlab/actions/help.lua @@ -19,7 +19,7 @@ M.open = function() end, {}) local longest_line = u.get_longest_string(help_content_lines) - local opts = {"Help", state.settings.popup.help, longest_line + 3, #help_content_lines, 60} + local opts = { "Help", state.settings.popup.help, longest_line + 3, #help_content_lines, 70 } local help_popup = Popup(u.create_popup_state(unpack(opts))) help_popup:on(event.BufLeave, function() diff --git a/lua/gitlab/utils/init.lua b/lua/gitlab/utils/init.lua index 5476053e..a5343db7 100644 --- a/lua/gitlab/utils/init.lua +++ b/lua/gitlab/utils/init.lua @@ -485,6 +485,7 @@ end ---@param settings table|nil User defined popup settings ---@param width number? Override default width ---@param height number? Override default height +---@param zindex number? Override default zindex ---@return table M.create_popup_state = function(title, settings, width, height, zindex) settings = M.merge(require("gitlab.state").settings.popup, settings or {}) From a6a83f31441c24d7ee1a6a3eb61392242c2b2d4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Tue, 19 Nov 2024 11:12:00 +0100 Subject: [PATCH 16/21] feat: make popup position customizable This makes it possible to place the popup in the bottom right corner where it doesn't obscure the content of the reviewer. --- doc/gitlab.nvim.txt | 1 + lua/gitlab/actions/comment.lua | 2 +- lua/gitlab/actions/create_mr.lua | 2 +- lua/gitlab/actions/summary.lua | 2 +- lua/gitlab/state.lua | 1 + lua/gitlab/utils/init.lua | 2 +- 6 files changed, 6 insertions(+), 4 deletions(-) diff --git a/doc/gitlab.nvim.txt b/doc/gitlab.nvim.txt index f9f93667..0dc13552 100644 --- a/doc/gitlab.nvim.txt +++ b/doc/gitlab.nvim.txt @@ -237,6 +237,7 @@ you call this function with no values the defaults will be used: popup = { -- The popup for comment creation, editing, and replying width = "40%", -- Can be a percentage (string or decimal, "40%" = 0.4) of editor screen width, or an integer (number of columns) height = "60%", -- Can be a percentage (string or decimal, "60%" = 0.6) of editor screen width, or an integer (number of rows) + position = "50%", -- Position (from the top left corner), either a number or percentage string that applies to both horizontal and vertical position, or a table that specifies them separately, e.g., { row = "90%", col = "100%" } places popups in the bottom right corner while leaving the status line visible border = "rounded", -- One of "rounded", "single", "double", "solid" opacity = 1.0, -- From 0.0 (fully transparent) to 1.0 (fully opaque) comment = nil, -- Individual popup overrides, e.g. { width = "60%", height = "80%", border = "single", opacity = 0.85 }, diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index 5f899279..9786672c 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -215,7 +215,7 @@ M.create_comment_layout = function(opts) }, { dir = "col" }) local layout = Layout({ - position = "50%", + position = settings.position, relative = "editor", size = { width = settings.width, diff --git a/lua/gitlab/actions/create_mr.lua b/lua/gitlab/actions/create_mr.lua index 9dd6fe6c..cac40b43 100644 --- a/lua/gitlab/actions/create_mr.lua +++ b/lua/gitlab/actions/create_mr.lua @@ -362,7 +362,7 @@ M.create_layout = function() }, { dir = "col" }) local layout = Layout({ - position = "50%", + position = settings.position, relative = "editor", size = { width = settings.width, diff --git a/lua/gitlab/actions/summary.lua b/lua/gitlab/actions/summary.lua index 560c4ba6..0ca63a6c 100644 --- a/lua/gitlab/actions/summary.lua +++ b/lua/gitlab/actions/summary.lua @@ -201,7 +201,7 @@ M.create_layout = function(info_lines) end local layout = Layout({ - position = "50%", + position = settings.position, relative = "editor", size = { width = settings.width, diff --git a/lua/gitlab/state.lua b/lua/gitlab/state.lua index b5e37124..412a6f64 100644 --- a/lua/gitlab/state.lua +++ b/lua/gitlab/state.lua @@ -134,6 +134,7 @@ M.settings = { popup = { width = "40%", height = "60%", + position = "50%", border = "rounded", opacity = 1.0, comment = nil, diff --git a/lua/gitlab/utils/init.lua b/lua/gitlab/utils/init.lua index a5343db7..69b65ec4 100644 --- a/lua/gitlab/utils/init.lua +++ b/lua/gitlab/utils/init.lua @@ -503,7 +503,7 @@ M.create_popup_state = function(title, settings, width, height, zindex) top = title, }, }, - position = "50%", + position = settings.position, size = { width = width and math.min(width, vim.o.columns - 2) or settings.width, height = height and math.min(height, vim.o.lines - 3) or settings.height, From 984de358ecbd64158199e8de5065c956110b3410 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Wed, 4 Dec 2024 13:59:35 +0100 Subject: [PATCH 17/21] refactor: move popup to root level --- lua/gitlab/actions/comment.lua | 18 ++++++++++-------- lua/gitlab/actions/create_mr.lua | 6 +++--- lua/gitlab/actions/discussions/init.lua | 8 ++++---- lua/gitlab/actions/help.lua | 4 ++-- lua/gitlab/actions/merge.lua | 6 +++--- lua/gitlab/actions/pipeline.lua | 4 ++-- lua/gitlab/actions/summary.lua | 6 +++--- lua/gitlab/{utils => }/popup.lua | 0 8 files changed, 27 insertions(+), 25 deletions(-) rename lua/gitlab/{utils => }/popup.lua (100%) diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index 9786672c..a512e495 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -7,7 +7,7 @@ local diffview_lib = require("diffview.lib") local state = require("gitlab.state") local job = require("gitlab.job") local u = require("gitlab.utils") -local popup_utils = require("gitlab.utils.popup") +local popup = require("gitlab.popup") local git = require("gitlab.git") local discussions = require("gitlab.actions.discussions") local draft_notes = require("gitlab.actions.draft_notes") @@ -200,9 +200,11 @@ M.create_comment_layout = function(opts) local title = opts.discussion_id and "Reply" or opts.unlinked and "Note" or "Comment" - local popup = state.settings.popup - local overrides = opts.discussion_id ~= nil and popup.reply or opts.unlinked and popup.note or popup.comment - local settings = u.merge(popup, overrides or {}) + local popup_settings = state.settings.popup + local overrides = opts.discussion_id ~= nil and popup_settings.reply + or opts.unlinked and popup_settings.note + or popup_settings.comment + local settings = u.merge(popup_settings, overrides or {}) M.current_win = vim.api.nvim_get_current_win() M.comment_popup = Popup(u.create_popup_state(title, settings)) @@ -223,8 +225,8 @@ M.create_comment_layout = function(opts) }, }, internal_layout) - popup_utils.set_cycle_popups_keymaps({ M.comment_popup, M.draft_popup }) - popup_utils.set_up_autocommands(M.comment_popup, layout, M.current_win) + popup.set_cycle_popups_keymaps({ M.comment_popup, M.draft_popup }) + popup.set_up_autocommands(M.comment_popup, layout, M.current_win) local range = opts.ranged and { start_line = M.start_line, end_line = M.end_line } or nil local unlinked = opts.unlinked or false @@ -234,13 +236,13 @@ M.create_comment_layout = function(opts) local text = u.get_buffer_text(M.comment_popup.bufnr) confirm_create_comment(text, range, unlinked, opts.discussion_id) vim.api.nvim_set_current_win(M.current_win) - end, miscellaneous.toggle_bool, popup_utils.non_editable_popup_opts) + end, miscellaneous.toggle_bool, popup.non_editable_popup_opts) ---Keybinding for focus on text section state.set_popup_keymaps(M.comment_popup, function(text) confirm_create_comment(text, range, unlinked, opts.discussion_id) vim.api.nvim_set_current_win(M.current_win) - end, miscellaneous.attach_file, popup_utils.editable_popup_opts) + end, miscellaneous.attach_file, popup.editable_popup_opts) vim.schedule(function() local draft_mode = state.settings.discussion_tree.draft_mode diff --git a/lua/gitlab/actions/create_mr.lua b/lua/gitlab/actions/create_mr.lua index cac40b43..d631e860 100644 --- a/lua/gitlab/actions/create_mr.lua +++ b/lua/gitlab/actions/create_mr.lua @@ -5,7 +5,7 @@ local Input = require("nui.input") local Popup = require("nui.popup") local job = require("gitlab.job") local u = require("gitlab.utils") -local popup_utils = require("gitlab.utils.popup") +local popup = require("gitlab.popup") local git = require("gitlab.git") local state = require("gitlab.state") local common = require("gitlab.actions.common") @@ -284,7 +284,7 @@ M.open_confirmation_popup = function(mr) state.set_popup_keymaps(delete_branch_popup, M.create_mr, miscellaneous.toggle_bool, popup_opts) state.set_popup_keymaps(squash_popup, M.create_mr, miscellaneous.toggle_bool, popup_opts) state.set_popup_keymaps(forked_project_id_popup, M.create_mr, nil, popup_opts) - popup_utils.set_cycle_popups_keymaps(popups) + popup.set_cycle_popups_keymaps(popups) vim.api.nvim_set_current_buf(M.description_bufnr) end) @@ -370,7 +370,7 @@ M.create_layout = function() }, }, internal_layout) - popup_utils.set_up_autocommands(description_popup, layout, vim.api.nvim_get_current_win()) + popup.set_up_autocommands(description_popup, layout, vim.api.nvim_get_current_win()) layout:mount() diff --git a/lua/gitlab/actions/discussions/init.lua b/lua/gitlab/actions/discussions/init.lua index d7c8582b..7df4ca50 100644 --- a/lua/gitlab/actions/discussions/init.lua +++ b/lua/gitlab/actions/discussions/init.lua @@ -7,7 +7,7 @@ local Popup = require("nui.popup") local NuiTree = require("nui.tree") local job = require("gitlab.job") local u = require("gitlab.utils") -local popup_utils = require("gitlab.utils.popup") +local popup = require("gitlab.popup") local state = require("gitlab.state") local reviewer = require("gitlab.reviewer") local common = require("gitlab.actions.common") @@ -293,7 +293,7 @@ M.edit_comment = function(tree, unlinked) return end - popup_utils.set_up_autocommands(edit_popup, nil, vim.api.nvim_get_current_win()) + popup.set_up_autocommands(edit_popup, nil, vim.api.nvim_get_current_win()) edit_popup:mount() @@ -316,7 +316,7 @@ M.edit_comment = function(tree, unlinked) edit_popup, draft_notes.confirm_edit_draft_note(note_node.id, unlinked), nil, - popup_utils.editable_popup_opts + popup.editable_popup_opts ) else local comment = require("gitlab.actions.comment") @@ -324,7 +324,7 @@ M.edit_comment = function(tree, unlinked) edit_popup, comment.confirm_edit_comment(tostring(root_node.id), tonumber(note_node.root_note_id or note_node.id), unlinked), nil, - popup_utils.editable_popup_opts + popup.editable_popup_opts ) end end diff --git a/lua/gitlab/actions/help.lua b/lua/gitlab/actions/help.lua index 5d79b592..9995d57c 100644 --- a/lua/gitlab/actions/help.lua +++ b/lua/gitlab/actions/help.lua @@ -1,7 +1,7 @@ local M = {} local u = require("gitlab.utils") -local popup_utils = require("gitlab.utils.popup") +local popup = require("gitlab.popup") local event = require("nui.utils.autocmd").event local state = require("gitlab.state") local List = require("gitlab.utils.list") @@ -26,7 +26,7 @@ M.open = function() help_popup:unmount() end) - popup_utils.set_up_autocommands(help_popup, nil, vim.api.nvim_get_current_win(), opts) + popup.set_up_autocommands(help_popup, nil, vim.api.nvim_get_current_win(), opts) help_popup:mount() diff --git a/lua/gitlab/actions/merge.lua b/lua/gitlab/actions/merge.lua index ac830445..a667c627 100644 --- a/lua/gitlab/actions/merge.lua +++ b/lua/gitlab/actions/merge.lua @@ -1,5 +1,5 @@ local u = require("gitlab.utils") -local popup_utils = require("gitlab.utils.popup") +local popup = require("gitlab.popup") local Popup = require("nui.popup") local state = require("gitlab.state") local job = require("gitlab.job") @@ -31,11 +31,11 @@ M.merge = function(opts) if merge_body.squash then local squash_message_popup = create_squash_message_popup() - popup_utils.set_up_autocommands(squash_message_popup, nil, vim.api.nvim_get_current_win()) + popup.set_up_autocommands(squash_message_popup, nil, vim.api.nvim_get_current_win()) squash_message_popup:mount() state.set_popup_keymaps(squash_message_popup, function(text) M.confirm_merge(merge_body, text) - end, nil, popup_utils.editable_popup_opts) + end, nil, popup.editable_popup_opts) else M.confirm_merge(merge_body) end diff --git a/lua/gitlab/actions/pipeline.lua b/lua/gitlab/actions/pipeline.lua index 2029bd2d..e6993cf3 100644 --- a/lua/gitlab/actions/pipeline.lua +++ b/lua/gitlab/actions/pipeline.lua @@ -5,7 +5,7 @@ local Popup = require("nui.popup") local state = require("gitlab.state") local job = require("gitlab.job") local u = require("gitlab.utils") -local popup_utils = require("gitlab.utils.popup") +local popup = require("gitlab.popup") local M = { pipeline_jobs = nil, latest_pipeline = nil, @@ -42,7 +42,7 @@ M.open = function() local pipeline_popup = Popup(u.create_popup_state("Loading Pipeline...", state.settings.popup.pipeline, width, height, 60)) - popup_utils.set_up_autocommands(pipeline_popup, nil, vim.api.nvim_get_current_win()) + popup.set_up_autocommands(pipeline_popup, nil, vim.api.nvim_get_current_win()) M.pipeline_popup = pipeline_popup pipeline_popup:mount() diff --git a/lua/gitlab/actions/summary.lua b/lua/gitlab/actions/summary.lua index 0ca63a6c..b8fb23b9 100644 --- a/lua/gitlab/actions/summary.lua +++ b/lua/gitlab/actions/summary.lua @@ -7,7 +7,7 @@ local git = require("gitlab.git") local job = require("gitlab.job") local common = require("gitlab.actions.common") local u = require("gitlab.utils") -local popup_utils = require("gitlab.utils.popup") +local popup = require("gitlab.popup") local List = require("gitlab.utils.list") local state = require("gitlab.state") local miscellaneous = require("gitlab.actions.miscellaneous") @@ -78,7 +78,7 @@ M.summary = function() nil, { cb = exit, action_before_close = true, action_before_exit = true } ) - popup_utils.set_cycle_popups_keymaps(popups) + popup.set_cycle_popups_keymaps(popups) vim.api.nvim_set_current_buf(description_popup.bufnr) end) @@ -209,7 +209,7 @@ M.create_layout = function(info_lines) }, }, internal_layout) - popup_utils.set_up_autocommands(description_popup, layout, vim.api.nvim_get_current_win()) + popup.set_up_autocommands(description_popup, layout, vim.api.nvim_get_current_win()) layout:mount() return layout, title_popup, description_popup, details_popup diff --git a/lua/gitlab/utils/popup.lua b/lua/gitlab/popup.lua similarity index 100% rename from lua/gitlab/utils/popup.lua rename to lua/gitlab/popup.lua From dc8e50561a69219dc77c98a9c453d4fe953a3614 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Wed, 4 Dec 2024 14:16:59 +0100 Subject: [PATCH 18/21] refactor: move set_popup_keymaps to popup module --- lua/gitlab/actions/comment.lua | 4 +- lua/gitlab/actions/create_mr.lua | 12 ++-- lua/gitlab/actions/discussions/init.lua | 4 +- lua/gitlab/actions/help.lua | 2 +- lua/gitlab/actions/merge.lua | 2 +- lua/gitlab/actions/pipeline.lua | 2 +- lua/gitlab/actions/summary.lua | 6 +- lua/gitlab/popup.lua | 89 +++++++++++++++++++++++++ lua/gitlab/state.lua | 88 ------------------------ 9 files changed, 105 insertions(+), 104 deletions(-) diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index a512e495..b2a2c959 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -232,14 +232,14 @@ M.create_comment_layout = function(opts) local unlinked = opts.unlinked or false ---Keybinding for focus on draft section - state.set_popup_keymaps(M.draft_popup, function() + popup.set_popup_keymaps(M.draft_popup, function() local text = u.get_buffer_text(M.comment_popup.bufnr) confirm_create_comment(text, range, unlinked, opts.discussion_id) vim.api.nvim_set_current_win(M.current_win) end, miscellaneous.toggle_bool, popup.non_editable_popup_opts) ---Keybinding for focus on text section - state.set_popup_keymaps(M.comment_popup, function(text) + popup.set_popup_keymaps(M.comment_popup, function(text) confirm_create_comment(text, range, unlinked, opts.discussion_id) vim.api.nvim_set_current_win(M.current_win) end, miscellaneous.attach_file, popup.editable_popup_opts) diff --git a/lua/gitlab/actions/create_mr.lua b/lua/gitlab/actions/create_mr.lua index d631e860..6c30e01d 100644 --- a/lua/gitlab/actions/create_mr.lua +++ b/lua/gitlab/actions/create_mr.lua @@ -278,12 +278,12 @@ M.open_confirmation_popup = function(mr) action_before_exit = true, } - state.set_popup_keymaps(description_popup, M.create_mr, miscellaneous.attach_file, popup_opts) - state.set_popup_keymaps(title_popup, M.create_mr, nil, popup_opts) - state.set_popup_keymaps(target_popup, M.create_mr, M.select_new_target, popup_opts) - state.set_popup_keymaps(delete_branch_popup, M.create_mr, miscellaneous.toggle_bool, popup_opts) - state.set_popup_keymaps(squash_popup, M.create_mr, miscellaneous.toggle_bool, popup_opts) - state.set_popup_keymaps(forked_project_id_popup, M.create_mr, nil, popup_opts) + popup.set_popup_keymaps(description_popup, M.create_mr, miscellaneous.attach_file, popup_opts) + popup.set_popup_keymaps(title_popup, M.create_mr, nil, popup_opts) + popup.set_popup_keymaps(target_popup, M.create_mr, M.select_new_target, popup_opts) + popup.set_popup_keymaps(delete_branch_popup, M.create_mr, miscellaneous.toggle_bool, popup_opts) + popup.set_popup_keymaps(squash_popup, M.create_mr, miscellaneous.toggle_bool, popup_opts) + popup.set_popup_keymaps(forked_project_id_popup, M.create_mr, nil, popup_opts) popup.set_cycle_popups_keymaps(popups) vim.api.nvim_set_current_buf(M.description_bufnr) diff --git a/lua/gitlab/actions/discussions/init.lua b/lua/gitlab/actions/discussions/init.lua index 7df4ca50..2f029cc7 100644 --- a/lua/gitlab/actions/discussions/init.lua +++ b/lua/gitlab/actions/discussions/init.lua @@ -312,7 +312,7 @@ M.edit_comment = function(tree, unlinked) -- Draft notes module handles edits for draft notes if M.is_draft_note(tree) then - state.set_popup_keymaps( + popup.set_popup_keymaps( edit_popup, draft_notes.confirm_edit_draft_note(note_node.id, unlinked), nil, @@ -320,7 +320,7 @@ M.edit_comment = function(tree, unlinked) ) else local comment = require("gitlab.actions.comment") - state.set_popup_keymaps( + popup.set_popup_keymaps( edit_popup, comment.confirm_edit_comment(tostring(root_node.id), tonumber(note_node.root_note_id or note_node.id), unlinked), nil, diff --git a/lua/gitlab/actions/help.lua b/lua/gitlab/actions/help.lua index 9995d57c..4dd9d2c5 100644 --- a/lua/gitlab/actions/help.lua +++ b/lua/gitlab/actions/help.lua @@ -30,7 +30,7 @@ M.open = function() help_popup:mount() - state.set_popup_keymaps(help_popup, "Help", nil) + popup.set_popup_keymaps(help_popup, "Help", nil) local currentBuffer = vim.api.nvim_get_current_buf() vim.api.nvim_buf_set_lines(currentBuffer, 0, #help_content_lines, false, help_content_lines) u.switch_can_edit_buf(currentBuffer, false) diff --git a/lua/gitlab/actions/merge.lua b/lua/gitlab/actions/merge.lua index a667c627..4046b36c 100644 --- a/lua/gitlab/actions/merge.lua +++ b/lua/gitlab/actions/merge.lua @@ -33,7 +33,7 @@ M.merge = function(opts) local squash_message_popup = create_squash_message_popup() popup.set_up_autocommands(squash_message_popup, nil, vim.api.nvim_get_current_win()) squash_message_popup:mount() - state.set_popup_keymaps(squash_message_popup, function(text) + popup.set_popup_keymaps(squash_message_popup, function(text) M.confirm_merge(merge_body, text) end, nil, popup.editable_popup_opts) else diff --git a/lua/gitlab/actions/pipeline.lua b/lua/gitlab/actions/pipeline.lua index e6993cf3..f1a97684 100644 --- a/lua/gitlab/actions/pipeline.lua +++ b/lua/gitlab/actions/pipeline.lua @@ -93,7 +93,7 @@ M.open = function() end pipeline_popup.border:set_text("top", "Pipeline Status", "center") - state.set_popup_keymaps(pipeline_popup, M.retrigger, M.see_logs) + popup.set_popup_keymaps(pipeline_popup, M.retrigger, M.see_logs) u.switch_can_edit_buf(bufnr, false) end) end diff --git a/lua/gitlab/actions/summary.lua b/lua/gitlab/actions/summary.lua index b8fb23b9..d4b4172a 100644 --- a/lua/gitlab/actions/summary.lua +++ b/lua/gitlab/actions/summary.lua @@ -60,19 +60,19 @@ M.summary = function() M.color_details(info_popup.bufnr) -- Color values in details popup end - state.set_popup_keymaps( + popup.set_popup_keymaps( description_popup, M.edit_summary, miscellaneous.attach_file, { cb = exit, action_before_close = true, action_before_exit = true, save_to_temp_register = true } ) - state.set_popup_keymaps( + popup.set_popup_keymaps( title_popup, M.edit_summary, nil, { cb = exit, action_before_close = true, action_before_exit = true } ) - state.set_popup_keymaps( + popup.set_popup_keymaps( info_popup, M.edit_summary, nil, diff --git a/lua/gitlab/popup.lua b/lua/gitlab/popup.lua index 60ddb404..8c547790 100644 --- a/lua/gitlab/popup.lua +++ b/lua/gitlab/popup.lua @@ -2,6 +2,95 @@ local u = require("gitlab.utils") local M = {} +local function exit(popup, opts) + if opts.action_before_exit and opts.cb ~= nil then + opts.cb() + popup:unmount() + else + popup:unmount() + if opts.cb ~= nil then + opts.cb() + end + end +end + +-- These keymaps are buffer specific and are set dynamically when popups mount +M.set_popup_keymaps = function(popup, action, linewise_action, opts) + local settings = require("gitlab.state").settings + if settings.keymaps.disable_all or settings.keymaps.popup.disable_all then + return + end + + if opts == nil then + opts = {} + end + if action ~= "Help" and settings.keymaps.help then -- Don't show help on the help popup + vim.keymap.set("n", settings.keymaps.help, function() + local help = require("gitlab.actions.help") + help.open() + end, { buffer = popup.bufnr, desc = "Open help", nowait = settings.keymaps.help_nowait }) + end + if action ~= nil and settings.keymaps.popup.perform_action then + vim.keymap.set("n", settings.keymaps.popup.perform_action, function() + local text = u.get_buffer_text(popup.bufnr) + if opts.action_before_close then + action(text, popup.bufnr) + exit(popup, opts) + else + exit(popup, opts) + action(text, popup.bufnr) + end + end, { buffer = popup.bufnr, desc = "Perform action", nowait = settings.keymaps.popup.perform_action_nowait }) + end + + if linewise_action ~= nil and settings.keymaps.popup.perform_action then + vim.keymap.set("n", settings.keymaps.popup.perform_linewise_action, function() + local bufnr = vim.api.nvim_get_current_buf() + local linnr = vim.api.nvim_win_get_cursor(0)[1] + local text = u.get_line_content(bufnr, linnr) + linewise_action(text) + end, { + buffer = popup.bufnr, + desc = "Perform linewise action", + nowait = settings.keymaps.popup.perform_linewise_action_nowait, + }) + end + + if settings.keymaps.popup.discard_changes then + vim.keymap.set("n", settings.keymaps.popup.discard_changes, function() + local temp_registers = settings.popup.temp_registers + settings.popup.temp_registers = {} + vim.cmd("quit!") + settings.popup.temp_registers = temp_registers + end, { + buffer = popup.bufnr, + desc = "Quit discarding changes", + nowait = settings.keymaps.popup.discard_changes_nowait, + }) + end + + if opts.save_to_temp_register then + vim.api.nvim_create_autocmd("BufWinLeave", { + buffer = popup.bufnr, + callback = function() + local text = u.get_buffer_text(popup.bufnr) + for _, register in ipairs(settings.popup.temp_registers) do + vim.fn.setreg(register, text) + end + end, + }) + end + + if opts.action_before_exit then + vim.api.nvim_create_autocmd("BufWinLeave", { + buffer = popup.bufnr, + callback = function() + exit(popup, opts) + end, + }) + end +end + --- Setup autocommands for the popup --- @param popup NuiPopup --- @param layout NuiLayout|nil diff --git a/lua/gitlab/state.lua b/lua/gitlab/state.lua index 412a6f64..3ea3397c 100644 --- a/lua/gitlab/state.lua +++ b/lua/gitlab/state.lua @@ -433,94 +433,6 @@ M.setPluginConfiguration = function() return true end -local function exit(popup, opts) - if opts.action_before_exit and opts.cb ~= nil then - opts.cb() - popup:unmount() - else - popup:unmount() - if opts.cb ~= nil then - opts.cb() - end - end -end - --- These keymaps are buffer specific and are set dynamically when popups mount -M.set_popup_keymaps = function(popup, action, linewise_action, opts) - if M.settings.keymaps.disable_all or M.settings.keymaps.popup.disable_all then - return - end - - if opts == nil then - opts = {} - end - if action ~= "Help" and M.settings.keymaps.help then -- Don't show help on the help popup - vim.keymap.set("n", M.settings.keymaps.help, function() - local help = require("gitlab.actions.help") - help.open() - end, { buffer = popup.bufnr, desc = "Open help", nowait = M.settings.keymaps.help_nowait }) - end - if action ~= nil and M.settings.keymaps.popup.perform_action then - vim.keymap.set("n", M.settings.keymaps.popup.perform_action, function() - local text = u.get_buffer_text(popup.bufnr) - if opts.action_before_close then - action(text, popup.bufnr) - exit(popup, opts) - else - exit(popup, opts) - action(text, popup.bufnr) - end - end, { buffer = popup.bufnr, desc = "Perform action", nowait = M.settings.keymaps.popup.perform_action_nowait }) - end - - if linewise_action ~= nil and M.settings.keymaps.popup.perform_action then - vim.keymap.set("n", M.settings.keymaps.popup.perform_linewise_action, function() - local bufnr = vim.api.nvim_get_current_buf() - local linnr = vim.api.nvim_win_get_cursor(0)[1] - local text = u.get_line_content(bufnr, linnr) - linewise_action(text) - end, { - buffer = popup.bufnr, - desc = "Perform linewise action", - nowait = M.settings.keymaps.popup.perform_linewise_action_nowait, - }) - end - - if M.settings.keymaps.popup.discard_changes then - vim.keymap.set("n", M.settings.keymaps.popup.discard_changes, function() - local temp_registers = M.settings.popup.temp_registers - M.settings.popup.temp_registers = {} - vim.cmd("quit!") - M.settings.popup.temp_registers = temp_registers - end, { - buffer = popup.bufnr, - desc = "Quit discarding changes", - nowait = M.settings.keymaps.popup.discard_changes_nowait, - }) - end - - if opts.save_to_temp_register then - vim.api.nvim_create_autocmd("BufWinLeave", { - buffer = popup.bufnr, - callback = function() - local text = u.get_buffer_text(popup.bufnr) - for _, register in ipairs(M.settings.popup.temp_registers) do - vim.fn.setreg(register, text) - end - end, - }) - end - - if opts.action_before_exit then - vim.api.nvim_create_autocmd("BufWinLeave", { - buffer = popup.bufnr, - callback = function() - exit(popup, opts) - end, - }) - end -end - -- Dependencies -- These tables are passed to the async.sequence function, which calls them in sequence -- before calling an action. They are used to set global state that's required From 59f3e525c5977e6a6770e6559a842696e4d23293 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Wed, 4 Dec 2024 23:16:38 +0100 Subject: [PATCH 19/21] refactor: move helper functions to popup module --- lua/gitlab/actions/comment.lua | 4 +- lua/gitlab/actions/create_mr.lua | 12 ++--- lua/gitlab/actions/discussions/init.lua | 2 +- lua/gitlab/actions/help.lua | 2 +- lua/gitlab/actions/merge.lua | 2 +- lua/gitlab/actions/pipeline.lua | 2 +- lua/gitlab/actions/summary.lua | 6 +-- lua/gitlab/popup.lua | 58 ++++++++++++++++++++++++- lua/gitlab/utils/init.lua | 56 ------------------------ 9 files changed, 72 insertions(+), 72 deletions(-) diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index b2a2c959..d86bccc8 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -207,8 +207,8 @@ M.create_comment_layout = function(opts) local settings = u.merge(popup_settings, overrides or {}) M.current_win = vim.api.nvim_get_current_win() - M.comment_popup = Popup(u.create_popup_state(title, settings)) - M.draft_popup = Popup(u.create_box_popup_state("Draft", false, settings)) + M.comment_popup = Popup(popup.create_popup_state(title, settings)) + M.draft_popup = Popup(popup.create_box_popup_state("Draft", false, settings)) M.start_line, M.end_line = u.get_visual_selection_boundaries() local internal_layout = Layout.Box({ diff --git a/lua/gitlab/actions/create_mr.lua b/lua/gitlab/actions/create_mr.lua index 6c30e01d..7e6f35d4 100644 --- a/lua/gitlab/actions/create_mr.lua +++ b/lua/gitlab/actions/create_mr.lua @@ -330,19 +330,19 @@ end M.create_layout = function() local settings = u.merge(state.settings.popup, state.settings.popup.create_mr or {}) - local title_popup = Popup(u.create_box_popup_state("Title", false, settings)) + local title_popup = Popup(popup.create_box_popup_state("Title", false, settings)) M.title_bufnr = title_popup.bufnr - local description_popup = Popup(u.create_popup_state("Description", settings)) + local description_popup = Popup(popup.create_popup_state("Description", settings)) M.description_bufnr = description_popup.bufnr - local target_branch_popup = Popup(u.create_box_popup_state("Target branch", false, settings)) + local target_branch_popup = Popup(popup.create_box_popup_state("Target branch", false, settings)) M.target_bufnr = target_branch_popup.bufnr local delete_title = vim.o.columns > 110 and "Delete source branch" or "Delete source" - local delete_branch_popup = Popup(u.create_box_popup_state(delete_title, false, settings)) + local delete_branch_popup = Popup(popup.create_box_popup_state(delete_title, false, settings)) M.delete_branch_bufnr = delete_branch_popup.bufnr local squash_title = vim.o.columns > 110 and "Squash commits" or "Squash" - local squash_popup = Popup(u.create_box_popup_state(squash_title, false, settings)) + local squash_popup = Popup(popup.create_box_popup_state(squash_title, false, settings)) M.squash_bufnr = squash_popup.bufnr - local forked_project_id_popup = Popup(u.create_box_popup_state("Forked Project ID", false, settings)) + local forked_project_id_popup = Popup(popup.create_box_popup_state("Forked Project ID", false, settings)) M.forked_project_id_bufnr = forked_project_id_popup.bufnr local boxes = {} diff --git a/lua/gitlab/actions/discussions/init.lua b/lua/gitlab/actions/discussions/init.lua index 2f029cc7..1825c9ad 100644 --- a/lua/gitlab/actions/discussions/init.lua +++ b/lua/gitlab/actions/discussions/init.lua @@ -284,7 +284,7 @@ end -- This function (settings.keymaps.discussion_tree.edit_comment) will open the edit popup for the current comment in the discussion tree M.edit_comment = function(tree, unlinked) - local edit_popup = Popup(u.create_popup_state("Edit Comment", state.settings.popup.edit)) + local edit_popup = Popup(popup.create_popup_state("Edit Comment", state.settings.popup.edit)) local current_node = tree:get_node() local note_node = common.get_note_node(tree, current_node) local root_node = common.get_root_node(tree, current_node) diff --git a/lua/gitlab/actions/help.lua b/lua/gitlab/actions/help.lua index 4dd9d2c5..edbe5afc 100644 --- a/lua/gitlab/actions/help.lua +++ b/lua/gitlab/actions/help.lua @@ -20,7 +20,7 @@ M.open = function() local longest_line = u.get_longest_string(help_content_lines) local opts = { "Help", state.settings.popup.help, longest_line + 3, #help_content_lines, 70 } - local help_popup = Popup(u.create_popup_state(unpack(opts))) + local help_popup = Popup(popup.create_popup_state(unpack(opts))) help_popup:on(event.BufLeave, function() help_popup:unmount() diff --git a/lua/gitlab/actions/merge.lua b/lua/gitlab/actions/merge.lua index 4046b36c..81eed3f6 100644 --- a/lua/gitlab/actions/merge.lua +++ b/lua/gitlab/actions/merge.lua @@ -8,7 +8,7 @@ local reviewer = require("gitlab.reviewer") local M = {} local function create_squash_message_popup() - return Popup(u.create_popup_state("Squash Commit Message", state.settings.popup.squash_message)) + return Popup(popup.create_popup_state("Squash Commit Message", state.settings.popup.squash_message)) end ---@class MergeOpts diff --git a/lua/gitlab/actions/pipeline.lua b/lua/gitlab/actions/pipeline.lua index f1a97684..00dc5c29 100644 --- a/lua/gitlab/actions/pipeline.lua +++ b/lua/gitlab/actions/pipeline.lua @@ -41,7 +41,7 @@ M.open = function() local height = 6 + #M.pipeline_jobs + 3 local pipeline_popup = - Popup(u.create_popup_state("Loading Pipeline...", state.settings.popup.pipeline, width, height, 60)) + Popup(popup.create_popup_state("Loading Pipeline...", state.settings.popup.pipeline, width, height, 60)) popup.set_up_autocommands(pipeline_popup, nil, vim.api.nvim_get_current_win()) M.pipeline_popup = pipeline_popup pipeline_popup:mount() diff --git a/lua/gitlab/actions/summary.lua b/lua/gitlab/actions/summary.lua index d4b4172a..9f75ca7d 100644 --- a/lua/gitlab/actions/summary.lua +++ b/lua/gitlab/actions/summary.lua @@ -168,15 +168,15 @@ end M.create_layout = function(info_lines) local settings = u.merge(state.settings.popup, state.settings.popup.summary or {}) - local title_popup = Popup(u.create_box_popup_state(nil, false, settings)) + local title_popup = Popup(popup.create_box_popup_state(nil, false, settings)) M.title_bufnr = title_popup.bufnr - local description_popup = Popup(u.create_popup_state("Description", settings)) + local description_popup = Popup(popup.create_popup_state("Description", settings)) M.description_bufnr = description_popup.bufnr local details_popup local internal_layout if state.settings.info.enabled then - details_popup = Popup(u.create_box_popup_state("Details", false, settings)) + details_popup = Popup(popup.create_box_popup_state("Details", false, settings)) if state.settings.info.horizontal then local longest_line = u.get_longest_string(info_lines) internal_layout = Layout.Box({ diff --git a/lua/gitlab/popup.lua b/lua/gitlab/popup.lua index 8c547790..8ab0b513 100644 --- a/lua/gitlab/popup.lua +++ b/lua/gitlab/popup.lua @@ -2,6 +2,62 @@ local u = require("gitlab.utils") local M = {} +---Get the popup view_opts +---@param title string The string to appear on top of the popup +---@param user_settings table|nil User-defined popup settings +---@param width number? Override default width +---@param height number? Override default height +---@param zindex number? Override default zindex +---@return table +M.create_popup_state = function(title, user_settings, width, height, zindex) + local settings = u.merge(require("gitlab.state").settings.popup, user_settings or {}) + local view_opts = { + buf_options = { + filetype = "markdown", + }, + relative = "editor", + enter = true, + focusable = true, + zindex = zindex or 50, + border = { + style = settings.border, + text = { + top = title, + }, + }, + position = settings.position, + size = { + width = width and math.min(width, vim.o.columns - 2) or settings.width, + height = height and math.min(height, vim.o.lines - 3) or settings.height, + }, + opacity = settings.opacity, + } + + return view_opts +end + +---Create view_opts for Box popups used inside popup Layouts +---@param title string|nil The string to appear on top of the popup +---@param enter boolean Whether the pop should be focused after creation +---@param settings table User defined popup settings +---@return table +M.create_box_popup_state = function(title, enter, settings) + return { + buf_options = { + filetype = "markdown", + }, + enter = enter or false, + focusable = true, + border = { + style = settings.border, + text = { + top = title, + }, + }, + opacity = settings.opacity, + } +end + local function exit(popup, opts) if opts.action_before_exit and opts.cb ~= nil then opts.cb() @@ -102,7 +158,7 @@ M.set_up_autocommands = function(popup, layout, previous_window, opts) if layout ~= nil then layout:update() else - popup:update_layout(opts and u.create_popup_state(unpack(opts))) + popup:update_layout(opts and M.create_popup_state(unpack(opts))) end end) diff --git a/lua/gitlab/utils/init.lua b/lua/gitlab/utils/init.lua index 69b65ec4..776f05f0 100644 --- a/lua/gitlab/utils/init.lua +++ b/lua/gitlab/utils/init.lua @@ -480,62 +480,6 @@ M.difference = function(a, b) return not_included end ----Get the popup view_opts ----@param title string The string to appear on top of the popup ----@param settings table|nil User defined popup settings ----@param width number? Override default width ----@param height number? Override default height ----@param zindex number? Override default zindex ----@return table -M.create_popup_state = function(title, settings, width, height, zindex) - settings = M.merge(require("gitlab.state").settings.popup, settings or {}) - local view_opts = { - buf_options = { - filetype = "markdown", - }, - relative = "editor", - enter = true, - focusable = true, - zindex = zindex or 50, - border = { - style = settings.border, - text = { - top = title, - }, - }, - position = settings.position, - size = { - width = width and math.min(width, vim.o.columns - 2) or settings.width, - height = height and math.min(height, vim.o.lines - 3) or settings.height, - }, - opacity = settings.opacity, - } - - return view_opts -end - ----Create view_opts for Box popups used inside popup Layouts ----@param title string|nil The string to appear on top of the popup ----@param enter boolean Whether the pop should be focused after creation ----@param settings table User defined popup settings ----@return table -M.create_box_popup_state = function(title, enter, settings) - return { - buf_options = { - filetype = "markdown", - }, - enter = enter or false, - focusable = true, - border = { - style = settings.border, - text = { - top = title, - }, - }, - opacity = settings.opacity, - } -end - M.read_file = function(file_path, opts) local file = io.open(file_path, "r") if file == nil then From 22da41fd2d4d391a490409955168041130a59fea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Thu, 5 Dec 2024 00:15:46 +0100 Subject: [PATCH 20/21] refactor: use clearer syntax to select popup settings --- lua/gitlab/actions/comment.lua | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index d86bccc8..08e11da8 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -198,13 +198,20 @@ M.create_comment_layout = function(opts) end end - local title = opts.discussion_id and "Reply" or opts.unlinked and "Note" or "Comment" - local popup_settings = state.settings.popup - local overrides = opts.discussion_id ~= nil and popup_settings.reply - or opts.unlinked and popup_settings.note - or popup_settings.comment - local settings = u.merge(popup_settings, overrides or {}) + local title + local user_settings + if opts.discussion_id ~= nil then + title = "Reply" + user_settings = popup_settings.reply + elseif opts.unlinked then + title = "Note" + user_settings = popup_settings.note + else + title = "Comment" + user_settings = popup_settings.comment + end + local settings = u.merge(popup_settings, user_settings or {}) M.current_win = vim.api.nvim_get_current_win() M.comment_popup = Popup(popup.create_popup_state(title, settings)) From 82f509de98bf5dea35b8240bcedba61bf5171019 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Thu, 5 Dec 2024 00:26:47 +0100 Subject: [PATCH 21/21] revert: restore the help and pipeline popup settings --- doc/gitlab.nvim.txt | 2 ++ lua/gitlab/state.lua | 2 ++ 2 files changed, 4 insertions(+) diff --git a/doc/gitlab.nvim.txt b/doc/gitlab.nvim.txt index 0dc13552..c6e6bc79 100644 --- a/doc/gitlab.nvim.txt +++ b/doc/gitlab.nvim.txt @@ -243,6 +243,8 @@ you call this function with no values the defaults will be used: comment = nil, -- Individual popup overrides, e.g. { width = "60%", height = "80%", border = "single", opacity = 0.85 }, edit = nil, note = nil, + help = nil, -- Width and height are calculated automatically and cannot be overridden + pipeline = nil, -- Width and height are calculated automatically and cannot be overridden reply = nil, squash_message = nil, create_mr = { width = "95%", height = "95%" }, diff --git a/lua/gitlab/state.lua b/lua/gitlab/state.lua index 3ea3397c..c276af41 100644 --- a/lua/gitlab/state.lua +++ b/lua/gitlab/state.lua @@ -140,6 +140,8 @@ M.settings = { comment = nil, edit = nil, note = nil, + help = nil, + pipeline = nil, reply = nil, squash_message = nil, create_mr = { width = "95%", height = "95%" },