Add/Show/Delete Emojis on Notes (#181)

This MR adds the ability to view, add, and delete emojis from notes and comments.

This action can be performed by default with the `Ea` (emoji add) keybinding, and the `Ed` (emoji delete) keybinding. Only emojis added by the current user are eligible for deletion. The MR also implements a popup functionality which shows the user who added emojis on hover.

Implements #179
This commit is contained in:
Harrison (Harry) Cramer
2024-02-18 21:16:53 -05:00
committed by GitHub
parent 99741178f9
commit baee20b279
22 changed files with 11655 additions and 83 deletions

View File

@@ -66,6 +66,18 @@
---@field discussions Discussion[]
---@field unlinked_discussions UnlinkedDiscussion[]
---@class EmojiMap: table<string, Emoji>
---@class Emoji
---@field unicode string
---@field unicodeAlternates string[]
---@field name string
---@field shortname string
---@field category string
---@field aliases string[]
---@field aliasesASCII string[]
---@field keywords string[]
---@field moji string
---@class WinbarTable
---@field name string
---@field resolvable_discussions number

View File

@@ -14,6 +14,7 @@ local discussions_tree = require("gitlab.actions.discussions.tree")
local signs = require("gitlab.actions.discussions.signs")
local winbar = require("gitlab.actions.discussions.winbar")
local help = require("gitlab.actions.help")
local emoji = require("gitlab.emoji")
local M = {
split_visible = false,
@@ -24,6 +25,8 @@ local M = {
discussions = {},
---@type UnlinkedDiscussion[]
unlinked_discussions = {},
---@type EmojiMap
emojis = {},
---@type number
linked_bufnr = nil,
---@type number
@@ -40,6 +43,7 @@ M.load_discussions = function(callback)
job.run_job("/mr/discussions/list", "POST", { blacklist = state.settings.discussion_tree.blacklist }, function(data)
M.discussions = data.discussions ~= vim.NIL and data.discussions or {}
M.unlinked_discussions = data.unlinked_discussions ~= vim.NIL and data.unlinked_discussions or {}
M.emojis = data.emojis or {}
if type(callback) == "function" then
callback(data)
end
@@ -100,7 +104,7 @@ M.toggle = function(callback)
M.load_discussions(function()
if type(M.discussions) ~= "table" and type(M.unlinked_discussions) ~= "table" then
vim.notify("No discussions or notes for this MR", vim.log.levels.WARN)
u.notify("No discussions or notes for this MR", vim.log.levels.WARN)
vim.api.nvim_buf_set_lines(split.bufnr, 0, -1, false, { "" })
return
end
@@ -159,7 +163,7 @@ M.move_to_discussion_tree = function()
local discussion_id = diagnostic.user_data.discussion_id
local discussion_node, line_number = M.discussion_tree:get_node("-" .. discussion_id)
if discussion_node == {} or discussion_node == nil then
vim.notify("Discussion not found", vim.log.levels.WARN)
u.notify("Discussion not found", vim.log.levels.WARN)
return
end
if not discussion_node:is_expanded() then
@@ -181,7 +185,7 @@ M.move_to_discussion_tree = function()
end
if #diagnostics == 0 then
vim.notify("No diagnostics for this line", vim.log.levels.WARN)
u.notify("No diagnostics for this line", vim.log.levels.WARN)
return
elseif #diagnostics > 1 then
vim.ui.select(diagnostics, {
@@ -479,7 +483,6 @@ local function nui_tree_prepare_node(node)
end
local texts = node.text
if type(node.text) ~= "table" or node.text.content then
texts = { node.text }
end
@@ -502,6 +505,24 @@ local function nui_tree_prepare_node(node)
line:append(text, node.text_hl)
local note_id = tostring(node.is_root and node.root_note_id or node.id)
local e = require("gitlab.emoji")
---@type Emoji[]
local emojis = M.emojis[note_id]
local placed_emojis = {}
if emojis ~= nil then
for _, v in ipairs(emojis) do
local icon = e.emoji_map[v.name]
if icon ~= nil and not u.contains(placed_emojis, icon.moji) then
line:append(" ")
line:append(icon.moji)
table.insert(placed_emojis, icon.moji)
end
end
end
table.insert(lines, line)
end
@@ -689,7 +710,15 @@ M.set_tree_keymaps = function(tree, bufnr, unlinked)
end, { buffer = bufnr, desc = "Open the note in your browser" })
vim.keymap.set("n", "<leader>p", function()
M.print_node(tree)
end, { buffer = bufnr, desc = "dev_ Print current node (for debugging)" })
end, { buffer = bufnr, desc = "Print current node (for debugging)" })
vim.keymap.set("n", state.settings.discussion_tree.add_emoji, function()
M.add_emoji_to_note(tree, unlinked)
end, { buffer = bufnr, desc = "Add an emoji reaction to the note/comment" })
vim.keymap.set("n", state.settings.discussion_tree.delete_emoji, function()
M.delete_emoji_from_note(tree, unlinked)
end, { buffer = bufnr, desc = "Remove an emoji reaction from the note/comment" })
emoji.init_popup(tree, bufnr)
end
M.redraw_resolved_status = function(tree, note, mark_resolved)
@@ -829,6 +858,76 @@ M.open_in_browser = function(tree)
u.open_in_browser(url)
end
M.add_emoji_to_note = function(tree, unlinked)
local node = tree:get_node()
local note_node = M.get_note_node(tree, node)
local root_node = M.get_root_node(tree, node)
local note_id = tonumber(note_node.is_root and root_node.root_note_id or note_node.id)
local note_id_str = tostring(note_id)
local emojis = require("gitlab.emoji").emoji_list
emoji.pick_emoji(emojis, function(name)
local body = { emoji = name, note_id = note_id }
job.run_job("/mr/awardable/note/", "POST", body, function(data)
if M.emojis[note_id_str] == nil then
M.emojis[note_id_str] = {}
table.insert(M.emojis[note_id_str], data.Emoji)
else
table.insert(M.emojis[note_id_str], data.Emoji)
end
if unlinked then
M.rebuild_unlinked_discussion_tree()
else
M.rebuild_discussion_tree()
end
u.notify("Emoji added", vim.log.levels.INFO)
end)
end)
end
M.delete_emoji_from_note = function(tree, unlinked)
local node = tree:get_node()
local note_node = M.get_note_node(tree, node)
local root_node = M.get_root_node(tree, node)
local note_id = tonumber(note_node.is_root and root_node.root_note_id or note_node.id)
local note_id_str = tostring(note_id)
local e = require("gitlab.emoji")
local emojis = {}
local current_emojis = M.emojis[note_id_str]
for _, current_emoji in ipairs(current_emojis) do
if state.USER.id == current_emoji.user.id then
table.insert(emojis, e.emoji_map[current_emoji.name])
end
end
emoji.pick_emoji(emojis, function(name)
local awardable_id
for _, current_emoji in ipairs(current_emojis) do
if current_emoji.name == name and current_emoji.user.id == state.USER.id then
awardable_id = current_emoji.id
break
end
end
job.run_job(string.format("/mr/awardable/note/%d/%d", note_id, awardable_id), "DELETE", nil, function(_)
local keep = {} -- Emojis to keep after deletion in the UI
for _, saved in ipairs(M.emojis[note_id_str]) do
if saved.name ~= name or saved.user.id ~= state.USER.id then
table.insert(keep, saved)
end
end
M.emojis[note_id_str] = keep
if unlinked then
M.rebuild_unlinked_discussion_tree()
else
M.rebuild_discussion_tree()
end
e.init_popup(tree, unlinked and M.unlinked_bufnr or M.linked_bufnr)
u.notify("Emoji removed", vim.log.levels.INFO)
end)
end)
end
-- For developers!
M.print_node = function(tree)
local current_node = tree:get_node()

View File

@@ -267,7 +267,6 @@ M.parse_signs_from_discussions = function(discussions)
start_line = start_old_line
end_line = end_old_line
else
vim.print(start_type == "")
return {}, {}, string.format("Unsupported line range type found for discussion %s", discussion.id)
end

View File

@@ -245,7 +245,6 @@ M.color_labels = function(bufnr)
for i, v in ipairs(state.settings.info.fields) do
if v == "labels" then
local line_content = u.get_line_content(bufnr, i)
vim.print(line_content)
for j, label in ipairs(state.LABELS) do
local start_idx, end_idx = line_content:find(label.Name)
if start_idx ~= nil and end_idx ~= nil then

140
lua/gitlab/emoji.lua Normal file
View File

@@ -0,0 +1,140 @@
local u = require("gitlab.utils")
local state = require("gitlab.state")
local M = {
---@type EmojiMap|nil
emoji_map = {},
---@type Emoji[]
emoji_list = {},
}
M.init = function()
local bin_path = state.settings.bin_path
local emoji_path = bin_path
.. state.settings.file_separator
.. "config"
.. state.settings.file_separator
.. "emojis.json"
local emojis = u.read_file(emoji_path)
if emojis == nil then
u.notify("Could not read emoji file at " .. emoji_path, vim.log.levels.WARN)
end
local data_ok, data = pcall(vim.json.decode, emojis)
if not data_ok then
u.notify("Could not parse emoji file at " .. emoji_path, vim.log.levels.WARN)
end
M.emoji_map = data
M.emoji_list = {}
for _, v in pairs(M.emoji_map) do
table.insert(M.emoji_list, v)
end
end
-- Define the popup window options
M.popup_opts = {
relative = "cursor",
row = -2,
col = 0,
width = 2, -- Width set dynamically later
height = 1,
style = "minimal",
border = "single",
}
M.show_popup = function(char)
-- Close existing popup if it's open
if M.popup_win_id and vim.api.nvim_win_is_valid(M.popup_win_id) then
vim.api.nvim_win_close(M.popup_win_id, true)
end
-- Create a buffer for the popup window
local buf = vim.api.nvim_create_buf(false, true)
-- Set the content of the popup buffer to the character
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { char })
-- Open the popup window and store its ID
M.popup_win_id = vim.api.nvim_open_win(buf, false, M.popup_opts)
end
M.close_popup = function()
if M.popup_win_id and vim.api.nvim_win_is_valid(M.popup_win_id) then
vim.api.nvim_win_close(M.popup_win_id, true)
M.popup_win_id = nil -- Reset the window ID
end
end
M.init_popup = function(tree, bufnr)
vim.api.nvim_create_autocmd({ "CursorHold" }, {
callback = function()
local node = tree:get_node()
if node == nil then
return
end
local note_node = require("gitlab.actions.discussions").get_note_node(tree, node)
local root_node = require("gitlab.actions.discussions").get_root_node(tree, node)
local note_id_str = tostring(note_node.is_root and root_node.root_note_id or note_node.id)
local emojis = require("gitlab.actions.discussions").emojis
local note_emojis = emojis[note_id_str]
if note_emojis == nil then
return
end
local cursor_pos = vim.api.nvim_win_get_cursor(0)
vim.api.nvim_command('normal! "zyiw')
vim.api.nvim_win_set_cursor(0, cursor_pos)
local word = vim.fn.getreg("z")
for k, v in pairs(M.emoji_map) do
if v.moji == word then
local names = M.get_users_who_reacted_with_emoji(k, note_emojis)
M.popup_opts.width = string.len(names)
if M.popup_opts.width > 0 then
M.show_popup(names)
end
end
end
end,
buffer = bufnr,
})
vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
callback = function()
M.close_popup()
end,
buffer = bufnr,
})
end
---@param name string
---@return string
M.get_users_who_reacted_with_emoji = function(name, note_emojis)
local result = ""
for _, v in pairs(note_emojis) do
if v.name == name then
result = result .. v.user.name .. ", "
end
end
return string.len(result) > 3 and result:sub(1, -3) or result
end
M.pick_emoji = function(options, cb)
vim.ui.select(options, {
prompt = "Choose emoji",
format_item = function(val)
return string.format("%s %s", val.moji, val.name)
end,
}, function(choice)
if not choice then
return
end
local name = choice.shortname:sub(2, -2)
cb(name, choice)
end)
end
return M

78
lua/gitlab/hover.lua Normal file
View File

@@ -0,0 +1,78 @@
local M = {}
M.show_popup = function(char)
-- Close existing popup if it's open
if M.popup_win_id and vim.api.nvim_win_is_valid(M.popup_win_id) then
vim.api.nvim_win_close(M.popup_win_id, true)
end
-- Create a buffer for the popup window
local buf = vim.api.nvim_create_buf(false, true)
-- Set the content of the popup buffer to the character
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { char })
-- Define the popup window options
local opts = {
relative = "cursor",
row = -2,
col = 0,
width = 2, -- Width set to 2 to accommodate the border
height = 1,
style = "minimal",
border = "single",
}
-- Open the popup window and store its ID
M.popup_win_id = vim.api.nvim_open_win(buf, false, opts)
end
M.close_popup = function()
if M.popup_win_id and vim.api.nvim_win_is_valid(M.popup_win_id) then
vim.api.nvim_win_close(M.popup_win_id, true)
M.popup_win_id = nil -- Reset the window ID
end
end
M.init = function()
-- Set up autocommands
vim.api.nvim_create_autocmd({ "CursorHold" }, {
callback = function()
-- Get the current cursor position
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
row = row - 1 -- Adjust row because Lua is 1-indexed
col = col - 2 -- Adjust to account for > at front of line
if col < 1 then
return
end
-- Get the text of the current line
local line = vim.api.nvim_buf_get_lines(0, row, row + 1, false)[1]
-- Correctly handle multi-byte characters, such as emojis
local byteIndexStart = vim.str_byteindex(line, col)
local byteIndexEnd = vim.str_byteindex(line, col + 1)
-- Extract the character (or emoji) under the cursor
local char = line:sub(byteIndexStart + 1, byteIndexEnd)
-- Proceed only if char is not empty (to avoid empty popups)
if char == "" then
return
end
M.show_popup(char)
end,
buffer = vim.api.nvim_get_current_buf(),
})
vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
callback = function()
M.close_popup()
end,
buffer = vim.api.nvim_get_current_buf(),
})
end
return M

