Skip to content

Commit 7118bcf

Browse files
committed
factor out insert_item_below
1 parent 0ebe2e3 commit 7118bcf

File tree

1 file changed

+68
-64
lines changed

1 file changed

+68
-64
lines changed

lua/orgmode/org/mappings.lua

+68-64
Original file line numberDiff line numberDiff line change
@@ -659,79 +659,83 @@ 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_this(listitem)
663+
end
664+
end
665+
666+
function OrgMappings:insert_item_below_this(listitem)
667+
local line = vim.fn.getline(listitem:start() + 1)
668+
local srow, _, end_row, end_col = listitem:range()
669+
local is_multiline = (end_row - srow) > 1 or end_col == 0
670+
-- For last item in file, ts grammar is not parsing the end column as 0
671+
-- while in other cases end column is always 0
672+
local is_last_item_in_file = end_col ~= 0
673+
if not is_multiline or is_last_item_in_file then
674+
end_row = end_row + 1
675+
end
676+
local range = {
677+
start = { line = end_row, character = 0 },
678+
['end'] = { line = end_row, character = 0 },
679+
}
680+
681+
local checkbox = line:match('^(%s*[%+%-%*])%s*%[[%sXx%-]?%]')
682+
local plain_list = line:match('^%s*[%+%-%*]')
683+
local indent, number_in_list, closer = line:match('^(%s*)(%d+)([%)%.])%s?')
684+
local text_edits = config:respect_blank_before_new_entry({}, 'plain_list_item', {
685+
range = range,
686+
newText = '\n',
687+
})
688+
local add_empty_line = #text_edits > 0
689+
if checkbox then
690+
table.insert(text_edits, {
680691
range = range,
681-
newText = '\n',
692+
newText = checkbox .. ' [ ] \n',
682693
})
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()
694+
elseif plain_list then
695+
table.insert(text_edits, {
696+
range = range,
697+
newText = plain_list .. ' \n',
698+
})
699+
elseif number_in_list then
700+
local next_sibling = listitem
701+
local counter = 1
702+
while next_sibling do
703+
local bullet = next_sibling:child(0)
704+
local text = bullet and vim.treesitter.get_node_text(bullet, 0) or ''
705+
local new_text = tostring(tonumber(text:match('%d+')) + 1) .. closer
706+
707+
if counter == 1 then
708+
table.insert(text_edits, {
709+
range = range,
710+
newText = indent .. new_text .. ' ' .. '\n',
711+
})
712+
else
713+
table.insert(text_edits, {
714+
range = ts_utils.node_to_lsp_range(bullet),
715+
newText = new_text,
716+
})
716717
end
718+
719+
counter = counter + 1
720+
next_sibling = next_sibling:next_sibling()
717721
end
722+
end
718723

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)
724+
if #text_edits > 0 then
725+
vim.lsp.util.apply_text_edits(text_edits, vim.api.nvim_get_current_buf(), constants.default_offset_encoding)
721726

722-
vim.fn.cursor(end_row + 1 + (add_empty_line and 1 or 0), 1) -- +1 for next line
727+
vim.fn.cursor(end_row + 1 + (add_empty_line and 1 or 0), 1) -- +1 for next line
723728

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
729+
-- update all parents when we insert a new checkbox
730+
if checkbox then
731+
local new_listitem = self.files:get_closest_listitem()
732+
if new_listitem then
733+
new_listitem:update_checkbox('off')
730734
end
731-
732-
vim.cmd([[startinsert!]])
733-
return true
734735
end
736+
737+
vim.cmd([[startinsert!]])
738+
return true
735739
end
736740
end
737741

0 commit comments

Comments
 (0)