diff --git a/lua/orgmode/config/init.lua b/lua/orgmode/config/init.lua index 81f117d9b..1c533b95c 100644 --- a/lua/orgmode/config/init.lua +++ b/lua/orgmode/config/init.lua @@ -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( @@ -333,7 +337,7 @@ 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() @@ -341,7 +345,7 @@ function Config:get_priorities() 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 diff --git a/lua/orgmode/objects/priority_state.lua b/lua/orgmode/objects/priority_state.lua index ef60d452a..72c1dcebc 100644 --- a/lua/orgmode/objects/priority_state.lua +++ b/lua/orgmode/objects/priority_state.lua @@ -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 @@ -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 diff --git a/tests/plenary/object/priority_state_spec.lua b/tests/plenary/object/priority_state_spec.lua index f9713bded..f10afb557 100644 --- a/tests/plenary/object/priority_state_spec.lua +++ b/tests/plenary/object/priority_state_spec.lua @@ -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() @@ -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('') diff --git a/tests/plenary/ui/mappings/priority_spec.lua b/tests/plenary/ui/mappings/priority_spec.lua index aca7f4716..0d2534246 100644 --- a/tests/plenary/ui/mappings/priority_spec.lua +++ b/tests/plenary/ui/mappings/priority_spec.lua @@ -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', }) @@ -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', }) @@ -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', }) @@ -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 is pressed', function() + it('should remove the alpha priority if is pressed', function() + alpha_config() helpers.create_file({ '* TODO [#B] Test orgmode', }) @@ -73,7 +99,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', }) @@ -83,7 +110,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', }) @@ -93,15 +121,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() @@ -115,18 +231,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', @@ -136,4 +252,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)