From 978e4ece4f342e06a5410676a28a78f164e6d0e2 Mon Sep 17 00:00:00 2001 From: Chiel ten Brinke Date: Fri, 11 Mar 2016 18:09:05 +0100 Subject: [PATCH] Add more default fallback features. Add removing whitespace and retabbing. --- README.md | 45 +++++++++++++++++++++++++++++-------- plugin/autoformat.vim | 52 ++++++++++++++++++++++++++++++++++--------- plugin/defaults.vim | 10 ++++++++- 3 files changed, 86 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 8ac8355..83bec77 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,8 @@ Format code with one button press! This plugin makes use of external formatprograms to achieve the best results. Check the list of formatprograms to see which languages are supported by default. You can easily customize or add your own formatprogram. -When no formatprogram exists (or no formatprogram is installed) for a certain filetype, vim-autoformat uses vim's indent functionality as a fallback. +When no formatprogram exists (or no formatprogram is installed) for a certain filetype, +vim-autoformat falls back by default to indenting, (using vim's auto indent functionality), retabbing and removing trailing whitespace. ## How to install @@ -48,8 +49,9 @@ If the formatprogram you want to use is installed in one of the following ways, let g:formatterpath = ['/some/path/to/a/folder', '/home/superman/formatters'] ``` -Remember that when no formatprograms exists for a certain filetype, vim-autoformat uses vim's indent functionality as a fallback. -This will fix at least the indentation of your code, according to vim's indentfile for that filetype. +Remember that when no formatprograms exists for a certain filetype, +vim-autoformat falls back by default to indenting, retabbing and removing trailing whitespace. +This will fix at least the most basic things, according to vim's indentfile for that filetype. When you have installed the formatter you need, you can format the entire buffer with the command `:Autoformat`. You can provide the command with a file type such as `:Autoformat json`, otherwise the buffer's filetype will be used. @@ -70,18 +72,33 @@ Or to have your code be formatted upon saving your file, you could use something au BufWrite * :Autoformat ``` -To disable the fallback to vim's indent file, set the following variable to be 0. +To disable the fallback to vim's indent file, retabbing and removing trailing whitespace, set the following variables to 0. ```vim let g:autoformat_autoindent = 0 +let g:autoformat_retab = 0 +let g:autoformat_remove_trailing_spaces = 0 +``` + +You can manually autoindent, retab or remove trailing whitespace with the following respective +commands. + +```vim +gg=G +:retab +:RemoveTrailingSpaces ``` For each filetype, vim-autoformat has a list of applicable formatters. -If you have multiple formatters installed that are supported for some filetype, vim-autoformat tries all formatters in this list of applicable formatters, until one succeeds. +If you have multiple formatters installed that are supported for some filetype, vim-autoformat +tries all formatters in this list of applicable formatters, until one succeeds. You can set this list manually in your vimrc (see section *How can I change the behaviour of formatters, or add one myself?*, or change the formatter with the highest priority by the commands `:NextFormatter` and `:PreviousFormatter`. To print the currently selected formatter use `:CurrentFormatter`. -If you have a composite filetype with dots (like `django.python` or `php.wordpress`), vim-autoformat first tries to detect and use formatters for the exact original filetype, and then tries the same for all supertypes occuring from left to right in the original filetype separated by dots (`.`). +If you have a composite filetype with dots (like `django.python` or `php.wordpress`), +vim-autoformat first tries to detect and use formatters for the exact original filetype, and +then tries the same for all supertypes occuring from left to right in the original filetype +separated by dots (`.`). ## Default formatprograms @@ -179,7 +196,8 @@ let g:formatters_cs = ['my_custom_cs'] ``` In this example, `my_custom_cs` is the identifier for our formatter definition. -The first line defines how to call the external formatter, while the second line tells vim-autoformat that this is the only formatter that we want to use for C# files. +The first line defines how to call the external formatter, while the second line tells +vim-autoformat that this is the only formatter that we want to use for C# files. *Please note the double quotes in `g:formatdef_my_custom_cs`*. This allows you to define the arguments dynamically: @@ -200,7 +218,9 @@ They are defined in `vim-autoformat/plugin/defaults.vim`. As a small side note, in the actual defaults the function `shiftwidth()` is used instead of the property. This is because it falls back to the value of `tabstop` if `shiftwidth` is 0. -If you have a composite filetype with dots (like `django.python` or `php.wordpress`), vim-autoformat internally replaces the dots with underscores so you can specify formatters through `g:formatters_django_python` and so on. +If you have a composite filetype with dots (like `django.python` or `php.wordpress`), +vim-autoformat internally replaces the dots with underscores so you can specify formatters +through `g:formatters_django_python` and so on. #### Ranged definitions @@ -236,10 +256,17 @@ To read all messages in a vim session type `:messages`. Pull requests are welcome. Any feedback is welcome. -If you have any suggestions on this plugin or on this readme, if you have some nice default formatter definition that can be added to the defaults, or if you experience problems, please contact me by creating an issue in this repository. +If you have any suggestions on this plugin or on this readme, if you have some nice default +formatter definition that can be added to the defaults, or if you experience problems, please +contact me by creating an issue in this repository. ## Change log +### March 2016 +* Add more fallback options. +* Don't use the option formatprg at all, to always have the possible of using the default `gq` + command. + ### June 2015 * *Backward incompatible patch!* diff --git a/plugin/autoformat.vim b/plugin/autoformat.vim index 96edfce..b3cd9fd 100644 --- a/plugin/autoformat.vim +++ b/plugin/autoformat.vim @@ -69,10 +69,10 @@ function! s:TryAllFormatters(...) range " Make sure formatters are defined and detected if !call('find_formatters', a:000) " No formatters defined - if g:autoformat_autoindent == 1 - " Autoindent code - exe "normal gg=G" + if verbose + echomsg "No format definitions are defined for this filetype." endif + call s:Fallback() return 0 endif @@ -128,17 +128,35 @@ function! s:TryAllFormatters(...) range echomsg "No format definitions were successful." endif " Tried all formatters, none worked - if g:autoformat_autoindent == 1 - if verbose - echomsg "Trying to autoindent code." - endif - " Autoindent code - exe "normal gg=G" - endif - return 0 + call s:Fallback() endif endwhile +endfunction +function! s:Fallback() + " Detect verbosity + let verbose = &verbose || g:autoformat_verbosemode == 1 + + if g:autoformat_remove_trailing_spaces == 1 + if verbose + echomsg "Removing trailing whitespace..." + endif + call s:RemoveTrailingSpaces() + endif + if g:autoformat_retab == 1 + if verbose + echomsg "Retabbing..." + endif + retab + endif + if g:autoformat_autoindent == 1 + if verbose + echomsg "Autoindenting..." + endif + " Autoindent code + exe "normal gg=G" + endif + return 0 endfunction @@ -282,3 +300,15 @@ endfunction command! NextFormatter call s:NextFormatter() command! PreviousFormatter call s:PreviousFormatter() command! CurrentFormatter call s:CurrentFormatter() + +" Other commands +function! s:RemoveTrailingSpaces() + let user_gdefault = &gdefault + try + set nogdefault + silent! %s/\s\+$ + finally + let &gdefault = user_gdefault + endtry +endfunction +command! RemoveTrailingSpaces call s:RemoveTrailingSpaces() diff --git a/plugin/defaults.vim b/plugin/defaults.vim index 8164b94..8a21b32 100644 --- a/plugin/defaults.vim +++ b/plugin/defaults.vim @@ -5,7 +5,15 @@ " Vim-autoformat configuration variables if !exists('g:autoformat_autoindent') - let g:autoformat_verbosemode = 0 + let g:autoformat_autoindent = 1 +endif + +if !exists('g:autoformat_retab') + let g:autoformat_retab = 1 +endif + +if !exists('g:autoformat_remove_trailing_spaces') + let g:autoformat_remove_trailing_spaces = 1 endif if !exists('g:autoformat_verbosemode')