Skip to content

Commit

Permalink
fix(sort): Sort associative tables alphabetically where necessary
Browse files Browse the repository at this point in the history
Closes #851
  • Loading branch information
kristijanhusak committed Jan 15, 2025
1 parent 4e4a14a commit 1e97173
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lua/orgmode/agenda/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function Agenda:_build_custom_commands()

return opts_by_type[opts.type]
end
for shortcut, command in pairs(config.org_agenda_custom_commands) do
for shortcut, command in utils.sorted_pairs(config.org_agenda_custom_commands) do
table.insert(custom_commands, {
label = command.description or '',
key = shortcut,
Expand Down
2 changes: 1 addition & 1 deletion lua/orgmode/agenda/types/agenda.lua
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ function OrgAgendaType:_matches_filters(headline)
self.agenda_filter,
}

for _, filter in pairs(valid_filters) do
for _, filter in ipairs(valid_filters) do
if filter and not filter:matches(headline) then
return false
end
Expand Down
4 changes: 2 additions & 2 deletions lua/orgmode/capture/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ end
---@param templates table<string, OrgCaptureTemplate>
function Capture:_get_subtemplates(base_key, templates)
local subtemplates = {}
for key, template in pairs(templates) do
for key, template in utils.sorted_pairs(templates) do
if string.len(key) > 1 and string.sub(key, 1, 1) == base_key then
subtemplates[string.sub(key, 2, string.len(key))] = template
end
Expand All @@ -570,7 +570,7 @@ end
---@param templates table<string, OrgCaptureTemplate>
function Capture:_create_menu_items(templates)
local menu_items = {}
for key, template in pairs(templates) do
for key, template in utils.sorted_pairs(templates) do
if string.len(key) == 1 then
local item = {
key = key,
Expand Down
4 changes: 2 additions & 2 deletions lua/orgmode/export/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function Export.prompt()

local exporters_names = {}

for name, opts in pairs(exporters) do
for name, opts in utils.sorted_pairs(exporters) do
table.insert(exporters_names, name)

opts.extension = extension
Expand Down Expand Up @@ -228,7 +228,7 @@ function Export.prompt()
}

if not vim.tbl_isempty(config.org_custom_exports) then
for key, data in pairs(config.org_custom_exports) do
for key, data in utils.sorted_pairs(config.org_custom_exports) do
table.insert(opts, {
key = key,
label = data.label,
Expand Down
4 changes: 2 additions & 2 deletions lua/orgmode/objects/help.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ end
function Help._generate_tree()
local tree = {}
local mappings = require('orgmode.config.mappings')
for category, category_mappings in pairs(mappings) do
for category, category_mappings in utils.sorted_pairs(mappings) do
tree[category] = {}
for key, mapping in pairs(category_mappings) do
for key, mapping in utils.sorted_pairs(category_mappings) do
if mapping.help_desc then
table.insert(tree[category], { key = key, description = mapping.help_desc })
end
Expand Down
12 changes: 11 additions & 1 deletion lua/orgmode/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ end
---@param acc any
---@return unknown
function utils.reduce(tbl, callback, acc)
for i, v in pairs(tbl) do
for i, v in ipairs(tbl) do
acc = callback(acc, v, i)
end
return acc
Expand Down Expand Up @@ -604,4 +604,14 @@ function utils.is_org_file(filename)
return ext == 'org' or ext == 'org_archive'
end

function utils.sorted_pairs(t)
local keys = vim.tbl_keys(t)
table.sort(keys)
local i = 0
return function()
i = i + 1
return keys[i], t[keys[i]]
end
end

return utils
4 changes: 2 additions & 2 deletions tests/plenary/org/indent_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ describe('with "indent" and "VirtualIndent" is enabled', function()
{ ' #+END_SRC', 5 },
}
local content = {}
for _, content_virtcol in pairs(content_virtcols) do
for _, content_virtcol in ipairs(content_virtcols) do
table.insert(content, content_virtcol[1])
end
helpers.create_file(content)
Expand Down Expand Up @@ -448,7 +448,7 @@ describe('with "indent" and "VirtualIndent" is enabled', function()
{ ' #+END_SRC', 5 },
}
local content = {}
for _, content_virtcol in pairs(content_virtcols) do
for _, content_virtcol in ipairs(content_virtcols) do
table.insert(content, content_virtcol[1])
end
helpers.create_file(content)
Expand Down

0 comments on commit 1e97173

Please sign in to comment.