Skip to content

Commit aeac2b2

Browse files
committed
refactor: deprecate create_comment_suggestion
1 parent ba1efaa commit aeac2b2

6 files changed

Lines changed: 67 additions & 97 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,5 +204,6 @@ These `keymaps` are active in the reviewer window (the diff view).
204204
```
205205
c Create a comment for the lines that the following {motion} moves over
206206
s Create a suggestion for the lines that the following {motion} moves over
207+
S Create a suggestion with preview in a new tab for the lines that the following {motion} moves over
207208
a Jump to the comment in the discussion tree
208209
```

doc/gitlab.nvim.txt

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -449,20 +449,19 @@ the configuration.
449449

450450
REVIEWING AN MR *gitlab.nvim.reviewing-an-mr*
451451

452-
The `review` action will open a diff of the changes. You can leave comments
453-
using the `create_comment` action, and add suggestions for changes with the
454-
`create_comment_suggestion` command:
452+
The `review` action opens a diff of the changes (see |gitlab.nvim.review|).
453+
Alternatively, use `choose_merge_request` for more flexibility in choosing the
454+
MR (see |gitlab.nvim.choose_merge_request|).
455455
>lua
456456
require("gitlab").review()
457-
require("gitlab").create_comment()
458-
require("gitlab").create_comment_suggestion()
457+
require("gitlab").choose_merge_request({ labels = {"include_mrs_with_label"} })
459458
<
460-
For suggesting changes you can use `create_comment_suggestion` in visual mode
461-
which prefills the comment window with Gitlab’s suggest changes
462-
<https://docs.gitlab.com/ee/user/project/merge_requests/reviews/suggestions.html>
463-
code block with prefilled code from the visual selection. Just like the
464-
summary, all the different kinds of comments are saved via the
465-
`keymaps.popup.perform_action` keybinding.
459+
You can leave comments in the reviewer windows using the `keymaps.reviewer`
460+
keybindings which work in normal mode as operators and in visual mode they
461+
create comments on the selected lines. Alternatively, you can use the
462+
`create_comment` action in your custom mappings (see
463+
|gitlab.nvim.create_comment|). See `settings.keymaps.popup` for the
464+
keybindings available in the popup windows.
466465

467466
DRAFT NOTES *gitlab.nvim.draft-comments*
468467

@@ -883,27 +882,26 @@ gitlab.create_comment() ~
883882
Opens a popup to create a comment on the selected line(s) in the current
884883
buffer. Must be called when focused on the reviewer pane (see the
885884
|gitlab.nvim.review| command). In normal mode comments on the current line. In
886-
visual mode comments on the selected lines
885+
visual mode comments on the selected lines:
887886
>lua
888887
require("gitlab").create_comment()
888+
require("gitlab").create_comment({ with_suggestion = true })
889889
<
890-
You can use the `keymaps.popup.perform_linewise_action` keybinding (by default
891-
`ZA`) to attach a file to the comment. After the comment is typed, submit it
892-
to Gitlab via the `keymaps.popup.perform_action` keybinding, by default `ZZ`.
893-
Discard the comment with `keymaps.popup.discard_changes` (`ZQ`), otherwise if
894-
you close the popup with something like `<c-w>q`, the comment contents are
895-
saved to the temporary register(s) (|gitlab.nvim.temp-registers|).
896-
897-
*gitlab.nvim.create_comment_suggestion*
898-
gitlab.create_comment_suggestion() ~
899-
900-
Opens a popup to create a comment suggestion (aka a comment that makes a committable
901-
change suggestion to the currently selected lines).
902-
>lua
903-
require("gitlab").create_comment_suggestion()
904-
<
905-
After the comment is typed, submit it to Gitlab via the
906-
`keymaps.popup.perform_linewise_action` keybinding, by default `ZA`.
890+
Parameters: ~
891+
{opts}: (table|nil) Keyword arguments that can be used to include
892+
a suggestion in the comment.
893+
• {with_suggestion} (boolean) When true, pastes into the comment
894+
buffer Gitlab’s suggestion code block prefilled with the
895+
original text from the visual selection. See
896+
https://docs.gitlab.com/ee/user/project/merge_requests/reviews/suggestions.html.
897+
898+
In the popup, you can use the `keymaps.popup.perform_linewise_action`
899+
keybinding (by default `ZA`) to attach a file to the comment. After the
900+
comment is typed, submit it to Gitlab via the `keymaps.popup.perform_action`
901+
keybinding, by default `ZZ`. Discard the comment with
902+
`keymaps.popup.discard_changes` (`ZQ`), otherwise if you close the popup with
903+
something like `<c-w>q`, the comment contents are saved to the temporary
904+
register(s) (|gitlab.nvim.temp-registers|).
907905

