Feat: Sort Discussions by File Name (#102)
This MR adds the ability to sort discussions by file name, rather than just by date. This is an optional configuration that can be passed in on startup. The MR also introduces a test suite for the Lua code that runs through Neovim, so that the plugin can be fully tested with required dependencies and APIs. Major props to @johnybx for the hard work on this change!
This commit is contained in:
66
lua/gitlab/actions/discussions/annotations.lua
Normal file
66
lua/gitlab/actions/discussions/annotations.lua
Normal file
@@ -0,0 +1,66 @@
|
||||
---@meta diagnostics
|
||||
|
||||
---@class Author
|
||||
---@field id integer
|
||||
---@field username string
|
||||
---@field email string
|
||||
---@field name string
|
||||
---@field state string
|
||||
---@field avatar_url string
|
||||
---@field web_url string
|
||||
|
||||
---@class LinePosition
|
||||
---@field line_code string
|
||||
---@field type string
|
||||
|
||||
---@class GitlabLineRange
|
||||
---@field start LinePosition
|
||||
---@field end LinePosition
|
||||
|
||||
---@class NotePosition
|
||||
---@field base_sha string
|
||||
---@field start_sha string
|
||||
---@field head_sha string
|
||||
---@field position_type string
|
||||
---@field new_path string?
|
||||
---@field new_line integer?
|
||||
---@field old_path string?
|
||||
---@field old_line integer?
|
||||
---@field line_range GitlabLineRange?
|
||||
|
||||
---@class Note
|
||||
---@field id integer
|
||||
---@field type string
|
||||
---@field body string
|
||||
---@field attachment string
|
||||
---@field title string
|
||||
---@field file_name string
|
||||
---@field author Author
|
||||
---@field system boolean
|
||||
---@field expires_at string?
|
||||
---@field updated_at string?
|
||||
---@field created_at string?
|
||||
---@field noteable_id integer
|
||||
---@field noteable_type string
|
||||
---@field commit_id string
|
||||
---@field position NotePosition
|
||||
---@field resolvable boolean
|
||||
---@field resolved boolean
|
||||
---@field resolved_by Author
|
||||
---@field resolved_at string?
|
||||
---@field noteable_iid integer
|
||||
|
||||
---@class UnlinkedNote: Note
|
||||
---@field position nil
|
||||
|
||||
---@class Discussion
|
||||
---@field id string
|
||||
---@field individual_note boolean
|
||||
---@field notes Note[]
|
||||
|
||||
---@class UnlinkedDiscussion: Discussion
|
||||
---@field notes UnlinkedNote[]
|
||||
|
||||
---@class DiscussionData
|
||||
---@field discussions Discussion[]
|
||||
---@field unlinked_discussions UnlinkedDiscussion[]
|
||||
@@ -4,12 +4,14 @@
|
||||
local Split = require("nui.split")
|
||||
local Popup = require("nui.popup")
|
||||
local NuiTree = require("nui.tree")
|
||||
local NuiLine = require("nui.line")
|
||||
local Layout = require("nui.layout")
|
||||
local job = require("gitlab.job")
|
||||
local u = require("gitlab.utils")
|
||||
local state = require("gitlab.state")
|
||||
local reviewer = require("gitlab.reviewer")
|
||||
local miscellaneous = require("gitlab.actions.miscellaneous")
|
||||
local discussions_tree = require("gitlab.actions.discussions.tree")
|
||||
|
||||
local edit_popup = Popup(u.create_popup_state("Edit Comment", "80%", "80%"))
|
||||
local reply_popup = Popup(u.create_popup_state("Reply", "80%", "80%"))
|
||||
@@ -32,79 +34,16 @@ local M = {
|
||||
discussion_tree = nil,
|
||||
}
|
||||
|
||||
---@class Author
|
||||
---@field id integer
|
||||
---@field username string
|
||||
---@field email string
|
||||
---@field name string
|
||||
---@field state string
|
||||
---@field avatar_url string
|
||||
---@field web_url string
|
||||
|
||||
---@class LinePosition
|
||||
---@field line_code string
|
||||
---@field type string
|
||||
|
||||
---@class GitlabLineRange
|
||||
---@field start LinePosition
|
||||
---@field end LinePosition
|
||||
|
||||
---@class NotePosition
|
||||
---@field base_sha string
|
||||
---@field start_sha string
|
||||
---@field head_sha string
|
||||
---@field position_type string
|
||||
---@field new_path string?
|
||||
---@field new_line integer?
|
||||
---@field old_path string?
|
||||
---@field old_line integer?
|
||||
---@field line_range GitlabLineRange?
|
||||
|
||||
---@class Note
|
||||
---@field id integer
|
||||
---@field type string
|
||||
---@field body string
|
||||
---@field attachment string
|
||||
---@field title string
|
||||
---@field file_name string
|
||||
---@field author Author
|
||||
---@field system boolean
|
||||
---@field expires_at string?
|
||||
---@field updated_at string?
|
||||
---@field created_at string?
|
||||
---@field noteable_id integer
|
||||
---@field noteable_type string
|
||||
---@field commit_id string
|
||||
---@field position NotePosition
|
||||
---@field resolvable boolean
|
||||
---@field resolved boolean
|
||||
---@field resolved_by Author
|
||||
---@field resolved_at string?
|
||||
---@field noteable_iid integer
|
||||
|
||||
---@class UnlinkedNote: Note
|
||||
---@field position nil
|
||||
|
||||
---@class Discussion
|
||||
---@field id string
|
||||
---@field individual_note boolean
|
||||
---@field notes Note[]
|
||||
|
||||
---@class UnlinkedDiscussion: Discussion
|
||||
---@field notes UnlinkedNote[]
|
||||
|
||||
---@class DiscussionData
|
||||
---@field discussions Discussion[]
|
||||
---@field unlinked_discussions UnlinkedDiscussion[]
|
||||
|
||||
---Load the discussion data, storage them in M.discussions and M.unlinked_discussions and call
|
||||
---callback with data
|
||||
---@param callback fun(data: DiscussionData): nil
|
||||
---@param callback (fun(data: DiscussionData): nil)?
|
||||
M.load_discussions = function(callback)
|
||||
job.run_job("/discussions/list", "POST", { blacklist = state.settings.discussion_tree.blacklist }, function(data)
|
||||
M.discussions = data.discussions
|
||||
M.unlinked_discussions = data.unlinked_discussions
|
||||
callback(data)
|
||||
if type(callback) == "function" then
|
||||
callback(data)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
@@ -145,7 +84,8 @@ M.filter_discussions_for_signs_and_diagnostics = function()
|
||||
--Skip discussions from old revisions
|
||||
and not (
|
||||
state.settings.discussion_sign_and_diagnostic.skip_old_revision_discussion
|
||||
and first_note.position.base_sha ~= state.MR_REVISIONS[1].base_sha
|
||||
and u.from_iso_format_date_to_timestamp(first_note.created_at)
|
||||
<= u.from_iso_format_date_to_timestamp(state.MR_REVISIONS[1].created_at)
|
||||
)
|
||||
then
|
||||
table.insert(discussions, discussion)
|
||||
@@ -253,6 +193,13 @@ M.refresh_signs = function()
|
||||
reviewer.place_sign(new_signs, "new")
|
||||
end
|
||||
|
||||
---Build note header from note.
|
||||
---@param note Note
|
||||
---@return string
|
||||
M.build_note_header = function(note)
|
||||
return "@" .. note.author.username .. " " .. u.time_since(note.created_at)
|
||||
end
|
||||
|
||||
---Refresh the diagnostics for the currently reviewed file
|
||||
M.refresh_diagnostics = function()
|
||||
-- Keep in mind that diagnostic line numbers use 0-based indexing while line numbers use
|
||||
@@ -537,6 +484,7 @@ M.send_reply = function(tree, discussion_id)
|
||||
job.run_job("/reply", "POST", body, function(data)
|
||||
u.notify("Sent reply!", vim.log.levels.INFO)
|
||||
M.add_reply_to_tree(tree, data.note, discussion_id)
|
||||
M.load_discussions()
|
||||
end)
|
||||
end
|
||||
end
|
||||
@@ -561,7 +509,7 @@ M.send_deletion = function(tree, unlinked)
|
||||
local root_node = M.get_root_node(tree, current_node)
|
||||
local note_id = note_node.is_root and root_node.root_note_id or note_node.id
|
||||
|
||||
local body = { discussion_id = root_node.id, note_id = note_id }
|
||||
local body = { discussion_id = root_node.id, note_id = tonumber(note_id) }
|
||||
|
||||
job.run_job("/comment", "DELETE", body, function(data)
|
||||
u.notify(data.message, vim.log.levels.INFO)
|
||||
@@ -608,11 +556,14 @@ M.edit_comment = function(tree, unlinked)
|
||||
vim.api.nvim_buf_set_lines(currentBuffer, 0, -1, false, lines)
|
||||
state.set_popup_keymaps(
|
||||
edit_popup,
|
||||
M.send_edits(tostring(root_node.id), note_node.root_note_id or note_node.id, unlinked)
|
||||
M.send_edits(tostring(root_node.id), tonumber(note_node.root_note_id or note_node.id), unlinked)
|
||||
)
|
||||
end
|
||||
|
||||
-- This function sends the edited comment to the Go server
|
||||
---This function sends the edited comment to the Go server
|
||||
---@param discussion_id string
|
||||
---@param note_id integer
|
||||
---@param unlinked boolean
|
||||
M.send_edits = function(discussion_id, note_id, unlinked)
|
||||
return function(text)
|
||||
local body = {
|
||||
@@ -623,10 +574,10 @@ M.send_edits = function(discussion_id, note_id, unlinked)
|
||||
job.run_job("/comment", "PATCH", body, function(data)
|
||||
u.notify(data.message, vim.log.levels.INFO)
|
||||
if unlinked then
|
||||
M.unlinked_discussions = M.replace_text(M.unlinked_discussions, discussion_id, note_id, text)
|
||||
M.replace_text(M.unlinked_discussions, discussion_id, note_id, text)
|
||||
M.rebuild_unlinked_discussion_tree()
|
||||
else
|
||||
M.discussions = M.replace_text(M.discussions, discussion_id, note_id, text)
|
||||
M.replace_text(M.discussions, discussion_id, note_id, text)
|
||||
M.rebuild_discussion_tree()
|
||||
end
|
||||
end)
|
||||
@@ -684,12 +635,16 @@ M.toggle_node = function(tree)
|
||||
end
|
||||
if node:is_expanded() then
|
||||
node:collapse()
|
||||
for _, child in ipairs(children) do
|
||||
tree:get_node(child):collapse()
|
||||
if M.is_node_note(node) then
|
||||
for _, child in ipairs(children) do
|
||||
tree:get_node(child):collapse()
|
||||
end
|
||||
end
|
||||
else
|
||||
for _, child in ipairs(children) do
|
||||
tree:get_node(child):expand()
|
||||
if M.is_node_note(node) then
|
||||
for _, child in ipairs(children) do
|
||||
tree:get_node(child):expand()
|
||||
end
|
||||
end
|
||||
node:expand()
|
||||
end
|
||||
@@ -700,12 +655,48 @@ end
|
||||
--
|
||||
-- 🌲 Helper Functions
|
||||
--
|
||||
---Inspired by default func https://github.com/MunifTanjim/nui.nvim/blob/main/lua/nui/tree/util.lua#L38
|
||||
local function nui_tree_prepare_node(node)
|
||||
if not node.text then
|
||||
error("missing node.text")
|
||||
end
|
||||
|
||||
local texts = node.text
|
||||
|
||||
if type(node.text) ~= "table" or node.text.content then
|
||||
texts = { node.text }
|
||||
end
|
||||
|
||||
local lines = {}
|
||||
|
||||
for i, text in ipairs(texts) do
|
||||
local line = NuiLine()
|
||||
|
||||
line:append(string.rep(" ", node._depth - 1))
|
||||
|
||||
if i == 1 and node:has_children() then
|
||||
line:append(node:is_expanded() and " " or " ")
|
||||
if node.icon then
|
||||
line:append(node.icon .. " ", node.icon_hl)
|
||||
end
|
||||
else
|
||||
line:append(" ")
|
||||
end
|
||||
|
||||
line:append(text, node.text_hl)
|
||||
|
||||
table.insert(lines, line)
|
||||
end
|
||||
|
||||
return lines
|
||||
end
|
||||
|
||||
M.rebuild_discussion_tree = function()
|
||||
M.switch_can_edit_bufs(true)
|
||||
vim.api.nvim_buf_set_lines(M.linked_section.bufnr, 0, -1, false, {})
|
||||
local discussion_tree_nodes = M.add_discussions_to_table(M.discussions)
|
||||
local discussion_tree = NuiTree({ nodes = discussion_tree_nodes, bufnr = M.linked_section.bufnr })
|
||||
local discussion_tree_nodes = discussions_tree.add_discussions_to_table(M.discussions, false)
|
||||
local discussion_tree =
|
||||
NuiTree({ nodes = discussion_tree_nodes, bufnr = M.linked_section.bufnr, prepare_node = nui_tree_prepare_node })
|
||||
discussion_tree:render()
|
||||
M.set_tree_keymaps(discussion_tree, M.linked_section.bufnr, false)
|
||||
M.discussion_tree = discussion_tree
|
||||
@@ -716,8 +707,12 @@ end
|
||||
M.rebuild_unlinked_discussion_tree = function()
|
||||
M.switch_can_edit_bufs(true)
|
||||
vim.api.nvim_buf_set_lines(M.unlinked_section.bufnr, 0, -1, false, {})
|
||||
local unlinked_discussion_tree_nodes = M.add_discussions_to_table(M.unlinked_discussions)
|
||||
local unlinked_discussion_tree = NuiTree({ nodes = unlinked_discussion_tree_nodes, bufnr = M.unlinked_section.bufnr })
|
||||
local unlinked_discussion_tree_nodes = discussions_tree.add_discussions_to_table(M.unlinked_discussions, true)
|
||||
local unlinked_discussion_tree = NuiTree({
|
||||
nodes = unlinked_discussion_tree_nodes,
|
||||
bufnr = M.unlinked_section.bufnr,
|
||||
prepare_node = nui_tree_prepare_node,
|
||||
})
|
||||
unlinked_discussion_tree:render()
|
||||
M.set_tree_keymaps(unlinked_discussion_tree, M.unlinked_section.bufnr, true)
|
||||
M.unlinked_discussion_tree = unlinked_discussion_tree
|
||||
@@ -793,29 +788,59 @@ M.add_empty_titles = function(args)
|
||||
end
|
||||
end
|
||||
|
||||
---Check if type of node is note or note body
|
||||
---@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
|
||||
|
||||
---Check if type of current node is note or note body
|
||||
---@param tree NuiTree
|
||||
---@return boolean
|
||||
M.is_current_node_note = function(tree)
|
||||
return M.is_node_note(tree:get_node())
|
||||
end
|
||||
|
||||
M.set_tree_keymaps = function(tree, bufnr, unlinked)
|
||||
vim.keymap.set("n", state.settings.discussion_tree.edit_comment, function()
|
||||
M.edit_comment(tree, unlinked)
|
||||
if M.is_current_node_note(tree) then
|
||||
M.edit_comment(tree, unlinked)
|
||||
end
|
||||
end, { buffer = bufnr })
|
||||
vim.keymap.set("n", state.settings.discussion_tree.delete_comment, function()
|
||||
M.delete_comment(tree, unlinked)
|
||||
if M.is_current_node_note(tree) then
|
||||
M.delete_comment(tree, unlinked)
|
||||
end
|
||||
end, { buffer = bufnr })
|
||||
vim.keymap.set("n", state.settings.discussion_tree.toggle_resolved, function()
|
||||
M.toggle_discussion_resolved(tree)
|
||||
if M.is_current_node_note(tree) then
|
||||
M.toggle_discussion_resolved(tree)
|
||||
end
|
||||
end, { buffer = bufnr })
|
||||
vim.keymap.set("n", state.settings.discussion_tree.toggle_node, function()
|
||||
M.toggle_node(tree)
|
||||
end, { buffer = bufnr })
|
||||
vim.keymap.set("n", state.settings.discussion_tree.reply, function()
|
||||
M.reply(tree)
|
||||
if M.is_current_node_note(tree) then
|
||||
M.reply(tree)
|
||||
end
|
||||
end, { buffer = bufnr })
|
||||
|
||||
if not unlinked then
|
||||
vim.keymap.set("n", state.settings.discussion_tree.jump_to_file, function()
|
||||
M.jump_to_file(tree)
|
||||
if M.is_current_node_note(tree) then
|
||||
M.jump_to_file(tree)
|
||||
end
|
||||
end, { buffer = bufnr })
|
||||
vim.keymap.set("n", state.settings.discussion_tree.jump_to_reviewer, function()
|
||||
M.jump_to_reviewer(tree)
|
||||
if M.is_current_node_note(tree) then
|
||||
M.jump_to_reviewer(tree)
|
||||
end
|
||||
end, { buffer = bufnr })
|
||||
end
|
||||
end
|
||||
@@ -850,153 +875,68 @@ M.redraw_resolved_status = function(tree, note, mark_resolved)
|
||||
tree:render()
|
||||
end
|
||||
|
||||
---Replace text in discussion after note update.
|
||||
---@param data Discussion[]|UnlinkedDiscussion[]
|
||||
---@param discussion_id string
|
||||
---@param note_id integer
|
||||
---@param text string
|
||||
M.replace_text = function(data, discussion_id, note_id, text)
|
||||
for i, discussion in ipairs(data) do
|
||||
if discussion.id == discussion_id then
|
||||
for j, note in ipairs(discussion.notes) do
|
||||
if note.id == note_id then
|
||||
data[i].notes[j].body = text
|
||||
return data
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---Get root node
|
||||
---@param tree NuiTree
|
||||
---@param node NuiTree.Node?
|
||||
---@return NuiTree.Node?
|
||||
M.get_root_node = function(tree, node)
|
||||
if not node.is_root then
|
||||
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()
|
||||
return M.get_root_node(tree, tree:get_node(parent_id))
|
||||
else
|
||||
elseif node.is_root then
|
||||
return node
|
||||
end
|
||||
end
|
||||
|
||||
---Get note node
|
||||
---@param tree NuiTree
|
||||
---@param node NuiTree.Node?
|
||||
---@return NuiTree.Node?
|
||||
M.get_note_node = function(tree, node)
|
||||
if not node.is_note then
|
||||
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))
|
||||
else
|
||||
elseif node.type == "note" then
|
||||
return node
|
||||
end
|
||||
end
|
||||
|
||||
local attach_uuid = function(str)
|
||||
return { text = str, id = u.uuid() }
|
||||
end
|
||||
|
||||
---Build note header from note.
|
||||
---@param note Note
|
||||
---@return string
|
||||
M.build_note_header = function(note)
|
||||
return "@" .. note.author.username .. " " .. u.time_since(note.created_at)
|
||||
end
|
||||
|
||||
M.build_note_body = function(note, resolve_info)
|
||||
local text_nodes = {}
|
||||
for bodyLine in note.body:gmatch("[^\n]+") do
|
||||
local line = attach_uuid(bodyLine)
|
||||
table.insert(
|
||||
text_nodes,
|
||||
NuiTree.Node({
|
||||
new_line = (type(note.position) == "table" and note.position.new_line),
|
||||
old_line = (type(note.position) == "table" and note.position.old_line),
|
||||
text = line.text,
|
||||
id = line.id,
|
||||
is_body = true,
|
||||
}, {})
|
||||
)
|
||||
end
|
||||
|
||||
local resolve_symbol = ""
|
||||
if resolve_info ~= nil and resolve_info.resolvable then
|
||||
resolve_symbol = resolve_info.resolved and state.settings.discussion_tree.resolved
|
||||
or state.settings.discussion_tree.unresolved
|
||||
end
|
||||
|
||||
local noteHeader = M.build_note_header(note) .. " " .. resolve_symbol
|
||||
|
||||
return noteHeader, text_nodes
|
||||
end
|
||||
|
||||
M.build_note = function(note, resolve_info)
|
||||
local text, text_nodes = M.build_note_body(note, resolve_info)
|
||||
local note_node = NuiTree.Node({
|
||||
text = text,
|
||||
id = note.id,
|
||||
file_name = (type(note.position) == "table" and note.position.new_path),
|
||||
new_line = (type(note.position) == "table" and note.position.new_line),
|
||||
old_line = (type(note.position) == "table" and note.position.old_line),
|
||||
is_note = true,
|
||||
}, text_nodes)
|
||||
|
||||
return note_node, text, text_nodes
|
||||
end
|
||||
|
||||
M.add_reply_to_tree = function(tree, note, discussion_id)
|
||||
local note_node = M.build_note(note)
|
||||
local note_node = discussions_tree.build_note(note)
|
||||
note_node:expand()
|
||||
tree:add_node(note_node, discussion_id and ("-" .. discussion_id) or nil)
|
||||
tree:render()
|
||||
end
|
||||
|
||||
M.add_discussions_to_table = function(items)
|
||||
local t = {}
|
||||
for _, discussion in ipairs(items) do
|
||||
local discussion_children = {}
|
||||
|
||||
-- These properties are filled in by the first note
|
||||
local root_text = ""
|
||||
local root_note_id = ""
|
||||
local root_file_name = ""
|
||||
local root_id = 0
|
||||
local root_text_nodes = {}
|
||||
local resolvable = false
|
||||
local resolved = false
|
||||
local root_new_line = nil
|
||||
local root_old_line = nil
|
||||
|
||||
for j, note in ipairs(discussion.notes) do
|
||||
if j == 1 then
|
||||
_, root_text, root_text_nodes = M.build_note(note, { resolved = note.resolved, resolvable = note.resolvable })
|
||||
|
||||
root_file_name = (type(note.position) == "table" and note.position.new_path)
|
||||
root_new_line = (type(note.position) == "table" and note.position.new_line)
|
||||
root_old_line = (type(note.position) == "table" and note.position.old_line)
|
||||
root_id = discussion.id
|
||||
root_note_id = note.id
|
||||
resolvable = note.resolvable
|
||||
resolved = note.resolved
|
||||
else -- Otherwise insert it as a child node...
|
||||
local note_node = M.build_note(note)
|
||||
table.insert(discussion_children, note_node)
|
||||
end
|
||||
end
|
||||
|
||||
-- Creates the first node in the discussion, and attaches children
|
||||
local body = u.spread(root_text_nodes, discussion_children)
|
||||
local root_node = NuiTree.Node({
|
||||
text = root_text,
|
||||
is_note = true,
|
||||
is_root = true,
|
||||
id = root_id,
|
||||
root_note_id = root_note_id,
|
||||
file_name = root_file_name,
|
||||
new_line = root_new_line,
|
||||
old_line = root_old_line,
|
||||
resolvable = resolvable,
|
||||
resolved = resolved,
|
||||
}, body)
|
||||
|
||||
table.insert(t, root_node)
|
||||
end
|
||||
|
||||
return t
|
||||
end
|
||||
|
||||
---Get note location
|
||||
---@param tree NuiTree
|
||||
M.get_note_location = function(tree)
|
||||
local node = tree:get_node()
|
||||
if node == nil then
|
||||
279
lua/gitlab/actions/discussions/tree.lua
Normal file
279
lua/gitlab/actions/discussions/tree.lua
Normal file
@@ -0,0 +1,279 @@
|
||||
local state = require("gitlab.state")
|
||||
local u = require("gitlab.utils")
|
||||
local NuiTree = require("nui.tree")
|
||||
|
||||
local M = {}
|
||||
|
||||
local attach_uuid = function(str)
|
||||
return { text = str, id = u.uuid() }
|
||||
end
|
||||
|
||||
---Create path node
|
||||
---@param relative_path string
|
||||
---@param full_path string
|
||||
---@param child_nodes NuiTree.Node[]?
|
||||
---@return NuiTree.Node
|
||||
local function create_path_node(relative_path, full_path, child_nodes)
|
||||
return NuiTree.Node({
|
||||
text = relative_path,
|
||||
path = full_path,
|
||||
id = full_path,
|
||||
type = "path",
|
||||
icon = " ",
|
||||
icon_hl = "GitlabDirectoryIcon",
|
||||
text_hl = "GitlabDirectory",
|
||||
}, child_nodes or {})
|
||||
end
|
||||
|
||||
---Create file name node
|
||||
---@param file_name string
|
||||
---@param full_file_path string
|
||||
---@param child_nodes NuiTree.Node[]?
|
||||
---@return NuiTree.Node
|
||||
local function create_file_name_node(file_name, full_file_path, child_nodes)
|
||||
local icon, icon_hl = u.get_icon(file_name)
|
||||
return NuiTree.Node({
|
||||
text = file_name,
|
||||
file_name = full_file_path,
|
||||
id = full_file_path,
|
||||
type = "file_name",
|
||||
icon = icon,
|
||||
icon_hl = icon_hl,
|
||||
text_hl = "GitlabFileName",
|
||||
}, child_nodes or {})
|
||||
end
|
||||
|
||||
---Sort list of nodes (in place) of type "path" or "file_name"
|
||||
---@param nodes NuiTree.Node[]
|
||||
local function sort_nodes(nodes)
|
||||
table.sort(nodes, function(node1, node2)
|
||||
if node1.type == "path" and node2.type == "path" then
|
||||
return node1.path < node2.path
|
||||
elseif node1.type == "file_name" and node2.type == "file_name" then
|
||||
return node1.file_name < node2.file_name
|
||||
elseif node1.type == "path" and node2.type == "file_name" then
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
---Merge path nodes which have only single path child
|
||||
---@param node NuiTree.Node
|
||||
local function flatten_nodes(node)
|
||||
if node.type ~= "path" then
|
||||
return
|
||||
end
|
||||
for _, child in ipairs(node.__children) do
|
||||
flatten_nodes(child)
|
||||
end
|
||||
if #node.__children == 1 and node.__children[1].type == "path" then
|
||||
local child = node.__children[1]
|
||||
node.__children = child.__children
|
||||
node.id = child.id
|
||||
node.path = child.path
|
||||
node.text = node.text .. u.path_separator .. child.text
|
||||
end
|
||||
sort_nodes(node.__children)
|
||||
end
|
||||
|
||||
---Build note header from note.
|
||||
---@param note Note
|
||||
---@return string
|
||||
local function build_note_header(note)
|
||||
return "@" .. note.author.username .. " " .. u.time_since(note.created_at)
|
||||
end
|
||||
|
||||
---Build note node body
|
||||
---@param note Note
|
||||
---@param resolve_info table?
|
||||
---@return string
|
||||
---@return NuiTree.Node[]
|
||||
local function build_note_body(note, resolve_info)
|
||||
local text_nodes = {}
|
||||
for bodyLine in note.body:gmatch("[^\n]+") do
|
||||
local line = attach_uuid(bodyLine)
|
||||
table.insert(
|
||||
text_nodes,
|
||||
NuiTree.Node({
|
||||
new_line = (type(note.position) == "table" and note.position.new_line),
|
||||
old_line = (type(note.position) == "table" and note.position.old_line),
|
||||
text = line.text,
|
||||
id = line.id,
|
||||
type = "note_body",
|
||||
}, {})
|
||||
)
|
||||
end
|
||||
|
||||
local resolve_symbol = ""
|
||||
if resolve_info ~= nil and resolve_info.resolvable then
|
||||
resolve_symbol = resolve_info.resolved and state.settings.discussion_tree.resolved
|
||||
or state.settings.discussion_tree.unresolved
|
||||
end
|
||||
|
||||
local noteHeader = build_note_header(note) .. " " .. resolve_symbol
|
||||
|
||||
return noteHeader, text_nodes
|
||||
end
|
||||
|
||||
---Build note node
|
||||
---@param note Note
|
||||
---@param resolve_info table?
|
||||
---@return NuiTree.Node
|
||||
---@return string
|
||||
---@return NuiTree.Node[]
|
||||
M.build_note = function(note, resolve_info)
|
||||
local text, text_nodes = build_note_body(note, resolve_info)
|
||||
local note_node = NuiTree.Node({
|
||||
text = text,
|
||||
id = note.id,
|
||||
file_name = (type(note.position) == "table" and note.position.new_path),
|
||||
new_line = (type(note.position) == "table" and note.position.new_line),
|
||||
old_line = (type(note.position) == "table" and note.position.old_line),
|
||||
type = "note",
|
||||
}, text_nodes)
|
||||
|
||||
return note_node, text, text_nodes
|
||||
end
|
||||
|
||||
---Create nodes for NuiTree from discussions
|
||||
---@param items Discussion[]
|
||||
---@param unlinked boolean? False or nil means that discussions are linked to code lines
|
||||
---@return NuiTree.Node[]
|
||||
M.add_discussions_to_table = function(items, unlinked)
|
||||
local t = {}
|
||||
for _, discussion in ipairs(items) do
|
||||
local discussion_children = {}
|
||||
|
||||
-- These properties are filled in by the first note
|
||||
---@type string?
|
||||
local root_text = ""
|
||||
---@type string?
|
||||
local root_note_id = ""
|
||||
---@type string?
|
||||
local root_file_name = ""
|
||||
---@type string
|
||||
local root_id
|
||||
local root_text_nodes = {}
|
||||
local resolvable = false
|
||||
local resolved = false
|
||||
local root_new_line = nil
|
||||
local root_old_line = nil
|
||||
|
||||
for j, note in ipairs(discussion.notes) do
|
||||
if j == 1 then
|
||||
_, root_text, root_text_nodes = M.build_note(note, { resolved = note.resolved, resolvable = note.resolvable })
|
||||
|
||||
root_file_name = (type(note.position) == "table" and note.position.new_path or nil)
|
||||
root_new_line = (type(note.position) == "table" and note.position.new_line or nil)
|
||||
root_old_line = (type(note.position) == "table" and note.position.old_line or nil)
|
||||
root_id = discussion.id
|
||||
root_note_id = tostring(note.id)
|
||||
resolvable = note.resolvable
|
||||
resolved = note.resolved
|
||||
else -- Otherwise insert it as a child node...
|
||||
local note_node = M.build_note(note)
|
||||
table.insert(discussion_children, note_node)
|
||||
end
|
||||
end
|
||||
|
||||
-- Creates the first node in the discussion, and attaches children
|
||||
local body = u.spread(root_text_nodes, discussion_children)
|
||||
local root_node = NuiTree.Node({
|
||||
text = root_text,
|
||||
type = "note",
|
||||
is_root = true,
|
||||
id = root_id,
|
||||
root_note_id = root_note_id,
|
||||
file_name = root_file_name,
|
||||
new_line = root_new_line,
|
||||
old_line = root_old_line,
|
||||
resolvable = resolvable,
|
||||
resolved = resolved,
|
||||
}, body)
|
||||
|
||||
table.insert(t, root_node)
|
||||
end
|
||||
if state.settings.discussion_tree.tree_type == "simple" or unlinked == true then
|
||||
return t
|
||||
end
|
||||
|
||||
-- Create all the folder and file name nodes.
|
||||
local discussion_by_file_name = {}
|
||||
local top_level_path_to_node = {}
|
||||
for _, node in ipairs(t) do
|
||||
local path = ""
|
||||
local parent_node = nil
|
||||
local path_parts = u.split_path(node.file_name)
|
||||
local file_name = table.remove(path_parts, #path_parts)
|
||||
-- Create folders
|
||||
for i, path_part in ipairs(path_parts) do
|
||||
path = path ~= nil and path .. u.path_separator .. path_part or path_part
|
||||
if i == 1 then
|
||||
if top_level_path_to_node[path] == nil then
|
||||
parent_node = create_path_node(path_part, path)
|
||||
top_level_path_to_node[path] = parent_node
|
||||
table.insert(discussion_by_file_name, parent_node)
|
||||
end
|
||||
parent_node = top_level_path_to_node[path]
|
||||
elseif parent_node then
|
||||
local child_node = nil
|
||||
for _, child in ipairs(parent_node.__children) do
|
||||
if child.path == path then
|
||||
child_node = child
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if child_node == nil then
|
||||
child_node = create_path_node(path_part, path)
|
||||
table.insert(parent_node.__children, child_node)
|
||||
parent_node:expand()
|
||||
parent_node = child_node
|
||||
else
|
||||
parent_node = child_node
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Create file name nodes
|
||||
if parent_node == nil then
|
||||
---Top level file name
|
||||
if top_level_path_to_node[node.file_name] ~= nil then
|
||||
table.insert(top_level_path_to_node[node.file_name].__children, node)
|
||||
else
|
||||
local file_node = create_file_name_node(file_name, node.file_name, { node })
|
||||
file_node:expand()
|
||||
top_level_path_to_node[node.file_name] = file_node
|
||||
table.insert(discussion_by_file_name, file_node)
|
||||
end
|
||||
else
|
||||
local child_node = nil
|
||||
for _, child in ipairs(parent_node.__children) do
|
||||
if child.file_name == node.file_name then
|
||||
child_node = child
|
||||
break
|
||||
end
|
||||
end
|
||||
if child_node == nil then
|
||||
child_node = create_file_name_node(file_name, node.file_name, { node })
|
||||
table.insert(parent_node.__children, child_node)
|
||||
parent_node:expand()
|
||||
child_node:expand()
|
||||
else
|
||||
table.insert(child_node.__children, node)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Flatten empty folders
|
||||
for _, node in ipairs(discussion_by_file_name) do
|
||||
flatten_nodes(node)
|
||||
end
|
||||
sort_nodes(discussion_by_file_name)
|
||||
|
||||
return discussion_by_file_name
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -38,7 +38,7 @@ M.attach_file = function()
|
||||
if not choice then
|
||||
return
|
||||
end
|
||||
local full_path = attachment_dir .. (u.is_windows() and "\\" or "/") .. choice
|
||||
local full_path = attachment_dir .. u.path_separator .. choice
|
||||
local body = { file_path = full_path, file_name = choice }
|
||||
job.run_job("/mr/attachment", "POST", body, function(data)
|
||||
local markdown = data.markdown
|
||||
|
||||
@@ -193,7 +193,6 @@ M.create_layout = function(info_lines)
|
||||
details_popup = Popup(details_popup_settings)
|
||||
if state.settings.info.horizontal then
|
||||
local longest_line = u.get_longest_string(info_lines)
|
||||
print(longest_line)
|
||||
internal_layout = Layout.Box({
|
||||
Layout.Box(title_popup, { size = 3 }),
|
||||
Layout.Box({
|
||||
|
||||
@@ -7,3 +7,6 @@ local discussion = colors.discussion_tree
|
||||
vim.api.nvim_set_hl(0, "GitlabUsername", u.get_colors_for_group(discussion.username))
|
||||
vim.api.nvim_set_hl(0, "GitlabDate", u.get_colors_for_group(discussion.date))
|
||||
vim.api.nvim_set_hl(0, "GitlabChevron", u.get_colors_for_group(discussion.chevron))
|
||||
vim.api.nvim_set_hl(0, "GitlabDirectory", u.get_colors_for_group(discussion.directory))
|
||||
vim.api.nvim_set_hl(0, "GitlabDirectoryIcon", u.get_colors_for_group(discussion.directory_icon))
|
||||
vim.api.nvim_set_hl(0, "GitlabFileName", u.get_colors_for_group(discussion.file_name))
|
||||
|
||||
@@ -1,219 +0,0 @@
|
||||
describe("utils/init.lua", function()
|
||||
it("Loads package", function()
|
||||
local utils_ok, _ = pcall(require, "gitlab.utils")
|
||||
assert._is_true(utils_ok)
|
||||
end)
|
||||
|
||||
local _, u = pcall(require, "gitlab.utils")
|
||||
describe("extract", function()
|
||||
it("Extracts a single value", function()
|
||||
local t = { { one = 1, two = 2 }, { three = 3, four = 4 } }
|
||||
local got = u.extract(t, "one")
|
||||
local want = { 1 }
|
||||
assert.are.same(want, got)
|
||||
end)
|
||||
it("Returns nothing with empty table", function()
|
||||
local t = {}
|
||||
local got = u.extract(t, "one")
|
||||
local want = {}
|
||||
assert.are.same(want, got)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("get_last_word", function()
|
||||
it("Returns the last word in a sentence", function()
|
||||
local sentence = "Hello world!"
|
||||
local got = u.get_last_word(sentence)
|
||||
local want = "world!"
|
||||
assert.are.same(want, got)
|
||||
end)
|
||||
it("Returns an empty string without text", function()
|
||||
local sentence = ""
|
||||
local got = u.get_last_word(sentence)
|
||||
local want = ""
|
||||
assert.are.same(want, got)
|
||||
end)
|
||||
it("Returns whole string w/out divider", function()
|
||||
local sentence = "Thisdoesnothavebreaks"
|
||||
local got = u.get_last_word(sentence)
|
||||
assert.are.same(sentence, got)
|
||||
end)
|
||||
it("Returns correct word w/ different divider", function()
|
||||
local sentence = "this|uses|a|different|divider"
|
||||
local got = u.get_last_word(sentence, "|")
|
||||
local want = "divider"
|
||||
assert.are.same(want, got)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("format_date", function()
|
||||
local current_date = {
|
||||
day = 19,
|
||||
hour = 22,
|
||||
isdst = false,
|
||||
min = 0,
|
||||
month = 11,
|
||||
sec = 44,
|
||||
wday = 1,
|
||||
yday = 323,
|
||||
year = 2023,
|
||||
}
|
||||
it("Returns days since a valid UTC timestamp", function()
|
||||
local stamp = "2023-11-16T19:52:36.946Z"
|
||||
local got = u.time_since(stamp, current_date)
|
||||
local want = "3 days ago"
|
||||
assert.are.same(want, got)
|
||||
end)
|
||||
|
||||
it("Returns hours since a valid UTC timestamp", function()
|
||||
local stamp = "2023-11-19T19:52:36.946Z"
|
||||
local got = u.time_since(stamp, current_date)
|
||||
local want = "2 hours ago"
|
||||
assert.are.same(want, got)
|
||||
end)
|
||||
it("Returns readable time if > 1 year", function()
|
||||
local stamp = "2011-11-19T19:52:36.946Z"
|
||||
local got = u.time_since(stamp, current_date)
|
||||
local want = "November 19, 2011"
|
||||
assert.are.same(want, got)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("remove_first_value", function()
|
||||
it("Removes the first value correctly", function()
|
||||
local got = u.remove_first_value({ 1, 2 })
|
||||
local want = { 2 }
|
||||
assert.are.same(want, got)
|
||||
end)
|
||||
it("Handles a one-length list", function()
|
||||
local got = u.remove_first_value({ 1 })
|
||||
local want = {}
|
||||
assert.are.same(want, got)
|
||||
end)
|
||||
it("Handles a zero-length list", function()
|
||||
local got = u.remove_first_value({})
|
||||
local want = {}
|
||||
assert.are.same(want, got)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("table_size", function()
|
||||
it("Works for associative tables", function()
|
||||
local got = u.remove_first_value({ 1, 2 })
|
||||
local want = { 2 }
|
||||
assert.are.same(want, got)
|
||||
end)
|
||||
it("Handles a one-length list", function()
|
||||
local got = u.remove_first_value({ 1 })
|
||||
local want = {}
|
||||
assert.are.same(want, got)
|
||||
end)
|
||||
it("Handles a zero-length list", function()
|
||||
local got = u.remove_first_value({})
|
||||
local want = {}
|
||||
assert.are.same(want, got)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("contains", function()
|
||||
it("Finds a value in a list", function()
|
||||
local got = u.contains({ 1, 2 }, 1)
|
||||
assert._is_true(got)
|
||||
end)
|
||||
it("Handles missing values", function()
|
||||
local got = u.contains({ 1, 3, 4 }, 2)
|
||||
assert._is_false(got)
|
||||
end)
|
||||
it("Handles empty lists", function()
|
||||
local got = u.contains({}, 1)
|
||||
assert._is_false(got)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("reverse", function()
|
||||
it("Reverses the values in a list", function()
|
||||
local got = u.reverse({ 1, 2, 3, 4 })
|
||||
local want = { 4, 3, 2, 1 }
|
||||
assert.are.same(got, want)
|
||||
end)
|
||||
it("Handles single value", function()
|
||||
local got = u.reverse({ 1 })
|
||||
local want = { 1 }
|
||||
assert.are.same(got, want)
|
||||
end)
|
||||
it("Handles empty list", function()
|
||||
local got = u.reverse({})
|
||||
local want = {}
|
||||
assert.are.same(got, want)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("spread", function()
|
||||
it("Spreads the values", function()
|
||||
local t1 = { 1, 2, 3 }
|
||||
local t2 = { 4, 5, 6 }
|
||||
local got = u.spread(t1, t2)
|
||||
local want = { 1, 2, 3, 4, 5, 6 }
|
||||
assert.are.same(got, want)
|
||||
end)
|
||||
it("Handles an empty t1 table", function()
|
||||
local t1 = {}
|
||||
local t2 = { 4, 5, 6 }
|
||||
local got = u.spread(t1, t2)
|
||||
local want = { 4, 5, 6 }
|
||||
assert.are.same(got, want)
|
||||
end)
|
||||
it("Handles an empty t2 table", function()
|
||||
local t1 = { 1, 2, 3 }
|
||||
local t2 = {}
|
||||
local got = u.spread(t1, t2)
|
||||
local want = { 1, 2, 3 }
|
||||
assert.are.same(got, want)
|
||||
end)
|
||||
it("Handles both empty tables", function()
|
||||
local t1 = {}
|
||||
local t2 = {}
|
||||
local got = u.spread(t1, t2)
|
||||
local want = {}
|
||||
assert.are.same(got, want)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("offset_to_seconds", function()
|
||||
local tests = {
|
||||
est = { "-0500", -18000 },
|
||||
pst = { "-0800", -28800 },
|
||||
gmt = { "+0000", 0 },
|
||||
cet = { "+0100", 360 },
|
||||
jst = { "+0900", 32400 },
|
||||
ist = { "+0530", 19800 },
|
||||
art = { "-0300", -10800 },
|
||||
aest = { "+1100", 39600 },
|
||||
mmt = { "+0630", 23400 },
|
||||
}
|
||||
|
||||
for _, val in ipairs(tests) do
|
||||
local got = u.offset_to_seconds(val[1])
|
||||
local want = val[2]
|
||||
assert.are.same(got, want)
|
||||
end
|
||||
end)
|
||||
|
||||
describe("format_to_local", function()
|
||||
local tests = {
|
||||
{ "2023-10-28T16:25:09.482Z", "-0500", "10/28/2023 at 11:25" },
|
||||
{ "2016-11-22T1:25:09.482Z", "-0500", "11/21/2016 at 20:25" },
|
||||
{ "2016-11-22T1:25:09.482Z", "-0000", "11/22/2016 at 01:25" },
|
||||
{ "2017-3-22T13:25:09.482Z", "+0700", "03/22/2017 at 20:25" },
|
||||
{ "2023-10-28T11:25:09.482-05:00", "-0500", "10/28/2023 at 11:25" },
|
||||
{ "2016-11-21T20:25:09.482-05:00", "-0500", "11/21/2016 at 20:25" },
|
||||
{ "2016-11-22T1:25:09.482-00:00", "-0000", "11/22/2016 at 01:25" },
|
||||
{ "2017-3-22T20:25:09.482+07:00", "+0700", "03/22/2017 at 20:25" },
|
||||
}
|
||||
for _, val in ipairs(tests) do
|
||||
local got = u.format_to_local(val[1], val[2])
|
||||
local want = val[3]
|
||||
assert.are.same(got, want)
|
||||
end
|
||||
end)
|
||||
end)
|
||||
@@ -33,6 +33,7 @@ M.settings = {
|
||||
size = "20%",
|
||||
resolved = "✓",
|
||||
unresolved = "",
|
||||
tree_type = "simple",
|
||||
},
|
||||
info = {
|
||||
enabled = true,
|
||||
@@ -97,7 +98,10 @@ M.settings = {
|
||||
discussion_tree = {
|
||||
username = "Keyword",
|
||||
date = "Comment",
|
||||
chevron = "Comment",
|
||||
chevron = "DiffviewNonText",
|
||||
directory = "Directory",
|
||||
directory_icon = "DiffviewFolderSign",
|
||||
file_name = "Normal",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
local has_devicons, devicons = pcall(require, "nvim-web-devicons")
|
||||
local M = {}
|
||||
|
||||
---Pulls out a list of values matching a given key from an array of tables
|
||||
@@ -246,12 +247,83 @@ M.is_windows = function()
|
||||
return false
|
||||
end
|
||||
|
||||
---Path separator based on current OS.
|
||||
---@type string
|
||||
M.path_separator = M.is_windows() and "\\" or "/"
|
||||
|
||||
---Split path by OS path separator.
|
||||
---@param path string
|
||||
---@return string[]
|
||||
M.split_path = function(path)
|
||||
local path_parts = {}
|
||||
for part in string.gmatch(path, "([^" .. M.path_separator .. "]+)") do
|
||||
table.insert(path_parts, part)
|
||||
end
|
||||
return path_parts
|
||||
end
|
||||
|
||||
M.P = function(...)
|
||||
local objects = {}
|
||||
for i = 1, select("#", ...) do
|
||||
local v = select(i, ...)
|
||||
table.insert(objects, vim.inspect(v))
|
||||
end
|
||||
|
||||
print(table.concat(objects, "\n"))
|
||||
return ...
|
||||
end
|
||||
|
||||
M.get_buffer_text = function(bufnr)
|
||||
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
|
||||
local text = table.concat(lines, "\n")
|
||||
return text
|
||||
end
|
||||
|
||||
M.string_starts = function(str, start)
|
||||
return str:sub(1, #start) == start
|
||||
end
|
||||
|
||||
M.press_enter = function()
|
||||
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<CR>", false, true, true), "n", false)
|
||||
end
|
||||
|
||||
---Return timestamp from ISO 8601 formatted date string.
|
||||
---@param date_string string ISO 8601 formatted date string
|
||||
---@return integer timestamp
|
||||
M.from_iso_format_date_to_timestamp = function(date_string)
|
||||
local year, month, day, hour, min, sec = date_string:match("(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)")
|
||||
return os.time({ year = year, month = month, day = day, hour = hour, min = min, sec = sec })
|
||||
end
|
||||
|
||||
M.format_date = function(date_string)
|
||||
local date_table = os.date("!*t")
|
||||
local date = M.from_iso_format_date_to_timestamp(date_string)
|
||||
|
||||
local current_date = os.time({
|
||||
year = date_table.year,
|
||||
month = date_table.month,
|
||||
day = date_table.day,
|
||||
hour = date_table.hour,
|
||||
min = date_table.min,
|
||||
sec = date_table.sec,
|
||||
})
|
||||
|
||||
local time_diff = current_date - date
|
||||
|
||||
if time_diff < 60 then
|
||||
return pluralize(time_diff, "second")
|
||||
elseif time_diff < 3600 then
|
||||
return pluralize(math.floor(time_diff / 60), "minute")
|
||||
elseif time_diff < 86400 then
|
||||
return pluralize(math.floor(time_diff / 3600), "hour")
|
||||
elseif time_diff < 2592000 then
|
||||
return pluralize(math.floor(time_diff / 86400), "day")
|
||||
else
|
||||
local formatted_date = os.date("%A, %B %e", date)
|
||||
return formatted_date
|
||||
end
|
||||
end
|
||||
|
||||
M.jump_to_file = function(filename, line_number)
|
||||
if line_number == nil then
|
||||
line_number = 1
|
||||
@@ -362,7 +434,7 @@ M.list_files_in_folder = function(folder_path)
|
||||
local files = {}
|
||||
if folder ~= nil then
|
||||
for _, file in ipairs(folder) do
|
||||
local file_path = folder_path .. (M.is_windows() and "\\" or "/") .. file
|
||||
local file_path = folder_path .. M.path_separator .. file
|
||||
local timestamp = vim.fn.getftime(file_path)
|
||||
table.insert(files, { name = file, timestamp = timestamp })
|
||||
end
|
||||
@@ -528,4 +600,21 @@ M.get_visual_selection_boundaries = function()
|
||||
return start_line, end_line
|
||||
end
|
||||
|
||||
---Get icon for filename if nvim-web-devicons plugin is available otherwise return empty string
|
||||
---@return string?
|
||||
---@return string?
|
||||
M.get_icon = function(filename)
|
||||
if has_devicons then
|
||||
local extension = vim.fn.fnamemodify(filename, ":e")
|
||||
local icon, icon_hl = devicons.get_icon(filename, extension, { default = true })
|
||||
if icon ~= nil then
|
||||
return icon .. " ", icon_hl
|
||||
else
|
||||
return nil, nil
|
||||
end
|
||||
else
|
||||
return nil, nil
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user