From 8ccbed5dc05becb19b5376552110a1dbca82ba4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Wed, 20 Nov 2024 13:56:44 +0100 Subject: [PATCH 1/4] feat: show refresh animation in winbar --- doc/gitlab.nvim.txt | 1 + lua/gitlab/actions/discussions/init.lua | 3 ++- lua/gitlab/actions/discussions/winbar.lua | 24 +++++++++++++++++++++-- lua/gitlab/actions/draft_notes/init.lua | 1 + lua/gitlab/annotations.lua | 1 + lua/gitlab/init.lua | 1 - lua/gitlab/state.lua | 1 + lua/gitlab/utils/init.lua | 2 +- 8 files changed, 29 insertions(+), 5 deletions(-) diff --git a/doc/gitlab.nvim.txt b/doc/gitlab.nvim.txt index 523afa20..2f344f48 100644 --- a/doc/gitlab.nvim.txt +++ b/doc/gitlab.nvim.txt @@ -253,6 +253,7 @@ you call this function with no values the defaults will be used: collapsed = " ", -- Icon for collapsed discussion thread indentation = " ", -- Indentation Icon }, + spinner_chars = { "/", "|", "\\", "-" }, -- Characters for the refresh animation auto_open = true, -- Automatically open when the reviewer is opened default_view = "discussions" -- Show "discussions" or "notes" by default blacklist = {}, -- List of usernames to remove from tree (bots, CI, etc) diff --git a/lua/gitlab/actions/discussions/init.lua b/lua/gitlab/actions/discussions/init.lua index aaa4d48f..d4b0090f 100644 --- a/lua/gitlab/actions/discussions/init.lua +++ b/lua/gitlab/actions/discussions/init.lua @@ -55,6 +55,7 @@ end ---Makes API call to get the discussion data, stores it in the state, and calls the callback ---@param callback function|nil M.load_discussions = function(callback) + state.discussion_tree.last_updated = nil state.load_new_state("discussion_data", function(data) if not state.DISCUSSION_DATA then state.DISCUSSION_DATA = {} @@ -108,6 +109,7 @@ M.refresh_diagnostics_and_winbar = function() end winbar.update_winbar() common.add_empty_titles() + state.discussion_tree.last_updated = os.time() end ---Opens the discussion tree, sets the keybindings. It also @@ -603,7 +605,6 @@ M.set_tree_keymaps = function(tree, bufnr, unlinked) if keymaps.discussion_tree.refresh_data then vim.keymap.set("n", keymaps.discussion_tree.refresh_data, function() - u.notify("Refreshing data...", vim.log.levels.INFO) draft_notes.rebuild_view(unlinked, false) end, { buffer = bufnr, diff --git a/lua/gitlab/actions/discussions/winbar.lua b/lua/gitlab/actions/discussions/winbar.lua index f526a2ac..c3fa8617 100644 --- a/lua/gitlab/actions/discussions/winbar.lua +++ b/lua/gitlab/actions/discussions/winbar.lua @@ -54,7 +54,19 @@ local get_data = function(nodes) return total_resolvable, total_resolved, total_non_resolvable end +local spinner_index = 0 +state.discussion_tree.last_updated = nil + local function content() + local updated + if state.discussion_tree.last_updated then + local last_update = tostring(os.date("!%Y-%m-%dT%H:%M:%S", state.discussion_tree.last_updated)) + updated = "Updated: " .. u.time_since(last_update) + else + spinner_index = (spinner_index % #state.settings.discussion_tree.spinner_chars) + 1 + updated = "Updated: " .. state.settings.discussion_tree.spinner_chars[spinner_index] + end + local resolvable_discussions, resolved_discussions, non_resolvable_discussions = get_data(state.DISCUSSION_DATA.discussions) local resolvable_notes, resolved_notes, non_resolvable_notes = get_data(state.DISCUSSION_DATA.unlinked_discussions) @@ -82,9 +94,10 @@ local function content() resolved_notes = resolved_notes, non_resolvable_notes = non_resolvable_notes, help_keymap = state.settings.keymaps.help, + updated = updated, } - return M.make_winbar(t) + return state.settings.discussion_tree.winbar and state.settings.discussion_tree.winbar(t) or M.make_winbar(t) end ---This function updates the winbar @@ -175,13 +188,16 @@ M.make_winbar = function(t) -- Join everything together and return it local separator = "%#Comment#|" local end_section = "%=" + local updated = "%#Text#" .. t.updated local help = "%#Comment#Help: " .. (t.help_keymap and t.help_keymap:gsub(" ", "") .. " " or "unmapped") return string.format( - " %s %s %s %s %s %s %s %s %s", + " %s %s %s %s %s %s %s %s %s %s %s", discussion_title, separator, notes_title, end_section, + updated, + separator, sort_method, separator, mode, @@ -225,4 +241,8 @@ M.switch_view_type = function(override) M.update_winbar() end +-- Set up a timer to update the winbar periodically +local timer = vim.uv.new_timer() +timer:start(0, 100, vim.schedule_wrap(M.update_winbar)) + return M diff --git a/lua/gitlab/actions/draft_notes/init.lua b/lua/gitlab/actions/draft_notes/init.lua index ea7b5b42..e23665e9 100755 --- a/lua/gitlab/actions/draft_notes/init.lua +++ b/lua/gitlab/actions/draft_notes/init.lua @@ -25,6 +25,7 @@ end ---Makes API call to get the discussion data, stores it in the state, and calls the callback ---@param callback function|nil M.load_draft_notes = function(callback) + state.discussion_tree.last_updated = nil state.load_new_state("draft_notes", function() if callback ~= nil then callback() diff --git a/lua/gitlab/annotations.lua b/lua/gitlab/annotations.lua index 9b1ae923..b1ade3c8 100644 --- a/lua/gitlab/annotations.lua +++ b/lua/gitlab/annotations.lua @@ -92,6 +92,7 @@ ---@field resolved_notes number ---@field non_resolvable_notes number ---@field help_keymap string +---@field updated string --- ---@class SignTable ---@field name string diff --git a/lua/gitlab/init.lua b/lua/gitlab/init.lua index c0f2aab5..f17c6320 100644 --- a/lua/gitlab/init.lua +++ b/lua/gitlab/init.lua @@ -96,7 +96,6 @@ return { publish_all_drafts = draft_notes.publish_all_drafts, refresh_data = function() -- This also rebuilds the regular views - u.notify("Refreshing data...", vim.log.levels.INFO) draft_notes.rebuild_view(false, true) end, -- Other functions 🤷 diff --git a/lua/gitlab/state.lua b/lua/gitlab/state.lua index 22278c67..1da4396f 100644 --- a/lua/gitlab/state.lua +++ b/lua/gitlab/state.lua @@ -151,6 +151,7 @@ M.settings = { collapsed = " ", indentation = " ", }, + spinner_chars = { "-", "\\", "|", "/" }, auto_open = true, default_view = "discussions", blacklist = {}, diff --git a/lua/gitlab/utils/init.lua b/lua/gitlab/utils/init.lua index aa71136b..34c87a59 100644 --- a/lua/gitlab/utils/init.lua +++ b/lua/gitlab/utils/init.lua @@ -122,7 +122,7 @@ M.time_since = function(date_string, current_date_table) local time_diff = current_date - date if time_diff < 60 then - return M.pluralize(time_diff, "second") .. " ago" + return "moments ago" elseif time_diff < 3600 then return M.pluralize(math.floor(time_diff / 60), "minute") .. " ago" elseif time_diff < 86400 then From 949577e5811d5501e61ba9cec1eeeba7fd29288c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Sat, 23 Nov 2024 02:06:28 +0100 Subject: [PATCH 2/4] perf: remove unnecessary update_winbar calls Since the winbar is now updated automatically, it's not necessary to update it explicitly. For some reason, however, removing the `update_winbar` call in `switch_view_type()` (`gitlab/actions/discussions/winbar.lua`) causes a minor screen flicker, so I've kept that one in. --- lua/gitlab/actions/discussions/init.lua | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/lua/gitlab/actions/discussions/init.lua b/lua/gitlab/actions/discussions/init.lua index d4b0090f..6811bd7c 100644 --- a/lua/gitlab/actions/discussions/init.lua +++ b/lua/gitlab/actions/discussions/init.lua @@ -48,7 +48,7 @@ M.rebuild_view = function(unlinked, all) else M.rebuild_discussion_tree() end - M.refresh_diagnostics_and_winbar() + M.refresh_diagnostics() end) end @@ -73,7 +73,7 @@ end M.initialize_discussions = function() signs.setup_signs() reviewer.set_callback_for_file_changed(function() - M.refresh_diagnostics_and_winbar() + M.refresh_diagnostics() M.modifiable(false) reviewer.set_reviewer_keymaps() end) @@ -103,11 +103,10 @@ M.modifiable = function(bool) end --- Take existing data and refresh the diagnostics, the winbar, and the signs -M.refresh_diagnostics_and_winbar = function() +M.refresh_diagnostics = function() if state.settings.discussion_signs.enabled then diagnostics.refresh_diagnostics() end - winbar.update_winbar() common.add_empty_titles() state.discussion_tree.last_updated = os.time() end @@ -156,7 +155,7 @@ M.open = function(callback) end vim.schedule(function() - M.refresh_diagnostics_and_winbar() + M.refresh_diagnostics() end) end @@ -587,7 +586,7 @@ M.set_tree_keymaps = function(tree, bufnr, unlinked) if keymaps.discussion_tree.jump_to_reviewer then vim.keymap.set("n", keymaps.discussion_tree.jump_to_reviewer, function() if M.is_current_node_note(tree) then - common.jump_to_reviewer(tree, M.refresh_diagnostics_and_winbar) + common.jump_to_reviewer(tree, M.refresh_diagnostics) end end, { buffer = bufnr, desc = "Jump to reviewer", nowait = keymaps.discussion_tree.jump_to_reviewer_nowait }) end @@ -803,7 +802,6 @@ end ---Toggle between draft mode (comments posted as drafts) and live mode (comments are posted immediately) M.toggle_draft_mode = function() state.settings.discussion_tree.draft_mode = not state.settings.discussion_tree.draft_mode - winbar.update_winbar() end ---Toggle between sorting by "original comment" (oldest at the top) or "latest reply" (newest at the From 4ed8b515475daddd23956e1281acfa5387d59254 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Sat, 23 Nov 2024 08:51:45 +0100 Subject: [PATCH 3/4] Update lua/gitlab/utils/init.lua fix: unify with Gitlab.com --- lua/gitlab/utils/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/gitlab/utils/init.lua b/lua/gitlab/utils/init.lua index 34c87a59..b75c597e 100644 --- a/lua/gitlab/utils/init.lua +++ b/lua/gitlab/utils/init.lua @@ -122,7 +122,7 @@ M.time_since = function(date_string, current_date_table) local time_diff = current_date - date if time_diff < 60 then - return "moments ago" + return "just now" elseif time_diff < 3600 then return M.pluralize(math.floor(time_diff / 60), "minute") .. " ago" elseif time_diff < 86400 then From bd3a92b2f5b36fabbf00b8e7407c026e72c52c73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20F=2E=20Bortl=C3=ADk?= Date: Wed, 27 Nov 2024 20:35:02 +0100 Subject: [PATCH 4/4] fix: set last_updated time in the correct place Setting last_updated in the `discussions.refresh_dagnostics` caused incorrect last_updated time when switching tabs (due to `discussions.refresh_dagnostics` being included the callback for file changes with the DiffviewDiffBufWinEnter User autocommand). --- lua/gitlab/actions/discussions/init.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/gitlab/actions/discussions/init.lua b/lua/gitlab/actions/discussions/init.lua index 6811bd7c..7130bfe2 100644 --- a/lua/gitlab/actions/discussions/init.lua +++ b/lua/gitlab/actions/discussions/init.lua @@ -48,6 +48,7 @@ M.rebuild_view = function(unlinked, all) else M.rebuild_discussion_tree() end + state.discussion_tree.last_updated = os.time() M.refresh_diagnostics() end) end @@ -71,6 +72,7 @@ end ---Initialize everything for discussions like setup of signs, callbacks for reviewer, etc. M.initialize_discussions = function() + state.discussion_tree.last_updated = os.time() signs.setup_signs() reviewer.set_callback_for_file_changed(function() M.refresh_diagnostics() @@ -108,7 +110,6 @@ M.refresh_diagnostics = function() diagnostics.refresh_diagnostics() end common.add_empty_titles() - state.discussion_tree.last_updated = os.time() end ---Opens the discussion tree, sets the keybindings. It also