908906
*gitlab.nvim.create_mr*
909907
gitlab.create_mr({opts}) ~

lua/gitlab/actions/comment.lua

Lines changed: 17 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,30 @@ end
223223
--- Creates a comment on the selected line(s) in the current buffer.
224224
--- In normal mode comments on the current line.
225225
--- In visual mode comments on the whole selection.
226-
M.create_comment = function()
226+
---@param opts CreateCommentsOpts?
227+
M.create_comment = function(opts)
228+
opts = opts or {}
227229
M.location = Location.new()
228230
if not M.can_create_comment(false) then
229231
return
230232
end
231233

234+
local suggestion_lines = require("gitlab.actions.suggestions").build_suggestion(
235+
vim.api.nvim_buf_get_lines(0, 0, -1, false),
236+
M.location.visual_range.start_line,
237+
M.location.visual_range.end_line
238+
)
239+
232240
local layout = M.create_comment_layout({ unlinked = false })
233241
layout:mount()
242+
243+
if opts.with_suggestion then
244+
vim.schedule(function()
245+
if suggestion_lines then
246+
vim.api.nvim_buf_set_lines(M.comment_popup.bufnr, 0, -1, false, suggestion_lines)
247+
end
248+
end)
249+
end
234250
end
235251

236252
--- This function will open a a popup to create a "note" (e.g. unlinked comment)
@@ -240,63 +256,6 @@ M.create_note = function()
240256
layout:mount()
241257
end
242258

243-
---Given the current visually selected area of text, builds text to fill in the
244-
---comment popup with a suggested change
245-
---@return LineRange|nil
246-
local build_suggestion = function()
247-
local current_line = vim.api.nvim_win_get_cursor(0)[1]
248-
local range_length = M.location.visual_range.end_line - M.location.visual_range.start_line
249-
local backticks = "```"
250-
local selected_lines = u.get_lines(M.location.visual_range.start_line, M.location.visual_range.end_line)
251-
252-
for _, line in ipairs(selected_lines) do
253-
if string.match(line, "^```%S*$") then
254-
backticks = "````"
255-
break
256-
end
257-
end
258-
259-
local suggestion_start
260-
if M.location.visual_range.start_line == current_line then
261-
suggestion_start = backticks .. "suggestion:-0+" .. range_length
262-
elseif M.location.visual_range.end_line == current_line then
263-
suggestion_start = backticks .. "suggestion:-" .. range_length .. "+0"
264-
else
265-
--- This should never happen afaik
266-
u.notify("Unexpected suggestion position", vim.log.levels.ERROR)
267-
return nil
268-
end
269-
suggestion_start = suggestion_start
270-
local suggestion_lines = {}
271-
table.insert(suggestion_lines, suggestion_start)
272-
vim.list_extend(suggestion_lines, selected_lines)
273-
table.insert(suggestion_lines, backticks)
274-
275-
return suggestion_lines
276-
end
277-
278-
--- This function will open a a popup to create a suggestion comment
279-
--- on the changed/updated line in the current MR
280-
--- See: https://docs.gitlab.com/ee/user/project/merge_requests/reviews/suggestions.html
281-
M.create_comment_suggestion = function()
282-
M.location = Location.new()
283-
if not M.can_create_comment(true) then
284-
u.press_escape()
285-
return
286-
end
287-
288-
local suggestion_lines = build_suggestion()
289-
290-
local layout = M.create_comment_layout({ unlinked = false })
291-
layout:mount()
292-
293-
vim.schedule(function()
294-
if suggestion_lines then
295-
vim.api.nvim_buf_set_lines(M.comment_popup.bufnr, 0, -1, false, suggestion_lines)
296-
end
297-
end)
298-
end
299-
300259
--- This function will create a new tab with a suggestion preview for the changed/updated line in
301260
--- the current MR.
302261
M.create_comment_with_suggestion = function()

