44local List = require (" gitlab.utils.list" )
55local u = require (" gitlab.utils" )
66local reviewer = require (" gitlab.reviewer" )
7+ local indicators_common = require (" gitlab.indicators.common" )
78local common_indicators = require (" gitlab.indicators.common" )
89local state = require (" gitlab.state" )
910local M = {}
176177
177178--- Takes a node and returns the line where the note is positioned in the new SHA. If
178179--- the line is not in the new SHA, returns nil
179- --- @param node any
180+ --- @param node NuiTree.Node
180181--- @return number | nil
181182local function get_new_line (node )
182183 --- @type GitlabLineRange | nil
191192
192193--- Takes a node and returns the line where the note is positioned in the old SHA. If
193194--- the line is not in the old SHA, returns nil
194- --- @param node any
195+ --- @param node NuiTree.Node
195196--- @return number | nil
196197local function get_old_line (node )
197198 --- @type GitlabLineRange | nil
@@ -204,6 +205,36 @@ local function get_old_line(node)
204205 return start_old_line
205206end
206207
208+ --- @param id string | integer
209+ --- @return integer | nil
210+ M .get_line_number = function (id )
211+ --- @type Discussion | DraftNote | nil
212+ local d_or_n
213+ d_or_n = List .new (state .DISCUSSION_DATA .discussions or {}):find (function (d )
214+ return d .id == id
215+ end ) or List .new (state .DRAFT_NOTES or {}):find (function (d )
216+ return d .id == id
217+ end )
218+
219+ if d_or_n == nil then
220+ return
221+ end
222+
223+ local first_note = indicators_common .get_first_note (d_or_n )
224+ return (indicators_common .is_new_sha (d_or_n ) and first_note .position .new_line or first_note .position .old_line ) or 1
225+ end
226+
227+ --- @param root_node NuiTree.Node
228+ --- @return integer | nil
229+ M .get_line_number_from_node = function (root_node )
230+ if root_node .range then
231+ local start_old_line , start_new_line = common_indicators .parse_line_code (root_node .range .start .line_code )
232+ return root_node .old_line and start_old_line or start_new_line
233+ else
234+ return M .get_line_number (root_node .id )
235+ end
236+ end
237+
207238-- This function (settings.discussion_tree.jump_to_reviewer) will jump the cursor to the reviewer's location associated with the note. The implementation depends on the reviewer
208239M .jump_to_reviewer = function (tree , callback )
209240 local node = tree :get_node ()
@@ -212,10 +243,10 @@ M.jump_to_reviewer = function(tree, callback)
212243 u .notify (" Could not get discussion node" , vim .log .levels .ERROR )
213244 return
214245 end
215- local line_number = (root_node . new_line or root_node . old_line or 1 )
216- if root_node . range then
217- local start_old_line , start_new_line = common_indicators . parse_line_code ( root_node . range . start . line_code )
218- line_number = root_node . old_line and start_old_line or start_new_line
246+ local line_number = M . get_line_number_from_node (root_node )
247+ if line_number == nil then
248+ u . notify ( " Could not get line number " , vim . log . levels . ERROR )
249+ return
219250 end
220251 reviewer .jump (root_node .file_name , line_number , root_node .old_line == nil )
221252 callback ()
0 commit comments