Update default definitions. Readme update.

This commit is contained in:
Chiel92 2015-05-23 14:25:29 +02:00
parent 2ed8f00bb5
commit a4533d1769
3 changed files with 146 additions and 99 deletions

View File

@ -34,12 +34,12 @@ This can either be one of the programs that are listed below as defaultprograms,
For using a custom formatprogram, read the text below *How can I change the behaviour of formatters, or add one myself?*
If the formatprogram you want to use is installed correctly, in one of the following ways, vim automatically detects it.
* It suffices to make the formatprogram globally available, which is the case if you install it via your package manager.
* Alternatively you can point vim to the the binary by explicitly putting the absolute path in `g:formatprg_<filetype>` in your .vimrc.
* Alternatively you can point vim to the the binary by explicitly putting the absolute path in `g:formatdef_<some_indentifier>` in your .vimrc. TODO
Remember that when no formatprogram 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.
When you have installed the formatters you need, you can format the entire buffer with the command `:Autoformat`.
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.
For convenience it is recommended that you assign a key for this, like so:
@ -48,27 +48,31 @@ For convenience it is recommended that you assign a key for this, like so:
noremap <F3> :Autoformat<CR><CR>
```
If you don't want to format the entire buffer, you can alternatively format visually selected code with `gq`.
However, some formatprograms will behave a bit weird this way, because they need the context of a piece of code.
For more ways to perform autoformatting type `:help gq`.
If you format a piece of code manually by using `gq`, vim will not fallback to auto indentation if there is no formatprogram available.
This way, the default behaviour of `gq` will remain functional.
If you have multiple formatters installed that are supported, vim-autoformat just uses the
first that occurs in the list of available formatters.
You can either 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`.
<!--If you don't want to format the entire buffer, you can alternatively format visually selected code with `gq`.-->
<!--However, some formatprograms will behave a bit weird this way, because they need the context of a piece of code.-->
<!--For more ways to perform autoformatting type `:help gq`.-->
<!--If you format a piece of code manually by using `gq`, vim will not fallback to auto indentation if there is no formatprogram available.-->
<!--This way, the default behaviour of `gq` will remain functional.-->
Default formatprograms
------------------------
Here is a list of formatprograms that are supported by default, and thus will be detected and used by vim when they are installed properly.
* `astyle` for __C#__, __C++__, __C__ and __Java__.
It's probably in your distro's repository, so you can download it as a regular package.
For Ubuntu type `sudo apt-get install astyle` in a terminal.
Otherwise, download it here: http://astyle.sourceforge.net/.
* `clang-format` for __Objective-C__.
* `clang-format` for __C__, __C++__, __Objective-C__.
clang-format is a product of LLVM source builds.
If you `brew install llvm`, clang-format can be found in /usr/local/Cellar/llvm/bin/.
To to load style configuration from a .clang-format file, add to your .vimrc: `let g:format_args_objc = "-style=file"`.
To to load style configuration from a .clang-format file, add to your .vimrc: `let g:formatdef_clangformat_objc = '"clang-format -style=file"'`.
Details: http://clang.llvm.org/docs/ClangFormat.html.
* `astyle` for __C#__, __C++__, __C__ and __Java__.
*Important*: version `2.0.4` or higher is required, since only those versions correctly support piping.
Download it here: http://astyle.sourceforge.net/.
* `autopep8` for __Python__.
It's probably in your distro's repository, so you can download it as a regular package.
For Ubuntu type `sudo apt-get install python-autopep8` in a terminal.
@ -79,9 +83,9 @@ And here the link to its page on the python website: http://pypi.python.org/pypi
It can be installed by running `npm install -g js-beautify`.
Note that `nodejs` is needed for this to work.
The *python version is not supported by default* because it needs different arguments.
If you want (or need) to use the python version, you manually have to specify a `formatprg` definition that is valid for the python version in your `.vimrc`. Example:
If you want (or need) to use the python version, you manually have to specify a formatter definition that is valid for the python version in your `.vimrc`. Example:
```vim
let g:formatprg_args_expr_javascript = '"-".(&expandtab ? "s ".&shiftwidth : "t").(&textwidth ? " -w ".&textwidth : "")." -"'
let g:formatdef_jsbeautify_javascript = '"-".(&expandtab ? "s ".&shiftwidth : "t").(&textwidth ? " -w ".&textwidth : "")." -"'
```
Here is the link to the repository: https://github.com/einars/js-beautify.
@ -119,34 +123,35 @@ How can I change the behaviour of formatters, or add one myself?
---------------------------------------------
If you need a formatter that is not among the defaults, or if you are not satisfied with the default formatting behaviour that is provided by vim-autoformat, you can define it yourself.
*The formatprogram must read the unformatted code from the standard input, and write the formatted code to the standard output.*
The formatprogram that is used for a certain `<filetype>` is defined in `g:formatprg_<filetype>`.
The arguments that are passed to the formatprogram are either defined in `g:formatprg_args_expr_<filetype>` as an expression which can be evaluated, or else in `g:formatprg_args_<filetype>` as a plain string.
The formatprograms that available for a certain `<filetype>` are defined in `g:formatters_<filetype>`.
This is a list containing string indentifiers.
The formatter definitions themselves are defined in `g:formatdef_<identifier>`.
Defining any of these variable manually in your .vimrc, will override the default value, if existing.
So, a complete definition in your .vimrc for C# files could look like this:
```vim
let g:formatprg_cs = "astyle"
let g:formatprg_args_cs = "--mode=cs --style=ansi -pcHs4"
let g:formatdef_my_custom_cs = '"astyle --mode=cs --style=ansi -pcHs4"'
let g:formatters_cs = ['my_custom_cs']
```
If you want to define the arguments dynamically, you can use the more powerful `g:formatprg_args_expr_<filetype>` instead.
Then, a similar definition would look like this:
*Please note the double quotes in `g:formatdef_my_custom_cs`*.
This allows you to define the arguments dynamically:
```vim
let g:formatprg_cs = "astyle"
let g:formatprg_args_expr_cs = '"--mode=cs --style=ansi -pcHs".&shiftwidth'
let g:formatdef_my_custom_cs = '"--mode=cs --style=ansi -pcHs".&shiftwidth'
let g:formatters_cs = ['my_custom_cs']
```
Notice that `g:formatprg_args_expr_cs` now contains an expression that can be evaluated, as required.
Notice that `g:formatdef_my_custom_cs` now contains an expression that can be evaluated, as required.
As you see, this allows us to dynamically define some parameters.
In this example, the indent width that astyle will use, depends on the buffer local value of `&shiftwidth`, instead of being fixed at 4.
So if you're editing a csharp file and change the `shiftwidth` (even runtime), the `formatprg_args_expr_<filetype>` will change correspondingly.
So if you're editing a csharp file and change the `shiftwidth` (even at runtime), the `g:formatdef_my_custom_cs` will change correspondingly.
For the default formatprogram definitions, the options `expandtab`, `shiftwidth` and `textwidth` are taken into account whenever possible.
This means that the formatting style will match your current vim settings as much as possible.
For the exact default definitions, have a look in `vim-autoformat/plugin/defaults.vim`.
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 define formatters through `g:formatprg_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.
Debugging
---------
@ -159,21 +164,20 @@ To read all messages in a vim session type `:messages`.
Things that are not (yet) implemented
--------------------------------------
* Support toggling between multiple formatters, as requested and described in #46.
* Make `:Autoformat` command accept ranges and provide range information to formatter if they support that, as requested and described in #47.
* Automatically check for formatters of supertypes, as requested and described in #50.
* Option for on-the-fly code-formatting, like visual studio (If ever. When you have a clever idea about how to do this, I'd be glad to hear.)
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 formatprg 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
----------
### May 21 2015
* Backwards incompatible patch!
* *Backwards incompatible patch!*
* Multiple formatters per filetype are now supported
* configuration variable names changed
* Configuration variable names changed
* `gq` no longer supported
### Dec 9 2014

View File

@ -55,27 +55,27 @@ function! s:TryAllFormatters(...)
endif
" Try all formatters, starting with selected one
let index = b:current_formatter_index
let s:index = b:current_formatter_index
while 1
let format_def_var = 'g:format_def_'.b:formatters[index]
let formatdef_var = 'g:formatdef_'.b:formatters[s:index]
" Formatter definition must be existent
if !exists(format_def_var)
echoerr "No format definition found in '".s:format_def_var."'."
if !exists(formatdef_var)
echoerr "No format definition found in '".formatdef_var."'."
return 0
endif
" Eval twice, once for getting definition content,
" once for getting the final expression
let &formatprg = eval(eval(format_def_var))
let &formatprg = eval(eval(formatdef_var))
if s:TryFormatter()
return 1
else
let index = (index + 1) % len(b:formatters)
let s:index = (s:index + 1) % len(b:formatters)
endif
if s:index == b:current_formatter_index
" Tried all formatters, none worked
if index == b:current_formatter_index
return 0
endif
endwhile
@ -107,8 +107,10 @@ p = subprocess.Popen(formatprg, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE
stdoutdata, stderrdata = p.communicate(text)
if stderrdata:
if verbose:
vim.command('echom "Formatter {} has errors: {}. Skipping."'.format(formatprg,
formattername = vim.eval('b:formatters[s:index]')
vim.command('echom "Formatter {} has errors: {}. Skipping."'.format(formattername,
stderrdata))
vim.command('echom "Failing config: {} "'.format(repr(formatprg), stderrdata))
vim.command('return 0')
else:
vim.current.buffer[:] = stdoutdata.split('\n')
@ -132,6 +134,7 @@ function! s:NextFormatter()
let b:current_formatter_index = 0
endif
let b:current_formatter_index = (b:current_formatter_index + 1) % len(b:formatters)
echom 'Selected formatter: '.b:formatters[b:current_formatter_index]
endfunction
function! s:PreviousFormatter()
@ -141,6 +144,7 @@ function! s:PreviousFormatter()
endif
let l = len(b:formatters)
let b:current_formatter_index = (b:current_formatter_index - 1 + l) % l
echom 'Selected formatter: '.b:formatters[b:current_formatter_index]
endfunction
" Create commands for iterating through formatter list

View File

@ -1,8 +1,13 @@
let g:format_def_autopep8 = '"autopep8 - ".(&textwidth ? "--max-line-length=".&textwidth : "")'
let g:format_def_test = '"asdf"'
let g:format_def_another_autopep8 = '"autopep8 - --indent-size 2 ".(&textwidth ? "--max-line-length=".&textwidth : "")'
"
" This file contains all default format program definitions and links them to filetypes
"
if !exists("g:formatters_python")
" Python
let g:formatdef_autopep8 = '"autopep8 - ".(&textwidth ? "--max-line-length=".&textwidth : "")'
let g:formatdef_test = '"asdf"'
let g:formatdef_another_autopep8 = '"autopep8 - --indent-size 2 ".(&textwidth ? "--max-line-length=".&textwidth : "")'
if !exists('g:formatters_python')
let g:formatters_python = [
\ 'test',
\ 'another_autopep8',
@ -11,70 +16,104 @@ if !exists("g:formatters_python")
endif
"if !exists("g:formatters_cs")
"let g:formatters_cs = ['"astyle --mode=cs --style=ansi --indent-namespaces -pcH".(&expandtab ? "s".&shiftwidth : "t")']
"endif
" C#
let g:formatdef_astyle_cs = '"astyle --mode=cs --style=ansi --indent-namespaces -pcH".(&expandtab ? "s".&shiftwidth : "t")'
if !exists('g:formatters_cs')
let g:formatters_cs = ['astyle_cs']
endif
"if !exists("g:formatters_c")
"let g:formatters_c = ['"astyle --mode=c --style=ansi -pcH".(&expandtab ? "s".&shiftwidth : "t")']
"endif
"if !exists("g:formatprg_cpp") | let g:formatprg_cpp = "astyle" | endif
"if !exists("g:formatprg_args_expr_cpp") && !exists("g:formatprg_args_cpp")
"let g:formatprg_args_expr_cpp = '"--mode=c --style=ansi -pcH".(&expandtab ? "s".&shiftwidth : "t")'
"endif
" C
let g:formatdef_clangformat_c = '"clang-format -style=\"{BasedOnStyle: WebKit, AlignTrailingComments: true, ".(&textwidth ? "ColumnLimit: ".&textwidth.", " : "").(&expandtab ? "UseTab: Never, IndentWidth: ".&shiftwidth : "UseTab: Always")."}\""'
let g:formatdef_astyle_c = '"astyle --mode=c --style=ansi -pcH".(&expandtab ? "s".&shiftwidth : "t")'
if !exists('g:formatters_c')
"let g:formatters_c = ['clangformat_c', 'astyle_c']
let g:formatters_c = ['astyle_c']
endif
"if !exists("g:formatprg_objc") | let g:formatprg_objc = "clang-format" | endif
"if !exists("g:formatprg_args_expr_objc") && !exists("g:formatprg_args_objc")
"let g:formatprg_args_expr_objc = '"-style=\"{BasedOnStyle: WebKit, AlignTrailingComments: true, ".(&textwidth ? "ColumnLimit: ".&textwidth.", " : "").(&expandtab ? "UseTab: Never, IndentWidth: ".&shiftwidth : "UseTab: Always")."}\""'
"endif
"if !exists("g:formatprg_java") | let g:formatprg_java = "astyle" | endif
"if !exists("g:formatprg_args_expr_java") && !exists("g:formatprg_args_java")
"let g:formatprg_args_expr_java = '"--mode=java --style=ansi -pcH".(&expandtab ? "s".&shiftwidth : "t")'
"endif
" C++
let g:formatdef_clangformat_cpp = '"clang-format -style=\"{BasedOnStyle: WebKit, AlignTrailingComments: true, ".(&textwidth ? "ColumnLimit: ".&textwidth.", " : "").(&expandtab ? "UseTab: Never, IndentWidth: ".&shiftwidth : "UseTab: Always")."}\""'
let g:formatdef_astyle_cpp = '"astyle --mode=c --style=ansi -pcH".(&expandtab ? "s".&shiftwidth : "t")'
if !exists('g:formatters_cpp')
let g:formatters_cpp = ['clangformat_cpp', 'astyle_cpp']
endif
"if !exists("g:formatprg_xml") | let g:formatprg_xml = "tidy" | endif
"if !exists("g:formatprg_args_expr_xml") && !exists("g:formatprg_args_xml")
"let g:formatprg_args_expr_xml = '"-q -xml --show-errors 0 --show-warnings 0 --force-output --indent auto --indent-spaces ".&shiftwidth." --vertical-space yes --tidy-mark no -wrap ".&textwidth'
"endif
"if !exists("g:formatprg_xhtml") | let g:formatprg_xhtml = "tidy" | endif
"if !exists("g:formatprg_args_expr_xhtml") && !exists("g:formatprg_args_xhtml")
"let g:formatprg_args_expr_xhtml = '"-q --show-errors 0 --show-warnings 0 --force-output --indent auto --indent-spaces ".&shiftwidth." --vertical-space yes --tidy-mark no -asxhtml -wrap ".&textwidth'
"endif
" Objective C
let g:formatdef_clangformat_objc = '"clang-format -style=\"{BasedOnStyle: WebKit, AlignTrailingComments: true, ".(&textwidth ? "ColumnLimit: ".&textwidth.", " : "").(&expandtab ? "UseTab: Never, IndentWidth: ".&shiftwidth : "UseTab: Always")."}\""'
if !exists('g:formatters_objc')
let g:formatters_objc = ['clangformat_objc']
endif
"if !exists("g:formatprg_css") | let g:formatprg_css = "css-beautify" | endif
"if !exists("g:formatprg_args_expr_css") && !exists("g:formatprg_args_css")
"let g:formatprg_args_expr_css = '"-f - -s ".&shiftwidth'
"endif
"if !exists("g:formatprg_scss") | let g:formatprg_scss = "sass-convert" | endif
"if !exists("g:formatprg_args_expr_scss") && !exists("g:formatprg_args_scss")
"let g:formatprg_args_expr_scss = '"-F scss -T scss --indent " . (&expandtab ? &shiftwidth : "t")'
"endif
" Java
let g:formatdef_astyle_java = '"astyle --mode=java --style=ansi -pcH".(&expandtab ? "s".&shiftwidth : "t")'
if !exists('g:formatters_java')
let g:formatters_java = ['astyle_java']
endif
"if !exists("g:formatprg_html") | let g:formatprg_html = "html-beautify" | endif
"if !exists("g:formatprg_args_expr_html") && !exists("g:formatprg_args_html")
"let g:formatprg_args_expr_html = '"-f - -s ".&shiftwidth'
"endif
"if !exists("g:formatprg_javascript") | let g:formatprg_javascript = "js-beautify" | endif
"if !exists("g:formatprg_args_expr_javascript") && !exists("g:formatprg_args_javascript")
"let g:formatprg_args_expr_javascript = '"-f - -".(&expandtab ? "s ".&shiftwidth : "t").(&textwidth ? " -w ".&textwidth : "")'
"endif
" HTML
let g:formatdef_htmlbeautify = '"html-beautify -f - -s ".&shiftwidth'
if !exists('g:formatters_html')
let g:formatters_html = ['htmlbeautify']
endif
"if !exists("g:formatprg_typescript") | let g:formatprg_typescript = "tsfmt" | endif
"if !exists("g:formatprg_args_expr_typescript") && !exists("g:formatprg_args_typescript")
"let g:formatprg_args_expr_typescript = '"--stdin %"'
"endif
"if !exists("g:formatprg_json") | let g:formatprg_json = "js-beautify" | endif
"if !exists("g:formatprg_args_expr_json") && !exists("g:formatprg_args_json")
"let g:formatprg_args_expr_json = '"-f - -".(&expandtab ? "s ".&shiftwidth : "t")'
"endif
" Javascript
let g:formatdef_jsbeautify_javascript = '"js-beautify -f - -".(&expandtab ? "s ".&shiftwidth : "t").(&textwidth ? " -w ".&textwidth : "")'
if !exists('g:formatters_javascript')
let g:formatters_javascript = ['jsbeautify_javascript']
endif
" JSON
let g:formatdef_jsbeautify_json = '"js-beautify -f - -".(&expandtab ? "s ".&shiftwidth : "t")'
if !exists('g:formatters_json')
let g:formatters_json = ['jsbeautify_json']
endif
" Ruby
let g:formatdef_rbeautify = 'rbeautify (&expandtab ? "-s -c ".&shiftwidth : "-t")'
if !exists('g:formatters_ruby')
let g:formatters_ruby = ['rbeautify']
endif
" CSS
let g:formatdef_cssbeautify = '"css-beautify -f - -s ".&shiftwidth'
if !exists('g:formatters_css')
let g:formatters_css = ['cssbeautify']
endif
" SCSS
let g:formatdef_sassconvert = '"sass-convert -F scss -T scss --indent " . (&expandtab ? &shiftwidth : "t")'
if !exists('g:formatters_scss')
let g:formatters_scss = ['sassconvert']
endif
" Typescript
let g:formatdef_tsfmt = '"tsfmt --stdin %"'
if !exists('g:formatters_typescript')
let g:formatters_typescript = ['tsfmt']
endif
" XML
let g:formatdef_tidy_xml = '"tidy -q -xml --show-errors 0 --show-warnings 0 --force-output --indent auto --indent-spaces ".&shiftwidth." --vertical-space yes --tidy-mark no -wrap ".&textwidth'
if !exists('g:formatters_xml')
let g:formatters_xml = ['tidy_xml']
endif
" XHTML
let g:formatdef_tidy_xhtml = '"tidy -q --show-errors 0 --show-warnings 0 --force-output --indent auto --indent-spaces ".&shiftwidth." --vertical-space yes --tidy-mark no -asxhtml -wrap ".&textwidth'
if !exists('g:formatters_xhtml')
let g:formatters_xhtml = ['tidy_xhtml']
endif
"if !exists("g:formatprg_ruby") | let g:formatprg_ruby = "rbeautify" | endif
"if !exists("g:formatprg_args_expr_ruby") && !exists("g:formatprg_args_ruby")
"let g:formatprg_args_expr_ruby = '(&expandtab ? "-s -c ".&shiftwidth : "-t")'
"endif