lua/gitlab/annotations.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@
143143
---@field commit_id string -- This will always be ""
144144
---@field line_code string
145145
---@field position NotePosition
146+
147+
---@class CreateCommentsOpts Options for the create_comment function.
148+
---@field with_suggestion boolean When true, paste the default suggestion into the comment buffer.
146149
---
147150
---
148151
--- Plugin Settings

lua/gitlab/init.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@ return {
7979
u.notify("create_multiline_comment() is deprecated, use create_comment()", vim.log.levels.WARN)
8080
comment.create_comment()
8181
end),
82-
create_comment_suggestion = async.sequence({ info, revisions }, comment.create_comment_suggestion),
82+
create_comment_suggestion = async.sequence({ info, revisions }, function()
83+
u.notify(
84+
"create_comment_suggestion() is deprecated, use create_comment({with_suggestion=true})",
85+
vim.log.levels.WARN
86+
)
87+
comment.create_comment({ with_suggestion = true })
88+
end),
8389
create_comment_with_suggestion = async.sequence({ info, revisions }, comment.create_comment_with_suggestion),
8490
move_to_discussion_tree_from_diagnostic = async.sequence({}, discussions.move_to_discussion_tree),
8591
create_note = async.sequence({ info }, comment.create_note),

lua/gitlab/reviewer/init.lua

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,14 @@ end
328328
---@param callback string Name of the gitlab.nvim API function to call
329329
M.execute_callback = function(callback)
330330
return function()
331+
local opts = M.callback_opts
332+
M.callback_opts = nil
331333
vim.api.nvim_cmd({ cmd = "normal", bang = true, args = { "'[V']" } }, {})
332-
local _, err = pcall(
333-
vim.api.nvim_cmd,
334-
{ cmd = "lua", args = { ("require'gitlab'.%s()"):format(callback) }, mods = { lockmarks = true } },
335-
{}
336-
)
334+
local _, err = pcall(vim.api.nvim_cmd, {
335+
cmd = "lua",
336+
args = { ("require'gitlab'.%s(%s)"):format(callback, vim.inspect(opts or {})) },
337+
mods = { lockmarks = true },
338+
}, {})
337339
vim.api.nvim_win_set_cursor(M.old_winnr, M.old_cursor_position)
338340
vim.opt.operatorfunc = M.old_opfunc
339341
if err ~= "" then
@@ -345,10 +347,12 @@ end
345347
---Set the operatorfunc that will work on the lines defined by the motion that follows after the
346348
---operator mapping, and enter the operator-pending mode.
347349
---@param cb string Name of the gitlab.nvim API function to call, e.g., "create_comment".
348-
local function execute_operatorfunc(cb)
350+
---@param opts table? Optional arguments for the callback.
351+
local function execute_operatorfunc(cb, opts)
349352
M.old_opfunc = vim.opt.operatorfunc
350353
M.old_winnr = vim.api.nvim_get_current_win()
351354
M.old_cursor_position = vim.api.nvim_win_get_cursor(M.old_winnr)
355+
M.callback_opts = opts
352356
vim.opt.operatorfunc = ("v:lua.require'gitlab.reviewer'.execute_callback'%s'"):format(cb)
353357
-- Use the operator count before motion to allow, e.g., 2cc == c2c
354358
local count = M.operator_count > 0 and tostring(M.operator_count) or ""
@@ -413,8 +417,7 @@ M.set_keymaps = function(bufnr)
413417
-- Set operator keybinding
414418
vim.keymap.set("n", keymaps.reviewer.create_suggestion, function()
415419
M.operator_count = vim.v.count
416-
M.operator = keymaps.reviewer.create_suggestion
417-
execute_operatorfunc("create_comment_suggestion")
420+
execute_operatorfunc("create_comment", { with_suggestion = true })
418421
end, {
419422
buffer = bufnr,
420423
desc = "Create suggestion for range of motion",
@@ -423,7 +426,7 @@ M.set_keymaps = function(bufnr)
423426

424427
-- Set visual mode keybinding
425428
vim.keymap.set("v", keymaps.reviewer.create_suggestion, function()
426-
require("gitlab").create_comment_suggestion()
429+
require("gitlab").create_comment({ with_suggestion = true })
427430
end, {
428431
buffer = bufnr,
429432
desc = "Create suggestion for selected text",

0 commit comments

Comments
 (0)