Skip to content

Commit aeb68fa

Browse files
committed
Add filetype blacklist
Exist languages with significant trailing whitespace, e.g. Markdown. Adds the flag `b:strip_trailing_whitespace_enabled` for selectively disabling the plugin on a per-buffer basis. The tracking of modified lines still always has to happen though, to support re-enabling the plugin. In the autocmd group `strip_trailing_whitespace_filetype` an autocommand is added for the `FileType` event that blacklists diff and Markdown files and activates the plugin for other filetypes. To get rid of the default behaviour: :autocmd! strip_trailing_whitespace_filetype Alternatively an explicit global variable could have been added as a more easily customizable blacklist. However, since a buffer toggle is warranted also for one-off situations, this was not preferred.
1 parent 6aea8b0 commit aeb68fa

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

plugin/strip_trailing_whitespace.vim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ function s:StripTree(n, offset) abort
218218
endfunction
219219

220220
function s:OnWrite() abort
221+
if !get(b:, 'strip_trailing_whitespace_enabled', 1) | return | endif
222+
221223
if !has('nvim') | call listener_flush() | endif
222224

223225
let s:is_stripping = 1
@@ -237,4 +239,10 @@ augroup strip_trailing_whitespace
237239
autocmd BufWritePre * call s:OnWrite()
238240
augroup END
239241

242+
augroup strip_trailing_whitespace_filetype
243+
autocmd!
244+
autocmd FileType * let b:strip_trailing_whitespace_enabled = index(['diff', 'markdown'],
245+
\ &filetype) == -1
246+
augroup END
247+
240248
let &cpo = s:save_cpo | unlet s:save_cpo

test/test.vim

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,28 @@ function Test_AddLineAboveChange() abort
2626
endfunction
2727
call s:TestEdits(['foo '], function('s:EditCb'), ['', 'zoo'])
2828
endfunction
29+
30+
function Test_AbleToDisable() abort
31+
function! s:EditCb() abort
32+
let b:strip_trailing_whitespace_enabled = 0
33+
normal! rf
34+
endfunction
35+
call s:TestEdits(['line '], function('s:EditCb'), ['fine '])
36+
37+
" Should remember modification sites even if disabled
38+
function! s:EditCb() abort
39+
let b:strip_trailing_whitespace_enabled = 0
40+
normal! rf
41+
silent write
42+
let b:strip_trailing_whitespace_enabled = 1
43+
endfunction
44+
call s:TestEdits(['line '], function('s:EditCb'), ['fine'])
45+
endfunction
46+
47+
function Test_DisabledForMarkdown() abort
48+
function! s:EditCb() abort
49+
set filetype=markdown
50+
normal! rf
51+
endfunction
52+
call s:TestEdits(['line '], function('s:EditCb'), ['fine '])
53+
endfunction

0 commit comments

Comments
 (0)