diff --git a/README.md b/README.md index 0ad25a0..f5e7764 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,14 @@ let g:elm_setup_keybindings = 0 ## Integration +### Automatically Format Upon Save + +To automatically format your code before a save, add the following to your `.vimrc` + +```vim +au BufWritePre *.elm call elm#Format() +``` + ### [Syntastic](https://github.com/scrooloose/syntastic) Syntastic support should work out of the box, but we recommend the following settings: @@ -113,6 +121,7 @@ let g:elm_detailed_complete = 0 let g:elm_format_autosave = 0 let g:elm_format_fail_silently = 0 let g:elm_setup_keybindings = 1 +let g:elm_format_two_spaces = 1 ``` * `:ElmMake [filename]` calls `elm-make` with the given file. If no file is given it uses the current file being edited. diff --git a/autoload/elm.vim b/autoload/elm.vim index b170cb1..695dd02 100644 --- a/autoload/elm.vim +++ b/autoload/elm.vim @@ -54,6 +54,7 @@ fun! elm#Format() " call elm-format on the temporary file let out = system("elm-format " . l:tmpname . " --output " . l:tmpname) + " if there is no error if v:shell_error == 0 try | silent undojoin | catch | endtry @@ -61,6 +62,9 @@ fun! elm#Format() " replace current file with temp file, then reload buffer let old_fileformat = &fileformat call rename(l:tmpname, expand('%')) + if get(g:, "elm_format_two_spaces", 0) == 1 + call system("sed -e 's/^/~/' -e ': r' -e 's/^\\( *\\)~ /\\1 ~/' -e 't r' -e 's/~//' -i " . expand('%')) + endif silent edit! let &fileformat = old_fileformat let &syntax = &syntax