fix(text): indent() ignores expandtab when indent unchanged #40932

Problem:
After expanding tabs with the `expandtab` option, if the old indent
matches the requested one, `vim.text.indent()` returns the input
unchanged.
The optimization path assumes that if old_indent == size, the input
wouldn't be changed, which is not correct when old_indent is formed by
expanded tabs.

Solution:
Apply the optimization only when `expandtab` is not set.
This commit is contained in:
Artem Krinitsyn
2026-07-26 13:24:39 +03:00
committed by GitHub
parent 6539bd6602
commit dec3d8215d
2 changed files with 3 additions and 1 deletions

View File

@@ -190,7 +190,7 @@ function M.indent(size, text, opts)
old_indent = old_indent or 0
prefix = prefix and prefix or ' '
if old_indent == size then
if not opts.expandtab and old_indent == size then
-- Optimization: if the indent is the same, return the text unchanged.
return text, old_indent
end

View File

@@ -47,6 +47,8 @@ describe('vim.text', function()
{ ' \n \nfoo\n\nbar\nbaz\n \n', 1 },
{ vim.text.indent(0, ' \n \n foo\n\n bar\n baz\n \n') }
)
-- Tabs expand when old indent matches new indent #40932.
eq({ ' a', 2 }, { vim.text.indent(2, '\t a', { expandtab = 1 }) })
end)
it('real-world cases', function()