Temporary registers, SSH Port, Bug Fixes (#251)

- allows SSH Gitlab connection to have custom port
- introduces temporary registers
- fixes issue w/ quitting the popup on MR creation

This is a #MINOR release
This commit is contained in:
Harrison (Harry) Cramer
2024-04-09 12:24:07 -04:00
committed by GitHub
parent 36f512cd6d
commit 7c3ee0530b
15 changed files with 404 additions and 290 deletions

View File

@@ -35,7 +35,7 @@ M.create_comment = function()
comment_popup:mount()
state.set_popup_keymaps(comment_popup, function(text)
M.confirm_create_comment(text)
end, miscellaneous.attach_file)
end, miscellaneous.attach_file, miscellaneous.editable_popup_opts)
end
---Create multiline comment for the last selection.
@@ -48,7 +48,7 @@ M.create_multiline_comment = function()
comment_popup:mount()
state.set_popup_keymaps(comment_popup, function(text)
M.confirm_create_comment(text, { start_line = start_line, end_line = end_line })
end, miscellaneous.attach_file)
end, miscellaneous.attach_file, miscellaneous.editable_popup_opts)
end
---Create comment prepopulated with gitlab suggestion
@@ -95,7 +95,7 @@ M.create_comment_suggestion = function()
else
M.confirm_create_comment(text, nil)
end
end, miscellaneous.attach_file)
end, miscellaneous.attach_file, miscellaneous.editable_popup_opts)
end
M.create_note = function()
@@ -103,7 +103,7 @@ M.create_note = function()
note_popup:mount()
state.set_popup_keymaps(note_popup, function(text)
M.confirm_create_comment(text, nil, true)
end, miscellaneous.attach_file)
end, miscellaneous.attach_file, miscellaneous.editable_popup_opts)
end
---This function (settings.popup.perform_action) will send the comment to the Go server

View File

