Skip to content

Commit aac1ebc

Browse files
authored
Feat: Enable toggling date format between relative and absolute (#491)
1 parent 4fc1bbd commit aac1ebc

5 files changed

Lines changed: 27 additions & 3 deletions

File tree

after/syntax/gitlab.vim

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ endif
55
let expanders = '^\s*\%(' . g:gitlab_discussion_tree_expander_open . '\|' . g:gitlab_discussion_tree_expander_closed . '\)'
66
let username = '@[a-zA-Z0-9.]\+'
77

8-
" Covers times like '14 days ago', 'just now', as well as 'October 3, 2024'
8+
" Covers times like '14 days ago', 'just now', as well as 'October 3, 2024', and '02/28/2025 at 00:50'
99
let time_ago = '\d\+ \w\+ ago'
1010
let formatted_date = '\w\+ \{1,2}\d\{1,2}, \d\{4}'
11-
let date = '\%(' . time_ago . '\|' . formatted_date . '\|just now\)'
11+
let absolute_time = '\d\{2}/\d\{2}/\d\{4} at \d\{2}:\d\{2}'
12+
let date = '\%(' . time_ago . '\|' . formatted_date . '\|' . absolute_time . '\|just now\)'
1213

1314
let published = date . ' \%(' . g:gitlab_discussion_tree_resolved . '\|' . g:gitlab_discussion_tree_unresolved . '\|' . g:gitlab_discussion_tree_unlinked . '\)\?'
1415
let state = ' \%(' . published . '\|' . g:gitlab_discussion_tree_draft . '\)'

doc/gitlab.nvim.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ you call this function with no values the defaults will be used:
213213
switch_view = "c", -- Toggle between the notes and discussions views
214214
toggle_tree_type = "i", -- Toggle type of discussion tree - "simple", or "by_file_name"
215215
publish_draft = "P", -- Publish the currently focused note/comment
216+
toggle_date_format = "dt", -- Toggle between date formats: relative (e.g., "5 days ago", "just now", "October 13, 2024" for dates more than a month ago) and absolute (e.g., "03/01/2024 at 11:43")
216217
toggle_draft_mode = "D", -- Toggle between draft mode (comments posted as drafts) and live mode (comments are posted immediately)
217218
toggle_sort_method = "st", -- Toggle whether discussions are sorted by the "latest_reply", or by "original_comment", see `:h gitlab.nvim.toggle_sort_method`
218219
toggle_node = "t", -- Open or close the discussion
@@ -267,6 +268,7 @@ you call this function with no values the defaults will be used:
267268
draft = "✎", -- Symbol to show next to draft comments/notes
268269
tree_type = "simple", -- Type of discussion tree - "simple" means just list of discussions, "by_file_name" means file tree with discussions under file
269270
draft_mode = false, -- Whether comments are posted as drafts as part of a review
271+
relative_date = true, -- Whether to show relative time like "5 days ago" or absolute time like "03/01/2025 at 01:43"
270272
winbar = nil, -- Custom function to return winbar title, should return a string. Provided with WinbarTable (defined in annotations.lua)
271273
-- If using lualine, please add "gitlab" to disabled file types, otherwise you will not see the winbar.
272274
},

lua/gitlab/actions/common.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ M.build_note_header = function(note)
1515
if note.note then
1616
return "@" .. state.USER.username .. " " .. state.settings.discussion_tree.draft
1717
end
18-
return "@" .. note.author.username .. " " .. u.time_since(note.created_at)
18+
local time = state.settings.discussion_tree.relative_date and u.time_since(note.created_at)
19+
or u.format_to_local(note.created_at, vim.fn.strftime("%z"))
20+
return "@" .. note.author.username .. " " .. time
1921
end
2022

2123
M.switch_can_edit_bufs = function(bool, ...)

lua/gitlab/actions/discussions/init.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,16 @@ M.set_tree_keymaps = function(tree, bufnr, unlinked)
649649
})
650650
end
651651

652+
if keymaps.discussion_tree.toggle_date_format then
653+
vim.keymap.set("n", keymaps.discussion_tree.toggle_date_format, function()
654+
M.toggle_date_format()
655+
end, {
656+
buffer = bufnr,
657+
desc = "Toggle date format",
658+
nowait = keymaps.discussion_tree.toggle_date_format_nowait,
659+
})
660+
end
661+
652662
if keymaps.discussion_tree.toggle_resolved then
653663
vim.keymap.set("n", keymaps.discussion_tree.toggle_resolved, function()
654664
if M.is_current_node_note(tree) and not M.is_draft_note(tree) then
@@ -809,6 +819,13 @@ M.toggle_sort_method = function()
809819
M.rebuild_view(false, true)
810820
end
811821

822+
---Toggle between displaying relative time (e.g., "5 days ago") and absolute time (e.g., "04/10/2025 at 22:49")
823+
M.toggle_date_format = function()
824+
state.settings.discussion_tree.relative_date = not state.settings.discussion_tree.relative_date
825+
M.rebuild_unlinked_discussion_tree()
826+
M.rebuild_discussion_tree()
827+
end
828+
812829
---Indicates whether the node under the cursor is a draft note or not
813830
---@param tree NuiTree
814831
---@return boolean

lua/gitlab/state.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ M.settings = {
115115
switch_view = "c",
116116
toggle_tree_type = "i",
117117
publish_draft = "P",
118+
toggle_date_format = "dt",
118119
toggle_draft_mode = "D",
119120
toggle_sort_method = "st",
120121
toggle_node = "t",
@@ -169,6 +170,7 @@ M.settings = {
169170
draft = "",
170171
tree_type = "simple",
171172
draft_mode = false,
173+
relative_date = true,
172174
},
173175
emojis = {
174176
formatter = nil,

0 commit comments

Comments
 (0)