View File

@@ -1,6 +1,7 @@
local u = require("gitlab.utils")
local async = require("gitlab.async")
local server = require("gitlab.server")
local emoji = require("gitlab.emoji")
local state = require("gitlab.state")
local reviewer = require("gitlab.reviewer")
local discussions = require("gitlab.actions.discussions")
@@ -13,6 +14,7 @@ local create_mr = require("gitlab.actions.create_mr")
local approvals = require("gitlab.actions.approvals")
local labels = require("gitlab.actions.labels")
local user = state.dependencies.user
local info = state.dependencies.info
local labels_dep = state.dependencies.labels
local project_members = state.dependencies.project_members
@@ -28,6 +30,7 @@ return {
require("gitlab.colors") -- Sets colors
reviewer.init()
discussions.initialize_discussions() -- place signs / diagnostics for discussions in reviewer
emoji.init() -- Read in emojis for lookup purposes
end,
-- Global Actions 🌎
summary = async.sequence({ u.merge(info, { refresh = true }), labels_dep }, summary.summary),
@@ -45,7 +48,7 @@ return {
move_to_discussion_tree_from_diagnostic = async.sequence({}, discussions.move_to_discussion_tree),
create_note = async.sequence({ info }, comment.create_note),
create_mr = async.sequence({}, create_mr.start),
review = async.sequence({ u.merge(info, { refresh = true }), revisions }, function()
review = async.sequence({ u.merge(info, { refresh = true }), revisions, user }, function()
reviewer.open()
end),
close_review = function()
@@ -54,7 +57,7 @@ return {
pipeline = async.sequence({ info }, pipeline.open),
merge = async.sequence({ u.merge(info, { refresh = true }) }, merge.merge),
-- Discussion Tree Actions 🌴
toggle_discussions = async.sequence({ info }, discussions.toggle),
toggle_discussions = async.sequence({ info, user }, discussions.toggle),
edit_comment = async.sequence({ info }, discussions.edit_comment),
delete_comment = async.sequence({ info }, discussions.delete_comment),
toggle_resolved = async.sequence({ info }, discussions.toggle_discussion_resolved),

View File

@@ -6,6 +6,8 @@
local u = require("gitlab.utils")
local M = {}
M.emoji_map = nil
-- These are the default settings for the plugin
M.settings = {
port = nil, -- choose random port
@@ -49,6 +51,8 @@ M.settings = {
open_in_browser = "b",
reply = "r",
toggle_node = "t",
add_emoji = "Ea",
delete_emoji = "Ed",
toggle_all_discussions = "T",
toggle_resolved_discussions = "R",
toggle_unresolved_discussions = "U",
@@ -311,6 +315,7 @@ end
-- for each of the actions to occur. This is necessary because some Gitlab behaviors (like
-- adding a reviewer) requires some initial state.
M.dependencies = {
user = { endpoint = "/users/me", key = "user", state = "USER", refresh = false },
info = { endpoint = "/mr/info", key = "info", state = "INFO", refresh = false },
labels = { endpoint = "/mr/label", key = "labels", state = "LABELS", refresh = false },
revisions = { endpoint = "/mr/revisions", key = "Revisions", state = "MR_REVISIONS", refresh = false },

View File

@@ -38,6 +38,15 @@ M.filter = function(input_table, value_to_remove)
return resultTable
end
M.filter_by_key_value = function(input_table, target_key, target_value)
local result_table = {}
for _, v in ipairs(input_table) do
if v[target_key] ~= target_value then
table.insert(result_table, v)
end
end
end
---Merges two deeply nested tables together, overriding values from the first with conflicts
---@param defaults table The first table
---@param overrides table The second table