diff --git a/lua/orgmode/files/headline.lua b/lua/orgmode/files/headline.lua index f494b1483..332064a55 100644 --- a/lua/orgmode/files/headline.lua +++ b/lua/orgmode/files/headline.lua @@ -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 .. ':' diff --git a/tests/plenary/api/api_spec.lua b/tests/plenary/api/api_spec.lua index 16636d31e..8c0748086 100644 --- a/tests/plenary/api/api_spec.lua +++ b/tests/plenary/api/api_spec.lua @@ -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()