1
- local u = require (" gitlab.utils" )
2
- local Menu = require (" nui.menu" )
3
- local NuiTree = require (" nui.tree" )
4
- local NuiSplit = require (" nui.split" )
5
- local job = require (" gitlab.job" )
6
- local state = require (" gitlab.state" )
7
- local Popup = require (" nui.popup" )
8
- local settings = require (" gitlab.settings" )
9
- local reviewer = require (" gitlab.reviewer" )
10
-
11
- local edit_popup = Popup (u .create_popup_state (" Edit Comment" , " 80%" , " 80%" ))
12
- local reply_popup = Popup (u .create_popup_state (" Reply" , " 80%" , " 80%" ))
13
-
1
+ local u = require (" gitlab.utils" )
2
+ local Menu = require (" nui.menu" )
3
+ local NuiTree = require (" nui.tree" )
4
+ local NuiSplit = require (" nui.split" )
5
+ local job = require (" gitlab.job" )
6
+ local state = require (" gitlab.state" )
7
+ local Popup = require (" nui.popup" )
8
+ local settings = require (" gitlab.settings" )
9
+ local reviewer = require (" gitlab.reviewer" )
10
+
11
+ local edit_popup = Popup (u .create_popup_state (" Edit Comment" , " 80%" , " 80%" ))
12
+ local reply_popup = Popup (u .create_popup_state (" Reply" , " 80%" , " 80%" ))
14
13
15
14
-- This module is responsible for the discussion tree. That includes things like
16
15
-- editing existing notes in the tree, replying to notes in the tree,
17
16
-- and marking discussions as resolved/unresolved.
18
- local M = {}
19
17
20
- M .split = nil
21
- M .split_visible = false
18
+ local M = {
19
+ split_visible = false ,
20
+ split = nil ,
21
+ split_buf = nil ,
22
+ tree = nil
23
+ }
24
+
22
25
M .list_discussions = function ()
23
26
job .run_job (" discussions" , " GET" , nil , function (data )
24
27
if type (data .discussions ) ~= " table" then
@@ -34,21 +37,31 @@ M.list_discussions = function()
34
37
})
35
38
36
39
split :mount ()
40
+ local buf = split .bufnr
37
41
38
42
M .split = split
39
43
M .split_visible = true
40
-
41
- local buf = split .bufnr
42
- state .SPLIT_BUF = buf
44
+ M .split_buf = split .bufnr
43
45
44
46
local tree_nodes = M .add_discussions_to_table (data .discussions )
45
47
46
- state .tree = NuiTree ({ nodes = tree_nodes , bufnr = buf })
48
+ M .tree = NuiTree ({ nodes = tree_nodes , bufnr = buf })
47
49
M .set_tree_keymaps (buf )
48
50
49
- state .tree :render ()
51
+ M .tree :render ()
50
52
vim .api .nvim_buf_set_option (buf , ' filetype' , ' markdown' )
51
53
u .darken_metadata (buf , ' ' )
54
+
55
+ vim .keymap .set (' n' , state .settings .review_pane .toggle_discussions , function ()
56
+ if not M .split then return end
57
+ if M .split_visible then
58
+ M .split :hide ()
59
+ M .split_visible = false
60
+ else
61
+ M .split :show ()
62
+ M .split_visible = true
63
+ end
64
+ end )
52
65
end )
53
66
end
54
67
72
85
-- This function (settings.discussion_tree.jump_to_location) will
73
86
-- jump you to the file and line where the comment was left
74
87
M .jump_to_location = function ()
75
- local node = state .tree :get_node ()
88
+ local node = M .tree :get_node ()
76
89
if node == nil then return end
77
90
78
91
local discussion_node = M .get_root_node (node )
135
148
-- when you make a selection
136
149
M .send_deletion = function (item )
137
150
if item .text == " Confirm" then
138
- local current_node = state .tree :get_node ()
151
+ local current_node = M .tree :get_node ()
139
152
140
153
local note_node = M .get_note_node (current_node )
141
154
local root_node = M .get_root_node (current_node )
@@ -147,8 +160,8 @@ M.send_deletion = function(item)
147
160
job .run_job (" comment" , " DELETE" , json , function (data )
148
161
vim .notify (data .message , vim .log .levels .INFO )
149
162
if not note_node .is_root then
150
- state .tree :remove_node (" -" .. note_id )
151
- state .tree :render ()
163
+ M .tree :remove_node (" -" .. note_id )
164
+ M .tree :render ()
152
165
else
153
166
-- We are removing the root node of the discussion,
154
167
-- we need to move all the children around, the easiest way
162
175
163
176
-- This function (settings.discussion_tree.edit_comment) will open the edit popup for the current comment in the discussion tree
164
177
M .edit_comment = function ()
165
- local current_node = state .tree :get_node ()
178
+ local current_node = M .tree :get_node ()
166
179
local note_node = M .get_note_node (current_node )
167
180
local root_node = M .get_root_node (current_node )
168
181
@@ -171,9 +184,9 @@ M.edit_comment = function()
171
184
local lines = {} -- Gather all lines from immediate children that aren't note nodes
172
185
local children_ids = note_node :get_child_ids ()
173
186
for _ , child_id in ipairs (children_ids ) do
174
- local child_node = state .tree :get_node (child_id )
187
+ local child_node = M .tree :get_node (child_id )
175
188
if (not child_node :has_children ()) then
176
- local line = state .tree :get_node (child_id ).text
189
+ local line = M .tree :get_node (child_id ).text
177
190
table.insert (lines , line )
178
191
end
179
192
end
201
214
202
215
-- This comment (settings.discussion_tree.toggle_resolved) will toggle the resolved status of the current discussion and send the change to the Go server
203
216
M .toggle_resolved = function ()
204
- local note = state .tree :get_node ()
217
+ local note = M .tree :get_node ()
205
218
if not note or not note .resolvable then return end
206
219
207
220
local json_table = {
@@ -219,12 +232,12 @@ end
219
232
220
233
-- Helpers
221
234
M .update_resolved_status = function (note , mark_resolved )
222
- local current_text = state .tree .nodes .by_id [" -" .. note .id ].text
235
+ local current_text = M .tree .nodes .by_id [" -" .. note .id ].text
223
236
local target = mark_resolved and ' resolved' or ' unresolved'
224
237
local current = mark_resolved and ' unresolved' or ' resolved'
225
238
226
239
local function set_property (key , val )
227
- state .tree .nodes .by_id [" -" .. note .id ][key ] = val
240
+ M .tree .nodes .by_id [" -" .. note .id ][key ] = val
228
241
end
229
242
230
243
local has_symbol = function (s )
@@ -243,7 +256,7 @@ M.update_resolved_status = function(note, mark_resolved)
243
256
set_property (' text' , (u .remove_last_chunk (current_text ) .. " " .. state .settings .discussion_tree [target ]))
244
257
end
245
258
246
- state .tree :render ()
259
+ M .tree :render ()
247
260
end
248
261
249
262
M .set_tree_keymaps = function (buf )
@@ -257,29 +270,29 @@ M.set_tree_keymaps = function(buf)
257
270
258
271
-- Expands/collapses the current node
259
272
vim .keymap .set (' n' , state .settings .discussion_tree .toggle_node , function ()
260
- local node = state .tree :get_node ()
273
+ local node = M .tree :get_node ()
261
274
if node == nil then return end
262
275
local children = node :get_child_ids ()
263
276
if node == nil then return end
264
277
if node :is_expanded () then
265
278
node :collapse ()
266
279
for _ , child in ipairs (children ) do
267
- state .tree :get_node (child ):collapse ()
280
+ M .tree :get_node (child ):collapse ()
268
281
end
269
282
else
270
283
for _ , child in ipairs (children ) do
271
- state .tree :get_node (child ):expand ()
284
+ M .tree :get_node (child ):expand ()
272
285
end
273
286
node :expand ()
274
287
end
275
288
276
- state .tree :render ()
289
+ M .tree :render ()
277
290
u .darken_metadata (buf , ' ' )
278
291
end ,
279
292
{ buffer = true })
280
293
281
294
vim .keymap .set (' n' , ' r' , function ()
282
- local node = state .tree :get_node ()
295
+ local node = M .tree :get_node ()
283
296
if node == nil then return end
284
297
local discussion_node = M .get_root_node (node )
285
298
M .reply (tostring (discussion_node .id ))
@@ -291,30 +304,30 @@ end
291
304
--
292
305
293
306
M .redraw_text = function (text )
294
- local current_node = state .tree :get_node ()
307
+ local current_node = M .tree :get_node ()
295
308
local note_node = M .get_note_node (current_node )
296
309
297
310
local childrenIds = note_node :get_child_ids ()
298
311
for _ , value in ipairs (childrenIds ) do
299
- state .tree :remove_node (value )
312
+ M .tree :remove_node (value )
300
313
end
301
314
302
315
local newNoteTextNodes = {}
303
316
for bodyLine in text :gmatch (" [^\n ]+" ) do
304
317
table.insert (newNoteTextNodes , NuiTree .Node ({ text = bodyLine , is_body = true }, {}))
305
318
end
306
319
307
- state .tree :set_nodes (newNoteTextNodes , " -" .. note_node .id )
320
+ M .tree :set_nodes (newNoteTextNodes , " -" .. note_node .id )
308
321
309
- state .tree :render ()
322
+ M .tree :render ()
310
323
local buf = vim .api .nvim_get_current_buf ()
311
324
u .darken_metadata (buf , ' ' )
312
325
end
313
326
314
327
M .get_root_node = function (node )
315
328
if (not node .is_root ) then
316
329
local parent_id = node :get_parent_id ()
317
- return M .get_root_node (state .tree :get_node (parent_id ))
330
+ return M .get_root_node (M .tree :get_node (parent_id ))
318
331
else
319
332
return node
320
333
end
@@ -324,7 +337,7 @@ M.get_note_node = function(node)
324
337
if (not node .is_note ) then
325
338
local parent_id = node :get_parent_id ()
326
339
if parent_id == nil then return node end
327
- return M .get_note_node (state .tree :get_node (parent_id ))
340
+ return M .get_note_node (M .tree :get_node (parent_id ))
328
341
else
329
342
return node
330
343
end
371
384
M .add_note_to_tree = function (note , discussion_id )
372
385
local note_node = M .build_note (note )
373
386
note_node :expand ()
374
- state .tree :add_node (note_node , discussion_id and (" -" .. discussion_id ) or nil )
375
- state .tree :render ()
387
+ M .tree :add_node (note_node , discussion_id and (" -" .. discussion_id ) or nil )
388
+ M .tree :render ()
376
389
local buf = vim .api .nvim_get_current_buf ()
377
390
u .darken_metadata (buf , ' ' )
378
391
vim .notify (" Sent reply!" , vim .log .levels .INFO )
@@ -385,20 +398,20 @@ M.refresh_tree = function()
385
398
return
386
399
end
387
400
388
- if not state . SPLIT_BUF or (vim .fn .bufwinid (state . SPLIT_BUF ) == - 1 ) then return end
401
+ if not M . split_buf or (vim .fn .bufwinid (M . split_buf ) == - 1 ) then return end
389
402
390
- vim .api .nvim_buf_set_option (state . SPLIT_BUF , ' modifiable' , true )
391
- vim .api .nvim_buf_set_option (state . SPLIT_BUF , ' readonly' , false )
392
- vim .api .nvim_buf_set_lines (state . SPLIT_BUF , 0 , - 1 , false , {})
393
- vim .api .nvim_buf_set_option (state . SPLIT_BUF , ' readonly' , true )
394
- vim .api .nvim_buf_set_option (state . SPLIT_BUF , ' modifiable' , false )
403
+ vim .api .nvim_buf_set_option (M . split_buf , ' modifiable' , true )
404
+ vim .api .nvim_buf_set_option (M . split_buf , ' readonly' , false )
405
+ vim .api .nvim_buf_set_lines (M . split_buf , 0 , - 1 , false , {})
406
+ vim .api .nvim_buf_set_option (M . split_buf , ' readonly' , true )
407
+ vim .api .nvim_buf_set_option (M . split_buf , ' modifiable' , false )
395
408
396
409
local tree_nodes = M .add_discussions_to_table (data .discussions )
397
- state .tree = NuiTree ({ nodes = tree_nodes , bufnr = state . SPLIT_BUF })
398
- M .set_tree_keymaps (state . SPLIT_BUF )
399
- state .tree :render ()
400
- vim .api .nvim_buf_set_option (state . SPLIT_BUF , ' filetype' , ' markdown' )
401
- u .darken_metadata (state . SPLIT_BUF , ' ' )
410
+ M .tree = NuiTree ({ nodes = tree_nodes , bufnr = M . split_buf })
411
+ M .set_tree_keymaps (M . split_buf )
412
+ M .tree :render ()
413
+ vim .api .nvim_buf_set_option (M . split_buf , ' filetype' , ' markdown' )
414
+ u .darken_metadata (M . split_buf , ' ' )
402
415
end )
403
416
end
404
417
0 commit comments