LSP formatting deletes characters when `set expandtab`

vim.lsp.buf.formatting() messes up lines it fixes. It’s only lines where it fixes wrong indentation, where wrong number of tabs is used (works ok with spaces). Formatting deletes first X characters of those lines. After undo, and repeat, all consecutive runs are ok, with no error (until next indentation is changed).

Issue is consistent with all language servers I tried (TS, Go).

After stripping config to only nvim-lspconfig plugin, and a basic ‘set’ of variables, issue persisted. However, changing ‘set noexpandtab’ to ‘set expandtab’ fixed it! Relevant part of my config is here:

set tabstop=4 softtabstop=4
set shiftwidth=4
set expandtab
set smartindent

I changed my style of coding from tabs to spaces but still have many projects that use tabs. Haven’t really spent much time configuring this properly, but I feel (mis)configuration should not cause this behavior?

Which version of neovim? Tim Pope fixed some things around tabstop for 0.7. If it’s in 0.7 file a GH issue following the minimal reproduction steps so I can reproduce and I can look into it.

It’s latest 0.7, just updated all before posting. I’ll post GH issue.

I’m running into this exact same issue @psiho I tried to search for a related GH issue, but was unable to find it. So can you maybe share a link?

And most importantly: did you managed to find a solution? And if so can you share it with me?

Thanks!

@svenharmelen sorry but it was so long time ago, completely forgot what the solution was. I think I used the opportunity to review my complete config, move config files to lua and revise some plugins, but don’t remember what exactly did the trick.

I just checked my current config, and all those sets are still the same, just now in lua:

vim.o.tabstop = 4
vim.o.softtabstop = 4
vim.o.shiftwidth = 4
vim.o.expandtab = true
vim.o.smartindent = true

Guess the big question is if you still use tabs? I’m working in a repo that contains a .editorconfig file which sets indent_style = tab for Go files. Since that config was added I noticed the weird behavior…

The rest of my settings are more or less the same (also in Lua):

set.tabstop = 2 – Tabs width in spaces
set.shiftwidth = 2 – Amount of spaces when shifting
set.expandtab = true – Expand tabs to the proper size