|
1 | 1 | local u = require("gitlab.utils") |
2 | 2 | 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" } |
4 | 7 |
|
5 | 8 | local M = { |
6 | | - ---@type EmojiMap|nil |
7 | | - emoji_map = {}, |
| 9 | + ---@type EmojiMap |
| 10 | + emoji_map = { ["+1"] = thumbsup, ["-1"] = thumbsdown }, |
8 | 11 | ---@type Emoji[] |
9 | | - emoji_list = {}, |
| 12 | + emoji_list = { thumbsup, thumbsdown }, |
10 | 13 | } |
11 | 14 |
|
| 15 | +-- Fetch emojis from Gitlab and make them available in the plugin. |
12 | 16 | 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) |
36 | 44 | end |
37 | 45 |
|
38 | 46 | -- Define the popup window options |
@@ -80,7 +88,7 @@ M.init_popup = function(tree, bufnr) |
80 | 88 | local note_node = common.get_note_node(tree, node) |
81 | 89 | local root_node = common.get_root_node(tree, node) |
82 | 90 | 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 |
84 | 92 |
|
85 | 93 | local note_emojis = emojis[note_id_str] |
86 | 94 | if note_emojis == nil then |
@@ -131,11 +139,12 @@ M.get_users_who_reacted_with_emoji = function(name, note_emojis) |
131 | 139 | end |
132 | 140 |
|
133 | 141 | M.pick_emoji = function(options, cb) |
| 142 | + local settings = require("gitlab.state").settings |
134 | 143 | vim.ui.select(options, { |
135 | 144 | prompt = "Choose emoji", |
136 | 145 | 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) |
139 | 148 | end |
140 | 149 | return string.format("%s %s", val.moji, val.name) |
141 | 150 | end, |
|
0 commit comments