@@ -9,8 +9,10 @@ local job = require("gitlab.job")
99local u = require (" gitlab.utils" )
1010local state = require (" gitlab.state" )
1111local reviewer = require (" gitlab.reviewer" )
12+ local List = require (" gitlab.utils.list" )
1213local miscellaneous = require (" gitlab.actions.miscellaneous" )
1314local discussions_tree = require (" gitlab.actions.discussions.tree" )
15+ local diffview_lib = require (" diffview.lib" )
1416local signs = require (" gitlab.actions.discussions.signs" )
1517local winbar = require (" gitlab.actions.discussions.winbar" )
1618local help = require (" gitlab.actions.help" )
5355--- Initialize everything for discussions like setup of signs, callbacks for reviewer, etc.
5456M .initialize_discussions = function ()
5557 signs .setup_signs ()
56- -- Setup callback to refresh discussion data, discussion signs and diagnostics whenever the reviewed file changes.
57- reviewer .set_callback_for_file_changed (M .refresh_discussion_data )
58- -- Setup callback to clear signs and diagnostics whenever reviewer is left.
59- reviewer .set_callback_for_reviewer_leave (signs .clear_signs_and_diagnostics )
58+ reviewer .set_callback_for_file_changed (function ()
59+ M .refresh_view ()
60+ M .modifiable (false )
61+ end )
62+ reviewer .set_callback_for_reviewer_enter (function ()
63+ M .modifiable (false )
64+ end )
65+ reviewer .set_callback_for_reviewer_leave (function ()
66+ signs .clear_signs_and_diagnostics ()
67+ M .modifiable (true )
68+ end )
69+ end
70+
71+ --- Ensures that the both buffers in the reviewer are/not modifiable. Relevant if the user is using
72+ --- the --imply-local setting
73+ M .modifiable = function (bool )
74+ local view = diffview_lib .get_current_view ()
75+ local a = view .cur_layout .a .file .bufnr
76+ local b = view .cur_layout .b .file .bufnr
77+ if vim .api .nvim_buf_is_loaded (a ) then
78+ vim .api .nvim_buf_set_option (a , " modifiable" , bool )
79+ end
80+ if vim .api .nvim_buf_is_loaded (b ) then
81+ vim .api .nvim_buf_set_option (b , " modifiable" , bool )
82+ end
6083end
6184
6285--- Refresh discussion data, signs, diagnostics, and winbar with new data from API
63- M .refresh_discussion_data = function ()
86+ --- and rebuild the entire view
87+ M .refresh = function ()
6488 M .load_discussions (function ()
65- if state .settings .discussion_sign .enabled then
66- signs .refresh_signs (M .discussions )
67- end
68- if state .settings .discussion_diagnostic .enabled then
69- signs .refresh_diagnostics (M .discussions )
70- end
71- if M .split_visible then
72- local linked_is_focused = M .linked_bufnr == M .focused_bufnr
73- winbar .update_winbar (M .discussions , M .unlinked_discussions , linked_is_focused and " Discussions" or " Notes" )
74- end
89+ M .refresh_view ()
7590 end )
7691end
7792
93+ --- Take existing data and refresh the diagnostics, the winbar, and the signs
94+ M .refresh_view = function ()
95+ if state .settings .discussion_sign .enabled then
96+ signs .refresh_signs (M .discussions )
97+ end
98+ if state .settings .discussion_diagnostic .enabled then
99+ signs .refresh_diagnostics (M .discussions )
100+ end
101+ if M .split_visible then
102+ local linked_is_focused = M .linked_bufnr == M .focused_bufnr
103+ winbar .update_winbar (M .discussions , M .unlinked_discussions , linked_is_focused and " Discussions" or " Notes" )
104+ end
105+ end
106+
78107--- Toggle Discussions tree type between "simple" and "by_file_name"
79108--- @param unlinked boolean True if selected view type is Notes (unlinked discussions )
80109M .toggle_tree_type = function (unlinked )
@@ -148,6 +177,7 @@ M.toggle = function(callback)
148177 end )
149178end
150179
180+ -- Change between views in the discussion panel, either notes or discussions
151181local switch_view_type = function ()
152182 local change_to_unlinked = M .linked_bufnr == M .focused_bufnr
153183 local new_bufnr = change_to_unlinked and M .unlinked_bufnr or M .linked_bufnr
@@ -254,36 +284,23 @@ end
254284
255285-- This function will actually send the deletion to Gitlab
256286-- when you make a selection, and re-render the tree
257- M .send_deletion = function (tree , unlinked )
287+ M .send_deletion = function (tree )
258288 local current_node = tree :get_node ()
259289
260290 local note_node = M .get_note_node (tree , current_node )
261291 local root_node = M .get_root_node (tree , current_node )
262292 local note_id = note_node .is_root and root_node .root_note_id or note_node .id
263-
264293 local body = { discussion_id = root_node .id , note_id = tonumber (note_id ) }
265-
266294 job .run_job (" /mr/comment" , " DELETE" , body , function (data )
267295 u .notify (data .message , vim .log .levels .INFO )
268- if not note_node .is_root then
269- tree : remove_node ( " - " .. note_id ) -- Note is not a discussion root, safe to remove
270- tree :render ( )
296+ if note_node .is_root then
297+ -- Replace root node w/ current node's contents...
298+ tree :remove_node ( " - " .. root_node . id )
271299 else
272- if unlinked then
273- M .unlinked_discussions = u .remove_first_value (M .unlinked_discussions )
274- M .rebuild_unlinked_discussion_tree ()
275- else
276- M .discussions = u .remove_first_value (M .discussions )
277- M .rebuild_discussion_tree ()
278- end
279- M .add_empty_titles ({
280- { M .linked_bufnr , M .discussions , " No Discussions for this MR" },
281- { M .unlinked_bufnr , M .unlinked_discussions , " No Notes (Unlinked Discussions) for this MR" },
282- })
283- M .switch_can_edit_bufs (false )
300+ tree :remove_node (" -" .. note_id )
284301 end
285-
286- M .refresh_discussion_data ()
302+ tree : render ()
303+ M .refresh ()
287304 end )
288305end
289306
@@ -293,18 +310,22 @@ M.edit_comment = function(tree, unlinked)
293310 local current_node = tree :get_node ()
294311 local note_node = M .get_note_node (tree , current_node )
295312 local root_node = M .get_root_node (tree , current_node )
313+ if note_node == nil or root_node == nil then
314+ u .notify (" Could not get root or note node" , vim .log .levels .ERROR )
315+ return
316+ end
296317
297318 edit_popup :mount ()
298319
299- local lines = {} -- Gather all lines from immediate children that aren't note nodes
300- local children_ids = note_node :get_child_ids ()
301- for _ , child_id in ipairs (children_ids ) do
320+ -- Gather all lines from immediate children that aren't note nodes
321+ local lines = List .new (note_node :get_child_ids ()):reduce (function (agg , child_id )
302322 local child_node = tree :get_node (child_id )
303323 if not child_node :has_children () then
304324 local line = tree :get_node (child_id ).text
305- table.insert (lines , line )
325+ table.insert (agg , line )
306326 end
307- end
327+ return agg
328+ end , {})
308329
309330 local currentBuffer = vim .api .nvim_get_current_buf ()
310331 vim .api .nvim_buf_set_lines (currentBuffer , 0 , - 1 , false , lines )
@@ -362,7 +383,7 @@ M.toggle_discussion_resolved = function(tree)
362383 job .run_job (" /mr/discussions/resolve" , " PUT" , body , function (data )
363384 u .notify (data .message , vim .log .levels .INFO )
364385 M .redraw_resolved_status (tree , note , not note .resolved )
365- M .refresh_discussion_data ()
386+ M .refresh ()
366387 end )
367388end
368389
@@ -373,7 +394,17 @@ M.jump_to_reviewer = function(tree)
373394 u .notify (error , vim .log .levels .ERROR )
374395 return
375396 end
376- reviewer .jump (file_name , new_line , old_line , { is_undefined_type = is_undefined_type })
397+
398+ local new_line_int = tonumber (new_line )
399+ local old_line_int = tonumber (old_line )
400+
401+ if new_line_int == nil and old_line_int == nil then
402+ u .notify (" Could not get new or old line" , vim .log .levels .ERROR )
403+ return
404+ end
405+
406+ reviewer .jump (file_name , new_line_int , old_line_int , { is_undefined_type = is_undefined_type })
407+ M .refresh_view ()
377408end
378409
379410-- This function (settings.discussion_tree.jump_to_file) will jump to the file changed in a new tab
0 commit comments