Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve rustfmt #515

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions autoload/rustfmt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -253,6 +269,7 @@ function! rustfmt#PreWrite()
return
endif

call s:DetectVersion()
call s:RunRustfmt(s:RustfmtCommand(), '', v:true)
endfunction

Expand Down
7 changes: 6 additions & 1 deletion doc/rust.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down