Skip to content

Commit

Permalink
Merge pull request #2 from mboughaba/confirmation_feature
Browse files Browse the repository at this point in the history
Implementation of Confirmation Feature
  • Loading branch information
mboughaba authored Nov 28, 2017
2 parents 046cd5d + d730311 commit 5cece2a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This plugin is capable of doing the following things for you:
* Remove empty lines at the end of the file;

By default the plugin does all of the above when you save a file, but this can be disabled to be able to execute clean-up on demand only.
Additionally, The plugin can be configuration to request user confirmation before removing white-spaces.


## Instalation
Expand Down Expand Up @@ -72,9 +73,11 @@ This is fully tested and blazing fast but if you don't mind white-spaces in your
Out of the box, this plugin will clean trailing white-space, fix mixed-indent and remove empty lines at the end of a file when the file is saved.
In more technical details, there are two important functions `LessmessExecute` and `LessmessDisplayToggle`.


### Automagically remove all annoying white-spaces :heavy_check_mark:
White-spaces removal `onsave` is enabled by default. Just save the buffer and the file is clean.


### Manually remove all annoying white-spaces
First, to disable lessmess `onsave` make sure to include the configuration below in your `.vimrc`
```vim
Expand All @@ -85,13 +88,15 @@ To remove (on demand) trailing white-spaces, fix mixed-indent and remove newline
LessmessExecute
```


### Simple white-spaces highlighting
I personally don't see a need of white-space highlighting, if I know they will be removed when I save. Nevertheless, to toggle highlighting of hidden characters, use command below.
```vim
LessmessDisplayToggle
```
This is simply calling vim native toggle list, I highly believe that syntax highlighting for white-spaces is a bit overkill. So why not just use ViM built-in list.


### Configuration
1. To Enable/disable Lessmess
```vim
Expand All @@ -114,18 +119,32 @@ aug end
let g:disable_lessmess = 1
```


### Handy tool
When **Lessmess is disabled**, white-spaces can be removed by calling `force` execute:
```vim
LessmessForceExecute
```


### Checking plugin status
To check status of the plugin one should call
```vim
LessmessStatus
```


# Advanced configuration
1. Lessmess can be configured to request user's confirmation before removing white-space. To enable this feature following configuration need to be added to the user's `.vimrc`
```vim
let g:confirm_whitespace_removal = 1
```
**This feature is disabled by default.**

Before execution, the plugin will ask something similar to: `White-spaces found in file, remove them? y/N: `, user then decides to apply `y` or skip `N` (default) white-space removal.
Confirmation is done once per buffer, this means that the plugin will remember user's choice for a given buffer.


## Running tests
check the setup in the test folder.
```shell
Expand Down
61 changes: 56 additions & 5 deletions autoload/lessmess.vim
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ el
let g:disable_lessmess = 0
en
"
" Plugin won't ask for confirmation unless configured to do so
" Once authorization is granted this will be the default for the current
" buffer
"
if !exists("g:confirm_whitespace_removal")
let g:confirm_whitespace_removal = 0
en
if !exists("b:confirmed_whitespace_removal")
let b:confirmed_whitespace_removal = 0
en
"
"
" Set the interval in which ViM will dirty check the file
" this is triggered after idle
Expand Down Expand Up @@ -140,6 +151,34 @@ fun! s:isEditable()
en
endf

"
" Check if user confirms removal of whitespaces
"
fun! s:isRemovalConfirmed()
"
" If the user confirmed removal of whitespace for the current buffer
" we will remember his choice
"
if exists("b:confirmed_whitespace_removal") && b:confirmed_whitespace_removal
retu 1
en

"
" As user confirmation
"
let l:res = input('White-spaces found in file, remove them? y/N: ')
if l:res == 'y'
echomsg "OK! White-spaces will be removed for the current buffer"
let b:confirmed_whitespace_removal = 1
retu 1
el
let b:confirmed_whitespace_removal = 0
echomsg "Fine! White-space removal will be skipped for the current buffer"
retu 0
en
retu 0
endf

"
" Dirty check
"
Expand Down Expand Up @@ -186,18 +225,27 @@ fun! lessmess#LessmessExecute(force)
try
if a:force || s:contains_empty_lines || s:contains_trailing_whitespaces || s:contains_mixed_indent

"
"
" Do nothing if the user disabled the plugin for this specific
" buffer
if !a:force && exists("b:lessmess_disable_buffer")
retu
en
" done.

"
" If configured, ask confirmation before removing white-spaces
if !a:force && g:confirm_whitespace_removal && !s:isRemovalConfirmed()
retu
en

" Let's remember where the cursor is
let l:save_cursor = getpos(".")

"
" We should add it then only if there is something to be done?
" Remove Empty lines at the end of file
if a:force || s:contains_empty_lines > 0
:keepp %s#\($\n\s*\)\+\%$##e | norm!``
:keepp %s#\($\n\s*\)\+\%$##e
en

" Replace all sequences of white-space containing a
Expand All @@ -210,7 +258,7 @@ fun! lessmess#LessmessExecute(force)
"
"
" retab the whole file
sil retab | norm!``
sil retab
"
"
" restore paste option
Expand All @@ -219,9 +267,12 @@ fun! lessmess#LessmessExecute(force)

" Remove Trailing white-space
if a:force || s:contains_trailing_whitespaces > 0
:keepp %s/\s\+$//e | norm!``
:keepp %s/\s\+$//e
en
en

" Restore cursor position
cal setpos('.', l:save_cursor)
cat
echomsg 'lessmess: darn it'
echomsg v:exception
Expand Down
4 changes: 4 additions & 0 deletions doc/lessmess.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ Lessmess is enabled by default.
When plugin is disabled white-spaces removal can be forced using command:
LessmessForceExecute

To make the plugin ask for confirmation before removing white-spaces following
configuration needs to be added
let g:confirm_whitespace_removal = 1

==============================================================================
3. Configuration~

Expand Down

0 comments on commit 5cece2a

Please sign in to comment.