* fix: Jumping to wrong buffer (#261)
* fix: Go to last line and show warning when diagnostic is past the end of buffer (#262)
* fix: Get recent pipeline through other means (#266)
* feat: Add keymaps and linewise actions to layouts (#265)

This is a #MINOR release, because we are introducing new keybindings for the comment/note popups.

---------

Co-authored-by: Jakub F. Bortlík <jakub.bortlik@proton.me>
Co-authored-by: sunfuze <sunfuze.1989@gmail.com>
This commit is contained in:
Harrison (Harry) Cramer
2024-04-15 09:56:21 -04:00
committed by GitHub
parent 138b96baea
commit f906af0c3a
20 changed files with 358 additions and 103 deletions

View File

@@ -74,14 +74,10 @@ M.pick_target = function(mr)
return
end
local all_branch_names = u.get_all_git_branches(true)
vim.ui.select(all_branch_names, {
prompt = "Choose target branch for merge",
}, function(choice)
if choice then
mr.target = choice
M.pick_template(mr)
end
-- Select target branch interactively if it hasn't been selected by other means
u.select_target_branch(function(target)
mr.target = target
M.pick_template(mr)
end)
end
@@ -177,6 +173,14 @@ M.open_confirmation_popup = function(mr)
local layout, title_popup, description_popup, target_popup, delete_branch_popup, squash_popup = M.create_layout()
local popups = {
title_popup,
description_popup,
delete_branch_popup,
squash_popup,
target_popup,
}
M.layout = layout
M.layout_buf = layout.bufnr
M.layout_visible = true
@@ -209,6 +213,10 @@ M.open_confirmation_popup = function(mr)
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) })
u.switch_can_edit_buf(M.delete_branch_bufnr, false)
u.switch_can_edit_buf(M.squash_bufnr, false)
u.switch_can_edit_buf(M.target_bufnr, false)
local popup_opts = {
cb = exit,
action_before_close = true,
@@ -217,9 +225,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)
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)
miscellaneous.set_cycle_popups_keymaps(popups)
vim.api.nvim_set_current_buf(M.description_bufnr)
end)
@@ -237,6 +246,18 @@ M.build_description_lines = function(template_content)
return description_lines
end
---Prompts for interactive selection of a new target among remote-tracking branches
M.select_new_target = function()
local bufnr = vim.api.nvim_get_current_buf()
u.select_target_branch(function(target)
vim.schedule(function()
u.switch_can_edit_buf(bufnr, true)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { target })
u.switch_can_edit_buf(bufnr, false)
end)
end)
end
---This function will POST the new MR to create it
M.create_mr = function()
local description = u.get_buffer_text(M.description_bufnr)

View File

@@ -432,7 +432,12 @@ M.jump_to_reviewer = function(tree)
u.notify("Could not get discussion node", vim.log.levels.ERROR)
return
end
reviewer.jump(root_node.file_name, get_new_line(root_node), get_old_line(root_node))
local line_number = (root_node.new_line or root_node.old_line or 1)
if root_node.range then
local start_old_line, start_new_line = common.parse_line_code(root_node.range.start.line_code)
line_number = root_node.old_line and start_old_line or start_new_line
end
reviewer.jump(root_node.file_name, line_number, root_node.old_line == nil)
M.refresh_view()
end
@@ -973,20 +978,18 @@ end
---@param tree NuiTree
M.open_in_browser = function(tree)
local url = M.get_url(tree)
if url == nil then
return
if url ~= nil then
u.open_in_browser(url)
end
u.open_in_browser(url)
end
---@param tree NuiTree
M.copy_node_url = function(tree)
local url = M.get_url(tree)
if url == nil then
return
if url ~= nil then
vim.fn.setreg("+", url)
u.notify("Copied '" .. url .. "' to clipboard", vim.log.levels.INFO)
end
u.notify("Copied '" .. url .. "' to clipboard", vim.log.levels.INFO)
vim.fn.setreg("+", url)
end
M.add_emoji_to_note = function(tree, unlinked)

