Skip to content

Commit ad88620

Browse files
refactor(agenda): Attach sort function to exported module
1 parent 630f997 commit ad88620

File tree

2 files changed

+50
-46
lines changed

2 files changed

+50
-46
lines changed

lua/orgmode/agenda/views/agenda.lua

+36-35
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,6 @@ local function sort_by_date_or_priority_or_category(a, b)
1616
return a.index < b.index
1717
end
1818

19-
---@param agenda_items OrgAgendaItem[]
20-
---@return OrgAgendaItem[]
21-
local function sort_agenda_items(agenda_items)
22-
table.sort(agenda_items, function(a, b)
23-
if a.is_same_day and b.is_same_day then
24-
if a.real_date:has_time() and not b.real_date:has_time() then
25-
return true
26-
end
27-
if b.real_date:has_time() and not a.real_date:has_time() then
28-
return false
29-
end
30-
if a.real_date:has_time() and b.real_date:has_time() then
31-
return a.real_date:is_before(b.real_date)
32-
end
33-
return sort_by_date_or_priority_or_category(a, b)
34-
end
35-
36-
if a.is_same_day and not b.is_same_day then
37-
if a.real_date:has_time() or (b.real_date:is_none() and not a.real_date:is_none()) then
38-
return true
39-
end
40-
end
41-
42-
if not a.is_same_day and b.is_same_day then
43-
if b.real_date:has_time() or (a.real_date:is_none() and not b.real_date:is_none()) then
44-
return false
45-
end
46-
end
47-
48-
return sort_by_date_or_priority_or_category(a, b)
49-
end)
50-
return agenda_items
51-
end
52-
5319
---@class OrgAgendaView
5420
---@field span string|number
5521
---@field from OrgDate
@@ -157,7 +123,7 @@ function AgendaView:_build_items()
157123
end
158124
end
159125

160-
date.agenda_items = sort_agenda_items(date.agenda_items)
126+
date.agenda_items = self._sort(date.agenda_items)
161127

162128
table.insert(agenda_days, date)
163129
end
@@ -348,4 +314,39 @@ function AgendaView:_format_day(day)
348314
return string.format('%-10s %s', day:format('%A'), day:format('%d %B %Y'))
349315
end
350316

317+
---@private
318+
---@param agenda_items OrgAgendaItem[]
319+
---@return OrgAgendaItem[]
320+
function AgendaView._sort(agenda_items)
321+
table.sort(agenda_items, function(a, b)
322+
if a.is_same_day and b.is_same_day then
323+
if a.real_date:has_time() and not b.real_date:has_time() then
324+
return true
325+
end
326+
if b.real_date:has_time() and not a.real_date:has_time() then
327+
return false
328+
end
329+
if a.real_date:has_time() and b.real_date:has_time() then
330+
return a.real_date:is_before(b.real_date)
331+
end
332+
return sort_by_date_or_priority_or_category(a, b)
333+
end
334+
335+
if a.is_same_day and not b.is_same_day then
336+
if a.real_date:has_time() or (b.real_date:is_none() and not a.real_date:is_none()) then
337+
return true
338+
end
339+
end
340+
341+
if not a.is_same_day and b.is_same_day then
342+
if b.real_date:has_time() or (a.real_date:is_none() and not b.real_date:is_none()) then
343+
return false
344+
end
345+
end
346+
347+
return sort_by_date_or_priority_or_category(a, b)
348+
end)
349+
return agenda_items
350+
end
351+
351352
return AgendaView

lua/orgmode/agenda/views/todos.lua

+14-11
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,6 @@ local utils = require('orgmode.utils')
44
local agenda_highlights = require('orgmode.colors.highlights')
55
local hl_map = agenda_highlights.get_agenda_hl_map()
66

7-
local function sort_todos(todos)
8-
table.sort(todos, function(a, b)
9-
if a:get_priority_sort_value() ~= b:get_priority_sort_value() then
10-
return a:get_priority_sort_value() > b:get_priority_sort_value()
11-
end
12-
return a:get_category() < b:get_category()
13-
end)
14-
return todos
15-
end
16-
177
---@class OrgAgendaTodosView
188
---@field items table[]
199
---@field content table[]
@@ -59,7 +49,7 @@ function AgendaTodosView:build()
5949
end
6050

6151
function AgendaTodosView.generate_view(items, content, filters)
62-
items = sort_todos(items)
52+
items = AgendaTodosView._sort(items)
6353
local offset = #content
6454
local longest_category = utils.reduce(items, function(acc, todo)
6555
return math.max(acc, vim.api.nvim_strwidth(todo:get_category()))
@@ -142,4 +132,17 @@ function AgendaTodosView.generate_todo_item(headline, longest_category, line_nr)
142132
}
143133
end
144134

135+
---@private
136+
---@param todos OrgHeadline[]
137+
---@return OrgHeadline[]
138+
function AgendaTodosView._sort(todos)
139+
table.sort(todos, function(a, b)
140+
if a:get_priority_sort_value() ~= b:get_priority_sort_value() then
141+
return a:get_priority_sort_value() > b:get_priority_sort_value()
142+
end
143+
return a:get_category() < b:get_category()
144+
end)
145+
return todos
146+
end
147+
145148
return AgendaTodosView

0 commit comments

Comments
 (0)