From c35bf818a484ff5357066d83f1924717c960fbc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Mon, 25 Nov 2024 08:39:46 +0100 Subject: [PATCH 01/21] fix: return Boolean from has_clean_tree --- lua/gitlab/actions/merge_requests.lua | 2 +- lua/gitlab/git.lua | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lua/gitlab/actions/merge_requests.lua b/lua/gitlab/actions/merge_requests.lua index 0af9db0c..371d8803 100644 --- a/lua/gitlab/actions/merge_requests.lua +++ b/lua/gitlab/actions/merge_requests.lua @@ -15,7 +15,7 @@ M.choose_merge_request = function(opts) local has_clean_tree, clean_tree_err = git.has_clean_tree() if clean_tree_err ~= nil then return - elseif has_clean_tree ~= "" then + elseif not has_clean_tree then u.notify("Your local branch has changes, please stash or commit and push", vim.log.levels.ERROR) return end diff --git a/lua/gitlab/git.lua b/lua/gitlab/git.lua index aa74abc0..7ccdbde0 100644 --- a/lua/gitlab/git.lua +++ b/lua/gitlab/git.lua @@ -25,10 +25,11 @@ M.branches = function(args) return run_system(u.combine({ "git", "branch" }, args or {})) end ----Checks whether the tree has any changes that haven't been pushed to the remote ----@return string|nil, string|nil +---Returns true if the working tree hasn't got any changes that haven't been commited +---@return boolean, string|nil M.has_clean_tree = function() - return run_system({ "git", "status", "--short", "--untracked-files=no" }) + local changes, err = run_system({ "git", "status", "--short", "--untracked-files=no" }) + return changes == "", err end ---Gets the base directory of the current project From 1e9bd80ec26125e4b548a5b2e8d4fd1daeef539e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Mon, 25 Nov 2024 09:28:35 +0100 Subject: [PATCH 02/21] fix: only look for changes in the current file --- lua/gitlab/actions/comment.lua | 7 +++---- lua/gitlab/git.lua | 8 ++++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index 08e11da8..f9909192 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -262,13 +262,12 @@ end --- This function will open a comment popup in order to create a comment on the changed/updated --- line in the current MR M.create_comment = function() - local has_clean_tree, err = git.has_clean_tree() + local has_changes, err = git.has_changes(reviewer.get_current_file_path()) -- Saved modifications if err ~= nil then return end - - local is_modified = vim.bo[0].modified - if state.settings.reviewer_settings.diffview.imply_local and (is_modified or not has_clean_tree) then + local is_modified = vim.bo[0].modified -- Unsaved modifications + if state.settings.reviewer_settings.diffview.imply_local and (is_modified or has_changes) then u.notify( "Cannot leave comments on changed files. \n Please stash all local changes or push them to the feature branch.", vim.log.levels.WARN diff --git a/lua/gitlab/git.lua b/lua/gitlab/git.lua index 7ccdbde0..a633c86d 100644 --- a/lua/gitlab/git.lua +++ b/lua/gitlab/git.lua @@ -32,6 +32,14 @@ M.has_clean_tree = function() return changes == "", err end +---Returns true if the `file` has got any uncommitted changes +---@param file string File to check for changes +---@return boolean, string|nil +M.has_changes = function(file) + local changes, err = run_system({ "git", "status", "--short", "--untracked-files=no", "--", file }) + return changes ~= "", err +end + ---Gets the base directory of the current project ---@return string|nil, string|nil M.base_dir = function() From 3fea5d2a75441a36a7e5e5e40b762fb014400599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Mon, 25 Nov 2024 09:34:38 +0100 Subject: [PATCH 03/21] docs: unify error/warning messages --- lua/gitlab/actions/comment.lua | 5 +---- lua/gitlab/actions/merge_requests.lua | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index f9909192..f154335a 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -268,10 +268,7 @@ M.create_comment = function() end local is_modified = vim.bo[0].modified -- Unsaved modifications if state.settings.reviewer_settings.diffview.imply_local and (is_modified or has_changes) then - u.notify( - "Cannot leave comments on changed files. \n Please stash all local changes or push them to the feature branch.", - vim.log.levels.WARN - ) + u.notify("Cannot leave comments on changed files, please stash or commit and push", vim.log.levels.WARN) return end diff --git a/lua/gitlab/actions/merge_requests.lua b/lua/gitlab/actions/merge_requests.lua index 371d8803..2dee2d31 100644 --- a/lua/gitlab/actions/merge_requests.lua +++ b/lua/gitlab/actions/merge_requests.lua @@ -16,7 +16,7 @@ M.choose_merge_request = function(opts) if clean_tree_err ~= nil then return elseif not has_clean_tree then - u.notify("Your local branch has changes, please stash or commit and push", vim.log.levels.ERROR) + u.notify("Your working tree has changes, please stash or commit and push", vim.log.levels.ERROR) return end From fdb1bfce25d691457cd7bb78285454c40a0ac094 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Mon, 25 Nov 2024 09:42:52 +0100 Subject: [PATCH 04/21] fix: make dirty tree override imply_local properly If `imply_local` is set but unused due to local changes present when review is started, `imply_local` is overridden so that local changes do not unnecessarily block comments. Now, uncommitted changes on a file will only block comments when they are made after the review is started with `imply_local` applied. --- lua/gitlab/reviewer/init.lua | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lua/gitlab/reviewer/init.lua b/lua/gitlab/reviewer/init.lua index 41c00478..1f286db0 100644 --- a/lua/gitlab/reviewer/init.lua +++ b/lua/gitlab/reviewer/init.lua @@ -46,8 +46,16 @@ M.open = function() if err ~= nil then return end - if state.settings.reviewer_settings.diffview.imply_local and has_clean_tree then - diffview_open_command = diffview_open_command .. " --imply-local" + if state.settings.reviewer_settings.diffview.imply_local then + if has_clean_tree then + diffview_open_command = diffview_open_command .. " --imply-local" + else + u.notify( + "Your working tree has changes, cannot use 'imply_local' setting for gitlab reviews.\n Stash or commit all changes to use.", + vim.log.levels.WARN + ) + state.settings.reviewer_settings.diffview.imply_local = false + end end vim.api.nvim_command(string.format("%s %s..%s", diffview_open_command, diff_refs.base_sha, diff_refs.head_sha)) @@ -55,13 +63,6 @@ M.open = function() M.is_open = true M.tabnr = vim.api.nvim_get_current_tabpage() - if state.settings.reviewer_settings.diffview.imply_local and not has_clean_tree then - u.notify( - "There are uncommited changes in the working tree, cannot use 'imply_local' setting for gitlab reviews.\n Stash or commit all changes to use.", - vim.log.levels.WARN - ) - end - if state.settings.discussion_diagnostic ~= nil or state.settings.discussion_sign ~= nil then u.notify( "Diagnostics are now configured as settings.discussion_signs, see :h gitlab.nvim.signs-and-diagnostics", From 1eb187bb3a412206b2a0e7a344d093556686db79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Mon, 25 Nov 2024 10:56:16 +0100 Subject: [PATCH 05/21] perf: only check tree cleanness with imply_local --- lua/gitlab/reviewer/init.lua | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lua/gitlab/reviewer/init.lua b/lua/gitlab/reviewer/init.lua index 1f286db0..b57c22dc 100644 --- a/lua/gitlab/reviewer/init.lua +++ b/lua/gitlab/reviewer/init.lua @@ -42,11 +42,12 @@ M.open = function() end local diffview_open_command = "DiffviewOpen" - local has_clean_tree, err = git.has_clean_tree() - if err ~= nil then - return - end + if state.settings.reviewer_settings.diffview.imply_local then + local has_clean_tree, err = git.has_clean_tree() + if err ~= nil then + return + end if has_clean_tree then diffview_open_command = diffview_open_command .. " --imply-local" else From 77f79dbb94fa6156442de9d415f45ffa54fa033d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Mon, 25 Nov 2024 11:29:03 +0100 Subject: [PATCH 06/21] fix: only block choose_merge_request on dirty tree when switching branch --- lua/gitlab/actions/merge_requests.lua | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lua/gitlab/actions/merge_requests.lua b/lua/gitlab/actions/merge_requests.lua index 2dee2d31..a9097983 100644 --- a/lua/gitlab/actions/merge_requests.lua +++ b/lua/gitlab/actions/merge_requests.lua @@ -12,14 +12,6 @@ local M = {} ---Opens up a select menu that lets you choose a different merge request. ---@param opts ChooseMergeRequestOptions|nil M.choose_merge_request = function(opts) - local has_clean_tree, clean_tree_err = git.has_clean_tree() - if clean_tree_err ~= nil then - return - elseif not has_clean_tree then - u.notify("Your working tree has changes, please stash or commit and push", vim.log.levels.ERROR) - return - end - if opts == nil then opts = state.settings.choose_merge_request end @@ -38,6 +30,16 @@ M.choose_merge_request = function(opts) reviewer.close() end + if choice.source_branch ~= git.get_current_branch() then + local has_clean_tree, clean_tree_err = git.has_clean_tree() + if clean_tree_err ~= nil then + return + elseif not has_clean_tree then + u.notify("Your working tree has changes, please stash or commit and push", vim.log.levels.ERROR) + return + end + end + vim.schedule(function() local _, branch_switch_err = git.switch_branch(choice.source_branch) if branch_switch_err ~= nil then From 95ba38a24583bc16f6ecbb7c4635b8007473c4e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Mon, 25 Nov 2024 11:30:50 +0100 Subject: [PATCH 07/21] docs: use more explicit error message --- lua/gitlab/actions/merge_requests.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lua/gitlab/actions/merge_requests.lua b/lua/gitlab/actions/merge_requests.lua index a9097983..d9af5e29 100644 --- a/lua/gitlab/actions/merge_requests.lua +++ b/lua/gitlab/actions/merge_requests.lua @@ -35,7 +35,10 @@ M.choose_merge_request = function(opts) if clean_tree_err ~= nil then return elseif not has_clean_tree then - u.notify("Your working tree has changes, please stash or commit and push", vim.log.levels.ERROR) + u.notify( + "Cannot switch branch when working tree has changes, please stash or commit and push", + vim.log.levels.ERROR + ) return end end From a7c66ee26043a1a1d3b59b5c71ef3c8b04d3fc28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Mon, 25 Nov 2024 13:39:36 +0100 Subject: [PATCH 08/21] refactor: move logic to separate function --- lua/gitlab/actions/comment.lua | 93 ++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 39 deletions(-) diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index f154335a..3fdf6a39 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -158,46 +158,8 @@ end ---multi-line comment. It also sets up the basic keybindings for switching between ---window panes, and for the non-primary sections. ---@param opts LayoutOpts ----@return NuiLayout|nil +---@return NuiLayout M.create_comment_layout = function(opts) - if opts.unlinked ~= true and opts.discussion_id == nil then - -- Check that diffview is initialized - if reviewer.tabnr == nil then - u.notify("Reviewer must be initialized first", vim.log.levels.ERROR) - return - end - - -- Check that Diffview is the current view - local view = diffview_lib.get_current_view() - if view == nil and not opts.reply then - u.notify("Comments should be left in the reviewer pane", vim.log.levels.ERROR) - return - end - - -- Check that we are in the diffview tab - local tabnr = vim.api.nvim_get_current_tabpage() - if tabnr ~= reviewer.tabnr then - u.notify("Line location can only be determined within reviewer window", vim.log.levels.ERROR) - return - end - - -- Check that the file has not been renamed - if reviewer.is_file_renamed() and not reviewer.does_file_have_changes() then - u.notify("Commenting on (unchanged) renamed or moved files is not supported", vim.log.levels.WARN) - return - end - - -- Check that we are hovering over the code - local filetype = vim.bo[0].filetype - if not opts.reply and (filetype == "DiffviewFiles" or filetype == "gitlab") then - u.notify( - "Comments can only be left on the code. To leave unlinked comments, use gitlab.create_note() instead", - vim.log.levels.ERROR - ) - return - end - end - local popup_settings = state.settings.popup local title local user_settings @@ -276,6 +238,10 @@ M.create_comment = function() return end + if not M.can_create_comment() then + return + end + local layout = M.create_comment_layout({ ranged = false, unlinked = false }) if layout ~= nil then layout:mount() @@ -292,6 +258,10 @@ M.create_multiline_comment = function() return end + if not M.can_create_comment() then + return + end + local layout = M.create_comment_layout({ ranged = true, unlinked = false }) if layout ~= nil then layout:mount() @@ -356,6 +326,10 @@ M.create_comment_suggestion = function() return end + if not M.can_create_comment() then + return + end + local suggestion_lines, range_length = build_suggestion() local layout = M.create_comment_layout({ ranged = range_length > 0, unlinked = false }) @@ -371,6 +345,47 @@ M.create_comment_suggestion = function() end) end +---Returns true if it's possible to create an Inline Comment +---@return boolean +M.can_create_comment = function() + -- Check that diffview is initialized + if reviewer.tabnr == nil then + u.notify("Reviewer must be initialized first", vim.log.levels.ERROR) + return false + end + + -- Check that Diffview is the current view + local view = diffview_lib.get_current_view() + if view == nil then + u.notify("Comments should be left in the reviewer pane", vim.log.levels.ERROR) + return false + end + + -- Check that we are in the diffview tab + local tabnr = vim.api.nvim_get_current_tabpage() + if tabnr ~= reviewer.tabnr then + u.notify("Line location can only be determined within reviewer window", vim.log.levels.ERROR) + return false + end + + -- Check that the file has not been renamed + if reviewer.is_file_renamed() and not reviewer.does_file_have_changes() then + u.notify("Commenting on (unchanged) renamed or moved files is not supported", vim.log.levels.WARN) + return false + end + + -- Check that we are hovering over the code + local filetype = vim.bo[0].filetype + if filetype == "DiffviewFiles" or filetype == "gitlab" then + u.notify( + "Comments can only be left on the code. To leave unlinked comments, use gitlab.create_note() instead", + vim.log.levels.ERROR + ) + return false + end + return true +end + ---Checks to see whether you are commenting on a valid buffer. The Diffview plugin names non-existent ---buffers as 'null' ---@return boolean From 474d58f92512e5e0ca6625c327997a94bed6e071 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Mon, 25 Nov 2024 13:49:51 +0100 Subject: [PATCH 09/21] refactor: move sha_exists check to common function --- lua/gitlab/actions/comment.lua | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index 3fdf6a39..99f19f07 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -234,10 +234,6 @@ M.create_comment = function() return end - if not M.sha_exists() then - return - end - if not M.can_create_comment() then return end @@ -254,9 +250,6 @@ M.create_multiline_comment = function() if not u.check_visual_mode() then return end - if not M.sha_exists() then - return - end if not M.can_create_comment() then return @@ -322,9 +315,6 @@ M.create_comment_suggestion = function() if not u.check_visual_mode() then return end - if not M.sha_exists() then - return - end if not M.can_create_comment() then return @@ -383,6 +373,11 @@ M.can_create_comment = function() ) return false end + + if not M.sha_exists() then + return false + end + return true end From 6fa3e92dcbd58836955b307dbf3c138d79b094bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Mon, 25 Nov 2024 13:54:39 +0100 Subject: [PATCH 10/21] fix: check for modification in common function --- lua/gitlab/actions/comment.lua | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index 99f19f07..0916898d 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -224,16 +224,6 @@ end --- This function will open a comment popup in order to create a comment on the changed/updated --- line in the current MR M.create_comment = function() - local has_changes, err = git.has_changes(reviewer.get_current_file_path()) -- Saved modifications - if err ~= nil then - return - end - local is_modified = vim.bo[0].modified -- Unsaved modifications - if state.settings.reviewer_settings.diffview.imply_local and (is_modified or has_changes) then - u.notify("Cannot leave comments on changed files, please stash or commit and push", vim.log.levels.WARN) - return - end - if not M.can_create_comment() then return end @@ -378,6 +368,16 @@ M.can_create_comment = function() return false end + local has_changes, err = git.has_changes(reviewer.get_current_file_path()) -- Saved modifications + if err ~= nil then + return false + end + local is_modified = vim.bo[0].modified -- Unsaved modifications + if state.settings.reviewer_settings.diffview.imply_local and (is_modified or has_changes) then + u.notify("Cannot leave comments on changed files, please stash or commit and push", vim.log.levels.WARN) + return false + end + return true end From b381bee23d3315c96abab71516f48745b8c455d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Mon, 25 Nov 2024 14:11:38 +0100 Subject: [PATCH 11/21] docs: use more complete warning --- lua/gitlab/utils/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/gitlab/utils/init.lua b/lua/gitlab/utils/init.lua index 776f05f0..51bea51c 100644 --- a/lua/gitlab/utils/init.lua +++ b/lua/gitlab/utils/init.lua @@ -578,7 +578,7 @@ end M.check_visual_mode = function() local mode = vim.api.nvim_get_mode().mode if mode ~= "v" and mode ~= "V" then - M.notify("Code suggestions are only available in visual mode", vim.log.levels.WARN) + M.notify("Code suggestions and multiline comments are only available in visual mode", vim.log.levels.WARN) return false end return true From 3bbe5882906e1e181cc9467c1102f11db93ee76d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Mon, 25 Nov 2024 14:16:22 +0100 Subject: [PATCH 12/21] refactor: move mode checking to common function --- lua/gitlab/actions/comment.lua | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index 0916898d..1ffe4771 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -224,7 +224,7 @@ end --- This function will open a comment popup in order to create a comment on the changed/updated --- line in the current MR M.create_comment = function() - if not M.can_create_comment() then + if not M.can_create_comment(false) then return end @@ -237,11 +237,7 @@ end --- This function will open a multi-line comment popup in order to create a multi-line comment --- on the changed/updated line in the current MR M.create_multiline_comment = function() - if not u.check_visual_mode() then - return - end - - if not M.can_create_comment() then + if not M.can_create_comment(true) then return end @@ -302,11 +298,7 @@ end --- on the changed/updated line in the current MR --- See: https://docs.gitlab.com/ee/user/project/merge_requests/reviews/suggestions.html M.create_comment_suggestion = function() - if not u.check_visual_mode() then - return - end - - if not M.can_create_comment() then + if not M.can_create_comment(true) then return end @@ -326,8 +318,9 @@ M.create_comment_suggestion = function() end ---Returns true if it's possible to create an Inline Comment +---@param must_be_visual boolean True if current mode must be visual ---@return boolean -M.can_create_comment = function() +M.can_create_comment = function(must_be_visual) -- Check that diffview is initialized if reviewer.tabnr == nil then u.notify("Reviewer must be initialized first", vim.log.levels.ERROR) @@ -378,6 +371,11 @@ M.can_create_comment = function() return false end + -- Check we're in visual mode for code suggestions and multiline comments + if must_be_visual and not u.check_visual_mode() then + return false + end + return true end From 460fc6b7c04243cb2728984ea5c3e6266d075f9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Mon, 25 Nov 2024 14:33:03 +0100 Subject: [PATCH 13/21] fix: check correct window before checking file renamed --- lua/gitlab/actions/comment.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index 1ffe4771..9d4413d2 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -341,12 +341,6 @@ M.can_create_comment = function(must_be_visual) return false end - -- Check that the file has not been renamed - if reviewer.is_file_renamed() and not reviewer.does_file_have_changes() then - u.notify("Commenting on (unchanged) renamed or moved files is not supported", vim.log.levels.WARN) - return false - end - -- Check that we are hovering over the code local filetype = vim.bo[0].filetype if filetype == "DiffviewFiles" or filetype == "gitlab" then @@ -357,6 +351,12 @@ M.can_create_comment = function(must_be_visual) return false end + -- Check that the file has not been renamed + if reviewer.is_file_renamed() and not reviewer.does_file_have_changes() then + u.notify("Commenting on (unchanged) renamed or moved files is not supported", vim.log.levels.WARN) + return false + end + if not M.sha_exists() then return false end From 180e53cef56ac32f17896f66077e95cee7dea82b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Mon, 25 Nov 2024 16:31:08 +0100 Subject: [PATCH 14/21] perf: remove unnecessary nil checks The function create_comment_layout should now always return a layout (it used to return nil if the comment could not be created at all). --- lua/gitlab/actions/comment.lua | 19 +++++-------------- lua/gitlab/actions/discussions/init.lua | 4 +--- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index 9d4413d2..cc9e2f85 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -229,9 +229,7 @@ M.create_comment = function() end local layout = M.create_comment_layout({ ranged = false, unlinked = false }) - if layout ~= nil then - layout:mount() - end + layout:mount() end --- This function will open a multi-line comment popup in order to create a multi-line comment @@ -242,18 +240,14 @@ M.create_multiline_comment = function() end local layout = M.create_comment_layout({ ranged = true, unlinked = false }) - if layout ~= nil then - layout:mount() - end + layout:mount() end --- This function will open a a popup to create a "note" (e.g. unlinked comment) --- on the changed/updated line in the current MR M.create_note = function() local layout = M.create_comment_layout({ ranged = false, unlinked = true }) - if layout ~= nil then - layout:mount() - end + layout:mount() end ---Given the current visually selected area of text, builds text to fill in the @@ -305,11 +299,8 @@ M.create_comment_suggestion = function() local suggestion_lines, range_length = build_suggestion() local layout = M.create_comment_layout({ ranged = range_length > 0, unlinked = false }) - if layout ~= nil then - layout:mount() - else - return -- Failure in creating the comment layout - end + layout:mount() + vim.schedule(function() if suggestion_lines then vim.api.nvim_buf_set_lines(M.comment_popup.bufnr, 0, -1, false, suggestion_lines) diff --git a/lua/gitlab/actions/discussions/init.lua b/lua/gitlab/actions/discussions/init.lua index 1825c9ad..d620edec 100644 --- a/lua/gitlab/actions/discussions/init.lua +++ b/lua/gitlab/actions/discussions/init.lua @@ -251,9 +251,7 @@ M.reply = function(tree) reply = true, }) - if layout then - layout:mount() - end + layout:mount() end -- This function (settings.keymaps.discussion_tree.delete_comment) will trigger a popup prompting you to delete the current comment From c99a16db2cc76f4af72d39c0b9b1638a839206e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Mon, 25 Nov 2024 16:33:05 +0100 Subject: [PATCH 15/21] fix: exit visual mode when comment cannot be created --- lua/gitlab/actions/comment.lua | 2 ++ lua/gitlab/utils/init.lua | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index cc9e2f85..fa69fb7a 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -236,6 +236,7 @@ end --- on the changed/updated line in the current MR M.create_multiline_comment = function() if not M.can_create_comment(true) then + u.press_escape() return end @@ -293,6 +294,7 @@ end --- See: https://docs.gitlab.com/ee/user/project/merge_requests/reviews/suggestions.html M.create_comment_suggestion = function() if not M.can_create_comment(true) then + u.press_escape() return end diff --git a/lua/gitlab/utils/init.lua b/lua/gitlab/utils/init.lua index 51bea51c..80952f47 100644 --- a/lua/gitlab/utils/init.lua +++ b/lua/gitlab/utils/init.lua @@ -427,6 +427,10 @@ M.press_enter = function() vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("", false, true, true), "n", false) end +M.press_escape = function() + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("", false, true, true), "nx", false) +end + ---Return timestamp from ISO 8601 formatted date string. ---@param date_string string ISO 8601 formatted date string ---@return integer timestamp @@ -588,7 +592,7 @@ end ---Exists visual mode in order to access marks "<" , ">" ---@return integer start,integer end Start line and end line M.get_visual_selection_boundaries = function() - vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("", false, true, true), "nx", false) + M.press_escape() local start_line = vim.api.nvim_buf_get_mark(0, "<")[1] local end_line = vim.api.nvim_buf_get_mark(0, ">")[1] return start_line, end_line From e24bb66b9350692007d6a1132f139becaa2618c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Mon, 25 Nov 2024 16:42:46 +0100 Subject: [PATCH 16/21] perf: remove unnecessary check The check that `diffview_lib.get_current_view() == nil` is superfluous because we later also check explicitly that we are in the reviewer tab. --- lua/gitlab/actions/comment.lua | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index fa69fb7a..5861629c 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -320,17 +320,10 @@ M.can_create_comment = function(must_be_visual) return false end - -- Check that Diffview is the current view - local view = diffview_lib.get_current_view() - if view == nil then - u.notify("Comments should be left in the reviewer pane", vim.log.levels.ERROR) - return false - end - - -- Check that we are in the diffview tab + -- Check that we are in the Diffview tab local tabnr = vim.api.nvim_get_current_tabpage() if tabnr ~= reviewer.tabnr then - u.notify("Line location can only be determined within reviewer window", vim.log.levels.ERROR) + u.notify("Comments can only be left in the reviewer pane", vim.log.levels.ERROR) return false end From c83ca0a69c32726d2388ad4953273114c383b9bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Mon, 25 Nov 2024 16:51:30 +0100 Subject: [PATCH 17/21] docs: use more explicit comments --- lua/gitlab/actions/comment.lua | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index 5861629c..242516ee 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -343,15 +343,18 @@ M.can_create_comment = function(must_be_visual) return false end + -- Check that we are in a valid buffer if not M.sha_exists() then return false end - local has_changes, err = git.has_changes(reviewer.get_current_file_path()) -- Saved modifications + -- Check that there aren't saved modifications + local has_changes, err = git.has_changes(reviewer.get_current_file_path()) if err ~= nil then return false end - local is_modified = vim.bo[0].modified -- Unsaved modifications + -- Check that there aren't unsaved modifications + local is_modified = vim.bo[0].modified if state.settings.reviewer_settings.diffview.imply_local and (is_modified or has_changes) then u.notify("Cannot leave comments on changed files, please stash or commit and push", vim.log.levels.WARN) return false From f0e5dc7039ae14b1bcaec02bfbd8ad5cd228414c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Mon, 25 Nov 2024 17:30:26 +0100 Subject: [PATCH 18/21] fix: check for nil --- lua/gitlab/actions/comment.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index 242516ee..bf201720 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -349,7 +349,11 @@ M.can_create_comment = function(must_be_visual) end -- Check that there aren't saved modifications - local has_changes, err = git.has_changes(reviewer.get_current_file_path()) + local file = reviewer.get_current_file_path() + if file == nil then + return false + end + local has_changes, err = git.has_changes(file) if err ~= nil then return false end From d28664136870c99a4bc858cacc18c2f2be26b71e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Tue, 26 Nov 2024 07:08:07 +0100 Subject: [PATCH 19/21] fix: don't require position_data for draft replies The position data are not necessary for creating a draft reply and requiring it prevented draft replies in case the discussion tree was open outside of the reviewer pane. --- lua/gitlab/actions/comment.lua | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index bf201720..7ad11d53 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -47,6 +47,18 @@ local confirm_create_comment = function(text, visual_range, unlinked, discussion return end + -- Creating a draft reply, in response to a discussion ID + if discussion_id ~= nil and is_draft then + local body = { comment = text, discussion_id = discussion_id } + job.run_job("/mr/draft_notes/", "POST", body, function() + u.notify("Draft reply created!", vim.log.levels.INFO) + draft_notes.load_draft_notes(function() + discussions.rebuild_view(unlinked) + end) + end) + return + end + -- Creating a note (unlinked comment) if unlinked and discussion_id == nil then local body = { comment = text } @@ -90,18 +102,6 @@ local confirm_create_comment = function(text, visual_range, unlinked, discussion line_range = location_data.line_range, } - -- Creating a draft reply, in response to a discussion ID - if discussion_id ~= nil and is_draft then - local body = { comment = text, discussion_id = discussion_id, position = position_data } - job.run_job("/mr/draft_notes/", "POST", body, function() - u.notify("Draft reply created!", vim.log.levels.INFO) - draft_notes.load_draft_notes(function() - discussions.rebuild_view(unlinked) - end) - end) - return - end - -- Creating a new comment (linked to specific changes) local body = u.merge({ type = "text", comment = text }, position_data) local endpoint = is_draft and "/mr/draft_notes/" or "/mr/comment" From 4a6f219bcb529c9e54b261ba62a1a94f3cb1acee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Tue, 26 Nov 2024 08:03:22 +0100 Subject: [PATCH 20/21] fix: use correct message level for comment creation errors --- lua/gitlab/actions/comment.lua | 4 ++-- lua/gitlab/reviewer/init.lua | 6 +++++- lua/gitlab/utils/init.lua | 5 +++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index 7ad11d53..15aaab56 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -339,7 +339,7 @@ M.can_create_comment = function(must_be_visual) -- Check that the file has not been renamed if reviewer.is_file_renamed() and not reviewer.does_file_have_changes() then - u.notify("Commenting on (unchanged) renamed or moved files is not supported", vim.log.levels.WARN) + u.notify("Commenting on (unchanged) renamed or moved files is not supported", vim.log.levels.ERROR) return false end @@ -360,7 +360,7 @@ M.can_create_comment = function(must_be_visual) -- Check that there aren't unsaved modifications local is_modified = vim.bo[0].modified if state.settings.reviewer_settings.diffview.imply_local and (is_modified or has_changes) then - u.notify("Cannot leave comments on changed files, please stash or commit and push", vim.log.levels.WARN) + u.notify("Cannot leave comments on changed files, please stash or commit and push", vim.log.levels.ERROR) return false end diff --git a/lua/gitlab/reviewer/init.lua b/lua/gitlab/reviewer/init.lua index b57c22dc..c5b103be 100644 --- a/lua/gitlab/reviewer/init.lua +++ b/lua/gitlab/reviewer/init.lua @@ -291,12 +291,16 @@ end M.execute_callback = function(callback) return function() vim.api.nvim_cmd({ cmd = "normal", bang = true, args = { "'[V']" } }, {}) - vim.api.nvim_cmd( + local _, err = pcall( + vim.api.nvim_cmd, { cmd = "lua", args = { ("require'gitlab'.%s()"):format(callback) }, mods = { lockmarks = true } }, {} ) vim.api.nvim_win_set_cursor(M.old_winnr, M.old_cursor_position) vim.opt.operatorfunc = M.old_opfunc + if err ~= "" then + u.notify_vim_error(err, vim.log.levels.ERROR) + end end end diff --git a/lua/gitlab/utils/init.lua b/lua/gitlab/utils/init.lua index 80952f47..bf2fda97 100644 --- a/lua/gitlab/utils/init.lua +++ b/lua/gitlab/utils/init.lua @@ -335,6 +335,11 @@ M.notify = function(msg, lvl) vim.notify("gitlab.nvim: " .. msg, lvl) end +-- Re-raise Vimscript error message after removing existing message prefixes +M.notify_vim_error = function(msg, lvl) + M.notify(msg:gsub("^Vim:", ""):gsub("^gitlab.nvim: ", ""), lvl) +end + M.get_current_line_number = function() return vim.api.nvim_call_function("line", { "." }) end From 3479bde33ab93bb3bcc7b21dccebc22c266c8900 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Tue, 26 Nov 2024 10:57:42 +0100 Subject: [PATCH 21/21] style: remove unused variable --- lua/gitlab/actions/comment.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/gitlab/actions/comment.lua b/lua/gitlab/actions/comment.lua index 15aaab56..29fd08ca 100644 --- a/lua/gitlab/actions/comment.lua +++ b/lua/gitlab/actions/comment.lua @@ -3,7 +3,6 @@ --- to this module the data required to make the API calls local Popup = require("nui.popup") local Layout = require("nui.layout") -local diffview_lib = require("diffview.lib") local state = require("gitlab.state") local job = require("gitlab.job") local u = require("gitlab.utils")