Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 15d66ea

Browse files
authored
fix: indent an item's content when entering a new line from normal mode (#950)
* record match for all nodes * add a test
1 parent 27ab1cf commit 15d66ea

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

lua/orgmode/org/indent.lua

+8-5
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ local function get_indent_for_match(matches, linenr, mode, bufnr)
6262
end
6363
return indent
6464
end
65-
if mode:match('^[iR]') and prev_line_match.type == 'listitem' and linenr - prev_linenr < 3 then
66-
-- In insert mode, we also count the non-listitem line *after* a listitem as
65+
-- node type is nil while inserting!
66+
local is_inserting = (not match.type) or mode:match('^[iR]')
67+
if is_inserting and prev_line_match.type == 'listitem' and linenr - prev_linenr < 3 then
68+
-- While inserting, we also count the non-listitem line *after* a listitem as
6769
-- part of the listitem. Keep in mind that double empty lines end a list as
6870
-- per Orgmode syntax.
6971
--
@@ -217,6 +219,9 @@ local get_matches = ts_utils.memoize_by_buf_tick(function(bufnr)
217219
end
218220
elseif type == 'paragraph' or type == 'drawer' or type == 'property_drawer' then
219221
opts.indent_type = 'other'
222+
for i = range.start.line, range['end'].line - 1 do
223+
matches[i + 1] = opts
224+
end
220225
end
221226
end
222227

@@ -254,9 +259,7 @@ local function indentexpr(linenr, bufnr)
254259
end
255260

256261
local indentexpr_cache = buf_indentexpr_cache[bufnr] or { prev_linenr = -1 }
257-
if indentexpr_cache.prev_linenr ~= linenr - 1 or not mode:lower():find('n') then
258-
indentexpr_cache.matches = get_matches(bufnr)
259-
end
262+
indentexpr_cache.matches = get_matches(bufnr)
260263

261264
-- Treesitter failed to parse the document (due to errors or missing tree)
262265
-- So we just fallback to autoindent

tests/plenary/org/indent_spec.lua

+17
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,19 @@ local function test_add_line_breaks_to_existing_file()
237237
expect_whole_buffer(expected)
238238
end
239239

240+
local function test_insertion_from_normal_mode()
241+
helpers.create_file({ '- first item' })
242+
vim.cmd([[normal! o]])
243+
local user_input = vim.api.nvim_replace_termcodes('i- second item<Esc>ocontent', true, true, true)
244+
vim.api.nvim_feedkeys(user_input, 'ntix', false)
245+
local expected = {
246+
'- first item',
247+
'- second item',
248+
' content',
249+
}
250+
expect_whole_buffer(expected)
251+
end
252+
240253
-- The actual tests are here.
241254

242255
describe('with "indent",', function()
@@ -259,6 +272,10 @@ describe('with "indent",', function()
259272
it('adding line breaks to list items maintains indent', function()
260273
test_add_line_breaks_to_existing_file()
261274
end)
275+
276+
it('inserting content from nomral mode is well indented', function()
277+
test_insertion_from_normal_mode()
278+
end)
262279
end)
263280

264281
describe('with "noindent",', function()

0 commit comments

Comments
 (0)