Merge branch 'custom_config'
Conflicts: ftplugin/c.vim ftplugin/cpp.vim ftplugin/cs.vim ftplugin/html.vim ftplugin/java.vim ftplugin/javascript.vim ftplugin/php.vim ftplugin/python.vim ftplugin/xml.vim
This commit is contained in:
commit
807357f1c9
@ -2,7 +2,8 @@ vim-autoformat
|
||||
==============
|
||||
Format your code with only one button press!
|
||||
This plugin makes use of external formatprograms to achieve the best result.
|
||||
Check the list of formatprograms to see which languages are currently supported.
|
||||
Check the list of formatprograms to see which languages are supported by default.
|
||||
You can easily customize or add your own formatprogram.
|
||||
|
||||
How to install (Vundle)
|
||||
-----------------------
|
||||
@ -15,27 +16,23 @@ Bundle "Chiel92/vim-autoformat"
|
||||
How to use
|
||||
----------
|
||||
First you have to install an external program that can format code of the programming language you are using.
|
||||
It suffices to make the formatprogram either globally available
|
||||
(which is the case if you installed it via your package manager)
|
||||
or to put it in the `formatters/` folder.
|
||||
Sometimes alternative installation methods are supported.
|
||||
It suffices to make the formatprogram globally available, which is the case if you install it via your package manager.
|
||||
Alternatively, you can put the binary (or a link to it) in the `formatters/` folder.
|
||||
|
||||
When you have installed the formatters you need, you can format the buffer with the command `:Autoformat`.
|
||||
When you have installed the formatters you need, you can format the entire buffer with the command `:Autoformat`.
|
||||
For convenience it is recommended that you assign a key for this, like so:
|
||||
|
||||
```vim
|
||||
noremap <F7> :Autoformat<CR><CR>
|
||||
```
|
||||
|
||||
If you don't want to format the whole vimbuffer, you can alternatively format visually selected code with `gq`.
|
||||
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`.
|
||||
|
||||
Supported formatprograms
|
||||
Default formatprograms
|
||||
------------------------
|
||||
With most of the distro's, if you installed a formatprogram, it's automatically globally available.
|
||||
If this is not the case, you can either make it globally available manually, or put it's binary in the `formatters/` directory.
|
||||
Sometimes alternative installation methods are presented.
|
||||
Here is a list of formatprograms that are currently supported.
|
||||
Here is a list of formatprograms that are supported by default.
|
||||
|
||||
* `astyle` for __C#__, __C++__, __C__ and __Java__.
|
||||
It's probably in your distro's repository, so you can download it as a regular package.
|
||||
@ -54,30 +51,47 @@ For Ubuntu type `sudo apt-get install autopep8` in a terminal.
|
||||
Here is the link to the repository: https://github.com/hhatto/autopep8.
|
||||
And here the link to its page on the python website: http://pypi.python.org/pypi/autopep8/0.5.2.
|
||||
|
||||
* `phpCB` for __PHP__.
|
||||
You can download it's binary here:
|
||||
http://www.waterproof.fr/products/phpCodeBeautifier/download.php.
|
||||
I recommend putting the phpCB binary in the `formatters/` directory.
|
||||
|
||||
* `tidy` for __HTML__ and __XML__.
|
||||
* `tidy` for __HTML__, __XHTML__ and __XML__.
|
||||
It's probably in your distro's repository, so you can download it as a regular package.
|
||||
For Ubuntu type `sudo apt-get install tidy` in a terminal.
|
||||
|
||||
If you find yourself in need of support for another formatprogram, simply add a configuration file in the folder `vim-autoformat/ftplugin/`.
|
||||
You can take the existing ones as an example.
|
||||
Be sure to send me a patch. :)
|
||||
|
||||
How can I change the behaviour of formatters?
|
||||
---------------------------------------------
|
||||
Every formatter is called from a script in the `vim-autoformat/ftplugin/` directory.
|
||||
E.g. the file that calls the C# formatter is named `vim-autoformat/ftplugin/cs.vim`.
|
||||
You can change the arguments passed to the formatter in that file.
|
||||
The formatprg for a `<filetype>` is defined in `g:formatprg_<filetype>`.
|
||||
The arguments passed to the formatprogram are defined in `g:formatprg_args_<filetype>`.
|
||||
So, a complete definition could look like this:
|
||||
|
||||
```vim
|
||||
let g:formatprg_cs = "astyle"
|
||||
let g:formatprg_args_cs = "--mode=cs --style=ansi -p -c -H"
|
||||
```
|
||||
|
||||
If you are not satisfied with the default configuration, you can override it by defining these variables in your .vimrc, just like above.
|
||||
|
||||
The default tabwidth is set to 4 for all formatprograms as well as for vim itself.
|
||||
If you change the tabwidth for a certain formatprogram, I would suggest to change the indent options of vim correspondingly for that filetype.
|
||||
|
||||
```vim
|
||||
au filetype *.cs set tabstop=2
|
||||
au filetype *.cs set softtabstop=2
|
||||
au filetype *.cs set shiftwidth=2
|
||||
```
|
||||
|
||||
|
||||
Todo list
|
||||
---------
|
||||
* Check for windows support.
|
||||
* Option for on-the-fly code-formatting (like visual studio)
|
||||
* Better support for customizing formatter behaviour
|
||||
|
||||
|
||||
If you have any suggestions on this plugin or on this readme 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 think some default formatprg definition is missing, or if you experience problems, please contact me by creating an issue in this repository.
|
||||
|
||||
Change log
|
||||
----------
|
||||
### March 10 2013
|
||||
The custom_config branch has been merged into the master branch.
|
||||
* Customization of formatprograms can be done easily now, as explained above.
|
||||
* I set the default tabwidth to 4 for all formatprograms as well as for vim itself.
|
||||
* The default parameters for astyle have been slightly modified: it will wrap spaces around operators.
|
||||
* phpCB has been removed from the defaults, due to buggy behaviour.
|
||||
* XHTML default definition added
|
||||
|
@ -1,12 +0,0 @@
|
||||
if exists("b:formatprg_set")
|
||||
finish
|
||||
endif
|
||||
let b:formatprg_set = 1
|
||||
|
||||
"Set the formatter name and arguments for this filetype
|
||||
let s:prgname = "astyle"
|
||||
let s:arguments = "--mode=c --style=ansi"
|
||||
|
||||
"Set the formatprg option, if the formatter is installed
|
||||
"globally or in the formatters/ folder
|
||||
call g:FindFormatter(s:prgname, s:arguments)
|
@ -1,12 +0,0 @@
|
||||
if exists("b:formatprg_set")
|
||||
finish
|
||||
endif
|
||||
let b:formatprg_set = 1
|
||||
|
||||
"Set the formatter name and arguments for this filetype
|
||||
let s:prgname = "astyle"
|
||||
let s:arguments = "--mode=c --style=ansi"
|
||||
|
||||
"Set the formatprg option, if the formatter is installed
|
||||
"globally or in the formatters/ folder
|
||||
call g:FindFormatter(s:prgname, s:arguments)
|
@ -1,18 +0,0 @@
|
||||
if exists("b:formatprg_set")
|
||||
finish
|
||||
endif
|
||||
let b:formatprg_set = 1
|
||||
|
||||
"Set the formatter name and arguments for this filetype
|
||||
let s:prgname = "astyle"
|
||||
let s:arguments = "--mode=cs --style=ansi"
|
||||
|
||||
"Set the formatprg option, if the formatter is installed
|
||||
"globally or in the formatters/ folder
|
||||
call g:FindFormatter(s:prgname, s:arguments)
|
||||
|
||||
"Set indenting behaviour to match with the formatter
|
||||
set expandtab
|
||||
set tabstop=4
|
||||
set softtabstop=4
|
||||
set shiftwidth=4
|
@ -1,17 +0,0 @@
|
||||
if exists("b:formatprg_set")
|
||||
finish
|
||||
endif
|
||||
let b:formatprg_set = 1
|
||||
|
||||
"Set the formatter name and arguments for this filetype
|
||||
let s:prgname = "tidy"
|
||||
let s:arguments = "-q --show-errors 0 --show-warnings 0 --indent auto --indent-spaces 2 --vertical-space yes --tidy-mark no --wrap 68"
|
||||
|
||||
"Set the formatprg option, if the formatter is installed
|
||||
"globally or in the formatters/ folder
|
||||
call g:FindFormatter(s:prgname, s:arguments)
|
||||
|
||||
"Set indenting behaviour to match with the formatter
|
||||
set expandtab
|
||||
set tabstop=2
|
||||
set shiftwidth=2
|
@ -1,12 +0,0 @@
|
||||
if exists("b:formatprg_set")
|
||||
finish
|
||||
endif
|
||||
let b:formatprg_set = 1
|
||||
|
||||
"Set the formatter name and arguments for this filetype
|
||||
let s:prgname = "astyle"
|
||||
let s:arguments = "--mode=java --style=ansi"
|
||||
|
||||
"Set the formatprg option, if the formatter is installed
|
||||
"globally or in the formatters/ folder
|
||||
call g:FindFormatter(s:prgname, s:arguments)
|
@ -1,33 +0,0 @@
|
||||
if exists("b:formatprg_set")
|
||||
finish
|
||||
endif
|
||||
let b:formatprg_set = 1
|
||||
|
||||
"Besides installing the js-beautify globally or in the
|
||||
"formatters/ folder, installing as a vim bundle is supported.
|
||||
let s:prgname = "js-beautify"
|
||||
let s:arguments = "-i"
|
||||
|
||||
let s:bundleDir = fnamemodify(expand("<sfile>"), ":h:h:h")
|
||||
let s:prgpath = s:bundleDir."/js-beautify/python/".s:prgname
|
||||
|
||||
if executable(s:prgpath)
|
||||
"If js-beautify is installed as a bundle
|
||||
let b:formatprg=s:prgpath." ".s:arguments
|
||||
else
|
||||
"Else look for js-beautify globally
|
||||
"or in the formatters/ folder
|
||||
call g:FindFormatter(s:prgname,s:arguments)
|
||||
endif
|
||||
|
||||
let b:autoformat = 1
|
||||
"Enable on-the-fly formatting
|
||||
if b:autoformat==1
|
||||
"inoremap <CR> <C-O>gqq<CR>
|
||||
"inoremap } }<C-O>gq%
|
||||
endif
|
||||
|
||||
"Set indenting behaviour to match with the formatter
|
||||
set expandtab
|
||||
set tabstop=4
|
||||
set shiftwidth=4
|
@ -1,12 +0,0 @@
|
||||
if exists("b:formatprg_set")
|
||||
finish
|
||||
endif
|
||||
let b:formatprg_set = 1
|
||||
|
||||
"Set the formatter name and arguments for this filetype
|
||||
let s:prgname="phpCB"
|
||||
let s:arguments="--space-after-if --space-after-switch --space-after-while --space-before-start-angle-bracket --space-after-end-angle-bracket --one-true-brace-function-declaration --glue-amperscore --change-shell-comment-to-double-slashes-comment --force-large-php-code-tag --force-true-false-null-contant-lowercase --align-equal-statements --comment-rendering-style PEAR --equal-align-position 50 --padding-char-count 4"
|
||||
|
||||
"Set the formatprg option, if the formatter is installed
|
||||
"globally or in the formatters folder
|
||||
call g:FindFormatter(s:prgname, s:arguments)
|
@ -1,18 +0,0 @@
|
||||
if exists("b:formatprg_set")
|
||||
finish
|
||||
endif
|
||||
let b:formatprg_set = 1
|
||||
|
||||
"Set the formatter name and arguments for this filetype
|
||||
let s:prgname = "autopep8"
|
||||
let s:arguments = "/dev/stdin"
|
||||
|
||||
"Set the formatprg option, if the formatter is installed
|
||||
"globally or in the formatters folder
|
||||
call g:FindFormatter(s:prgname, s:arguments)
|
||||
|
||||
"Set indenting behaviour to match with the formatter
|
||||
set expandtab
|
||||
set tabstop=4
|
||||
set softtabstop=4
|
||||
set shiftwidth=4
|
@ -1,17 +0,0 @@
|
||||
if exists("b:formatprg_set")
|
||||
finish
|
||||
endif
|
||||
let b:formatprg_set = 1
|
||||
|
||||
"Set the formatter name and arguments for this filetype
|
||||
let s:prgname = "tidy"
|
||||
let s:arguments = "-q -xml --show-errors 10 --show-warnings 10 --indent auto --indent-spaces 2 --vertical-space yes --tidy-mark no --wrap 68"
|
||||
|
||||
"Set the formatprg option, if the formatter is installed
|
||||
"globally or in the formatters/ folder
|
||||
call g:FindFormatter(s:prgname, s:arguments)
|
||||
|
||||
"Set indenting behaviour to match with the formatter
|
||||
set expandtab
|
||||
set tabstop=2
|
||||
set shiftwidth=2
|
@ -5,11 +5,16 @@ function! s:Autoformat()
|
||||
"Save window state
|
||||
let winview=winsaveview()
|
||||
"Autoformat code
|
||||
:silent exe "normal gggqG"
|
||||
silent exe "normal gggqG"
|
||||
"Recall window state
|
||||
call winrestview(winview)
|
||||
else
|
||||
echo "No formatter installed for this filetype"
|
||||
let s:formatprg_var = "g:formatprg_".&filetype
|
||||
if !exists(s:formatprg_var)
|
||||
echo "No formatter defined for filetype ".&filetype
|
||||
else
|
||||
echo "Defined formatter ".eval(s:formatprg_var)." is not executable"
|
||||
endif
|
||||
endif
|
||||
endfunction
|
||||
|
||||
@ -19,21 +24,34 @@ command! Autoformat call s:Autoformat()
|
||||
"Function for finding and setting the formatter
|
||||
"with the given name, if the formatter is installed
|
||||
"globally or in the formatters folder
|
||||
let s:formatterdir = fnamemodify(expand("<sfile>"), ":h:h")."/formatters/"
|
||||
function! g:FindFormatter(name, args)
|
||||
if executable(a:name)
|
||||
let s:prgpath = a:name
|
||||
else
|
||||
let s:bars = expand("<sfile>")
|
||||
let s:prgpath = s:formatterdir.a:name
|
||||
let s:formatterdir = fnamemodify(expand("<sfile>"), ":p:h:h")."/formatters/"
|
||||
function! s:set_formatprg()
|
||||
"Reset previous formatprg
|
||||
set formatprg=""
|
||||
|
||||
"Get formatprg config for current filetype
|
||||
let s:formatprg_var = "g:formatprg_".&filetype
|
||||
let s:formatprg_args_var = "g:formatprg_args_".&filetype
|
||||
if !exists(s:formatprg_var) || !exists(s:formatprg_args_var)
|
||||
"No formatprg defined
|
||||
return
|
||||
endif
|
||||
let s:formatprg = eval(s:formatprg_var)
|
||||
let s:formatprg_args = eval(s:formatprg_args_var)
|
||||
|
||||
if executable(s:prgpath)
|
||||
let b:formatprg=s:prgpath." ".a:args
|
||||
"Set correct formatprg path, if it is installed
|
||||
if !executable(s:formatprg)
|
||||
let s:formatprg = s:formatterdir.s:formatprg
|
||||
if !executable(s:formatprg)
|
||||
"Configured formatprg not installed
|
||||
return
|
||||
endif
|
||||
endif
|
||||
let b:formatprg = s:formatprg." ".s:formatprg_args
|
||||
endfunction
|
||||
|
||||
"formatprg is a global option
|
||||
"So when buffer/window/tab changes,
|
||||
"(re)load formatprg from the bufferlocal variable
|
||||
au BufEnter,WinEnter * if exists("b:formatprg") | let &formatprg=b:formatprg
|
||||
"When filetype changes, set correct b:formatprg
|
||||
au FileType * call s:set_formatprg()
|
||||
|
||||
"When current buffer changes, store b:formatprg into &formatprg
|
||||
au BufEnter * if exists("b:formatprg") | let &formatprg = b:formatprg
|
||||
|
43
plugin/defaults.vim
Normal file
43
plugin/defaults.vim
Normal file
@ -0,0 +1,43 @@
|
||||
if !exists("g:formatprg_cs") | let g:formatprg_cs = "astyle" | endif
|
||||
if !exists("g:formatprg_args_cs") | let g:formatprg_args_cs = "--mode=cs --style=ansi -p -c -H" | endif
|
||||
|
||||
if !exists("g:formatprg_c") | let g:formatprg_c = "astyle" | endif
|
||||
if !exists("g:formatprg_args_c") | let g:formatprg_args_c = "--mode=c --style=ansi -p -c -H" | endif
|
||||
|
||||
if !exists("g:formatprg_cpp") | let g:formatprg_cpp = "astyle" | endif
|
||||
if !exists("g:formatprg_args_cpp") | let g:formatprg_args_cpp = "--mode=c --style=ansi -p -c -H" | endif
|
||||
|
||||
if !exists("g:formatprg_java") | let g:formatprg_java = "astyle" | endif
|
||||
if !exists("g:formatprg_args_java") | let g:formatprg_args_java = "--mode=java --style=ansi -p -c -H" | endif
|
||||
|
||||
if !exists("g:formatprg_python") | let g:formatprg_python = "autopep8" | endif
|
||||
if !exists("g:formatprg_args_python") | let g:formatprg_args_python = "/dev/stdin" | endif
|
||||
|
||||
if !exists("g:formatprg_xml") | let g:formatprg_xml = "tidy" | endif
|
||||
if !exists("g:formatprg_args_xml") | let g:formatprg_args_xml = "-q -xml --show-errors 10 --show-warnings 10 --indent auto --indent-spaces 4 --vertical-space yes --tidy-mark no --wrap 68" | endif
|
||||
if !exists("g:formatprg_html") | let g:formatprg_html = "tidy" | endif
|
||||
if !exists("g:formatprg_args_html") | let g:formatprg_args_html = "-q --show-errors 0 --show-warnings 0 --indent auto --indent-spaces 4 --vertical-space yes --tidy-mark no --wrap 68" | endif
|
||||
if !exists("g:formatprg_xhtml") | let g:formatprg_xhtml = "tidy" | endif
|
||||
if !exists("g:formatprg_args_xhtml") | let g:formatprg_args_xhtml = "-q --show-errors 0 --show-warnings 0 --indent auto --indent-spaces 4 --vertical-space yes --tidy-mark no --wrap 68 -asxhtml" | endif
|
||||
|
||||
if !exists("g:formatprg_javascript")
|
||||
let g:formatprg_javascript = "js-beautify"
|
||||
|
||||
"We allow an alternative path for js-beautify
|
||||
"If js-beautify is installed as a bundle, we still want to detect it
|
||||
let s:bundleDir = fnamemodify(expand("<sfile>"), ":h:h:h")
|
||||
let s:jsbeautify_alternative = s:bundleDir."/js-beautify/python/".g:formatprg_javascript
|
||||
if executable(s:jsbeautify_alternative)
|
||||
let g:formatprg_javascript = s:jsbeautify_alternative
|
||||
endif
|
||||
endif
|
||||
if !exists("g:formatprg_args_javascript")
|
||||
let g:formatprg_args_javascript = "-i"
|
||||
endif
|
||||
|
||||
|
||||
"Set default indent behaviour to match with the formatprg defaults
|
||||
set expandtab
|
||||
set tabstop=4
|
||||
set softtabstop=4
|
||||
set shiftwidth=4
|
1
samples/csharp.cs
Normal file
1
samples/csharp.cs
Normal file
@ -0,0 +1 @@
|
||||
using System; class Program{static int Main(string[] args){Console.WriteLine(args[0]);int i=0;i++;return 0;}}
|
Loading…
Reference in New Issue
Block a user