Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1e97173

Browse files
fix(sort): Sort associative tables alphabetically where necessary
Closes #851
1 parent 4e4a14a commit 1e97173

File tree

7 files changed

+21
-11
lines changed

7 files changed

+21
-11
lines changed

lua/orgmode/agenda/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function Agenda:_build_custom_commands()
135135

136136
return opts_by_type[opts.type]
137137
end
138-
for shortcut, command in pairs(config.org_agenda_custom_commands) do
138+
for shortcut, command in utils.sorted_pairs(config.org_agenda_custom_commands) do
139139
table.insert(custom_commands, {
140140
label = command.description or '',
141141
key = shortcut,

lua/orgmode/agenda/types/agenda.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ function OrgAgendaType:_matches_filters(headline)
419419
self.agenda_filter,
420420
}
421421

422-
for _, filter in pairs(valid_filters) do
422+
for _, filter in ipairs(valid_filters) do
423423
if filter and not filter:matches(headline) then
424424
return false
425425
end

lua/orgmode/capture/init.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ end
558558
---@param templates table<string, OrgCaptureTemplate>
559559
function Capture:_get_subtemplates(base_key, templates)
560560
local subtemplates = {}
561-
for key, template in pairs(templates) do
561+
for key, template in utils.sorted_pairs(templates) do
562562
if string.len(key) > 1 and string.sub(key, 1, 1) == base_key then
563563
subtemplates[string.sub(key, 2, string.len(key))] = template
564564
end
@@ -570,7 +570,7 @@ end
570570
---@param templates table<string, OrgCaptureTemplate>
571571
function Capture:_create_menu_items(templates)
572572
local menu_items = {}
573-
for key, template in pairs(templates) do
573+
for key, template in utils.sorted_pairs(templates) do
574574
if string.len(key) == 1 then
575575
local item = {
576576
key = key,

lua/orgmode/export/init.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ function Export.prompt()
133133

134134
local exporters_names = {}
135135

136-
for name, opts in pairs(exporters) do
136+
for name, opts in utils.sorted_pairs(exporters) do
137137
table.insert(exporters_names, name)
138138

139139
opts.extension = extension
@@ -228,7 +228,7 @@ function Export.prompt()
228228
}
229229

230230
if not vim.tbl_isempty(config.org_custom_exports) then
231-
for key, data in pairs(config.org_custom_exports) do
231+
for key, data in utils.sorted_pairs(config.org_custom_exports) do
232232
table.insert(opts, {
233233
key = key,
234234
label = data.label,

lua/orgmode/objects/help.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ end
6565
function Help._generate_tree()
6666
local tree = {}
6767
local mappings = require('orgmode.config.mappings')
68-
for category, category_mappings in pairs(mappings) do
68+
for category, category_mappings in utils.sorted_pairs(mappings) do
6969
tree[category] = {}
70-
for key, mapping in pairs(category_mappings) do
70+
for key, mapping in utils.sorted_pairs(category_mappings) do
7171
if mapping.help_desc then
7272
table.insert(tree[category], { key = key, description = mapping.help_desc })
7373
end

lua/orgmode/utils/init.lua

+11-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ end
178178
---@param acc any
179179
---@return unknown
180180
function utils.reduce(tbl, callback, acc)
181-
for i, v in pairs(tbl) do
181+
for i, v in ipairs(tbl) do
182182
acc = callback(acc, v, i)
183183
end
184184
return acc
@@ -604,4 +604,14 @@ function utils.is_org_file(filename)
604604
return ext == 'org' or ext == 'org_archive'
605605
end
606606

607+
function utils.sorted_pairs(t)
608+
local keys = vim.tbl_keys(t)
609+
table.sort(keys)
610+
local i = 0
611+
return function()
612+
i = i + 1
613+
return keys[i], t[keys[i]]
614+
end
615+
end
616+
607617
return utils

tests/plenary/org/indent_spec.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ describe('with "indent" and "VirtualIndent" is enabled', function()
378378
{ ' #+END_SRC', 5 },
379379
}
380380
local content = {}
381-
for _, content_virtcol in pairs(content_virtcols) do
381+
for _, content_virtcol in ipairs(content_virtcols) do
382382
table.insert(content, content_virtcol[1])
383383
end
384384
helpers.create_file(content)
@@ -448,7 +448,7 @@ describe('with "indent" and "VirtualIndent" is enabled', function()
448448
{ ' #+END_SRC', 5 },
449449
}
450450
local content = {}
451-
for _, content_virtcol in pairs(content_virtcols) do
451+
for _, content_virtcol in ipairs(content_virtcols) do
452452
table.insert(content, content_virtcol[1])
453453
end
454454
helpers.create_file(content)

0 commit comments

Comments
 (0)