@@ -13,10 +13,9 @@ local miscellaneous = require("gitlab.actions.miscellaneous")
---@field target? string
---@field title? string
---@field description? string
---@class Args
---@field target? string
---@field template_file? string
---@field delete_branch boolean?
---@field squash boolean?
local M = {
started = false,
@@ -39,49 +38,9 @@ M.reset_state = function()
M.mr.description = ""
end
local title_popup_settings = {
buf_options = {
filetype = "markdown",
},
focusable = true,
border = {
style = "rounded",
text = {
top = "Title",
},
},
}
local target_popup_settings = {
buf_options = {
filetype = "markdown",
},
focusable = true,
border = {
style = "rounded",
text = {
top = "Target branch",
},
},
}
local description_popup_settings = {
buf_options = {
filetype = "markdown",
},
enter = true,
focusable = true,
border = {
style = "rounded",
text = {
top = "Description",
},
},
}
---1. If the user has already begun writing an MR, prompt them to
--- continue working on it.
---@param args? Args
---@param args? Mr
M.start = function(args)
if M.started then
vim.ui.select({ "Yes", "No" }, { prompt = "Continue your previous MR?" }, function(choice)
@@ -99,18 +58,19 @@ M.start = function(args)
end
---2. Pick the target branch
---@param args? Args
M.pick_target = function(args)
if not args then
args = {}
---@param mr? Mr
M.pick_target = function(mr)
if not mr then
mr = {}
end
if args.target ~= nil then
M.pick_template({ target = args.target }, args)
if mr.target ~= nil then
M.pick_template(mr)
return
end
if state.settings.create_mr.target ~= nil then
M.pick_template({ target = state.settings.create_mr.target }, args)
mr.target = state.settings.create_mr.target
M.pick_template(mr)
return
end
@@ -119,7 +79,8 @@ M.pick_target = function(args)
prompt = "Choose target branch for merge",
}, function(choice)
if choice then
M.pick_template({ target = choice }, args)
mr.target = choice
M.pick_template(mr)
end
end)
end
@@ -137,22 +98,22 @@ end
---3. Pick template (if applicable). This is used as the description
---@param mr Mr
---@param args Args
M.pick_template = function(mr, args)
if not args then
args = {}
M.pick_template = function(mr)
if mr.description ~= nil then
M.add_title(mr)
return
end
local template_file = args.template_file or state.settings.create_mr.template_file
local template_file = mr.template_file or state.settings.create_mr.template_file
if template_file ~= nil then
local description = u.read_file(make_template_path(template_file))
M.add_title({ target = mr.target, description = description })
mr.description = u.read_file(make_template_path(template_file))
M.add_title(mr)
return
end
local all_templates = u.list_files_in_folder(".gitlab" .. state.settings.file_separator .. "merge_request_templates")
if all_templates == nil then
M.add_title({ target = mr.target })
M.add_title(mr)
return
end
@@ -163,18 +124,21 @@ M.pick_template = function(mr, args)
vim.ui.select(opts, {
prompt = "Choose Template",
}, function(choice)
if choice then
local description = u.read_file(make_template_path(choice))
M.add_title({ target = mr.target, description = description })
elseif choice == "Blank Template" then
M.add_title({ target = mr.target })
if choice and choice ~= "Blank Template" then
mr.description = u.read_file(make_template_path(choice))
end
M.add_title(mr)
end)
end
---4. Prompts the user for the title of the MR
---@param mr Mr
M.add_title = function(mr)
if mr.title ~= nil then
M.open_confirmation_popup(mr)
return
end
local input = Input({
position = "50%",
relative = "editor",
@@ -200,8 +164,8 @@ M.add_title = function(mr)
end
---5. Show the final popup.
---The function will render a popup containing the MR title and MR description, and
---target branch. The title and description are editable.
---The function will render a popup containing the MR title and MR description,
---target branch, and the "delete_branch" and "squash" options. All fields are editable.
---@param mr Mr
M.open_confirmation_popup = function(mr)
M.started = true
@@ -211,7 +175,7 @@ M.open_confirmation_popup = function(mr)
return
end
local layout, title_popup, description_popup, target_popup = M.create_layout()
local layout, title_popup, description_popup, target_popup, delete_branch_popup, squash_popup = M.create_layout()
M.layout = layout
M.layout_buf = layout.bufnr
@@ -220,22 +184,30 @@ M.open_confirmation_popup = function(mr)
local function exit()
local title = vim.fn.trim(u.get_buffer_text(M.title_bufnr))
local description = u.get_buffer_text(M.description_bufnr)
local target = vim.fn.trim(u.get_buffer_text(target_popup.bufnr))
local target = vim.fn.trim(u.get_buffer_text(M.target_bufnr))
local delete_branch = u.string_to_bool(u.get_buffer_text(M.delete_branch_bufnr))
local squash = u.string_to_bool(u.get_buffer_text(M.squash_bufnr))
M.mr = {
title = title,
description = description,
target = target,
delete_branch = delete_branch,
squash = squash,
}
layout:unmount()
M.layout_visible = false
end
local description_lines = mr.description and M.build_description_lines(mr.description) or { "" }
local delete_branch = u.get_first_non_nil_value({ mr.delete_branch, state.settings.create_mr.delete_branch })
local squash = u.get_first_non_nil_value({ mr.squash, state.settings.create_mr.squash })
vim.schedule(function()
vim.api.nvim_buf_set_lines(description_popup.bufnr, 0, -1, false, description_lines)
vim.api.nvim_buf_set_lines(title_popup.bufnr, 0, -1, false, { mr.title })
vim.api.nvim_buf_set_lines(target_popup.bufnr, 0, -1, false, { mr.target })
vim.api.nvim_buf_set_lines(M.description_bufnr, 0, -1, false, description_lines)
vim.api.nvim_buf_set_lines(M.title_bufnr, 0, -1, false, { mr.title })
vim.api.nvim_buf_set_lines(M.target_bufnr, 0, -1, false, { mr.target })
vim.api.nvim_buf_set_lines(M.delete_branch_bufnr, 0, -1, false, { u.bool_to_string(delete_branch) })
vim.api.nvim_buf_set_lines(M.squash_bufnr, 0, -1, false, { u.bool_to_string(squash) })
local popup_opts = {
cb = exit,
@@ -246,8 +218,10 @@ M.open_confirmation_popup = function(mr)
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, nil, popup_opts)
state.set_popup_keymaps(delete_branch_popup, M.create_mr, nil, popup_opts)
state.set_popup_keymaps(squash_popup, M.create_mr, nil, popup_opts)
vim.api.nvim_set_current_buf(description_popup.bufnr)
vim.api.nvim_set_current_buf(M.description_bufnr)
end)
end
@@ -268,11 +242,15 @@ M.create_mr = function()
local description = u.get_buffer_text(M.description_bufnr)
local title = u.get_buffer_text(M.title_bufnr):gsub("\n", " ")
local target = u.get_buffer_text(M.target_bufnr):gsub("\n", " ")
local delete_branch = u.string_to_bool(u.get_buffer_text(M.delete_branch_bufnr))
local squash = u.string_to_bool(u.get_buffer_text(M.squash_bufnr))
local body = {
title = title,
description = description,
target_branch = target,
delete_branch = delete_branch,
squash = squash,
}
job.run_job("/create_mr", "POST", body, function(data)
@@ -284,20 +262,30 @@ M.create_mr = function()
end
M.create_layout = function()
local title_popup = Popup(title_popup_settings)
local title_popup = Popup(u.create_box_popup_state("Title", false))
M.title_bufnr = title_popup.bufnr
local description_popup = Popup(description_popup_settings)
local description_popup = Popup(u.create_box_popup_state("Description", true))
M.description_bufnr = description_popup.bufnr
local target_branch_popup = Popup(target_popup_settings)
local target_branch_popup = Popup(u.create_box_popup_state("Target branch", false))
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))
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))
M.squash_bufnr = squash_popup.bufnr
local internal_layout
internal_layout = Layout.Box({
Layout.Box({
Layout.Box(title_popup, { grow = 1 }),
Layout.Box(target_branch_popup, { grow = 1 }),
}, { size = 3 }),
Layout.Box(description_popup, { grow = 1 }),
Layout.Box({
Layout.Box(delete_branch_popup, { size = { width = #delete_title + 4 } }),
Layout.Box(squash_popup, { size = { width = #squash_title + 4 } }),
Layout.Box(target_branch_popup, { grow = 1 }),
}, { size = 3 }),
}, { dir = "col" })
local layout = Layout({
@@ -311,7 +299,7 @@ M.create_layout = function()
layout:mount()
return layout, title_popup, description_popup, target_branch_popup
return layout, title_popup, description_popup, target_branch_popup, delete_branch_popup, squash_popup
end
return M

View File

@@ -256,7 +256,12 @@ M.reply = function(tree)
local discussion_node = M.get_root_node(tree, node)
local id = tostring(discussion_node.id)
reply_popup:mount()
state.set_popup_keymaps(reply_popup, M.send_reply(tree, id), miscellaneous.attach_file)
state.set_popup_keymaps(
reply_popup,
M.send_reply(tree, id),
miscellaneous.attach_file,
miscellaneous.editable_popup_opts
)
end
-- This function will send the reply to the Go API
@@ -331,7 +336,9 @@ M.edit_comment = function(tree, unlinked)
vim.api.nvim_buf_set_lines(currentBuffer, 0, -1, false, lines)
state.set_popup_keymaps(
edit_popup,
M.send_edits(tostring(root_node.id), tonumber(note_node.root_note_id or note_node.id), unlinked)
M.send_edits(tostring(root_node.id), tonumber(note_node.root_note_id or note_node.id), unlinked),
nil,
miscellaneous.editable_popup_opts
)
end

View File

@@ -3,6 +3,7 @@ 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 = {}
@@ -17,7 +18,7 @@ end
---@param opts MergeOpts
M.merge = function(opts)
local merge_body = { squash = state.settings.merge.squash, delete_branch = state.settings.merge.delete_branch }
local merge_body = { squash = state.INFO.squash, delete_branch = state.INFO.delete_branch }
if opts then
merge_body.squash = opts.squash ~= nil and opts.squash
merge_body.delete_branch = opts.delete_branch ~= nil and opts.delete_branch
@@ -33,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)
end, nil, miscellaneous.editable_popup_opts)
else
M.confirm_merge(merge_body)
end

View File

@@ -34,4 +34,8 @@ M.attach_file = function()
end)
end
M.editable_popup_opts = {
save_to_temp_register = true,
}
return M

View File

@@ -17,43 +17,6 @@ local M = {
description_bufnr = nil,
}
local title_popup_settings = {
buf_options = {
filetype = "markdown",
},
focusable = true,
border = {
style = "rounded",
},
}
local details_popup_settings = {
buf_options = {
filetype = "markdown",
},
focusable = true,
border = {
style = "rounded",
text = {
top = "Details",
},
},
}
local description_popup_settings = {
buf_options = {
filetype = "markdown",
},
enter = true,
focusable = true,
border = {
style = "rounded",
text = {
top = "Description",
},
},
}
-- The function will render a popup containing the MR title and MR description, and optionally,
-- any additional metadata that the user wants. The title and description are editable and
-- can be changed via the local action keybinding, which also closes the popup
@@ -66,7 +29,7 @@ M.summary = function()
local title = state.INFO.title
local description_lines = M.build_description_lines()
local info_lines = state.settings.info.enabled and M.build_info_lines() or nil
local info_lines = state.settings.info.enabled and M.build_info_lines() or { "" }
local layout, title_popup, description_popup, info_popup = M.create_layout(info_lines)
@@ -89,7 +52,7 @@ M.summary = function()
vim.api.nvim_set_option_value("readonly", false, { buf = info_popup.bufnr })
end
M.color_labels(info_popup.bufnr) -- Color labels in details popup
M.color_details(info_popup.bufnr) -- Color values in details popup
state.set_popup_keymaps(
description_popup,
@@ -134,7 +97,9 @@ M.build_info_lines = function()
reviewers = { title = "Reviewers", content = u.make_readable_list(info.reviewers, "name") },
branch = { title = "Branch", content = info.source_branch },
labels = { title = "Labels", content = table.concat(info.labels, ", ") },
target_branch = { title = "Target Branch", content = state.INFO.target_branch },
target_branch = { title = "Target Branch", content = info.target_branch },
delete_branch = { title = "Delete Source Branch", content = (info.force_remove_source_branch and "Yes" or "No") },
squash = { title = "Squash Commits", content = (info.squash and "Yes" or "No") },
pipeline = {
title = "Pipeline Status",
content = function()
@@ -196,15 +161,15 @@ M.edit_summary = function()
end
M.create_layout = function(info_lines)
local title_popup = Popup(title_popup_settings)
local title_popup = Popup(u.create_box_popup_state(nil, false))
M.title_bufnr = title_popup.bufnr
local description_popup = Popup(description_popup_settings)
local description_popup = Popup(u.create_box_popup_state("Description", true))
M.description_bufnr = description_popup.bufnr
local details_popup
local internal_layout
if state.settings.info.enabled then
details_popup = Popup(details_popup_settings)
details_popup = Popup(u.create_box_popup_state("Details", false))
if state.settings.info.horizontal then
local longest_line = u.get_longest_string(info_lines)
internal_layout = Layout.Box({
@@ -241,8 +206,8 @@ M.create_layout = function(info_lines)
return layout, title_popup, description_popup, details_popup
end
M.color_labels = function(bufnr)
local label_namespace = vim.api.nvim_create_namespace("Labels")
M.color_details = function(bufnr)
local details_namespace = vim.api.nvim_create_namespace("Details")
for i, v in ipairs(state.settings.info.fields) do
if v == "labels" then
local line_content = u.get_line_content(bufnr, i)
@@ -251,9 +216,16 @@ M.color_labels = function(bufnr)
if start_idx ~= nil and end_idx ~= nil then
vim.cmd("highlight " .. "label" .. j .. " guifg=white")
vim.api.nvim_set_hl(0, ("label" .. j), { fg = label.Color })
vim.api.nvim_buf_add_highlight(bufnr, label_namespace, ("label" .. j), i - 1, start_idx - 1, end_idx)
vim.api.nvim_buf_add_highlight(bufnr, details_namespace, ("label" .. j), i - 1, start_idx - 1, end_idx)
end
end
elseif v == "delete_branch" or v == "squash" or v == "draft" or v == "conflicts" then
local line_content = u.get_line_content(bufnr, i)
local start_idx, end_idx = line_content:find("%S-$")
if start_idx ~= nil and end_idx ~= nil then
vim.api.nvim_set_hl(0, "boolean", { link = "Constant" })
vim.api.nvim_buf_add_highlight(bufnr, details_namespace, "boolean", i - 1, start_idx - 1, end_idx)
end
end
end
end

View File

@@ -85,9 +85,14 @@ M.init_popup = function(tree, bufnr)
end
local cursor_pos = vim.api.nvim_win_get_cursor(0)
-- "zyiw on the next line erases the unnamed register. This may interfere with the
-- `temp_registers` used for backing up editable popup contents, so let's backup the unnamed
-- register.
local unnamed_register_contents = vim.fn.getreg('"')
vim.api.nvim_command('normal! "zyiw')
vim.api.nvim_win_set_cursor(0, cursor_pos)
local word = vim.fn.getreg("z")
vim.fn.setreg('"', unnamed_register_contents) -- restore the unnamed register
for k, v in pairs(M.emoji_map) do
if v.moji == word then

View File

@@ -39,6 +39,7 @@ M.settings = {
help = nil,
pipeline = nil,
squash_message = nil,
temp_registers = {},
},
discussion_tree = {
auto_open = true,
@@ -85,13 +86,11 @@ M.settings = {
return " " .. discussions_content .. " %#Comment#| " .. notes_content .. help
end,
},
merge = {
squash = false,
delete_branch = false,
},
create_mr = {
target = nil,
template_file = nil,
delete_branch = false,
squash = false,
title_input = {
width = 40,
border = "rounded",
@@ -112,6 +111,8 @@ M.settings = {
"pipeline",
"branch",
"target_branch",
"delete_branch",
"squash",
"labels",
},
},
@@ -296,9 +297,15 @@ M.set_popup_keymaps = function(popup, action, linewise_action, opts)
end, { buffer = popup.bufnr, desc = "Perform linewise action" })
end
vim.api.nvim_create_autocmd("BufUnload", {
vim.api.nvim_create_autocmd("BufWinLeave", {
buffer = popup.bufnr,
callback = function()
if opts.save_to_temp_register then
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
exit(popup, opts)
end,
})

View File

@@ -29,6 +29,17 @@ M.get_last_word = function(sentence, divider)
return words[#words] or ""
end
---Return the first non-nil value in the input table, or nil
---@param values table The list of input values
---@return any
M.get_first_non_nil_value = function(values)
for _, val in pairs(values) do
if val ~= nil then
return val
end
end
end
---Returns whether a string ends with a substring
---@param str string
---@param ending string
@@ -336,6 +347,30 @@ M.get_buffer_text = function(bufnr)
return text
end
---Convert string to corresponding boolean
---@param str string
---@return boolean
M.string_to_bool = function(str)
str = vim.fn.trim(str)
if str == "true" or str == "True" or str == "TRUE" then
return true
elseif str == "false" or str == "False" or str == "FALSE" then
return false
end
M.notify("Not a valid boolean value `" .. str .. "`. Defaulting to `false`", vim.log.levels.WARN)
return false
end
---Convert boolean to corresponding string
---@param bool boolean
---@return string
M.bool_to_string = function(bool)
if bool == true then
return "true"
end
return "false"
end
M.string_starts = function(str, start)
return str:sub(1, #start) == start
end
@@ -431,6 +466,27 @@ M.create_popup_state = function(title, settings, width, height, zindex)
return view_opts
end
---Create view_opts for Box popups used inside popup Layouts
---@param title string The string to appear on top of the popup
---@param enter boolean Whether the pop should be focused after creation
---@return table
M.create_box_popup_state = function(title, enter)
local settings = require("gitlab.state").settings.popup
return {
buf_options = {
filetype = "markdown",
},
enter = enter or false,
focusable = true,
border = {
style = settings.border,
text = {
top = title,
},
},
}
end
M.read_file = function(file_path, opts)
local file = io.open(file_path, "r")
if file == nil then