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
7 changes: 6 additions & 1 deletion lua/orgmode/files/headline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,12 @@ function Headline:set_tags(tags)
local end_col = line:len()

local text = ''
tags = vim.trim(tags):gsub('^:', ''):gsub(':$', '')
tags = vim
.trim(tags)
:gsub('[%s:-]+', ':') -- Convert all whitespace, existing colons and hyphens into a single colon
:gsub('^:', '')
:gsub(':$', '')

if tags ~= '' then
tags = ':' .. tags .. ':'

Expand Down
24 changes: 20 additions & 4 deletions tests/plenary/api/api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,27 @@ describe('Api', function()
assert.are.same('Second level', current_file.headlines[2].title)
assert.are.same({ 'WORK', 'OFFICE', 'NESTEDTAG' }, current_file.headlines[2].all_tags)

current_file.headlines[2]:set_tags({ 'PERSONAL', 'HEALTH' }):wait()
current_file.headlines[2]:set_tags({ 'PERSONAL', 'HEALTH', 'TAG1 TAG2', 'TAG3-TAG4-TAG5' }):wait()

assert.are.same({ 'PERSONAL', 'HEALTH', 'TAG1', 'TAG2', 'TAG3', 'TAG4', 'TAG5' }, cur_file().headlines[2].tags)
assert.are.same(
{ 'WORK', 'OFFICE', 'PERSONAL', 'HEALTH', 'TAG1', 'TAG2', 'TAG3', 'TAG4', 'TAG5' },
cur_file().headlines[2].all_tags
)
assert.is.True(vim.fn.getline(5):match(':PERSONAL:HEALTH:TAG1:TAG2:TAG3:TAG4:TAG5:$') ~= nil)
end)

it('should handle edge cases in tag input', function()
helpers.create_file({ '* TODO Test orgmode tags :TAG1:' })

assert.is.True(#api.load() > 1)
local current_file = cur_file()
assert.are.same({ 'TAG1' }, current_file.headlines[1].all_tags)

current_file.headlines[1]:set_tags({ ': -tag1- : tag2:::tag3 tag_4 : @tag5 :' }):wait()

assert.are.same({ 'PERSONAL', 'HEALTH' }, cur_file().headlines[2].tags)
assert.are.same({ 'WORK', 'OFFICE', 'PERSONAL', 'HEALTH' }, cur_file().headlines[2].all_tags)
assert.is.True(vim.fn.getline(5):match(':PERSONAL:HEALTH:$') ~= nil)
assert.are.same({ 'tag1', 'tag2', 'tag3', 'tag_4', '@tag5' }, cur_file().headlines[1].all_tags)
assert.is.True(vim.fn.getline(1):match(':tag1:tag2:tag3:tag_4:@tag5:') ~= nil)
end)

it('should cycle upwards through priorities, starting with default', function()
Expand Down