fix(editorconfig): avoid trim_trailing_whitespace in insert-mode #41175

Problem:
During insert-mode / replace-mode, `autowrite` may trigger. If it does, the
cursor position can shift due to the automatic removal of trailing spaces on the
current line. When I resume typing, the space between the last word and the new
word is suddenly gone.

Solution:
Disable the "remove trailing spaces" handler during Insert (or a similar) mode.
Autosave logic is not affected.
This commit is contained in:
Oleh Kostiuk
2026-08-05 20:06:28 +02:00
committed by GitHub
parent 69664a0aca
commit 23525dd4e3
2 changed files with 38 additions and 0 deletions

View File

@@ -166,6 +166,10 @@ function properties.trim_trailing_whitespace(bufnr, val)
)
if val == 'true' then
nvim_on('BufWritePre', 'nvim.editorconfig', { buf = bufnr }, function()
local mode = vim.api.nvim_get_mode().mode
if mode:sub(1, 1) == 'i' or mode:sub(1, 1) == 'R' or mode:sub(1, 2) == 'ni' then
return
end
local view = vim.fn.winsaveview()
vim.api.nvim_command('silent! undojoin')
vim.api.nvim_command('silent keepjumps keeppatterns %s/\\s\\+$//e')