diff --git a/autoload/rustfmt.vim b/autoload/rustfmt.vim index 59a58e84..d8a9bead 100644 --- a/autoload/rustfmt.vim +++ b/autoload/rustfmt.vim @@ -2,6 +2,16 @@ " " Adapted from https://github.com/fatih/vim-go " For bugs, patches and license go to https://github.com/rust-lang/rust.vim +if exists("g:rustfmt_loaded") + finish +endif +let g:rustfmt_loaded = 1 + +if exists("g:rustfmt_disable") + if g:rustfmt_disable + finish + endif +endif if !exists("g:rustfmt_autosave") let g:rustfmt_autosave = 0 @@ -19,25 +29,27 @@ if !exists("g:rustfmt_fail_silently") let g:rustfmt_fail_silently = 0 endif -function! rustfmt#DetectVersion() +function! s:DetectVersion() " Save rustfmt '--help' for feature inspection - silent let s:rustfmt_help = system(g:rustfmt_command . " --help") - let s:rustfmt_unstable_features = s:rustfmt_help =~# "--unstable-features" + if !exists("s:rustfmt_help") + silent let s:rustfmt_help = system(g:rustfmt_command . " --help") + let s:rustfmt_unstable_features = s:rustfmt_help =~# "--unstable-features" + endif " Build a comparable rustfmt version varible out of its `--version` output: - silent let l:rustfmt_version_full = system(g:rustfmt_command . " --version") - let l:rustfmt_version_list = matchlist(l:rustfmt_version_full, - \ '\vrustfmt ([0-9]+[.][0-9]+[.][0-9]+)') - if len(l:rustfmt_version_list) < 3 - let s:rustfmt_version = "0" - else - let s:rustfmt_version = l:rustfmt_version_list[1] + if !exists('s:rustfmt_version') + silent let l:rustfmt_version_full = system(g:rustfmt_command . " --version") + let l:rustfmt_version_list = matchlist(l:rustfmt_version_full, + \ '\vrustfmt ([0-9]+[.][0-9]+[.][0-9]+)') + if len(l:rustfmt_version_list) < 3 + let s:rustfmt_version = "0" + else + let s:rustfmt_version = l:rustfmt_version_list[1] + endif endif return s:rustfmt_version endfunction -call rustfmt#DetectVersion() - if !exists("g:rustfmt_emit_files") let g:rustfmt_emit_files = s:rustfmt_version >= "0.8.2" endif @@ -49,6 +61,7 @@ endif let s:got_fmt_error = 0 function! rustfmt#Load() + call s:DetectVersion() " Utility call to get this script loaded, for debugging endfunction @@ -217,6 +230,7 @@ function! s:RunRustfmt(command, tmpname, from_writepre) endfunction function! rustfmt#FormatRange(line1, line2) + call s:DetectVersion() let l:tmpname = tempname() call writefile(getline(1, '$'), l:tmpname) let command = s:RustfmtCommandRange(l:tmpname, a:line1, a:line2) @@ -225,10 +239,12 @@ function! rustfmt#FormatRange(line1, line2) endfunction function! rustfmt#Format() + call s:DetectVersion() call s:RunRustfmt(s:RustfmtCommand(), '', v:false) endfunction function! rustfmt#Cmd() + call s:DetectVersion() " Mainly for debugging return s:RustfmtCommand() endfunction @@ -253,6 +269,7 @@ function! rustfmt#PreWrite() return endif + call s:DetectVersion() call s:RunRustfmt(s:RustfmtCommand(), '', v:true) endfunction diff --git a/doc/rust.txt b/doc/rust.txt index 9d5eb8cc..34859093 100644 --- a/doc/rust.txt +++ b/doc/rust.txt @@ -110,7 +110,12 @@ g:ftplugin_rust_source_path~ source files: > let g:ftplugin_rust_source_path = $HOME.'/dev/rust' < - + *g:rustfmt_disable* +g:rustfmt_disable~ + Set this option to disable rustfmt function. If not specified it + defaults to 0 : > + let g:rustfmt_disable = 0 +< *g:rustfmt_command* g:rustfmt_command~ Set this option to the name of the 'rustfmt' executable in your $PATH. If