@@ -89,7 +89,7 @@ M.initialize_discussions = function()
8989 M .refresh_diagnostics ()
9090 end )
9191 reviewer .set_callback_for_buf_read (function (args )
92- vim .api .nvim_buf_set_option ( args . buf , " modifiable" , false )
92+ vim .api .nvim_set_option_value ( " modifiable" , false , { buf = args . buf } )
9393 reviewer .set_keymaps (args .buf )
9494 reviewer .set_reviewer_autocommands (args .buf )
9595 end )
@@ -112,46 +112,67 @@ end
112112--- @param callback function ?
113113--- @param view_type " discussions" | " notes" Defines the view type to select (useful for overriding the default view type when jumping to discussion tree when it ' s closed).
114114M .open = function (callback , view_type )
115- view_type = view_type and view_type or state .settings .discussion_tree .default_view
115+ local original_window = vim .api .nvim_get_current_win () -- The window from which ther user called M.open
116+
117+ M .current_view_type = view_type and view_type or state .settings .discussion_tree .default_view
118+ state .DISCUSSION_DATA = u .ensure_table (state .DISCUSSION_DATA )
116119 state .DISCUSSION_DATA .discussions = u .ensure_table (state .DISCUSSION_DATA .discussions )
117120 state .DISCUSSION_DATA .unlinked_discussions = u .ensure_table (state .DISCUSSION_DATA .unlinked_discussions )
118121 state .DRAFT_NOTES = u .ensure_table (state .DRAFT_NOTES )
119122
120- -- Make buffers, get and set buffer numbers, set filetypes
123+ -- Make discussion split window and buffers, store buffer numbers
121124 local split , linked_bufnr , unlinked_bufnr = M .create_split_and_bufs ()
122125 M .split = split
123126 M .linked_bufnr = linked_bufnr
124127 M .unlinked_bufnr = unlinked_bufnr
128+ M .split_visible = true
129+ split :mount ()
125130
131+ -- Set window and buffer local options to discussion tree split after mounting the split
126132 for opt , val in pairs (state .settings .discussion_tree .winopts ) do
127133 vim .api .nvim_set_option_value (opt , val , { win = M .split .winid })
128134 end
129135
130136 vim .api .nvim_set_option_value (" filetype" , " gitlab" , { buf = M .linked_bufnr })
131137 vim .api .nvim_set_option_value (" filetype" , " gitlab" , { buf = M .unlinked_bufnr })
132138
133- M .split = split
134- M .split_visible = true
135- split :mount ()
139+ -- Set autocmds to clean up state when discussions buffers are deleted manually
140+ vim .api .nvim_create_autocmd (" BufWipeout" , {
141+ buffer = M .linked_bufnr ,
142+ callback = function ()
143+ M .linked_bufnr = nil
144+ end ,
145+ })
146+ vim .api .nvim_create_autocmd (" BufWipeout" , {
147+ buffer = M .unlinked_bufnr ,
148+ callback = function ()
149+ M .unlinked_bufnr = nil
150+ end ,
151+ })
136152
137- -- Initialize winbar module with data from buffers
138- winbar .start_timer ()
139- winbar .set_buffers (M .linked_bufnr , M .unlinked_bufnr )
140- winbar .switch_view_type (view_type )
153+ -- Set autocmd to clean up state when discussions split is closed manually
154+ vim .api .nvim_create_autocmd (" WinClosed" , {
155+ pattern = tostring (M .split .winid ),
156+ callback = M .close ,
157+ })
141158
142- local current_window = vim . api . nvim_get_current_win () -- Save user's current window in case they switched while content was loading
143- vim . api . nvim_set_current_win ( M . split . winid )
159+ -- Initialize winbar
160+ winbar . start_timer ( )
144161
145- common . switch_can_edit_bufs ( true , M . linked_bufnr , M . unlinked_bufnr )
146- M .rebuild_discussion_tree ( )
162+ -- Rebuild trees in order to set keymaps and make buffers protected
163+ M .switch_view_type ( M . current_view_type )
147164 M .rebuild_unlinked_discussion_tree ()
165+ M .rebuild_discussion_tree ()
148166
149- -- Set default buffer
150- local default_buffer = winbar .bufnr_map [view_type ]
151- vim .api .nvim_set_current_buf (default_buffer )
152- common .switch_can_edit_bufs (false , M .linked_bufnr , M .unlinked_bufnr )
167+ -- Focus the correct window
168+ local win_to_enter = not state .settings .discussion_tree .enter_on_open and original_window or M .split .winid
169+ if vim .api .nvim_win_is_valid (win_to_enter ) then
170+ vim .api .nvim_set_current_win (win_to_enter )
171+ end
172+
173+ -- Relooad data
174+ draft_notes .rebuild_view (false , true )
153175
154- vim .api .nvim_set_current_win (current_window )
155176 if type (callback ) == " function" then
156177 callback ()
157178 end
@@ -195,7 +216,7 @@ M.move_to_discussion_tree = function()
195216 end
196217 M .discussion_tree :render ()
197218 vim .api .nvim_set_current_win (M .split .winid )
198- winbar .switch_view_type (" discussions" )
219+ M .switch_view_type (" discussions" )
199220 vim .api .nvim_win_set_cursor (M .split .winid , { line_number , 0 })
200221 end
201222
@@ -441,6 +462,7 @@ M.rebuild_discussion_tree = function()
441462 end
442463
443464 local current_node = discussions_tree .get_node_at_cursor (M .discussion_tree , M .last_node_at_cursor )
465+ local current_cursor_column = vim .api .nvim_win_get_cursor (0 )[2 ]
444466
445467 local expanded_node_ids = M .gather_expanded_node_ids (M .discussion_tree )
446468 common .switch_can_edit_bufs (true , M .linked_bufnr , M .unlinked_bufnr )
@@ -463,7 +485,7 @@ M.rebuild_discussion_tree = function()
463485 tree_utils .open_node_by_id (discussion_tree , id )
464486 end
465487 discussion_tree :render ()
466- discussions_tree .restore_cursor_position (M .split .winid , discussion_tree , current_node )
488+ discussions_tree .restore_cursor_position (M .split .winid , discussion_tree , current_cursor_column , current_node , nil )
467489
468490 M .set_tree_keymaps (discussion_tree , M .linked_bufnr , false )
469491 M .discussion_tree = discussion_tree
@@ -479,6 +501,7 @@ M.rebuild_unlinked_discussion_tree = function()
479501 end
480502
481503 local current_node = discussions_tree .get_node_at_cursor (M .unlinked_discussion_tree , M .last_node_at_cursor )
504+ local current_cursor_column = vim .api .nvim_win_get_cursor (0 )[2 ]
482505
483506 local expanded_node_ids = M .gather_expanded_node_ids (M .unlinked_discussion_tree )
484507 common .switch_can_edit_bufs (true , M .linked_bufnr , M .unlinked_bufnr )
@@ -501,7 +524,7 @@ M.rebuild_unlinked_discussion_tree = function()
501524 tree_utils .open_node_by_id (unlinked_discussion_tree , id )
502525 end
503526 unlinked_discussion_tree :render ()
504- discussions_tree .restore_cursor_position (M .split .winid , unlinked_discussion_tree , current_node )
527+ discussions_tree .restore_cursor_position (M .split .winid , unlinked_discussion_tree , current_cursor_column , current_node )
505528
506529 M .set_tree_keymaps (unlinked_discussion_tree , M .unlinked_bufnr , true )
507530 M .unlinked_discussion_tree = unlinked_discussion_tree
@@ -681,7 +704,7 @@ M.set_tree_keymaps = function(tree, bufnr, unlinked)
681704
682705 if keymaps .discussion_tree .toggle_node then
683706 vim .keymap .set (" n" , keymaps .discussion_tree .toggle_node , function ()
684- tree_utils .toggle_node (tree )
707+ tree_utils .toggle_node (M . split . winid , tree )
685708 end , { buffer = bufnr , desc = " Toggle node" , nowait = keymaps .discussion_tree .toggle_node_nowait })
686709 end
687710
@@ -737,7 +760,7 @@ M.set_tree_keymaps = function(tree, bufnr, unlinked)
737760
738761 if keymaps .discussion_tree .switch_view then
739762 vim .keymap .set (" n" , keymaps .discussion_tree .switch_view , function ()
740- winbar .switch_view_type ()
763+ M .switch_view_type ()
741764 end , {
742765 buffer = bufnr ,
743766 desc = " Change view type between discussions and notes" ,
@@ -804,6 +827,21 @@ M.set_tree_keymaps = function(tree, bufnr, unlinked)
804827 emoji .init_popup (tree , bufnr )
805828end
806829
830+ --- Toggles the current view type (or sets it to `override`) and then updates the view.
831+ --- @param override ? " discussions" | " notes" The view type to select.
832+ M .switch_view_type = function (override )
833+ vim .api .nvim_set_option_value (" winfixbuf" , false , { win = M .split .winid })
834+ if override == " discussions" or M .current_view_type == " notes" then
835+ M .current_view_type = " discussions"
836+ vim .api .nvim_set_current_buf (M .linked_bufnr )
837+ elseif override == " notes" or M .current_view_type == " discussions" then
838+ M .current_view_type = " notes"
839+ vim .api .nvim_set_current_buf (M .unlinked_bufnr )
840+ end
841+ vim .api .nvim_set_option_value (" winfixbuf" , true , { win = M .split .winid })
842+ winbar .update_winbar ()
843+ end
844+
807845--- Toggle comments tree type between "simple" and "by_file_name"
808846M .toggle_tree_type = function ()
809847 if state .settings .discussion_tree .tree_type == " simple" then
0 commit comments