diff --git a/lua/orgmode/org/indent.lua b/lua/orgmode/org/indent.lua index 756bf3b15..f8b0b3422 100644 --- a/lua/orgmode/org/indent.lua +++ b/lua/orgmode/org/indent.lua @@ -62,8 +62,10 @@ local function get_indent_for_match(matches, linenr, mode, bufnr) end return indent end - if mode:match('^[iR]') and prev_line_match.type == 'listitem' and linenr - prev_linenr < 3 then - -- In insert mode, we also count the non-listitem line *after* a listitem as + -- node type is nil while inserting! + local is_inserting = (not match.type) or mode:match('^[iR]') + if is_inserting and prev_line_match.type == 'listitem' and linenr - prev_linenr < 3 then + -- While inserting, we also count the non-listitem line *after* a listitem as -- part of the listitem. Keep in mind that double empty lines end a list as -- per Orgmode syntax. -- @@ -217,6 +219,9 @@ local get_matches = ts_utils.memoize_by_buf_tick(function(bufnr) end elseif type == 'paragraph' or type == 'drawer' or type == 'property_drawer' then opts.indent_type = 'other' + for i = range.start.line, range['end'].line - 1 do + matches[i + 1] = opts + end end end @@ -254,9 +259,7 @@ local function indentexpr(linenr, bufnr) end local indentexpr_cache = buf_indentexpr_cache[bufnr] or { prev_linenr = -1 } - if indentexpr_cache.prev_linenr ~= linenr - 1 or not mode:lower():find('n') then - indentexpr_cache.matches = get_matches(bufnr) - end + indentexpr_cache.matches = get_matches(bufnr) -- Treesitter failed to parse the document (due to errors or missing tree) -- So we just fallback to autoindent diff --git a/tests/plenary/org/indent_spec.lua b/tests/plenary/org/indent_spec.lua index fabd96a0d..66f8327f0 100644 --- a/tests/plenary/org/indent_spec.lua +++ b/tests/plenary/org/indent_spec.lua @@ -237,6 +237,19 @@ local function test_add_line_breaks_to_existing_file() expect_whole_buffer(expected) end +local function test_insertion_from_normal_mode() + helpers.create_file({ '- first item' }) + vim.cmd([[normal! o]]) + local user_input = vim.api.nvim_replace_termcodes('i- second itemocontent', true, true, true) + vim.api.nvim_feedkeys(user_input, 'ntix', false) + local expected = { + '- first item', + '- second item', + ' content', + } + expect_whole_buffer(expected) +end + -- The actual tests are here. describe('with "indent",', function() @@ -259,6 +272,10 @@ describe('with "indent",', function() it('adding line breaks to list items maintains indent', function() test_add_line_breaks_to_existing_file() end) + + it('inserting content from nomral mode is well indented', function() + test_insertion_from_normal_mode() + end) end) describe('with "noindent",', function()