Skip to content

Commit dcb3414

Browse files
committed
fix(priority): don't insert empty [# ] cookie when clearing priority
Headline:set_priority only treated a blank priority as "remove the cookie" when a cookie already existed. The other two branches ran when the headline had no cookie yet. They always built a new cookie, even when the priority was blank. As a result, pressing <Space> at the priority prompt on a headline without an existing cookie returned " " from PriorityState:prompt_user, which fell through to an insertion branch and produced a literal [# ] cookie instead of clearing the priority. Bring the empty check to the top of set_priority so it applies to all paths: if the trimmed priority is empty, remove the existing cookie if present, otherwise do nothing. The insertion branches now only run with a real priority and can no longer fabricate an empty cookie.
1 parent 4f99b37 commit dcb3414

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

lua/orgmode/files/headline.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,15 @@ end
347347
---@param priority string
348348
function Headline:set_priority(priority)
349349
local _, priority_node = self:get_priority()
350+
if vim.trim(priority) == '' then
351+
if priority_node then
352+
return self:_set_node_text(priority_node, '')
353+
end
354+
return
355+
end
356+
350357
if priority_node then
351-
local text = (vim.trim(priority) == '') and '' or ('[#%s]'):format(priority)
352-
return self:_set_node_text(priority_node, text)
358+
return self:_set_node_text(priority_node, ('[#%s]'):format(priority))
353359
end
354360

355361
local todo, todo_node = self:get_todo()

tests/plenary/ui/mappings/priority_spec.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ describe('Priority mappings', function()
6262
assert.are.same('* TODO Test orgmode', vim.fn.getline(1))
6363
end)
6464

65+
it('should do nothing if <Space> is pressed on a line without a priority', function()
66+
alpha_config()
67+
helpers.create_file({
68+
'* TODO Test orgmode',
69+
})
70+
vim.fn.cursor(1, 1)
71+
assert.are.same('* TODO Test orgmode', vim.fn.getline(1))
72+
vim.cmd('norm ,o, \r')
73+
assert.are.same('* TODO Test orgmode', vim.fn.getline(1))
74+
end)
75+
6576
it('should add a priority if the item does not already have one', function()
6677
helpers.create_file({
6778
'* TODO Test orgmode',

0 commit comments

Comments
 (0)