Skip to content

Commit b42236a

Browse files
authored
fix: meta_return on a link item (#943)
* factor out find item or headline * add a test
1 parent 79939cc commit b42236a

File tree

3 files changed

+54
-28
lines changed

3 files changed

+54
-28
lines changed

lua/orgmode/org/mappings.lua

+3-28
Original file line numberDiff line numberDiff line change
@@ -618,19 +618,7 @@ end
618618

619619
function OrgMappings:meta_return(suffix)
620620
suffix = suffix or ''
621-
local item = ts_utils.get_node_at_cursor()
622-
623-
if not item then
624-
return
625-
end
626-
627-
if item:type() == 'expr' then
628-
item = item:parent()
629-
end
630-
631-
if item and item:parent() and item:parent():type() == 'headline' then
632-
item = item:parent()
633-
end
621+
local item = ts_utils.closest_item_or_headline_node()
634622

635623
if not item then
636624
return
@@ -646,21 +634,8 @@ function OrgMappings:meta_return(suffix)
646634
return true
647635
end
648636

649-
if item:type() == 'list' or item:type() == 'listitem' then
650-
vim.cmd([[normal! ^]])
651-
item = ts_utils.get_node_at_cursor()
652-
end
653-
if not item then
654-
return
655-
end
656-
local type = item:type()
657-
if vim.tbl_contains({ 'paragraph', 'bullet', 'checkbox', 'status' }, type) then
658-
local listitem = item:parent()
659-
if not listitem or listitem:type() ~= 'listitem' then
660-
return
661-
end
662-
return self:_insert_item_below(listitem)
663-
end
637+
-- item is a listitem here
638+
return self:_insert_item_below(item)
664639
end
665640

666641
---@private

lua/orgmode/utils/treesitter/init.lua

+31
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,37 @@ function M.find_headline(node)
5252
return nil
5353
end
5454

55+
-- walks the tree to find an item or headline
56+
---@param node TSNode | nil
57+
---@return TSNode | nil
58+
function M.find_item_or_headline(node)
59+
if not node then
60+
return nil
61+
end
62+
63+
local node_type = node:type()
64+
if node_type == 'headline' or node_type == 'listitem' then
65+
return node
66+
end
67+
68+
if node_type == 'list' then
69+
-- Move right one character to pick up the current listitem
70+
vim.cmd([[norm l]])
71+
return M.find_item_or_headline(M.get_node_at_cursor())
72+
end
73+
74+
if node_type == 'section' then
75+
-- The headline is always the first child of a section
76+
return node:field('headline')[1]
77+
end
78+
return M.find_item_or_headline(node:parent())
79+
end
80+
81+
-- returns the nearest item or headline
82+
function M.closest_item_or_headline_node(cursor)
83+
return M.find_item_or_headline(M.get_node_at_cursor(cursor))
84+
end
85+
5586
-- returns the nearest headline
5687
function M.closest_headline_node(cursor)
5788
local node = M.get_node_at_cursor(cursor)

tests/plenary/ui/mappings/meta_return_spec.lua

+20
Original file line numberDiff line numberDiff line change
@@ -522,4 +522,24 @@ describe('Meta return mappings', function()
522522
}, vim.api.nvim_buf_get_lines(0, 3, 10, false))
523523
end
524524
)
525+
526+
it('should add list item after a link with Enter (org_meta_return)', function()
527+
helpers.create_agenda_file({
528+
'#TITLE: Test',
529+
'',
530+
'* TODO Test orgmode',
531+
' - Regular item',
532+
' - [[http://www.com][a link]]',
533+
' - [x] Checkbox item',
534+
})
535+
536+
vim.fn.cursor(5, 14)
537+
vim.cmd([[exe "norm ,\<CR>"]])
538+
assert.are.same({
539+
' - Regular item',
540+
' - [[http://www.com][a link]]',
541+
' - ',
542+
' - [x] Checkbox item',
543+
}, vim.api.nvim_buf_get_lines(0, 3, 10, false))
544+
end)
525545
end)

0 commit comments

Comments
 (0)