From 95dcc418852f171781d4d1566cc325055d339549 Mon Sep 17 00:00:00 2001 From: "Harrison (Harry) Cramer" <32515581+harrisoncramer@users.noreply.github.com> Date: Sat, 6 Jul 2024 13:09:56 -0400 Subject: [PATCH] Release (#330) 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. --- README.md | 6 ++- lua/gitlab/actions/common.lua | 57 +++++++++++++++++++------ lua/gitlab/actions/discussions/init.lua | 7 +-- lua/gitlab/hunks.lua | 8 ++-- lua/gitlab/indicators/common.lua | 5 ++- lua/gitlab/indicators/diagnostics.lua | 22 +++++----- lua/gitlab/init.lua | 18 +++++--- lua/gitlab/utils/init.lua | 1 + 8 files changed, 80 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 5b04bd2a..8ff73267 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,7 @@ For more settings, please see `:h gitlab.nvim.connecting-to-gitlab` The plugin expects you to call `setup()` and pass in a table of options. All of these values are optional, and if you call this function with no values the defaults will be used. -For a list of all these settings please run `:h gitlab.nvim` which is stored in `doc/gitlab.nvim.txt` +For a list of all these settings please run `:h gitlab.nvim.configuring-the-plugin` which will show you the help stored in [doc/gitlab.nvim.txt](doc/gitlab.nvim.txt). ## Keybindings @@ -155,3 +155,7 @@ vim.keymap.set("n", "glD", gitlab.toggle_draft_mode) ``` For more information about each of these commands, and about the APIs in general, run `:h gitlab.nvim.api` + +## Contributing + +Contributions to the plugin are welcome. Please read [.github/CONTRIBUTING.md](.github/CONTRIBUTING.md) before you start working on a pull request. diff --git a/lua/gitlab/actions/common.lua b/lua/gitlab/actions/common.lua index 8d8c6409..8ad4f6b8 100644 --- a/lua/gitlab/actions/common.lua +++ b/lua/gitlab/actions/common.lua @@ -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 diff --git a/lua/gitlab/actions/discussions/init.lua b/lua/gitlab/actions/discussions/init.lua index 4a418048..2fc59da6 100644 --- a/lua/gitlab/actions/discussions/init.lua +++ b/lua/gitlab/actions/discussions/init.lua @@ -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) diff --git a/lua/gitlab/hunks.lua b/lua/gitlab/hunks.lua index 15e7e8b5..cd6e5896 100644 --- a/lua/gitlab/hunks.lua +++ b/lua/gitlab/hunks.lua @@ -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 diff --git a/lua/gitlab/indicators/common.lua b/lua/gitlab/indicators/common.lua index e68ee48d..6a72be47 100644 --- a/lua/gitlab/indicators/common.lua +++ b/lua/gitlab/indicators/common.lua @@ -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 diff --git a/lua/gitlab/indicators/diagnostics.lua b/lua/gitlab/indicators/diagnostics.lua index 42454ac6..ad3c5d67 100644 --- a/lua/gitlab/indicators/diagnostics.lua +++ b/lua/gitlab/indicators/diagnostics.lua @@ -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. diff --git a/lua/gitlab/init.lua b/lua/gitlab/init.lua index 8e4c4d0c..8bba13ae 100644 --- a/lua/gitlab/init.lua +++ b/lua/gitlab/init.lua @@ -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() diff --git a/lua/gitlab/utils/init.lua b/lua/gitlab/utils/init.lua index f2371227..58f57b91 100644 --- a/lua/gitlab/utils/init.lua +++ b/lua/gitlab/utils/init.lua @@ -524,6 +524,7 @@ M.create_box_popup_state = function(title, enter) top = title, }, }, + opacity = settings.opacity, } end