Follow-up: Require Different Reviewers

This plugin previously only supported the Delta reviewer. Thanks to work
from @mrparalon it now supports Diffview also. This MR adjusts the
requirements to account for this, and the README.

It also addresses a small bug in the original implementation regarding
an async file opening action, and applies formatting to the diffview
file consistent with the rest of the project
This commit is contained in:
Harrison Cramer
2023-09-05 10:22:19 -04:00
parent a2bd0749f0
commit 4792e03416
6 changed files with 83 additions and 67 deletions

View File

@@ -17,11 +17,11 @@ https://github.com/harrisoncramer/gitlab.nvim/assets/32515581/ab5a8597-32fa-4a28
- <a href="https://go.dev/">Go >= v1.19</a> - <a href="https://go.dev/">Go >= v1.19</a>
- <a href="https://www.gnu.org/software/make/manual/make.html">make (for install)</a> - <a href="https://www.gnu.org/software/make/manual/make.html">make (for install)</a>
- <a href="https://github.com/dandavison/delta">delta</a>
## Quick Start ## Quick Start
1. Install Go and Delta Dependencies 1. Install Go
2. Install reviewer: <a href="https://github.com/dandavison/delta">delta</a> or <a href="https://github.com/sindrets/diffview.nvim">diffview</a>
2. Add configuration (see Installation section) 2. Add configuration (see Installation section)
3. Checkout your feature branch: `git checkout feature-branch` 3. Checkout your feature branch: `git checkout feature-branch`
4. Open Neovim 4. Open Neovim
@@ -42,7 +42,7 @@ return {
}, },
build = function () require("gitlab.server").build(true) end, -- Builds the Go binary build = function () require("gitlab.server").build(true) end, -- Builds the Go binary
config = function() config = function()
require("gitlab").setup() require("gitlab").setup() -- Uses delta reviewer by default
end, end,
} }
``` ```
@@ -87,7 +87,7 @@ Here is the default setup function. All of these values are optional, and if you
require("gitlab").setup({ require("gitlab").setup({
port = 21036, -- The port of the Go server, which runs in the background port = 21036, -- The port of the Go server, which runs in the background
log_path = vim.fn.stdpath("cache") .. "/gitlab.nvim.log", -- Log path for the Go server log_path = vim.fn.stdpath("cache") .. "/gitlab.nvim.log", -- Log path for the Go server
reviewer = "delta", -- The reviewer type (only delta is currently supported) reviewer = "delta", -- The reviewer type ("delta" or "diffview")
popup = { -- The popup for comment creation, editing, and replying popup = { -- The popup for comment creation, editing, and replying
exit = "<Esc>", exit = "<Esc>",
perform_action = "<leader>s", -- Once in normal mode, does action (like saving comment or editing description, etc) perform_action = "<leader>s", -- Once in normal mode, does action (like saving comment or editing description, etc)
@@ -108,7 +108,7 @@ require("gitlab").setup({
resolved = '', -- Symbol to show next to resolved discussions resolved = '', -- Symbol to show next to resolved discussions
unresolved = '', -- Symbol to show next to unresolved discussions unresolved = '', -- Symbol to show next to unresolved discussions
}, },
review_pane = { -- Specific settings for different reviewers, only delta currently supported review_pane = { -- Specific settings for different reviewers
delta = { delta = {
added_file = "", -- The symbol to show next to added files added_file = "", -- The symbol to show next to added files
modified_file = "", -- The symbol to show next to modified files modified_file = "", -- The symbol to show next to modified files

View File

@@ -3,6 +3,7 @@
local server = require("gitlab.server") local server = require("gitlab.server")
local job = require("gitlab.job") local job = require("gitlab.job")
local state = require("gitlab.state") local state = require("gitlab.state")
local u = require("gitlab.utils")
local M = {} local M = {}

View File

@@ -20,6 +20,7 @@ return {
state.setPluginConfiguration() -- Sets configuration from `.gitlab.nvim` file state.setPluginConfiguration() -- Sets configuration from `.gitlab.nvim` file
state.merge_settings(args) -- Sets keymaps and other settings from setup function state.merge_settings(args) -- Sets keymaps and other settings from setup function
reviewer.init() -- Picks and initializes reviewer (default is Delta) reviewer.init() -- Picks and initializes reviewer (default is Delta)
u.has_reviewer(args.reviewer or "delta")
end, end,
-- Global Actions 🌎 -- Global Actions 🌎
summary = async.sequence({ info }, summary.summary), summary = async.sequence({ info }, summary.summary),

View File

@@ -1,71 +1,79 @@
-- This Module contains all of the code specific to the Delta reviewer. -- This Module contains all of the code specific to the Diffview reviewer.
local state = require("gitlab.state") local state = require("gitlab.state")
local u = require("gitlab.utils") local async_ok, async = pcall(require, "diffview.async")
local M = { local M = {
bufnr = nil, bufnr = nil,
tabnr = nil tabnr = nil
} }
-- Public Functions -- Public Functions
-- These functions are exposed externally and are used -- These functions are exposed externally and are used
-- when the reviewer is consumed by other code. They must follow the specification -- when the reviewer is consumed by other code. They must follow the specification
-- outlined in the reviewer/init.lua file -- outlined in the reviewer/init.lua file
M.open = function() M.open = function()
vim.api.nvim_command(string.format("DiffviewOpen %s", state.INFO.target_branch)) vim.api.nvim_command(string.format("DiffviewOpen %s", state.INFO.target_branch))
M.tabnr = vim.api.nvim_get_current_tabpage() M.tabnr = vim.api.nvim_get_current_tabpage()
end end
M.jump = function(file_name, new_line, old_line) M.jump = function(file_name, new_line, old_line)
if M.tabnr == nil then if M.tabnr == nil then
vim.notify("Can't jump to Diffvew. Is it open?", vim.log.levels.ERROR) vim.notify("Can't jump to Diffvew. Is it open?", vim.log.levels.ERROR)
return
end
vim.api.nvim_set_current_tabpage(M.tabnr)
vim.cmd("DiffviewFocusFiles")
local view = require("diffview.lib").get_current_view()
if view == nil then
vim.notify("Could not find Diffview view", vim.log.levels.ERROR)
return
end
local files = view.panel:ordered_file_list()
local layout = view.cur_layout
for _, file in ipairs(files) do
if file.path == file_name then
if not async_ok then
vim.notify("Could not load Diffview async", vim.log.levels.ERROR)
return return
end
async.await(view:set_file(file))
if new_line ~= nil then
layout.b:focus()
vim.api.nvim_win_set_cursor(0, { tonumber(new_line), 0 })
elseif old_line ~= nil then
layout.a:focus()
vim.api.nvim_win_set_cursor(0, { tonumber(old_line), 0 })
end
break
end end
vim.api.nvim_set_current_tabpage(M.tabnr) end
vim.cmd("DiffviewFocusFiles")
local view = require("diffview.lib").get_current_view()
if view == nil then
vim.notify("Could not find Diffview view", vim.log.levels.ERROR)
return
end
local files = view.panel:ordered_file_list()
local layout = view.cur_layout
for _, file in ipairs(files) do
if file.path == file_name then
view:set_file(file)
if new_line ~= nil then
layout.b:focus()
vim.api.nvim_win_set_cursor(0, { tonumber(new_line), 0 })
elseif old_line ~= nil then
layout.a:focus()
vim.api.nvim_win_set_cursor(0, { tonumber(old_line), 0 })
end
break
end
end
end end
M.get_location = function() M.get_location = function()
if M.tabnr == nil then return nil, nil, "Diffview reviewer must be initialized first" end if M.tabnr == nil then return nil, nil, "Diffview reviewer must be initialized first" end
local bufnr = vim.api.nvim_get_current_buf() local bufnr = vim.api.nvim_get_current_buf()
-- check if we are in the diffview tab -- check if we are in the diffview tab
local tabnr = vim.api.nvim_get_current_tabpage() local tabnr = vim.api.nvim_get_current_tabpage()
if tabnr ~= M.tabnr then return nil, nil, "Line location can only be determined within reviewer window" end if tabnr ~= M.tabnr then return nil, nil, "Line location can only be determined within reviewer window" end
-- check if we are in the diffview buffer -- check if we are in the diffview buffer
local view = require("diffview.lib").get_current_view() local view = require("diffview.lib").get_current_view()
local layout = view.cur_layout if view == nil then
local file_name = nil vim.notify("Could not find Diffview view", vim.log.levels.ERROR)
local current_line_changes = nil return
if layout.a.file.bufnr == bufnr then end
file_name = layout.a.file.path local layout = view.cur_layout
current_line_changes = { new_line = nil, old_line = vim.api.nvim_win_get_cursor(0)[1] } local file_name = nil
return file_name, current_line_changes local current_line_changes = nil
elseif layout.b.file.bufnr == bufnr then if layout.a.file.bufnr == bufnr then
file_name = layout.b.file.path file_name = layout.a.file.path
current_line_changes = { new_line = vim.api.nvim_win_get_cursor(0)[1], old_line = nil } current_line_changes = { new_line = nil, old_line = vim.api.nvim_win_get_cursor(0)[1] }
return file_name, current_line_changes return file_name, current_line_changes
end elseif layout.b.file.bufnr == bufnr then
return nil, nil, "Line location can only be determined within reviewer window" file_name = layout.b.file.path
current_line_changes = { new_line = vim.api.nvim_win_get_cursor(0)[1], old_line = nil }
return file_name, current_line_changes
end
return nil, nil, "Line location can only be determined within reviewer window"
end end
return M return M

View File

@@ -43,11 +43,6 @@ end
-- Builds the Go binary -- Builds the Go binary
M.build = function(override) M.build = function(override)
if not u.has_delta() then
vim.notify("Please install delta to use gitlab.nvim!", vim.log.levels.ERROR)
return
end
local file_path = u.current_file_path() local file_path = u.current_file_path()
local parent_dir = vim.fn.fnamemodify(file_path, ":h:h:h:h") local parent_dir = vim.fn.fnamemodify(file_path, ":h:h:h:h")
state.settings.bin_path = parent_dir state.settings.bin_path = parent_dir

View File

@@ -4,8 +4,19 @@ M.get_current_line_number = function()
return vim.api.nvim_call_function('line', { '.' }) return vim.api.nvim_call_function('line', { '.' })
end end
M.has_delta = function() M.has_reviewer = function(reviewer)
return vim.fn.executable("delta") == 1 local has_reviewer = false
if reviewer == "diffview" then
has_reviewer = vim.fn.exists(":DiffviewOpen") ~= 0
else
has_reviewer = vim.fn.executable("delta") == 1
end
if not has_reviewer then
error(string.format("Please install %s or change your reviewer", reviewer))
end
return has_reviewer
end end
M.P = function(...) M.P = function(...)