editorconfig-vim icon indicating copy to clipboard operation
editorconfig-vim copied to clipboard

The trim_trailing_whitespace option changes the last substitute string at every file save

Open sroccaserra opened this issue 3 years ago • 2 comments

What I experienced

While editing a file, I started to do some substitutions with :s/o/x. I tested it on one line, saved the file, and tried to apply it to the whole file with g&.

What I expected: all "o" chars changing to "x" chars in the file. What happened: all the "o" chars of the file disapeared.

I had the editorconfig active, with the trim_trailing_whitespace active.

What I suppose

  • when I saved, the editorconfig plugin applied the trim_trailing_whitespace, using a %s/\s\+$//e substitute
  • the subsitute string was changed to '' and not restored
  • the search pattern was restored
  • when I tried to apply g&, it applied s/o// instead of s/o/x

To reproduce the bug

In an empty directory, add this simple .editorconfig file:

[*]
trim_trailing_whitespace = true

Then:

  • open a new file
  • type some text, for example hello world
  • make a subsitution, for example :s/o/x, the text is now hellx world
  • save the file with :w (this also applies the trim_trailing_whitespace rule)
  • repeat the last substitution with & or g& or :s
  • instead of hellx wxrld, the text is hellx wrld: this is a symptom of the bug: the last substitute used is now '' instead of 'x'.

What I tried & explored

  • Using :@: (execute last command) seems to work as I expect, it replaces "o" chars by "x" chars in my example
  • Commenting the trim_trailing_whitespace rule removes the bug
  • Commenting this line removes the bug: https://github.com/editorconfig/editorconfig-vim/blob/f305bc7/plugin/editorconfig.vim#L510

I saw in the code that the view is saved and restored by s:TrimTrailingWhitespace(), apparently this does not restore the substitute string.

According to this documentation, The last used search pattern and the redo command "." will not be changed by the function

  • https://vimhelp.org/eval.txt.html#function%2dsearch%2dundo

So maybe this is a Vim limitation, unlike the search pattern, the subsitute string is changed by the function and not restored?

sroccaserra avatar Jul 06 '21 12:07 sroccaserra

This is different (search pattern vs substitution is changed), but might be related to: #94

sroccaserra avatar Jul 20 '21 08:07 sroccaserra

Thanks very much for reporting!

I can reproduce if the "open a new file" step in your original post is vim bar.txt. However, if I just say vim and then :w foo.txt, the issue does not appear: image.
That might be yet another issue :) .

I agree this behaviour should be changed if possible, to avoid surprising the user.

cxw42 avatar Jul 31 '21 00:07 cxw42