From 1f284870023983936512c0e241dcf0f7920f4096 Mon Sep 17 00:00:00 2001 From: Gabriel Filion Date: Sun, 1 Sep 2019 04:17:27 -0400 Subject: [PATCH 1/2] Test that formatting does not happen when typing on a long line We currently have a bug caused by the auto formatting that happens when we reach textwidth. The last two characters are getting swapped before the line is formatted. This leads to many frustrating situations. To avoid this situation we want to avoid formatting when we have not explicitly called a formatting function such as 'gq' in normal mode. --- test/format/textwidth.vader | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/format/textwidth.vader b/test/format/textwidth.vader index 96ef888..eada027 100644 --- a/test/format/textwidth.vader +++ b/test/format/textwidth.vader @@ -1,3 +1,5 @@ +# First let's test that formatting without textwidth set doesn't do unexpected things + Given puppet (long line): # Long comment 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 file { 'foo': @@ -13,12 +15,18 @@ Expect puppet (nothing changed): ensure => present, } +------------------------------------------------------------------------------- +# Now let's check that the plugin does the right thing when textwidth is set. +# +# All of the tests below this point expect to have 'textwidth' set during the test. Before (set textwidth): set textwidth=76 After (unset textwidth): set textwidth=0 +------------------------------------------------------------------------------- +# note: using the same fixture as before Do (format all text with textwidth set): gqG @@ -30,3 +38,19 @@ Expect puppet (comment is wrapped into more lines): ensure => present, } +------------------------------------------------------------------------------- +Given puppet (long line before editing): + file { 'foo': + ensure => present, + source => ['puppet:///modules/very_long_module_name_that_will_not_stop'], + } + +Do (type in some more on long line): + jjA mode + +Expect puppet (formatting did not change line): + file { 'foo': + ensure => present, + source => ['puppet:///modules/very_long_module_name_that_will_not_stop'], mode + } + From cf5fb0257a4fe5aabf5a940a8e7fb108d3768367 Mon Sep 17 00:00:00 2001 From: Gabriel Filion Date: Tue, 10 Dec 2019 14:47:56 -0500 Subject: [PATCH 2/2] stop formatting text if editor is not in normal mode 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. --- autoload/puppet/format.vim | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/autoload/puppet/format.vim b/autoload/puppet/format.vim index a5aa6be..e996b7d 100644 --- a/autoload/puppet/format.vim +++ b/autoload/puppet/format.vim @@ -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