Skip to content

Commit d05a23a

Browse files
authored
Fix: Keep empty lines in discussion tree (#173)
* Docs: Fix typos and remove trailing spaces * Fix: Split strings by new lines correctly * Docs: Recommend breakindent option to improve tree nodes indentation * Fix: Replace whole buffer when creating a new suggestion Previously, a trailing empty line was left in the buffer. --------- Co-authored-by: Jakub Bortlík <[email protected]>
1 parent 132dd60 commit d05a23a

File tree

7 files changed

+46
-22
lines changed

7 files changed

+46
-22
lines changed

doc/gitlab.nvim.txt

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*gitlab.nvim.txt* Create, review, and manage Gitlab reources without leaving Neovim
1+
*gitlab.nvim.txt* Create, review, and manage Gitlab resources without leaving Neovim
22

33
==============================================================================
44
Table of Contents *gitlab.nvim.table-of-contents*
@@ -320,7 +320,7 @@ To prevent losing your carefully crafted note/comment/suggestion you can set
320320
which the contents of the popup window will be saved just before the action is
321321
performed. A practical setting is `settings.popup.backup_register = "+"` which
322322
saves to the system clipboard (see |quoteplus|). This lets you easily apply
323-
the action on Gitlab in a browser, if it keeps failing in `gitlab.nvim`.
323+
the action on Gitlab in a browser, if it keeps failing in `gitlab.nvim`.
324324

325325
If you experience such problems, please first read the
326326
|gitlab.nvim.troubleshooting| section. If it does not help, see if there are
@@ -354,6 +354,16 @@ delete/edit/reply are available on the note tree.
354354
require("gitlab").create_note()
355355
<
356356

357+
When the 'wrap' option is on, line wrapping can make the discussion tree
358+
harder to read. You can use the 'breakindent' option to visually indent tree
359+
nodes at the same level. If you don't want to set 'breakindent' globally, you
360+
can set this only for the `gitlab` file type (the file type of discussions and
361+
notes). One way of doing this is by creating a file called
362+
$XDG_CONFIG_HOME/nvim/after/ftplugin/gitlab.lua with the following contents:
363+
>lua
364+
vim.o.breakindent = true
365+
<
366+
357367
LABELS *gitlab.nvim.labels*
358368

359369
You can add or remove labels from the current MR.
@@ -514,7 +524,7 @@ reviewer when checking out a new branch:
514524
>lua
515525
local gitlab = require("gitlab")
516526
vim.keymap.set("n", "glB", function ()
517-
require("gitlab.server").restart(function ()
527+
require("gitlab.server").restart(function ()
518528
vim.cmd.tabclose()
519529
gitlab.review() -- Reopen the reviewer after the server restarts
520530
end)
@@ -630,7 +640,7 @@ reviewer pane (see the gitlab.nvim.review command), otherwise it will error.
630640
>lua
631641
require("gitlab").comment()
632642

633-
Afer the comment is typed, submit it to Gitlab via the |settings.popup.perform_action|
643+
After the comment is typed, submit it to Gitlab via the |settings.popup.perform_action|
634644
keybinding, by default `<leader>l`
635645

636646
create_multiline_comment() *gitlab.nvim.create_multiline_comment*
@@ -640,17 +650,17 @@ mode, and will use the currently selected lines.
640650
>lua
641651
require("gitlab").create_multiline_comment()
642652

643-
Afer the comment is typed, submit it to Gitlab via the |settings.popup.perform_linewise_action|
644-
keybinding, by default `<leader>l`
653+
After the comment is typed, submit it to Gitlab via the |settings.popup.perform_linewise_action|
654+
keybinding, by default `<leader>l`.
645655

646656
create_comment_suggestion() *gitlab.nvim.create_comment_suggestion*
647657

648-
Opens a popup to create a comment suggestion (aka a comment that makes a committable
649-
change suggestion to the currently selected lines).
658+
Opens a popup to create a comment suggestion (aka a comment that makes a committable
659+
change suggestion to the currently selected lines).
650660
>lua
651661
require("gitlab").create_multiline_comment()
652662

653-
Afer the comment is typed, submit it to Gitlab via the |settings.popup.perform_linewise_action|
663+
After the comment is typed, submit it to Gitlab via the |settings.popup.perform_linewise_action|
654664
keybinding, by default |<leader>l|
655665

656666
create_mr({opts}) *gitlab.nvim.create_mr*
@@ -661,7 +671,7 @@ Starts the process of creating an MR for the currently checked out branch.
661671
require("gitlab").create_mr({ target = "main" })
662672
require("gitlab").create_mr({ target = "main", template_file = "my-template.md" })
663673

664-
Parameters:
674+
Parameters:
665675
{opts} Lua table that can be used to skip certain steps in the MR
666676
creation process. If `target` is provided, you will not be prompted for one.
667677
If a template_file is provided, it will be used automatically. Must be
@@ -680,12 +690,12 @@ If there are no diagnostics for the current line, shows a warning message.
680690

681691
gitlab.create_note() *gitlab.nvim.create_note*
682692

683-
Opens a popup to create a note. Notes are like commments except they are not
693+
Opens a popup to create a note. Notes are like comments except they are not
684694
tied to specific changes in an MR.
685695
>lua
686696
require("gitlab").create_note()
687697

688-
Afer the comment is typed, submit it to Gitlab via the |settings.popup.perform_action|
698+
After the comment is typed, submit it to Gitlab via the |settings.popup.perform_action|
689699
keybinding, by default |<leader>l|
690700

691701
gitlab.toggle_discussions() *gitlab.nvim.toggle_discussions*
@@ -694,8 +704,8 @@ Toggles visibility of the discussion tree.
694704
>lua
695705
require("gitlab").toggle_discussions()
696706

697-
Once the discussion tree is open, a number of different keybindings are available
698-
for interacting with different discussions. Please see the `settings.discussion_tree`
707+
Once the discussion tree is open, a number of different keybindings are available
708+
for interacting with different discussions. Please see the `settings.discussion_tree`
699709
section of the setup call for more information about different keybindings.
700710

701711
gitlab.add_assignee() *gitlab.nvim.add_assignee*
@@ -740,7 +750,7 @@ Opens up a popup with information about the pipeline for the current merge reque
740750
>lua
741751
require("gitlab").pipeline()
742752

743-
To re-trigger failed jobs in the pipeline manually, use the `settings.popup.perform_action` keybinding.
753+
To re-trigger failed jobs in the pipeline manually, use the `settings.popup.perform_action` keybinding.
744754
To open the log trace of a job in a new Neovim buffer, use your `settings.popup.perform_linewise_action`
745755
keybinding.
746756

lua/gitlab/actions/comment.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ M.create_comment_suggestion = function()
7777
table.insert(suggestion_lines, backticks)
7878

7979
comment_popup:mount()
80-
vim.api.nvim_buf_set_lines(comment_popup.bufnr, 0, 0, false, suggestion_lines)
80+
vim.api.nvim_buf_set_lines(comment_popup.bufnr, 0, -1, false, suggestion_lines)
8181
state.set_popup_keymaps(comment_popup, function(text)
8282
if range > 0 then
8383
M.confirm_create_comment(text, { start_line = start_line, end_line = end_line })

lua/gitlab/actions/create_mr.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,11 @@ end
259259
---Builds a lua list of strings that contain the MR description
260260
M.build_description_lines = function(template_content)
261261
local description_lines = {}
262-
for line in template_content:gmatch("[^\n]+") do
262+
for line in u.split_by_new_lines(template_content) do
263263
table.insert(description_lines, line)
264-
table.insert(description_lines, "")
265264
end
265+
-- TODO: @harrisoncramer Same as in lua/gitlab/actions/summary.lua:114
266+
table.insert(description_lines, "")
266267

267268
return description_lines
268269
end

lua/gitlab/actions/discussions/tree.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ end
9292
---@return NuiTree.Node[]
9393
local function build_note_body(note, resolve_info)
9494
local text_nodes = {}
95-
for bodyLine in note.body:gmatch("[^\n]+") do
95+
for bodyLine in u.split_by_new_lines(note.body) do
9696
local line = attach_uuid(bodyLine)
9797
table.insert(
9898
text_nodes,

lua/gitlab/actions/pipeline.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ M.see_logs = function()
148148
end
149149

150150
local lines = {}
151-
for line in file:gmatch("[^\n]+") do
151+
for line in u.split_by_new_lines(file) do
152152
table.insert(lines, line)
153153
end
154154

lua/gitlab/actions/summary.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,13 @@ M.build_description_lines = function()
108108
local description_lines = {}
109109

110110
local description = state.INFO.description
111-
for line in description:gmatch("[^\n]+") do
111+
for line in u.split_by_new_lines(description) do
112112
table.insert(description_lines, line)
113-
table.insert(description_lines, "")
114113
end
114+
-- TODO: @harrisoncramer Not sure whether the following line should be here at all. It definitely
115+
-- didn't belong into the for loop, since it inserted an empty line after each line. But maybe
116+
-- there is a purpose for an empty line at the end of the buffer?
117+
table.insert(description_lines, "")
115118

116119
return description_lines
117120
end

lua/gitlab/utils/init.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ M.trim = function(s)
148148
return res
149149
end
150150

151+
---Splits a string by new lines and returns an iterator
152+
---@param s string The string to split
153+
---@return table: An iterator object
154+
M.split_by_new_lines = function(s)
155+
if s:sub(-1) ~= "\n" then
156+
s = s .. "\n"
157+
end -- Append a new line to the string, if there's none, otherwise the last line would be lost.
158+
return s:gmatch("(.-)\n") -- Match 0 or more (as few as possible) characters followed by a new line.
159+
end
160+
151161
-- Reverses the order of elements in a list
152162
---@param list table The list to reverse
153163
---@return table

0 commit comments

Comments
 (0)