Repository navigation
Expand file tree
/
Copy pathcommon.lua
More file actions
365 lines (334 loc) · 12.1 KB
/
Copy pathcommon.lua
File metadata and controls
365 lines (334 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
-- This module contains code shared between at least two modules. This includes
-- actions common to multiple tree types, as well as general utility functions
-- that are specific to actions (like jumping to a file or opening a URL).
local List = require("gitlab.utils.list")
local u = require("gitlab.utils")
local reviewer = require("gitlab.reviewer")
local indicators_common = require("gitlab.indicators.common")
local state = require("gitlab.state")
local M = {}
---Build note header from note.
---@param note Note|DraftNote
---@return string
M.build_note_header = function(note)
if note.note then
return "@" .. state.USER.username .. " " .. state.settings.discussion_tree.draft
end
local time = state.settings.discussion_tree.relative_date and u.time_since(note.created_at)
or u.format_to_local(note.created_at, vim.fn.strftime("%z"))
return "@" .. note.author.username .. " " .. time
end
---Switch the options that control if buffers can be modified.
---@param bool boolean The value to set
---@param ... integer List of buffer numbers
M.switch_can_edit_bufs = function(bool, ...)
local bufnrs = { ... }
---@param v integer
for _, v in ipairs(bufnrs) do
u.switch_can_edit_buf(v, bool)
end
end
---Transform a string containing new line characters into a table of lines.
---TODO: Replace this function with direct calls of u.lines_into_table, as the extra
---line is not necessary!
---@param content string
---@return string[]
M.build_content = function(content)
local description_lines = u.lines_into_table(content)
table.insert(description_lines, "")
return description_lines
end
---Fill discussion tree buffers with warnings, if there are no linked or unlinked comments.
M.add_empty_titles = function()
local draft_notes = require("gitlab.actions.draft_notes")
local discussions = require("gitlab.actions.discussions")
local linked, unlinked, drafts =
List.new(u.ensure_table(state.DISCUSSION_DATA and state.DISCUSSION_DATA.discussions)),
List.new(u.ensure_table(state.DISCUSSION_DATA and state.DISCUSSION_DATA.unlinked_discussions)),
List.new(u.ensure_table(state.DRAFT_NOTES))
local position_drafts = drafts:filter(function(note)
return draft_notes.has_position(note)
end)
local non_positioned_drafts = drafts:filter(function(note)
return not draft_notes.has_position(note)
end)
local fields = {
{
bufnr = discussions.linked_bufnr,
count = #linked + #position_drafts,
title = "No Discussions for this MR",
},
{
bufnr = discussions.unlinked_bufnr,
count = #unlinked + #non_positioned_drafts,
title = "No Notes (Unlinked Discussions) for this MR",
},
}
for _, v in ipairs(fields) do
if v.bufnr ~= nil then
M.switch_can_edit_bufs(true, v.bufnr)
local ns_id = vim.api.nvim_create_namespace("GitlabNamespace")
vim.cmd("highlight default TitleHighlight guifg=#787878")
-- Set empty title if applicable
if v.count == 0 then
vim.api.nvim_buf_set_lines(v.bufnr, 0, 1, false, { v.title })
local linnr = 1
vim.api.nvim_buf_set_extmark(
v.bufnr,
ns_id,
linnr - 1,
0,
{ end_row = linnr - 1, end_col = string.len(v.title), hl_group = "TitleHighlight" }
)
end
M.switch_can_edit_bufs(false, v.bufnr)
end
end
end
---Return the URL for the current note.
---@param tree NuiTree
---@return string?
M.get_url = function(tree)
local current_node = tree:get_node()
local note_node = M.get_note_node(tree, current_node)
if note_node == nil then
return
end
local url = note_node.url
if url == nil then
u.notify("Could not get URL of note", vim.log.levels.ERROR)
return
end
return url
end
---Open the current note in the browser.
---@param tree NuiTree
M.open_in_browser = function(tree)
local url = M.get_url(tree)
if url ~= nil then
u.open_in_browser(url)
end
end
---Copy the current note's URL to the clipboard.
---@param tree NuiTree
M.copy_node_url = function(tree)
local url = M.get_url(tree)
if url ~= nil then
vim.fn.setreg("+", url)
u.notify("Copied '" .. url .. "' to clipboard", vim.log.levels.INFO)
end
end
---For developers!
---@param tree NuiTree
M.print_node = function(tree)
local current_node = tree:get_node()
vim.print(current_node)
end
---Return true if type of node is note or note body (as opposed to e.g. "file_name").
---@param node? NuiTree.Node
---@return boolean
M.is_node_note = function(node)
if node and (node.type == "note_body" or node.type == "note") then
return true
else
return false
end
end
---Get root node at the current cursor position.
---@param tree NuiTree
---@param node? NuiTree.Node
---@return NuiTree.Node?
M.get_root_node = function(tree, node)
if not node then
return nil
end
if node.type == "note_body" or node.type == "note" and not node.is_root then
local parent_id = node:get_parent_id()
-- `tree:get_node(nil)` falls back to the node under the cursor, which can be this very
-- node again. Because this is a tail call recursion, it would loop forever instead of overflowing.
if parent_id == nil then
return nil
end
return M.get_root_node(tree, tree:get_node(parent_id))
elseif node.is_root then
return node
end
end
---Get the main note node at the current cursor position.
---@param tree NuiTree
---@param node? NuiTree.Node
---@return NuiTree.Node?
M.get_note_node = function(tree, node)
if not node then
return nil
end
if node.type == "note_body" then
local parent_id = node:get_parent_id()
if parent_id == nil then
return node
end
return M.get_note_node(tree, tree:get_node(parent_id))
elseif node.type == "note" then
return node
end
end
---Return the line where the note is positioned in the new SHA.
---If the line is not in the new SHA, return nil for a single-line comment.
---TODO: For ranged comments this function may return 0 for removed lines. Shouldn't it
---return nil instead?
---@param node NuiTree.Node
---@return integer?
local function get_new_linenr(node)
---@type GitlabLineRange?
local range = node.range
if range == nil then
return node.new_line
end
local _, new_start_line = indicators_common.parse_line_code(range.start.line_code)
return new_start_line
end
---Return the line where the note is positioned in the old SHA.
---If the line is not in the old SHA, return nil for a single-line comment.
---TODO: For ranged comments this function may return 0 for added lines. Shouldn't it
---return nil instead?
---@param node NuiTree.Node
---@return integer?
local function get_old_linenr(node)
---@type GitlabLineRange?
local range = node.range
if range == nil then
return node.old_line
end
local old_start_line, _ = indicators_common.parse_line_code(range.start.line_code)
return old_start_line
end
---Get the line number of a single-line comment.
---TODO: Consider if this can be removed if all comments would be created as "multi-line".
---See lua/gitlab/reviewer/location.lua:74.
---@param id string|integer
---@return integer? line_number
---@return boolean is_new_sha True if line number refers to NEW SHA
M.get_line_number = function(id)
---@type (Discussion|DraftNote)?
local d_or_n
d_or_n = List.new(state.DISCUSSION_DATA and state.DISCUSSION_DATA.discussions or {}):find(function(d)
return d.id == id
end) or List.new(state.DRAFT_NOTES or {}):find(function(d)
return d.id == id
end)
if d_or_n == nil then
return nil, true
end
local first_note = indicators_common.get_first_note(d_or_n)
local is_new_sha = indicators_common.is_new_sha(d_or_n)
return ((is_new_sha and first_note.position.new_line or first_note.position.old_line) or 1), is_new_sha
end
---Return the number of lines between the start and end of a range on one side of the diff.
---Line codes are preferred because they hold both line numbers, whereas the range's
---`old_line`/`new_line` are 0 on the side a line doesn't exist on.
---@param start_from_code? integer The start line parsed from the line code
---@param end_from_code? integer The end line parsed from the line code
---@param start_line? integer The start line from the range
---@param end_line? integer The end line from the range
---@return integer
local function get_range_length(start_from_code, end_from_code, start_line, end_line)
if start_from_code and end_from_code then
return end_from_code - start_from_code
end
if (start_line or 0) > 0 and (end_line or 0) > 0 then
return end_line - start_line
end
return 0
end
---Return start and end line numbers for the note range, and whether comment is on "NEW SHA".
---The range is calculated from the line codes, or from the range's line numbers if GitLab
---returns empty line codes, but the position itself is based on either the `new_line` or `old_line`.
---@param old_line? integer The line number in the OLD version
---@param new_line? integer The line number in the NEW version
---@param line_range LineRange The range of the note
---@return integer start_line
---@return integer end_line
---@return boolean is_new_sha True if line range refers to NEW SHA
M.get_line_numbers_for_range = function(old_line, new_line, line_range)
local start, end_ = line_range.start, line_range["end"]
local old_start_line, new_start_line = indicators_common.parse_line_code(start.line_code)
local old_end_line, new_end_line = indicators_common.parse_line_code(end_.line_code)
if old_line ~= nil and old_start_line ~= 0 then
local range = get_range_length(old_start_line, old_end_line, start.old_line, end_.old_line)
return (old_line - range), old_line, false
elseif new_line ~= nil then
local range = get_range_length(new_start_line, new_end_line, start.new_line, end_.new_line)
-- Force start_line to be greater than 0
-- TODO: use `math.max(new_line - range, 1)` instead
local start_line = (new_line - range > 0) and (new_line - range) or 1
return start_line, new_line, true
else
u.notify("Error getting new or old line for range", vim.log.levels.ERROR)
return 1, 1, false
end
end
---Return line number for jumping to reviewer.
---@param root_node NuiTree.Node
---@return integer? line_number
---@return boolean is_new_sha True if line number refers to NEW SHA
M.get_line_number_from_node = function(root_node)
if root_node.range then
local line_number, _, is_new_sha =
M.get_line_numbers_for_range(root_node.old_line, root_node.new_line, root_node.range)
return line_number, is_new_sha
else
return M.get_line_number(root_node.id)
end
end
---Move the cursor to the reviewer's location associated with the note.
---@param tree NuiTree
M.jump_to_reviewer = function(tree)
local node = tree:get_node()
local root_node = M.get_root_node(tree, node)
if root_node == nil then
u.notify("Could not get discussion node", vim.log.levels.ERROR)
return
end
local line_number, is_new_sha = M.get_line_number_from_node(root_node)
if line_number == nil then
u.notify("Could not get line number", vim.log.levels.ERROR)
return
end
reviewer.jump(root_node.file_name, root_node.old_file_name, line_number, is_new_sha)
end
---Jump to the file in a new tab.
---@param tree NuiTree
M.jump_to_file = function(tree)
local node = tree:get_node()
local root_node = M.get_root_node(tree, node)
if root_node == nil then
u.notify("Could not get discussion node", vim.log.levels.ERROR)
return
end
if root_node.file_name == nil then
u.notify("This comment was not left on a particular location", vim.log.levels.WARN)
return
end
if vim.fn.filereadable(root_node.file_name) == 0 then
u.notify(
string.format("The file %s for which the comment was made doesn't exist in HEAD.", root_node.file_name),
vim.log.levels.WARN
)
return
end
vim.cmd.tabnew()
local line_number = get_new_linenr(root_node) or get_old_linenr(root_node)
if line_number == nil or line_number == 0 then
line_number = 1
end
local bufnr = vim.fn.bufnr(root_node.file_name)
if bufnr ~= -1 then
vim.cmd("buffer " .. bufnr)
vim.api.nvim_win_set_cursor(0, { line_number, 0 })
return
end
-- If buffer is not already open, open it
vim.cmd("edit " .. root_node.file_name)
vim.api.nvim_win_set_cursor(0, { line_number, 0 })
end
return M