Skip to content

Commit b80ea57

Browse files
committed
feat: pull emoji data from Gitlab
Instead of using an incomplete local emojis.json list, the plugin now pulls the emoji data from Gitlab. This makes sure the data is complete and doesn't contain invalid items, which would cause a Go server error (e.g., cmd/config/emojis.json contained ⛵ for ⛵).
1 parent 569f158 commit b80ea57

7 files changed

Lines changed: 69 additions & 41 deletions

File tree

cmd/config/emojis.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

doc/gitlab.nvim.txt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,18 @@ you call this function with no values the defaults will be used:
297297
emojis = {
298298
-- Function for modifying how emojis are displayed in the picker. This does not affect the actual selected emoji.
299299
-- The function is passed an emoji object as a paramter, e.g.,
300-
-- {"unicode": "1F44D","name": "thumbs up sign", "shortname": ":thumbsup:", "moji": "👍"}
300+
-- {"name": "thumbs up", "shortname": ":thumbsup:", "moji": "👍", "category": "People & Body"}
301301
-- This is useful if your editor/terminal/font/tmux does not render some emojis properly,
302-
-- e.g., you can remove skin tones and additionally show the shortname with
302+
-- e.g., you can remove skin tones and additionally show the shortname and category with
303303
-- formatter = function(val)
304-
-- return string.format("%s %s %s", val.moji:gsub("[\240][\159][\143][\187-\191]", ""), val.shortname, val.name)
304+
-- return string.format("%s %s %s (%s)", val.moji:gsub("[\240][\159][\143][\187-\191]", ""), val.shortname, val.name, val.category)
305305
-- end
306306
formatter = nil,
307+
-- The emoji version used by the Gitlab instance: https://{GITLAB_URL}/-/emojis/{VERSION}/emojis.json. Can be a function that receives the gitlab_url as an argument and returns the version string, e.g.:
308+
-- version = function(gitlab_url)
309+
-- return gitlab_url == "https://gitlab.com" and "4" or "3"
310+
-- end
311+
version = "4",
307312
},
308313
choose_merge_request = {
309314
open_reviewer = true, -- Open the reviewer window automatically after switching merge requests
@@ -595,6 +600,14 @@ To see who has reacted with an emoji, hover over the emoji. A popup will
595600
appear with anyone who has responded with that emoji. You can only delete
596601
emojis that you have responded with.
597602

603+
The emoji data are pulled from the Gitlab server. This makes sure the data is
604+
complete, as compared to storing a limited list of emojis in the plugin repo.
605+
However, this also means that your Gitlab instance can use a different emoji
606+
version than the default expected by this plugin. Use
607+
`settings.emojis.version` to adjust to the emojis version of your particular
608+
Gitlab instance. You need to find the right `{VERSION}` for
609+
https://{GITLAB_URL}/-/emojis/{VERSION}/emojis.json.
610+
598611

599612
UPLOADING FILES *gitlab.nvim.uploading-files*
600613

lua/gitlab/annotations.lua

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@
7171

7272
---@class EmojiMap: table<string, Emoji>
7373
---@class Emoji
74-
---@field unicode string
75-
---@field name string
76-
---@field shortname string
77-
---@field moji string
74+
---@field name string
75+
---@field shortname string
76+
---@field moji string
77+
---@field category string
7878

7979
---@class WinbarTable
8080
---@field view_type string
@@ -154,6 +154,7 @@
154154
---@field keymaps? Keymaps -- Keymaps for the plugin
155155
---@field popup? PopupSettings -- Settings for the popup windows
156156
---@field discussion_tree? DiscussionSettings -- Settings for the popup windows
157+
---@field emojis? EmojisSettings -- Settings for emojis
157158
---@field choose_merge_request? ChooseMergeRequestSettings -- Default settings when choosing a merge request
158159
---@field info? InfoSettings -- Settings for the "info" or "summary" view
159160
---@field mergeability_checks? MergeabilityChecksSettings -- Settings for the mergeability checks in the "summary" view
@@ -308,7 +309,11 @@
308309
---@field draft_mode? boolean -- Whether comments are posted as drafts as part of a review
309310
---@field winbar? function -- Custom function to return winbar title, should return a string. Provided with WinbarTable (defined in annotations.lua)
310311

311-
---@class ExpanderOpts: table<string string>
312+
---@class EmojisSettings: table
313+
---@field formatter? function -- Custom function to modify how emojis are displayed in the picker.
314+
---@field version? string|fun(gitlab_url: string):string -- The (function that returns the) emoji version used by the Gitlab instance: https://{GITLAB_URL}/-/emojis/{VERSION}/emojis.json.
315+
316+
---@class ExpanderOpts: table<string, string>
312317
---@field expanded? string -- Icon for expanded discussion thread
313318
---@field collapsed? string -- Icon for collapsed discussion thread
314319
---@field indentation? string -- Indentation Icon

lua/gitlab/async.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ M.sequence = function(dependencies, cb)
5151

5252
-- Sets configuration for plugin, if not already set
5353
if not state.initialized then
54-
if not state.setPluginConfiguration() then
54+
if not state.set_plugin_configuration() then
5555
return
5656
end
5757
end

lua/gitlab/emoji.lua

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,46 @@
11
local u = require("gitlab.utils")
22
local common = require("gitlab.actions.common")
3-
local state = require("gitlab.state")
3+
4+
-- Basic emoji aliases that are missing in Gitlab's list.
5+
local thumbsup = { name = "+1", shortname = ":+1:", moji = "👍", category = "People & Body" }
6+
local thumbsdown = { name = "-1", shortname = ":-1:", moji = "👎", category = "People & Body" }
47

58
local M = {
6-
---@type EmojiMap|nil
7-
emoji_map = {},
9+
---@type EmojiMap
10+
emoji_map = { ["+1"] = thumbsup, ["-1"] = thumbsdown },
811
---@type Emoji[]
9-
emoji_list = {},
12+
emoji_list = { thumbsup, thumbsdown },
1013
}
1114

15+
-- Fetch emojis from Gitlab and make them available in the plugin.
1216
M.init = function()
13-
local root_path = state.settings.root_path
14-
local emoji_path = root_path
15-
.. state.settings.file_separator
16-
.. "cmd"
17-
.. state.settings.file_separator
18-
.. "config"
19-
.. state.settings.file_separator
20-
.. "emojis.json"
21-
local emojis = u.read_file(emoji_path)
22-
if emojis == nil then
23-
u.notify("Could not read emoji file at " .. emoji_path, vim.log.levels.WARN)
24-
end
25-
26-
local data_ok, data = pcall(vim.json.decode, emojis)
27-
if not data_ok then
28-
u.notify("Could not parse emoji file at " .. emoji_path, vim.log.levels.WARN)
29-
end
30-
31-
M.emoji_map = data
32-
M.emoji_list = {}
33-
for _, v in pairs(M.emoji_map) do
34-
table.insert(M.emoji_list, v)
35-
end
17+
local settings = require("gitlab.state").settings
18+
local version = type(settings.emojis.version) == "function" and settings.emojis.version(settings.gitlab_url)
19+
or settings.emojis.version
20+
local emojis_url = settings.gitlab_url .. "/-/emojis/" .. version .. "/emojis.json"
21+
local command = { "curl", emojis_url }
22+
vim.system(command, { text = true }, function(result)
23+
vim.schedule(function()
24+
if result.code ~= 0 then
25+
require("gitlab.utils").notify(result.stderr, vim.log.levels.ERROR)
26+
else
27+
local data_ok, data = pcall(vim.json.decode, result.stdout)
28+
if not data_ok then
29+
local message = "Could not get emojis from "
30+
.. emojis_url
31+
.. (data ~= nil and string.format(". JSON.decode error: %s", data) or "")
32+
u.notify(message, vim.log.levels.ERROR)
33+
u.notify(result.stdout, vim.log.levels.DEBUG)
34+
return
35+
end
36+
for _, v in ipairs(data or {}) do
37+
local emoji = { name = v.d, shortname = string.format(":%s:", v.n), moji = v.e, category = v.c }
38+
M.emoji_map[v.n] = emoji
39+
table.insert(M.emoji_list, emoji)
40+
end
41+
end
42+
end)
43+
end)
3644
end
3745

3846
-- Define the popup window options
@@ -80,7 +88,7 @@ M.init_popup = function(tree, bufnr)
8088
local note_node = common.get_note_node(tree, node)
8189
local root_node = common.get_root_node(tree, node)
8290
local note_id_str = tostring(note_node.is_root and root_node.root_note_id or note_node.id)
83-
local emojis = state.DISCUSSION_DATA.emojis
91+
local emojis = require("gitlab.state").DISCUSSION_DATA.emojis
8492

8593
local note_emojis = emojis[note_id_str]
8694
if note_emojis == nil then
@@ -131,11 +139,12 @@ M.get_users_who_reacted_with_emoji = function(name, note_emojis)
131139
end
132140

133141
M.pick_emoji = function(options, cb)
142+
local settings = require("gitlab.state").settings
134143
vim.ui.select(options, {
135144
prompt = "Choose emoji",
136145
format_item = function(val)
137-
if type(state.settings.emojis.formatter) == "function" then
138-
return state.settings.emojis.formatter(val)
146+
if type(settings.emojis.formatter) == "function" then
147+
return settings.emojis.formatter(val)
139148
end
140149
return string.format("%s %s", val.moji, val.name)
141150
end,

lua/gitlab/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ local function setup(args)
4545
end
4646

4747
state.merge_settings(args) -- Merges user settings with default settings
48+
state.set_plugin_configuration() -- Sets auth_token and gitlab_url into the settings module
4849
server.build() -- Builds the Go binary if it doesn't exist
4950
state.set_global_keymaps() -- Sets keymaps that are not bound to a specific buffer
5051
require("gitlab.colors") -- Sets colors

lua/gitlab/state.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ M.settings = {
190190
},
191191
emojis = {
192192
formatter = nil,
193+
version = "4",
193194
},
194195
create_mr = {
195196
target = nil,
@@ -501,7 +502,7 @@ end
501502
-- then attemps to read a `.gitlab.nvim` configuration file.
502503
-- If after doing this, any variables are missing, alerts the user.
503504
-- The `.gitlab.nvim` configuration file takes precedence.
504-
M.setPluginConfiguration = function()
505+
M.set_plugin_configuration = function()
505506
if M.initialized then
506507
return true
507508
end

0 commit comments

Comments
 (0)