Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: indent an item's content when entering a new line from normal mode #950

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lua/orgmode/org/indent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
--
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions tests/plenary/org/indent_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 item<Esc>ocontent', 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()
Expand All @@ -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()
Expand Down