Skip to content

Commit

Permalink
stop formatting text if editor is not in normal mode
Browse files Browse the repository at this point in the history
the autoformat function is called by vim when textwidth (if defined) is
exceeded. during these calls, the Fallback part of the fuction ends up
calling out to "normal! gww" which tends to reverse the last two
characters.

this is caused by how gww works, in relationship to how formatexpr is
called during insert mode -- gww tries to bring the cursor back to where
it was before, but the cursor position did not exist in the first place
(since the last typed character was not inserted into the buffer yet
before calling formatexpr)

This change works around the issue by entirely disabling autoformatting
when not in normal mode. It means that only using gq in normal mode will
be able to reformat the text.
  • Loading branch information
lelutin committed Jan 30, 2021
1 parent e75fe12 commit 2d76469
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions autoload/puppet/format.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
"
" Simple format using puppet's l:indents and align hashrockets function
function! puppet#format#Format() abort
" only allow reformatting through the gq command
" (e.g. Vim is in normal mode)
if mode() != 'n'
" do not fall back to internal formatting
return 0
endif

let l:start_lnum = v:lnum
let l:end_lnum = v:lnum + v:count - 1
" Don't modify indentation or alignment if called by textwidth. We'll only
Expand Down

0 comments on commit 2d76469

Please sign in to comment.