Fix diagnostic position when sha changes (#299)
Fix: Remove API calls on Discussion Close (#328)
Chore: Remove root node type (#329)

This is a PATCH release.
This commit is contained in:
Harrison (Harry) Cramer
2024-07-06 13:09:56 -04:00
committed by GitHub
parent dc70c97810
commit 95dcc41885
8 changed files with 80 additions and 44 deletions

View File

@@ -5,7 +5,6 @@ local List = require("gitlab.utils.list")
local u = require("gitlab.utils")
local reviewer = require("gitlab.reviewer")
local indicators_common = require("gitlab.indicators.common")
local common_indicators = require("gitlab.indicators.common")
local state = require("gitlab.state")
local M = {}
@@ -183,8 +182,8 @@ local function get_new_line(node)
return node.new_line
end
local _, start_new_line = common_indicators.parse_line_code(range.start.line_code)
return start_new_line
local _, new_start_line = indicators_common.parse_line_code(range.start.line_code)
return new_start_line
end
---Takes a node and returns the line where the note is positioned in the old SHA. If
@@ -198,12 +197,13 @@ local function get_old_line(node)
return node.old_line
end
local start_old_line, _ = common_indicators.parse_line_code(range.start.line_code)
return start_old_line
local old_start_line, _ = indicators_common.parse_line_code(range.start.line_code)
return old_start_line
end
---@param id string|integer
---@return integer|nil
---@return integer|nil line_number
---@return boolean is_new_sha True if line number refers to NEW SHA
M.get_line_number = function(id)
---@type Discussion|DraftNote|nil
local d_or_n
@@ -214,19 +214,50 @@ M.get_line_number = function(id)
end)
if d_or_n == nil then
return
return nil, true
end
local first_note = indicators_common.get_first_note(d_or_n)
return (indicators_common.is_new_sha(d_or_n) and first_note.position.new_line or first_note.position.old_line) or 1
local is_new_sha = indicators_common.is_new_sha(d_or_n)
return ((is_new_sha and first_note.position.new_line or first_note.position.old_line) or 1), is_new_sha
end
---Return the start and end line numbers for the note range. The range is calculated from the line
---codes but the position itself is based on either the `new_line` or `old_line`.
---@param old_line integer|nil The line number in the OLD version
---@param new_line integer|nil The line number in the NEW version
---@param start_line_code string The line code for the start of the range
---@param end_line_code string The line code for the end of the range
---@return integer start_line
---@return integer end_line
---@return boolean is_new_sha True if line range refers to NEW SHA
M.get_line_numbers_for_range = function(old_line, new_line, start_line_code, end_line_code)
local old_start_line, new_start_line = indicators_common.parse_line_code(start_line_code)
local old_end_line, new_end_line = indicators_common.parse_line_code(end_line_code)
if old_line ~= nil and old_start_line ~= 0 then
local range = old_end_line - old_start_line
return (old_line - range), old_line, false
elseif new_line ~= nil then
local range = new_end_line - new_start_line
return (new_line - range), new_line, true
else
u.notify("Error getting new or old line for range", vim.log.levels.ERROR)
return 1, 1, false
end
end
---@param root_node NuiTree.Node
---@return integer|nil
---@return integer|nil line_number
---@return boolean is_new_sha True if line number refers to NEW SHA
M.get_line_number_from_node = function(root_node)
if root_node.range then
local start_old_line, start_new_line = common_indicators.parse_line_code(root_node.range.start.line_code)
return root_node.old_line and start_old_line or start_new_line
local line_number, _, is_new_sha = M.get_line_numbers_for_range(
root_node.old_line,
root_node.new_line,
root_node.range.start.line_code,
root_node.range["end"].line_code
)
return line_number, is_new_sha
else
return M.get_line_number(root_node.id)
end
@@ -240,12 +271,12 @@ M.jump_to_reviewer = function(tree, callback)
u.notify("Could not get discussion node", vim.log.levels.ERROR)
return
end
local line_number = M.get_line_number_from_node(root_node)
local line_number, is_new_sha = M.get_line_number_from_node(root_node)
if line_number == nil then
u.notify("Could not get line number", vim.log.levels.ERROR)
return
end
reviewer.jump(root_node.file_name, line_number, root_node.old_line == nil)
reviewer.jump(root_node.file_name, line_number, is_new_sha)
callback()
end

View File

@@ -111,12 +111,7 @@ end
---Opens the discussion tree, sets the keybindings. It also
---creates the tree for notes (which are not linked to specific lines of code)
---@param callback function?
M.toggle = function(callback)
if M.split_visible then
M.close()
return
end
M.open = function(callback)
state.DISCUSSION_DATA.discussions = u.ensure_table(state.DISCUSSION_DATA.discussions)
state.DISCUSSION_DATA.unlinked_discussions = u.ensure_table(state.DISCUSSION_DATA.unlinked_discussions)
state.DRAFT_NOTES = u.ensure_table(state.DRAFT_NOTES)

View File

@@ -191,7 +191,7 @@ local function get_modification_type_from_new_sha(new_line, hunks, all_diff_outp
return nil
end
return List.new(hunks):find(function(hunk)
local new_line_end = hunk.new_line + hunk.new_range
local new_line_end = hunk.new_line + hunk.new_range - (hunk.new_range > 0 and 1 or 0)
local in_new_range = new_line >= hunk.new_line and new_line <= new_line_end
local is_range_zero = hunk.new_range == 0 and hunk.old_range == 0
return in_new_range and (is_range_zero or line_was_added(new_line, hunk, all_diff_output))
@@ -209,10 +209,10 @@ local function get_modification_type_from_old_sha(old_line, new_line, hunks, all
end
return List.new(hunks):find(function(hunk)
local old_line_end = hunk.old_line + hunk.old_range
local new_line_end = hunk.new_line + hunk.new_range
local old_line_end = hunk.old_line + hunk.old_range - (hunk.old_range > 0 and 1 or 0)
local new_line_end = hunk.new_line + hunk.new_range - (hunk.new_range > 0 and 1 or 0)
local in_old_range = old_line >= hunk.old_line and old_line <= old_line_end
local in_new_range = old_line >= hunk.new_line and new_line <= new_line_end
local in_new_range = new_line >= hunk.new_line and new_line <= new_line_end
return (in_old_range or in_new_range) and line_was_removed(old_line, hunk, all_diff_output)
end) and "deleted" or "unmodified"
end

View File

@@ -67,8 +67,9 @@ end
---@param d_or_n Discussion|DraftNote
---@return boolean
M.is_old_sha = function(d_or_n)
local first_note = M.get_first_note(d_or_n)
return first_note.position.old_line ~= nil
local position = M.get_first_note(d_or_n).position
local old_start_line = position.line_range ~= nil and M.parse_line_code(position.line_range.start.line_code) or nil
return position.old_line ~= nil and old_start_line ~= 0
end
---@param discussion Discussion|DraftNote

View File

@@ -72,19 +72,17 @@ local create_multiline_diagnostic = function(d_or_n)
error("Parsing multi-line comment but note does not contain line range")
end
local start_old_line, start_new_line = indicators_common.parse_line_code(line_range.start.line_code)
local start_line, end_line, _ = actions_common.get_line_numbers_for_range(
first_note.position.old_line,
first_note.position.new_line,
line_range.start.line_code,
line_range["end"].line_code
)
if indicators_common.is_new_sha(d_or_n) then
return create_diagnostic({
lnum = start_new_line - 1,
end_lnum = first_note.position.new_line - 1,
}, d_or_n)
else
return create_diagnostic({
lnum = start_old_line - 1,
end_lnum = first_note.position.old_line - 1,
}, d_or_n)
end
return create_diagnostic({
lnum = start_line - 1,
end_lnum = end_line - 1,
}, d_or_n)
end
---Set diagnostics in currently new SHA.

View File

@@ -68,12 +68,18 @@ return {
pipeline = async.sequence({ latest_pipeline }, pipeline.open),
merge = async.sequence({ u.merge(info, { refresh = true }) }, merge.merge),
-- Discussion Tree Actions 🌴
toggle_discussions = async.sequence({
info,
user,
u.merge(draft_notes_dep, { refresh = true }),
u.merge(discussion_data, { refresh = true }),
}, discussions.toggle),
toggle_discussions = function()
if discussions.split_visible then
discussions.close()
else
async.sequence({
info,
user,
u.merge(draft_notes_dep, { refresh = true }),
u.merge(discussion_data, { refresh = true }),
}, discussions.open)()
end
end,
toggle_draft_mode = discussions.toggle_draft_mode,
publish_all_drafts = draft_notes.publish_all_drafts,
refresh_data = function()

View File

@@ -524,6 +524,7 @@ M.create_box_popup_state = function(title, enter)
top = title,
},
},
opacity = settings.opacity,
}
end