Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim trailing whitespace only on modified lines #195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

FlexW
Copy link

@FlexW FlexW commented Jul 27, 2022

This will help when working on legacy codebases where one wants to move incrementally to .editorconfig. This mode is also more consistent with indent_style, which operates only on changes.

What are your thoughts? Alternatively, this mode could be made optional.

Fixes: #106

@FlexW FlexW force-pushed the bugfix/trim-only-modified branch from 9c1bf44 to 2ff5db7 Compare July 28, 2022 08:13
@h3xx
Copy link

h3xx commented Aug 3, 2022

Maybe if this was a configurable option (e.g. let g:EditorConfig_whitespace_trim_strategy="on_insert" with the default being "on_save" or something), it would be better.

To have this be the default behavior would annoy me. In my initial test, it prevented me from doing something I do on the regular: pasting in normal mode after trailing whitespace.

@mmrwoods
Copy link
Contributor

Hi,

I have the same problem, but I have solved it using https://github.com/ntpeters/vim-better-whitespace, which relies on an external diff command to identify modified lines. Maybe that approach could be copied to editorconfig-vim? Or maybe editorconfig-vim could have an option to set a function or command to use for stripping whitespace which would override the built-in functionality?

FWIW, this is the workaround in my .vimrc...

" Disable editorconfig trim_trailing_whitespace, use better_whitespace
let g:EditorConfig_disable_rules = ['trim_trailing_whitespace']

" Automatically strip whitespace on save using better_whitespace plugin
let g:strip_whitespace_on_save = 1

" Only strip whitespace on modified lines and don't ask for confirmation
let g:strip_only_modified_lines = 1
let g:strip_whitespace_confirm = 0

" Use editorconfig hook to automatically enable/disable whitespace stripping
function! <SID>EditorConfigHook(config)
  try
    let l:trim_trailing_whitespace = a:config->get('trim_trailing_whitespace')
    if l:trim_trailing_whitespace == 'true'
      exec 'EnableStripWhitespaceOnSave'
    elseif l:trim_trailing_whitespace == 'false'
      exec 'DisableStripWhitespaceOnSave'
    endif
  catch
    echo 'EditorConfigHook Failed: ' . v:exception
  endtry
  return 0 " Return 0 to show no error happened
endfunction
call editorconfig#AddNewHook(function('<SID>EditorConfigHook'))

I've been using this for a month or so now and it seems to be working fine (not noticed any performance impact).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Should only touch modified lines?
3 participants