Skip to content

Commit 7bc3ea0

Browse files
Fix adding list items at the end of the file
1 parent da87ec4 commit 7bc3ea0

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

lua/orgmode/org/mappings.lua

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -521,22 +521,18 @@ function OrgMappings:handle_return(suffix)
521521
vim.cmd([[normal! ^]])
522522
item = Files.get_current_file():get_current_node()
523523
end
524-
525524
if item.type == 'paragraph' or item.type == 'bullet' then
526525
local listitem = item.node:parent()
527526
if listitem:type() ~= 'listitem' then
528527
return
529528
end
530529
local line = vim.fn.getline(listitem:start() + 1)
531-
local end_row, _ = listitem:end_()
532-
local next_line_node = current_file:get_node_at_cursor({ end_row + 1, 0 })
533-
local second_line_node = current_file:get_node_at_cursor({ end_row + 2, 0 })
534-
local is_end_of_file = next_line_node
535-
and vim.tbl_contains({ 'paragraph', 'list' }, next_line_node:type())
536-
and second_line_node
537-
and second_line_node:type() == 'document'
538-
-- Range for list items at the very end of the file are not calculated properly
539-
if (end_row + 1) == vim.fn.line('$') and is_end_of_file then
530+
local srow, _, end_row, end_col = listitem:range()
531+
local is_multiline = (end_row - srow) > 1 or end_col == 0
532+
-- For last item in file, ts grammar is not parsing the end column as 0
533+
-- while in other cases end column is always 0
534+
local is_last_item_in_file = end_col ~= 0
535+
if not is_multiline or is_last_item_in_file then
540536
end_row = end_row + 1
541537
end
542538
local range = {

tests/plenary/ui/mappings_spec.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,23 @@ describe('Mappings', function()
763763
}, vim.api.nvim_buf_get_lines(0, 7, 12, false))
764764
end)
765765

766+
it('should add numbered list at the end of the file', function()
767+
helpers.load_file_content({
768+
'* TODO Working on this now :OFFICE:NESTED:',
769+
' 1. First item',
770+
' 2. Second item',
771+
})
772+
773+
vim.fn.cursor(2, 1)
774+
vim.cmd([[exe "norm ,\<CR>"]])
775+
assert.are.same({
776+
'* TODO Working on this now :OFFICE:NESTED:',
777+
' 1. First item',
778+
' 2. ',
779+
' 3. Second item',
780+
}, vim.api.nvim_buf_get_lines(0, 0, -1, false))
781+
end)
782+
766783
it('should insert new heading after current subtree (org_insert_heading_respect_content)', function()
767784
helpers.load_file_content({
768785
'#TITLE: Test',

0 commit comments

Comments
 (0)