From d5a8fcbc037555b49b2311af047b4aca07b79047 Mon Sep 17 00:00:00 2001 From: Zane Hitchcox Date: Wed, 28 Sep 2016 20:56:25 -0400 Subject: [PATCH 1/5] optional two spaces --- README.md | 1 + autoload/elm.vim | 3 +++ 2 files changed, 4 insertions(+) diff --git a/README.md b/README.md index 0ad25a0..864db58 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,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 257f0d5..4679df0 100644 --- a/autoload/elm.vim +++ b/autoload/elm.vim @@ -62,6 +62,9 @@ fun! elm#Format() let old_fileformat = &fileformat call rename(l:tmpname, expand('%')) silent edit! + if g:elm_format_two_spaces == 1 + %s;^\(\s\+\);\=repeat(' ', len(submatch(0))/2);g + endif let &fileformat = old_fileformat let &syntax = &syntax elseif g:elm_format_fail_silently == 0 From 8de3e08df381b072c3b440b711840bf931f1282c Mon Sep 17 00:00:00 2001 From: Zane Hitchcox Date: Wed, 28 Sep 2016 21:06:25 -0400 Subject: [PATCH 2/5] instructions for auto-formatting upon save --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 864db58..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: From 366d65fa7ebc4fa314d6ec86c6b964de720ad2c1 Mon Sep 17 00:00:00 2001 From: Zane Hitchcox Date: Wed, 28 Sep 2016 21:14:43 -0400 Subject: [PATCH 3/5] fix error with unspecified option --- autoload/elm.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/elm.vim b/autoload/elm.vim index 4679df0..b573939 100644 --- a/autoload/elm.vim +++ b/autoload/elm.vim @@ -62,7 +62,7 @@ fun! elm#Format() let old_fileformat = &fileformat call rename(l:tmpname, expand('%')) silent edit! - if g:elm_format_two_spaces == 1 + if get(g:, "elm_format_two_spaces", 0) == 1 %s;^\(\s\+\);\=repeat(' ', len(submatch(0))/2);g endif let &fileformat = old_fileformat From 5c9abe942438aab2b99484e10911d9bc6fe78321 Mon Sep 17 00:00:00 2001 From: Zane Hitchcox Date: Wed, 28 Sep 2016 21:17:15 -0400 Subject: [PATCH 4/5] make silent --- autoload/elm.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/elm.vim b/autoload/elm.vim index b573939..48f6b83 100644 --- a/autoload/elm.vim +++ b/autoload/elm.vim @@ -63,7 +63,7 @@ fun! elm#Format() call rename(l:tmpname, expand('%')) silent edit! if get(g:, "elm_format_two_spaces", 0) == 1 - %s;^\(\s\+\);\=repeat(' ', len(submatch(0))/2);g + silent! %s;^\(\s\+\);\=repeat(' ', len(submatch(0))/2);g endif let &fileformat = old_fileformat let &syntax = &syntax From aca409e43b52db55b301897370f41a0c752739da Mon Sep 17 00:00:00 2001 From: Zane Hitchcox Date: Tue, 4 Oct 2016 20:11:15 -0400 Subject: [PATCH 5/5] use sed --- autoload/elm.vim | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/autoload/elm.vim b/autoload/elm.vim index ac310c1..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,10 +62,10 @@ fun! elm#Format() " replace current file with temp file, then reload buffer let old_fileformat = &fileformat call rename(l:tmpname, expand('%')) - silent edit! if get(g:, "elm_format_two_spaces", 0) == 1 - silent! %s;^\(\s\+\);\=repeat(' ', len(submatch(0))/2);g + 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 elseif g:elm_format_fail_silently == 0