diff --git a/runtime/lua/vim/text.lua b/runtime/lua/vim/text.lua index ba038bacf8..3bdb058e30 100644 --- a/runtime/lua/vim/text.lua +++ b/runtime/lua/vim/text.lua @@ -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 diff --git a/test/functional/lua/text_spec.lua b/test/functional/lua/text_spec.lua index a4a41dc862..9e80f81bc5 100644 --- a/test/functional/lua/text_spec.lua +++ b/test/functional/lua/text_spec.lua @@ -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()