@@ -659,79 +659,85 @@ function OrgMappings:meta_return(suffix)
659659 if not listitem or listitem :type () ~= ' listitem' then
660660 return
661661 end
662- local line = vim .fn .getline (listitem :start () + 1 )
663- local srow , _ , end_row , end_col = listitem :range ()
664- local is_multiline = (end_row - srow ) > 1 or end_col == 0
665- -- For last item in file, ts grammar is not parsing the end column as 0
666- -- while in other cases end column is always 0
667- local is_last_item_in_file = end_col ~= 0
668- if not is_multiline or is_last_item_in_file then
669- end_row = end_row + 1
670- end
671- local range = {
672- start = { line = end_row , character = 0 },
673- [' end' ] = { line = end_row , character = 0 },
674- }
675-
676- local checkbox = line :match (' ^(%s*[%+%-%*])%s*%[[%sXx%-]?%]' )
677- local plain_list = line :match (' ^%s*[%+%-%*]' )
678- local indent , number_in_list , closer = line :match (' ^(%s*)(%d+)([%)%.])%s?' )
679- local text_edits = config :respect_blank_before_new_entry ({}, ' plain_list_item' , {
662+ return self :_insert_item_below (listitem )
663+ end
664+ end
665+
666+ --- @private
667+ --- @param listitem OrgListitem
668+ function OrgMappings :_insert_item_below (listitem )
669+ local line = vim .fn .getline (listitem :start () + 1 )
670+ local srow , _ , end_row , end_col = listitem :range ()
671+ local is_multiline = (end_row - srow ) > 1 or end_col == 0
672+ -- For last item in file, ts grammar is not parsing the end column as 0
673+ -- while in other cases end column is always 0
674+ local is_last_item_in_file = end_col ~= 0
675+ if not is_multiline or is_last_item_in_file then
676+ end_row = end_row + 1
677+ end
678+ local range = {
679+ start = { line = end_row , character = 0 },
680+ [' end' ] = { line = end_row , character = 0 },
681+ }
682+
683+ local checkbox = line :match (' ^(%s*[%+%-%*])%s*%[[%sXx%-]?%]' )
684+ local plain_list = line :match (' ^%s*[%+%-%*]' )
685+ local indent , number_in_list , closer = line :match (' ^(%s*)(%d+)([%)%.])%s?' )
686+ local text_edits = config :respect_blank_before_new_entry ({}, ' plain_list_item' , {
687+ range = range ,
688+ newText = ' \n ' ,
689+ })
690+ local add_empty_line = # text_edits > 0
691+ if checkbox then
692+ table.insert (text_edits , {
680693 range = range ,
681- newText = ' \n ' ,
694+ newText = checkbox .. ' [ ] \n ' ,
682695 })
683- local add_empty_line = # text_edits > 0
684- if checkbox then
685- table.insert (text_edits , {
686- range = range ,
687- newText = checkbox .. ' [ ] \n ' ,
688- })
689- elseif plain_list then
690- table.insert (text_edits , {
691- range = range ,
692- newText = plain_list .. ' \n ' ,
693- })
694- elseif number_in_list then
695- local next_sibling = listitem
696- local counter = 1
697- while next_sibling do
698- local bullet = next_sibling :child (0 )
699- local text = bullet and vim .treesitter .get_node_text (bullet , 0 ) or ' '
700- local new_text = tostring (tonumber (text :match (' %d+' )) + 1 ) .. closer
701-
702- if counter == 1 then
703- table.insert (text_edits , {
704- range = range ,
705- newText = indent .. new_text .. ' ' .. ' \n ' ,
706- })
707- else
708- table.insert (text_edits , {
709- range = ts_utils .node_to_lsp_range (bullet ),
710- newText = new_text ,
711- })
712- end
713-
714- counter = counter + 1
715- next_sibling = next_sibling :next_sibling ()
696+ elseif plain_list then
697+ table.insert (text_edits , {
698+ range = range ,
699+ newText = plain_list .. ' \n ' ,
700+ })
701+ elseif number_in_list then
702+ local next_sibling = listitem
703+ local counter = 1
704+ while next_sibling do
705+ local bullet = next_sibling :child (0 )
706+ local text = bullet and vim .treesitter .get_node_text (bullet , 0 ) or ' '
707+ local new_text = tostring (tonumber (text :match (' %d+' )) + 1 ) .. closer
708+
709+ if counter == 1 then
710+ table.insert (text_edits , {
711+ range = range ,
712+ newText = indent .. new_text .. ' ' .. ' \n ' ,
713+ })
714+ else
715+ table.insert (text_edits , {
716+ range = ts_utils .node_to_lsp_range (bullet ),
717+ newText = new_text ,
718+ })
716719 end
720+
721+ counter = counter + 1
722+ next_sibling = next_sibling :next_sibling ()
717723 end
724+ end
718725
719- if # text_edits > 0 then
720- vim .lsp .util .apply_text_edits (text_edits , vim .api .nvim_get_current_buf (), constants .default_offset_encoding )
726+ if # text_edits > 0 then
727+ vim .lsp .util .apply_text_edits (text_edits , vim .api .nvim_get_current_buf (), constants .default_offset_encoding )
721728
722- vim .fn .cursor (end_row + 1 + (add_empty_line and 1 or 0 ), 1 ) -- +1 for next line
729+ vim .fn .cursor (end_row + 1 + (add_empty_line and 1 or 0 ), 1 ) -- +1 for next line
723730
724- -- update all parents when we insert a new checkbox
725- if checkbox then
726- local new_listitem = self .files :get_closest_listitem ()
727- if new_listitem then
728- new_listitem :update_checkbox (' off' )
729- end
731+ -- update all parents when we insert a new checkbox
732+ if checkbox then
733+ local new_listitem = self .files :get_closest_listitem ()
734+ if new_listitem then
735+ new_listitem :update_checkbox (' off' )
730736 end
731-
732- vim .cmd ([[ startinsert!]] )
733- return true
734737 end
738+
739+ vim .cmd ([[ startinsert!]] )
740+ return true
735741 end
736742end
737743
0 commit comments