@@ -15,7 +15,6 @@ local reviewer = require("gitlab.reviewer")
15
15
local Location = require (" gitlab.reviewer.location" )
16
16
17
17
local M = {
18
- current_win = nil ,
19
18
start_line = nil ,
20
19
end_line = nil ,
21
20
draft_popup = nil ,
@@ -25,10 +24,9 @@ local M = {
25
24
--- Fires the API that sends the comment data to the Go server, called when you "confirm" creation
26
25
--- via the M.settings.keymaps.popup.perform_action keybinding
27
26
--- @param text string comment text
28
- --- @param visual_range LineRange | nil range of visual selection or nil
29
27
--- @param unlinked boolean if true , the comment is not linked to a line
30
28
--- @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 )
32
30
if text == nil then
33
31
u .notify (" Reviewer did not provide text of change" , vim .log .levels .ERROR )
34
32
return
@@ -75,30 +73,16 @@ local confirm_create_comment = function(text, visual_range, unlinked, discussion
75
73
return
76
74
end
77
75
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
-
92
76
local revision = state .MR_REVISIONS [1 ]
93
77
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 ,
96
80
base_commit_sha = revision .base_commit_sha ,
97
81
start_commit_sha = revision .start_commit_sha ,
98
82
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 ,
102
86
}
103
87
104
88
-- Creating a new comment (linked to specific changes)
@@ -148,10 +132,10 @@ M.confirm_edit_comment = function(discussion_id, note_id, unlinked)
148
132
end
149
133
150
134
--- @class LayoutOpts
151
- --- @field ranged boolean
152
135
--- @field unlinked boolean
153
136
--- @field discussion_id string | nil
154
137
--- @field reply boolean | nil
138
+ --- @field file_name string | nil
155
139
156
140
--- This function sets up the layout and popups needed to create a comment, note and
157
141
--- multi-line comment. It also sets up the basic keybindings for switching between
@@ -163,21 +147,24 @@ M.create_comment_layout = function(opts)
163
147
local title
164
148
local user_settings
165
149
if opts .discussion_id ~= nil then
166
- title = " Reply"
150
+ title = " Reply" .. ( opts . file_name and string.format ( " [%s] " , opts . file_name ) or " " )
167
151
user_settings = popup_settings .reply
168
152
elseif opts .unlinked then
169
153
title = " Note"
170
154
user_settings = popup_settings .note
171
155
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 )
173
161
user_settings = popup_settings .comment
174
162
end
175
163
local settings = u .merge (popup_settings , user_settings or {})
176
164
177
- M . current_win = vim .api .nvim_get_current_win ()
165
+ local current_win = vim .api .nvim_get_current_win ()
178
166
M .comment_popup = Popup (popup .create_popup_state (title , settings ))
179
167
M .draft_popup = Popup (popup .create_box_popup_state (" Draft" , false , settings ))
180
- M .start_line , M .end_line = u .get_visual_selection_boundaries ()
181
168
182
169
local internal_layout = Layout .Box ({
183
170
Layout .Box (M .comment_popup , { grow = 1 }),
@@ -194,22 +181,21 @@ M.create_comment_layout = function(opts)
194
181
}, internal_layout )
195
182
196
183
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 )
198
185
199
- local range = opts .ranged and { start_line = M .start_line , end_line = M .end_line } or nil
200
186
local unlinked = opts .unlinked or false
201
187
202
188
--- Keybinding for focus on draft section
203
189
popup .set_popup_keymaps (M .draft_popup , function ()
204
190
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 )
207
193
end , miscellaneous .toggle_bool , popup .non_editable_popup_opts )
208
194
209
195
--- Keybinding for focus on text section
210
196
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 )
213
199
end , miscellaneous .attach_file , popup .editable_popup_opts )
214
200
215
201
vim .schedule (function ()
@@ -223,44 +209,43 @@ end
223
209
--- This function will open a comment popup in order to create a comment on the changed/updated
224
210
--- line in the current MR
225
211
M .create_comment = function ()
212
+ M .location = Location .new ()
226
213
if not M .can_create_comment (false ) then
227
214
return
228
215
end
229
216
230
- local layout = M .create_comment_layout ({ ranged = false , unlinked = false })
217
+ local layout = M .create_comment_layout ({ unlinked = false })
231
218
layout :mount ()
232
219
end
233
220
234
221
--- This function will open a multi-line comment popup in order to create a multi-line comment
235
222
--- on the changed/updated line in the current MR
236
223
M .create_multiline_comment = function ()
224
+ M .location = Location .new ()
237
225
if not M .can_create_comment (true ) then
238
226
u .press_escape ()
239
227
return
240
228
end
241
229
242
- local layout = M .create_comment_layout ({ ranged = true , unlinked = false })
230
+ local layout = M .create_comment_layout ({ unlinked = false })
243
231
layout :mount ()
244
232
end
245
233
246
234
--- This function will open a a popup to create a "note" (e.g. unlinked comment)
247
235
--- on the changed/updated line in the current MR
248
236
M .create_note = function ()
249
- local layout = M .create_comment_layout ({ ranged = false , unlinked = true })
237
+ local layout = M .create_comment_layout ({ unlinked = true })
250
238
layout :mount ()
251
239
end
252
240
253
241
--- Given the current visually selected area of text, builds text to fill in the
254
242
--- comment popup with a suggested change
255
243
--- @return LineRange | nil
256
- --- @return integer
257
244
local build_suggestion = function ()
258
245
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
262
247
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 )
264
249
265
250
for _ , line in ipairs (selected_lines ) do
266
251
if string.match (line , " ^```%S*$" ) then
@@ -270,36 +255,37 @@ local build_suggestion = function()
270
255
end
271
256
272
257
local suggestion_start
273
- if M .start_line == current_line then
258
+ if M .location . visual_range . start_line == current_line then
274
259
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
276
261
suggestion_start = backticks .. " suggestion:-" .. range_length .. " +0"
277
262
else
278
263
--- This should never happen afaik
279
264
u .notify (" Unexpected suggestion position" , vim .log .levels .ERROR )
280
- return nil , 0
265
+ return nil
281
266
end
282
267
suggestion_start = suggestion_start
283
268
local suggestion_lines = {}
284
269
table.insert (suggestion_lines , suggestion_start )
285
270
vim .list_extend (suggestion_lines , selected_lines )
286
271
table.insert (suggestion_lines , backticks )
287
272
288
- return suggestion_lines , range_length
273
+ return suggestion_lines
289
274
end
290
275
291
276
--- This function will open a a popup to create a suggestion comment
292
277
--- on the changed/updated line in the current MR
293
278
--- See: https://docs.gitlab.com/ee/user/project/merge_requests/reviews/suggestions.html
294
279
M .create_comment_suggestion = function ()
280
+ M .location = Location .new ()
295
281
if not M .can_create_comment (true ) then
296
282
u .press_escape ()
297
283
return
298
284
end
299
285
300
- local suggestion_lines , range_length = build_suggestion ()
286
+ local suggestion_lines = build_suggestion ()
301
287
302
- local layout = M .create_comment_layout ({ ranged = range_length > 0 , unlinked = false })
288
+ local layout = M .create_comment_layout ({ unlinked = false })
303
289
layout :mount ()
304
290
305
291
vim .schedule (function ()
@@ -368,6 +354,11 @@ M.can_create_comment = function(must_be_visual)
368
354
return false
369
355
end
370
356
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
+
371
362
return true
372
363
end
373
364
0 commit comments