View File

@@ -38,4 +38,58 @@ M.editable_popup_opts = {
save_to_temp_register = true,
}
-- 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 number_of_popups = #popups
for i, popup in ipairs(popups) do
popup:map("n", state.settings.popup.keymaps.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)" })
popup:map("n", state.settings.popup.keymaps.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)" })
end
end
---Toggle the value in a "Boolean buffer"
M.toggle_bool = function()
local bufnr = vim.api.nvim_get_current_buf()
local current_val = u.get_buffer_text(bufnr)
vim.schedule(function()
u.switch_can_edit_buf(bufnr, true)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { u.toggle_string_bool(current_val) })
u.switch_can_edit_buf(bufnr, false)
end)
end
return M

View File

@@ -32,6 +32,11 @@ M.summary = function()
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)
local popups = {
title_popup,
description_popup,
info_popup,
}
M.layout = layout
M.layout_buf = layout.bufnr
@@ -48,12 +53,10 @@ M.summary = function()
if info_popup then
vim.api.nvim_buf_set_lines(info_popup.bufnr, 0, -1, false, info_lines)
vim.api.nvim_set_option_value("modifiable", false, { buf = info_popup.bufnr })
vim.api.nvim_set_option_value("readonly", false, { buf = info_popup.bufnr })
u.switch_can_edit_buf(info_popup.bufnr, false)
M.color_details(info_popup.bufnr) -- Color values in details popup
end
M.color_details(info_popup.bufnr) -- Color values in details popup
state.set_popup_keymaps(
description_popup,
M.edit_summary,
@@ -61,6 +64,8 @@ M.summary = function()
{ cb = exit, action_before_close = true }
)
state.set_popup_keymaps(title_popup, M.edit_summary, nil, { cb = exit, action_before_close = true })
state.set_popup_keymaps(info_popup, M.edit_summary, nil, { cb = exit, action_before_close = true })
miscellaneous.set_cycle_popups_keymaps(popups)
vim.api.nvim_set_current_buf(description_popup.bufnr)
end)

View File

@@ -73,10 +73,16 @@ return {
data = data.data,
print_settings = state.print_settings,
open_in_browser = async.sequence({ info }, function()
if state.INFO.web_url == nil then
u.notify("Could not get Gitlab URL", vim.log.levels.ERROR)
return
local web_url = u.get_web_url()
if web_url ~= nil then
u.open_in_browser(web_url)
end
end),
copy_mr_url = async.sequence({ info }, function()
local web_url = u.get_web_url()
if web_url ~= nil then
vim.fn.setreg("+", web_url)
u.notify("Copied '" .. web_url .. "' to clipboard", vim.log.levels.INFO)
end
u.open_in_browser(state.INFO.web_url)
end),
}

View File

@@ -93,9 +93,9 @@ end
-- Jumps to the location provided in the reviewer window
---@param file_name string
---@param new_line number|nil
---@param old_line number|nil
M.jump = function(file_name, new_line, old_line)
---@param line_number number
---@param new_buffer boolean
M.jump = function(file_name, line_number, new_buffer)
if M.tabnr == nil then
u.notify("Can't jump to Diffvew. Is it open?", vim.log.levels.ERROR)
return
@@ -115,13 +115,19 @@ M.jump = function(file_name, new_line, old_line)
async.await(view:set_file(file))
local layout = view.cur_layout
if old_line == nil then
local number_of_lines
if new_buffer then
layout.b:focus()
vim.api.nvim_win_set_cursor(0, { new_line, 0 })
number_of_lines = u.get_buffer_length(layout.b.file.bufnr)
else
layout.a:focus()
vim.api.nvim_win_set_cursor(0, { old_line, 0 })
number_of_lines = u.get_buffer_length(layout.a.file.bufnr)
end
if line_number > number_of_lines then
u.notify("Diagnostic position outside buffer. Jumping to last line instead.", vim.log.levels.WARN)
line_number = number_of_lines
end
vim.api.nvim_win_set_cursor(0, { line_number, 0 })
end
---Get the data from diffview, such as line information and file name. May be used by

