Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lua/orgmode/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,12 @@ function Config:get_priorities()
return self.priorities
end

local highest = tostring(self.opts.org_priority_highest)
local default = tostring(self.opts.org_priority_default)
local lowest = tostring(self.opts.org_priority_lowest)

local priorities = {
[self.opts.org_priority_highest] = { type = 'highest', hl_group = '@org.priority.highest' },
[highest] = { type = 'highest', hl_group = '@org.priority.highest' },
}

local current_prio = PriorityState:new(
Expand All @@ -333,15 +337,15 @@ function Config:get_priorities()
end

-- we need to overwrite the default value set by the first loop
priorities[self.opts.org_priority_default] = { type = 'default', hl_group = '@org.priority.default' }
priorities[default] = { type = 'default', hl_group = '@org.priority.default' }

while current_prio:as_num() < current_prio:lowest_as_num() do
current_prio:decrease()
priorities[current_prio.priority] = { type = 'low', hl_group = '@org.priority.low' }
end

-- we need to overwrite the lowest value set by the second loop
priorities[self.opts.org_priority_lowest] = { type = 'lowest', hl_group = '@org.priority.lowest' }
priorities[lowest] = { type = 'lowest', hl_group = '@org.priority.lowest' }

-- Cache priorities to avoid unnecessary recalculations
self.priorities = priorities
Expand Down
13 changes: 9 additions & 4 deletions lua/orgmode/objects/priority_state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@ function PriorityState:prompt_user()

choice = string.upper(choice)
if #choice > 1 and tonumber(choice) == nil then
utils.echo_warning(string.format('Only numeric priorities can be multiple characters long'))
utils.echo_warning('Only numeric priorities can be multiple characters long')
return nil
end
local choicenum = string.byte(choice)
if choice ~= ' ' and (choicenum < string.byte(self.high_priority) or choicenum > string.byte(self.low_priority)) then

if choice == ' ' then
return choice
end

local choicenum = PriorityState._as_number(choice)
if choicenum < self:highest_as_num() or choicenum > self:lowest_as_num() then
utils.echo_warning(string.format("Priority must be between '%s' and '%s'", self.high_priority, self.low_priority))
return nil
end
Expand All @@ -51,7 +56,7 @@ end

---@return number
function PriorityState:get_sort_value()
return -1 * string.byte(self.priority == '' and self.default_priority or self.priority)
return -1 * PriorityState._as_number(self.priority == '' and self.default_priority or self.priority)
end

---@return string
Expand Down
26 changes: 23 additions & 3 deletions tests/plenary/object/priority_state_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,16 @@ describe('Priority state', function()
assert.are.same(-65, priority:get_sort_value())
end)

it('should return the string representation of the value to use for sorting for numeric strings', function()
it('should return the numeric value for sorting single-digit numeric priorities', function()
numeric_config()
local priority = create_priority(1)
assert.are.same(-49, priority:get_sort_value())
assert.are.same(-1, priority:get_sort_value())
end)

it('should return the numeric value for sorting multi-digit numeric priorities', function()
numeric_config()
local priority = create_priority(10)
assert.are.same(-10, priority:get_sort_value())
end)

it('should return default priority value if empty when sorting', function()
Expand All @@ -107,13 +113,27 @@ describe('Priority state', function()
assert.is.True(higher:get_sort_value() > lower:get_sort_value())
end)

it('should compare numeric priorities correctly', function()
it('should compare single-digit numeric priorities correctly', function()
numeric_config()
local higher = create_priority(1)
local lower = create_priority(2)
assert.is.True(higher:get_sort_value() > lower:get_sort_value())
end)

it('should compare single and multi-digit numeric priorities correctly', function()
numeric_config()
local higher = create_priority(9)
local lower = create_priority(10)
assert.is.True(higher:get_sort_value() > lower:get_sort_value())
end)

it('should compare multi-digit numeric priorities correctly', function()
numeric_config()
local higher = create_priority(10)
local lower = create_priority(13)
assert.is.True(higher:get_sort_value() > lower:get_sort_value())
end)

it('should change to default priority if priority increased and currently empty', function()
alpha_config()
local priority = create_priority('')
Expand Down
185 changes: 167 additions & 18 deletions tests/plenary/ui/mappings/priority_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ describe('Priority mappings', function()
vim.cmd([[silent! %bw!]])
end)

it('should increase the priority of the current headline', function()
it('should increase the alpha priority of the current headline', function()
alpha_config()
helpers.create_file({
'* TODO [#B] Test orgmode',
})
Expand All @@ -32,7 +33,8 @@ describe('Priority mappings', function()
assert.are.same('* TODO [#A] Test orgmode', vim.fn.getline(1))
end)

it('should decrease the priority of the current headline', function()
it('should decrease the alpha priority of the current headline', function()
alpha_config()
helpers.create_file({
'* TODO [#B] Test orgmode',
})
Expand All @@ -42,7 +44,30 @@ describe('Priority mappings', function()
assert.are.same('* TODO [#C] Test orgmode', vim.fn.getline(1))
end)

it('should set the priority based on the input key', function()
it('should increase the default alpha priority, when it is explicitly defined', function()
alpha_config()
helpers.create_file({
'* [#C] Test orgmode',
})
vim.fn.cursor(1, 1)
assert.are.same('* [#C] Test orgmode', vim.fn.getline(1))
vim.cmd('norm ciR')
assert.are.same('* [#B] Test orgmode', vim.fn.getline(1))
end)

it('should increase an alpha priority, which is not explicitly defined', function()
alpha_config()
helpers.create_file({
'* [#D] Test orgmode',
})
vim.fn.cursor(1, 1)
assert.are.same('* [#D] Test orgmode', vim.fn.getline(1))
vim.cmd('norm ciR')
assert.are.same('* [#C] Test orgmode', vim.fn.getline(1))
end)

it('should set the alpha priority based on the input key', function()
alpha_config()
helpers.create_file({
'* TODO [#B] Test orgmode',
})
Expand All @@ -52,7 +77,8 @@ describe('Priority mappings', function()
assert.are.same('* TODO [#A] Test orgmode', vim.fn.getline(1))
end)

it('should remove the priority if <Space> is pressed', function()
it('should remove the alpha priority if <Space> is pressed', function()
alpha_config()
helpers.create_file({
'* TODO [#B] Test orgmode',
})
Expand All @@ -62,7 +88,8 @@ describe('Priority mappings', function()
assert.are.same('* TODO Test orgmode', vim.fn.getline(1))
end)

it('should add a priority if the item does not already have one', function()
it('should add an alpha priority if the item does not already have one', function()
alpha_config()
helpers.create_file({
'* TODO Test orgmode',
})
Expand All @@ -72,7 +99,8 @@ describe('Priority mappings', function()
assert.are.same('* TODO [#A] Test orgmode', vim.fn.getline(1))
end)

it('should add a priority if the item has no todo keyword', function()
it('should add an alpha priority if the item has no todo keyword', function()
alpha_config()
helpers.create_file({
'* Test orgmode',
})
Expand All @@ -82,15 +110,103 @@ describe('Priority mappings', function()
assert.are.same('* [#A] Test orgmode', vim.fn.getline(1))
end)

it('should increase the default character priority, when it is explicitly defined', function()
alpha_config()
it('should add a numeric priority if the item does not already have one', function()
numeric_config()
helpers.create_file({
'* [#C] Test orgmode',
'* Test orgmode',
})
vim.fn.cursor(1, 1)
assert.are.same('* [#C] Test orgmode', vim.fn.getline(1))
assert.are.same('* Test orgmode', vim.fn.getline(1))
vim.cmd('norm ,o,5\r')
assert.are.same('* [#5] Test orgmode', vim.fn.getline(1))
end)

it('should increase the numeric priority based on the input key', function()
numeric_config()
helpers.create_file({
'* [#3] Test orgmode',
})
vim.fn.cursor(1, 1)
assert.are.same('* [#3] Test orgmode', vim.fn.getline(1))
vim.cmd('norm ,o,2\r')
assert.are.same('* [#2] Test orgmode', vim.fn.getline(1))
end)

it('should decrease the numeric priority based on the input key', function()
numeric_config()
helpers.create_file({
'* [#2] Test orgmode',
})
vim.fn.cursor(1, 1)
assert.are.same('* [#2] Test orgmode', vim.fn.getline(1))
vim.cmd('norm ,o,3\r')
assert.are.same('* [#3] Test orgmode', vim.fn.getline(1))
end)

it('should increase the numeric priority from single-digit to highest based on the input key', function()
numeric_config()
helpers.create_file({
'* [#3] Test orgmode',
})
vim.fn.cursor(1, 1)
assert.are.same('* [#3] Test orgmode', vim.fn.getline(1))
vim.cmd('norm ,o,1\r')
assert.are.same('* [#1] Test orgmode', vim.fn.getline(1))
end)

it('should increase the numeric priority from multi-digit to highest based on the input key', function()
numeric_config()
helpers.create_file({
'* [#13] Test orgmode',
})
vim.fn.cursor(1, 1)
assert.are.same('* [#13] Test orgmode', vim.fn.getline(1))
vim.cmd('norm ,o,1\r')
assert.are.same('* [#1] Test orgmode', vim.fn.getline(1))
end)

it('should decrease the numeric priority from highest to single-digit based on the input key', function()
numeric_config()
helpers.create_file({
'* [#1] Test orgmode',
})
vim.fn.cursor(1, 1)
assert.are.same('* [#1] Test orgmode', vim.fn.getline(1))
vim.cmd('norm ,o,3\r')
assert.are.same('* [#3] Test orgmode', vim.fn.getline(1))
end)

it('should decrease the numeric priority from highest to multi-digit based on the input key', function()
numeric_config()
helpers.create_file({
'* [#1] Test orgmode',
})
vim.fn.cursor(1, 1)
assert.are.same('* [#1] Test orgmode', vim.fn.getline(1))
vim.cmd('norm ,o,13\r')
assert.are.same('* [#13] Test orgmode', vim.fn.getline(1))
end)

it('should increase the numeric priority from lowest', function()
numeric_config()
helpers.create_file({
'* [#14] Test orgmode',
})
vim.fn.cursor(1, 1)
assert.are.same('* [#14] Test orgmode', vim.fn.getline(1))
vim.cmd('norm ciR')
assert.are.same('* [#B] Test orgmode', vim.fn.getline(1))
assert.are.same('* [#13] Test orgmode', vim.fn.getline(1))
end)

it('should decrease the numeric priority from highest', function()
numeric_config()
helpers.create_file({
'* [#1] Test orgmode',
})
vim.fn.cursor(1, 1)
assert.are.same('* [#1] Test orgmode', vim.fn.getline(1))
vim.cmd('norm cir')
assert.are.same('* [#2] Test orgmode', vim.fn.getline(1))
end)

it('should increase a numeric priority, which is not explicitly defined', function()
Expand All @@ -104,18 +220,18 @@ describe('Priority mappings', function()
assert.are.same('* [#4] Test orgmode', vim.fn.getline(1))
end)

it('should increase a character priority, which is not explicitly defined', function()
alpha_config()
it('should decrease a numeric priority, which is not explicitly defined', function()
numeric_config()
helpers.create_file({
'* [#D] Test orgmode',
'* [#5] Test orgmode',
})
vim.fn.cursor(1, 1)
assert.are.same('* [#D] Test orgmode', vim.fn.getline(1))
vim.cmd('norm ciR')
assert.are.same('* [#C] Test orgmode', vim.fn.getline(1))
assert.are.same('* [#5] Test orgmode', vim.fn.getline(1))
vim.cmd('norm cir')
assert.are.same('* [#6] Test orgmode', vim.fn.getline(1))
end)

it('should increase a numeric priority, which is not explicitly defined', function()
it('should increase the numeric priority across the digit boundary', function()
numeric_config()
helpers.create_file({
'* [#10] Test orgmode',
Expand All @@ -125,4 +241,37 @@ describe('Priority mappings', function()
vim.cmd('norm ciR')
assert.are.same('* [#9] Test orgmode', vim.fn.getline(1))
end)

it('should decrease the numeric priority across the digit boundary', function()
numeric_config()
helpers.create_file({
'* [#9] Test orgmode',
})
vim.fn.cursor(1, 1)
assert.are.same('* [#9] Test orgmode', vim.fn.getline(1))
vim.cmd('norm cir')
assert.are.same('* [#10] Test orgmode', vim.fn.getline(1))
end)

it('should wrap from highest to lowest numeric priority', function()
numeric_config()
helpers.create_file({
'* [#1] Test orgmode',
})
vim.fn.cursor(1, 1)
assert.are.same('* [#1] Test orgmode', vim.fn.getline(1))
vim.cmd('norm ciR')
assert.are.same('* [#15] Test orgmode', vim.fn.getline(1))
end)

it('should wrap from lowest to highest numeric priority', function()
numeric_config()
helpers.create_file({
'* [#15] Test orgmode',
})
vim.fn.cursor(1, 1)
assert.are.same('* [#15] Test orgmode', vim.fn.getline(1))
vim.cmd('norm cir')
assert.are.same('* [#1] Test orgmode', vim.fn.getline(1))
end)
end)
Loading