@@ -15,7 +15,6 @@ local reviewer = require("gitlab.reviewer")
1515local Location = require (" gitlab.reviewer.location" )
1616
1717local M = {
18- current_win = nil ,
1918 start_line = nil ,
2019 end_line = nil ,
2120 draft_popup = nil ,
@@ -25,10 +24,9 @@ local M = {
2524--- Fires the API that sends the comment data to the Go server, called when you "confirm" creation
2625--- via the M.settings.keymaps.popup.perform_action keybinding
2726--- @param text string comment text
28- --- @param visual_range LineRange | nil range of visual selection or nil
2927--- @param unlinked boolean if true , the comment is not linked to a line
3028--- @param discussion_id string | nil The ID of the discussion to which the reply is responding , nil if not a reply
31- local confirm_create_comment = function (text , visual_range , unlinked , discussion_id )
29+ local confirm_create_comment = function (text , unlinked , discussion_id )
3230 if text == nil then
3331 u .notify (" Reviewer did not provide text of change" , vim .log .levels .ERROR )
3432 return
@@ -75,30 +73,16 @@ local confirm_create_comment = function(text, visual_range, unlinked, discussion
7573 return
7674 end
7775
78- local reviewer_data = reviewer .get_reviewer_data ()
79- if reviewer_data == nil then
80- u .notify (" Error getting reviewer data" , vim .log .levels .ERROR )
81- return
82- end
83-
84- local location = Location .new (reviewer_data , visual_range )
85- location :build_location_data ()
86- local location_data = location .location_data
87- if location_data == nil then
88- u .notify (" Error getting location information" , vim .log .levels .ERROR )
89- return
90- end
91-
9276 local revision = state .MR_REVISIONS [1 ]
9377 local position_data = {
94- file_name = reviewer_data .file_name ,
95- old_file_name = reviewer_data .old_file_name ,
78+ file_name = M . location . reviewer_data .file_name ,
79+ old_file_name = M . location . reviewer_data .old_file_name ,
9680 base_commit_sha = revision .base_commit_sha ,
9781 start_commit_sha = revision .start_commit_sha ,
9882 head_commit_sha = revision .head_commit_sha ,
99- old_line = location_data .old_line ,
100- new_line = location_data .new_line ,
101- line_range = location_data .line_range ,
83+ old_line = M . location . location_data .old_line ,
84+ new_line = M . location . location_data .new_line ,
85+ line_range = M . location . location_data .line_range ,
10286 }
10387
10488 -- Creating a new comment (linked to specific changes)
@@ -148,10 +132,10 @@ M.confirm_edit_comment = function(discussion_id, note_id, unlinked)
148132end
149133
150134--- @class LayoutOpts
151- --- @field ranged boolean
152135--- @field unlinked boolean
153136--- @field discussion_id string | nil
154137--- @field reply boolean | nil
138+ --- @field file_name string | nil
155139
156140--- This function sets up the layout and popups needed to create a comment, note and
157141--- multi-line comment. It also sets up the basic keybindings for switching between
@@ -163,21 +147,24 @@ M.create_comment_layout = function(opts)
163147 local title
164148 local user_settings
165149 if opts .discussion_id ~= nil then
166- title = " Reply"
150+ title = " Reply" .. ( opts . file_name and string.format ( " [%s] " , opts . file_name ) or " " )
167151 user_settings = popup_settings .reply
168152 elseif opts .unlinked then
169153 title = " Note"
170154 user_settings = popup_settings .note
171155 else
172- title = " Comment"
156+ -- TODO: investigate why `old_file_name` is in fact the new name for renamed files!
157+ local file_name = M .location .reviewer_data .old_file_name ~= " " and M .location .reviewer_data .old_file_name
158+ or M .location .reviewer_data .file_name
159+ title =
160+ popup .create_title (" Comment" , file_name , M .location .visual_range .start_line , M .location .visual_range .end_line )
173161 user_settings = popup_settings .comment
174162 end
175163 local settings = u .merge (popup_settings , user_settings or {})
176164
177- M . current_win = vim .api .nvim_get_current_win ()
165+ local current_win = vim .api .nvim_get_current_win ()
178166 M .comment_popup = Popup (popup .create_popup_state (title , settings ))
179167 M .draft_popup = Popup (popup .create_box_popup_state (" Draft" , false , settings ))
180- M .start_line , M .end_line = u .get_visual_selection_boundaries ()
181168
182169 local internal_layout = Layout .Box ({
183170 Layout .Box (M .comment_popup , { grow = 1 }),
@@ -194,22 +181,21 @@ M.create_comment_layout = function(opts)
194181 }, internal_layout )
195182
196183 popup .set_cycle_popups_keymaps ({ M .comment_popup , M .draft_popup })
197- popup .set_up_autocommands (M .comment_popup , layout , M . current_win )
184+ popup .set_up_autocommands (M .comment_popup , layout , current_win )
198185
199- local range = opts .ranged and { start_line = M .start_line , end_line = M .end_line } or nil
200186 local unlinked = opts .unlinked or false
201187
202188 --- Keybinding for focus on draft section
203189 popup .set_popup_keymaps (M .draft_popup , function ()
204190 local text = u .get_buffer_text (M .comment_popup .bufnr )
205- confirm_create_comment (text , range , unlinked , opts .discussion_id )
206- vim .api .nvim_set_current_win (M . current_win )
191+ confirm_create_comment (text , unlinked , opts .discussion_id )
192+ vim .api .nvim_set_current_win (current_win )
207193 end , miscellaneous .toggle_bool , popup .non_editable_popup_opts )
208194
209195 --- Keybinding for focus on text section
210196 popup .set_popup_keymaps (M .comment_popup , function (text )
211- confirm_create_comment (text , range , unlinked , opts .discussion_id )
212- vim .api .nvim_set_current_win (M . current_win )
197+ confirm_create_comment (text , unlinked , opts .discussion_id )
198+ vim .api .nvim_set_current_win (current_win )
213199 end , miscellaneous .attach_file , popup .editable_popup_opts )
214200
215201 vim .schedule (function ()
@@ -223,44 +209,43 @@ end
223209--- This function will open a comment popup in order to create a comment on the changed/updated
224210--- line in the current MR
225211M .create_comment = function ()
212+ M .location = Location .new ()
226213 if not M .can_create_comment (false ) then
227214 return
228215 end
229216
230- local layout = M .create_comment_layout ({ ranged = false , unlinked = false })
217+ local layout = M .create_comment_layout ({ unlinked = false })
231218 layout :mount ()
232219end
233220
234221--- This function will open a multi-line comment popup in order to create a multi-line comment
235222--- on the changed/updated line in the current MR
236223M .create_multiline_comment = function ()
224+ M .location = Location .new ()
237225 if not M .can_create_comment (true ) then
238226 u .press_escape ()
239227 return
240228 end
241229
242- local layout = M .create_comment_layout ({ ranged = true , unlinked = false })
230+ local layout = M .create_comment_layout ({ unlinked = false })
243231 layout :mount ()
244232end
245233
246234--- This function will open a a popup to create a "note" (e.g. unlinked comment)
247235--- on the changed/updated line in the current MR
248236M .create_note = function ()
249- local layout = M .create_comment_layout ({ ranged = false , unlinked = true })
237+ local layout = M .create_comment_layout ({ unlinked = true })
250238 layout :mount ()
251239end
252240
253241--- Given the current visually selected area of text, builds text to fill in the
254242--- comment popup with a suggested change
255243--- @return LineRange | nil
256- --- @return integer
257244local build_suggestion = function ()
258245 local current_line = vim .api .nvim_win_get_cursor (0 )[1 ]
259- M .start_line , M .end_line = u .get_visual_selection_boundaries ()
260-
261- local range_length = M .end_line - M .start_line
246+ local range_length = M .location .visual_range .end_line - M .location .visual_range .start_line
262247 local backticks = " ```"
263- local selected_lines = u .get_lines (M .start_line , M .end_line )
248+ local selected_lines = u .get_lines (M .location . visual_range . start_line , M . location . visual_range .end_line )
264249
265250 for _ , line in ipairs (selected_lines ) do
266251 if string.match (line , " ^```%S*$" ) then
@@ -270,36 +255,37 @@ local build_suggestion = function()
270255 end
271256
272257 local suggestion_start
273- if M .start_line == current_line then
258+ if M .location . visual_range . start_line == current_line then
274259 suggestion_start = backticks .. " suggestion:-0+" .. range_length
275- elseif M .end_line == current_line then
260+ elseif M .location . visual_range . end_line == current_line then
276261 suggestion_start = backticks .. " suggestion:-" .. range_length .. " +0"
277262 else
278263 --- This should never happen afaik
279264 u .notify (" Unexpected suggestion position" , vim .log .levels .ERROR )
280- return nil , 0
265+ return nil
281266 end
282267 suggestion_start = suggestion_start
283268 local suggestion_lines = {}
284269 table.insert (suggestion_lines , suggestion_start )
285270 vim .list_extend (suggestion_lines , selected_lines )
286271 table.insert (suggestion_lines , backticks )
287272
288- return suggestion_lines , range_length
273+ return suggestion_lines
289274end
290275
291276--- This function will open a a popup to create a suggestion comment
292277--- on the changed/updated line in the current MR
293278--- See: https://docs.gitlab.com/ee/user/project/merge_requests/reviews/suggestions.html
294279M .create_comment_suggestion = function ()
280+ M .location = Location .new ()
295281 if not M .can_create_comment (true ) then
296282 u .press_escape ()
297283 return
298284 end
299285
300- local suggestion_lines , range_length = build_suggestion ()
286+ local suggestion_lines = build_suggestion ()
301287
302- local layout = M .create_comment_layout ({ ranged = range_length > 0 , unlinked = false })
288+ local layout = M .create_comment_layout ({ unlinked = false })
303289 layout :mount ()
304290
305291 vim .schedule (function ()
@@ -368,6 +354,11 @@ M.can_create_comment = function(must_be_visual)
368354 return false
369355 end
370356
357+ if M .location == nil or M .location .location_data == nil then
358+ u .notify (" Error getting location information" , vim .log .levels .ERROR )
359+ return false
360+ end
361+
371362 return true
372363end
373364
0 commit comments