tweekmonster / wstrip.vim

Strip trailing whitespace only on changed lines
MIT License
16 stars 2 forks source link

Doc and functionality problems (or maybe user error?) #5

Closed smitgd closed 4 years ago

smitgd commented 7 years ago

doc/wscript.txt say to set g:wstrip_trailing_max. This has no effect. In the code there only seems to be a b:wstrip_trailing_max. If b:wstrip_trailing_max is set to 0, all trailing blanks are stripped; otherwise up to 2 blanks always remain ("markdown" filetype default?).

With these set: let g:wstrip_auto=1 let b:wstrip_trailing_max=0 let g:wstrip_highlight=1 If I edit a text file with several lines having trailing blanks and then write the file with :w or run the command :WStrip, all the lines with blanks at the end have their blanks removed, not just the single line I modified. Is there an option to remove all trailing blanks in the file that I may have enabled? Note: I originally tried wstrip-changed.vim (now deprecated) and it seemed to work as expected. Also, the file I am testing with is not in a GIT repo.

let g:wstrip_highlight=1 produces no red markers or other indicators in gvim but is ok in vim and I see red underlines as trailing blank placeholders in the console. (For me, this is not an important feature since I can always find trailing blanks using (g)vim search which marks them with yellow blocks.)

tweekmonster commented 7 years ago

You don't need to set b:wstrip_trailing_max unless you know for sure that some whitespace needs to be preserved on lines. This is mainly for the markdown filetype where two spaces at the end of the line causes a \n to be treated as a soft return instead of a new paragraph.

gvim

This might be an issue that was already addressed in a commit I pushed a few hours ago: f8bb2d98e14ec4c250cbc9b6908b3004a4c1c39e

Does :set fileformat? print dos in the affected file? If so, it was an issue with writefile() not preserving \r for Windows line endings. Because of that, a diff made it look like all lines were different.

smitgd commented 7 years ago

It says "fileformat=unix". Also, I have been using your latest code with the head version commit f8bb2d98e14ec4c250cbc9b6908b3004a4c1c39e Author: Tommy Allen tommy@esdf.io Date: Wed Apr 5 20:45:19 2017 -0400 Preserve dos line endings

On the b:wstrip_trailing_max, until I set it explicitly to 0 it always left 2 blanks on the lines. (By setting g:wstrip_trailing_max to 0, since it was actually an undefined variable, it was essentially set by me to default?)

smitgd commented 7 years ago

This attached diff fixes the problems for me. For some reason, v:shell_error is getting set. I don't see any errors when I run the plugin. If I ignore the error, all is well. Also, when I tried to edit my test file t.gds from a directory that was not immediately below my cwd it also cleaned up all the trailing blanks instead of just the lines I touched. Example; cwd is ~/.vim gvim ~/comm/t.gds " without the fix, removes all blanks on :WStrip

fixes-for-me.txt

One other thing I noticed is that my file t.gds contains several UTF-8 ellipsis "tri-graph" characters. When the temporary file .wstrip.t.gds is written out, the tri-graph characters seem to become 6-graph (six bytes instead of 3). This causes each of these lines to appear changed. I have no idea why the temp file is not exactly like the original on these lines. But, fortunately, in my test file the lines with tri-graphs don't have trailing blanks so they are not affected.

tweekmonster commented 7 years ago

Thanks. I'll need to re-check what the error codes are for git diff and diff. I think I was having it clean the entire file because an error meant that a file didn't exist for diff'ing. This was before I started using a temporary file for the diff.

tweekmonster commented 7 years ago

@smitgd See if #6 fixes your issue.

smitgd commented 7 years ago

Yes, that does fix one issue. However, there are others. When s:get_diff_lines() returns [] (empty groups due to various errors) the search/replace still occurs and all trailing whitespace lines, even if not touched, are removed. I have proposed some additional error information print (not sure if echom is the right function) and skip the search/replace if the groups contents indicate errors or if there is no change. (No change also caused all trailing whitespace to be removed so the search/replace should be skipped for no change too.) There is also the other issue I mentioned above about editing files that are not immediately below your cwd. I still have the "return []" commented out to fix that. Not sure if this has something to do with git but I don't always edit files in a git repo and often edit files that I am not right above path wise. I did find why the b:wstrip_trailing_max was defaulting to 2 for me, even when filetype is not markdown. The fix is to move ~/.vim/plugin/wstrip.vim/ftplug/markdown.vim to ~/vim/ftplugin/markdown.vim so it only runs when the filetype is markdown; otherwise, it runs for any filetype and sets b:wstrip_trailing_max to 2. (If this seems right, it should probably be documented for vim non-experts, like me.) Also, in the code, if b:wstrip_trailing_max does not exist, which it doesn't for non-markdown filetypes and if it is not set in gvimrc, it is now set to 0 as a default. I still haven't figured out why in gvim I don't see the whitespace graphical symbols. This is the only issue that my attached more-fixes.txt and moving the markdown.vim file doesn't resolve. more-fixes.txt