@@ -454,16 +454,26 @@ function OrgMappings:handle_return(suffix)
454454 item = Files .get_current_file ():get_current_node ()
455455 end
456456
457- if item .type == ' itemtext ' or item .type == ' bullet' or item . type == ' checkbox ' or item . type == ' description ' then
457+ if item .type == ' paragraph ' or item .type == ' bullet' then
458458 local list_item = item .node :parent ()
459459 if list_item :type () ~= ' listitem' then
460460 return
461461 end
462462 local line = vim .fn .getline (list_item :start () + 1 )
463463 local end_row , _ = list_item :end_ ()
464+ local next_line_node = current_file :get_node_at_cursor ({ end_row + 1 , 0 })
465+ local second_line_node = current_file :get_node_at_cursor ({ end_row + 2 , 0 })
466+ local is_end_of_file = next_line_node
467+ and vim .tbl_contains ({ ' paragraph' , ' list' }, next_line_node :type ())
468+ and second_line_node
469+ and second_line_node :type () == ' document'
470+ -- Range for list items at the very end of the file are not calculated properly
471+ if (end_row + 1 ) == vim .fn .line (' $' ) and is_end_of_file then
472+ end_row = end_row + 1
473+ end
464474 local range = {
465- start = { line = end_row + 1 , character = 0 },
466- [' end' ] = { line = end_row + 1 , character = 0 },
475+ start = { line = end_row , character = 0 },
476+ [' end' ] = { line = end_row , character = 0 },
467477 }
468478
469479 local checkbox = line :match (' ^(%s*[%+%-])%s*%[[%sXx%-]?%]' )
@@ -512,7 +522,7 @@ function OrgMappings:handle_return(suffix)
512522 if # text_edits > 0 then
513523 vim .lsp .util .apply_text_edits (text_edits , 0 , constants .default_offset_encoding )
514524
515- vim .fn .cursor (end_row + 2 + (add_empty_line and 1 or 0 ), 0 ) -- +1 for 0 index and +1 for next line
525+ vim .fn .cursor (end_row + 1 + (add_empty_line and 1 or 0 ), 0 ) -- +1 for next line
516526 vim .cmd ([[ startinsert!]] )
517527 end
518528 end
0 commit comments