diff --git a/lua/gitlab/actions/common.lua b/lua/gitlab/actions/common.lua index 03ca63a0..9d78fe1e 100644 --- a/lua/gitlab/actions/common.lua +++ b/lua/gitlab/actions/common.lua @@ -262,10 +262,10 @@ M.get_line_numbers_for_range = function(old_line, new_line, start_line_code, end 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 + local range = (old_start_line and old_end_line) and (old_end_line - old_start_line) or 0 return (old_line - range), old_line, false elseif new_line ~= nil then - local range = new_end_line - new_start_line + local range = (new_start_line and new_end_line) and (new_end_line - new_start_line) or 0 -- Force start_line to be greater than 0 -- TODO: use `math.max(new_line - range, 1)` instead local start_line = (new_line - range > 0) and (new_line - range) or 1 diff --git a/lua/gitlab/indicators/common.lua b/lua/gitlab/indicators/common.lua index 5324a463..4844b2d7 100644 --- a/lua/gitlab/indicators/common.lua +++ b/lua/gitlab/indicators/common.lua @@ -53,15 +53,14 @@ M.filter_placeable_discussions = function() end ---Parse old and new line from a line code like "3f454a98e586d1aa0d322e19afd5e67e08f2d3c8_10_44". ----@param line_code string A SHA hash of the file name and line numbers before and after change ----@return integer The line number before the change ----@return integer The line number after the change +---GitLab leaves a number empty when the line has no counterpart, e.g. "3f454a98...__44". +---@param line_code? string A SHA hash of the file name and line numbers before and after change +---@return integer? The line number before the change +---@return integer? The line number after the change M.parse_line_code = function(line_code) - local line_code_regex = "%w+_(%d+)_(%d+)" - local old_line, new_line = line_code:match(line_code_regex) - old_line = tonumber(old_line) --[[@as integer]] - new_line = tonumber(new_line) --[[@as integer]] - return old_line, new_line + local line_code_regex = "%w+_(%d*)_(%d*)" + local old_line, new_line = (line_code or ""):match(line_code_regex) + return tonumber(old_line), tonumber(new_line) end ---Return true if discussion/draft belongs to the old file, otherwise false. diff --git a/tests/spec/line_code_spec.lua b/tests/spec/line_code_spec.lua new file mode 100644 index 00000000..a5827024 --- /dev/null +++ b/tests/spec/line_code_spec.lua @@ -0,0 +1,45 @@ +local indicators_common = require("gitlab.indicators.common") +local actions_common = require("gitlab.actions.common") + +local sha = "3f454a98e586d1aa0d322e19afd5e67e08f2d3c8" + +describe("indicators/common.parse_line_code", function() + it("Parses both line numbers", function() + assert.are.same({ 10, 44 }, { indicators_common.parse_line_code(sha .. "_10_44") }) + end) + + it("Returns nil for an empty old line number", function() + assert.are.same({ nil, 44 }, { indicators_common.parse_line_code(sha .. "__44") }) + end) + + it("Returns nil for an empty new line number", function() + assert.are.same({ 10, nil }, { indicators_common.parse_line_code(sha .. "_10_") }) + end) + + it("Returns nil for a missing line code", function() + assert.are.same({ nil, nil }, { indicators_common.parse_line_code(nil) }) + end) +end) + +describe("actions/common.get_line_numbers_for_range", function() + it("Computes a range on the new SHA", function() + assert.are.same( + { 40, 44, true }, + { actions_common.get_line_numbers_for_range(nil, 44, sha .. "_1_40", sha .. "_1_44") } + ) + end) + + it("Falls back to a single line when a line code has an empty number", function() + assert.are.same( + { 44, 44, true }, + { actions_common.get_line_numbers_for_range(nil, 44, sha .. "__40", sha .. "_1_") } + ) + end) + + it("Falls back to a single line on the old SHA when a line code has an empty number", function() + assert.are.same( + { 12, 12, false }, + { actions_common.get_line_numbers_for_range(12, nil, sha .. "_10_", sha .. "__5") } + ) + end) +end)