View File

@@ -26,6 +26,10 @@ M.settings = {
attachment_dir = "",
help = "g?",
popup = {
keymaps = {
next_field = "<Tab>",
prev_field = "<S-Tab>",
},
perform_action = "<leader>s",
perform_linewise_action = "<leader>l",
width = "40%",

View File

@@ -347,6 +347,11 @@ M.get_buffer_text = function(bufnr)
return text
end
---Returns the number of lines in the buffer. Returns 1 even for empty buffers.
M.get_buffer_length = function(bufnr)
return #vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
end
---Convert string to corresponding boolean
---@param str string
---@return boolean
@@ -371,6 +376,27 @@ M.bool_to_string = function(bool)
return "false"
end
---Toggle boolean value
---@param bool string
---@return string
M.toggle_string_bool = function(bool)
local string_bools = {
["true"] = "false",
["True"] = "False",
["TRUE"] = "FALSE",
["false"] = "true",
["False"] = "True",
["FALSE"] = "TRUE",
}
bool = bool:gsub("^%s+", ""):gsub("%s+$", "")
local toggled = string_bools[bool]
if toggled == nil then
M.notify(("Cannot toggle value '%s'"):format(bool), vim.log.levels.ERROR)
return bool
end
return toggled
end
M.string_starts = function(str, start)
return str:sub(1, #start) == start
end
@@ -630,37 +656,77 @@ M.make_comma_separated_readable = function(str)
return string.gsub(str, ",", ", ")
end
---@param remote? boolean
M.get_all_git_branches = function(remote)
local branches = {}
local handle = remote == true and io.popen("git branch -r 2>&1") or io.popen("git branch 2>&1")
---Return the name of the current branch
---@return string|nil
M.get_current_branch = function()
local handle = io.popen("git branch --show-current 2>&1")
if handle then
for line in handle:lines() do
local branch
if remote then
for res in line:gmatch("origin/([^\n]+)") do
branch = res -- Trim /origin
end
else
branch = line:gsub("^%s*%*?%s*", "") -- Trim leading whitespace and the "* " marker for the current branch
end
table.insert(branches, branch)
end
handle:close()
return handle:read()
else
M.notify("Error running 'git branch' command.", vim.log.levels.ERROR)
end
end
---Return the list of names of all remote-tracking branches
M.get_all_merge_targets = function()
local handle = io.popen("git branch -r 2>&1")
if not handle then
M.notify("Error running 'git branch' command.", vim.log.levels.ERROR)
return
end
local current_branch = M.get_current_branch()
if not current_branch then
return
end
local lines = {}
for line in handle:lines() do
table.insert(lines, line)
end
handle:close()
-- Trim "origin/" and don't include the HEAD pointer
local branches = List.new(lines)
:map(function(line)
return line:match("origin/(%S+)")
end)
:filter(function(branch)
return not branch:match("^HEAD$") and branch ~= current_branch
end)
return branches
end
---Select a git branch and perform callback with the branch as an argument
---@param cb function The callback to perform with the selected branch
M.select_target_branch = function(cb)
local all_branch_names = M.get_all_merge_targets()
if not all_branch_names then
return
end
vim.ui.select(all_branch_names, {
prompt = "Choose target branch for merge",
}, function(choice)
if choice then
cb(choice)
end
end)
end
M.basename = function(str)
local name = string.gsub(str, "(.*/)(.*)", "%2")
return name
end
M.get_web_url = function()
local web_url = require("gitlab.state").INFO.web_url
if web_url ~= nil then
return web_url
end
M.notify("Could not get Gitlab URL", vim.log.levels.ERROR)
end
---@param url string?
M.open_in_browser = function(url)
if vim.fn.has("mac") == 1 then