Skip to content

Commit 905de98

Browse files
authored
Merge pull request #111 from agatan/feature/range_format
Support format range
2 parents e2e80be + 33c3e21 commit 905de98

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

autoload/rustfmt.vim

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,28 @@ endif
2020

2121
let s:got_fmt_error = 0
2222

23-
function! rustfmt#Format()
24-
let l:curw = winsaveview()
25-
let l:tmpname = expand("%:p:h") . "/." . expand("%:p:t") . ".rustfmt"
26-
call writefile(getline(1, '$'), l:tmpname)
23+
function! s:RustfmtCommandRange(filename, line1, line2)
24+
let l:arg = {"file": shellescape(a:filename), "range": [a:line1, a:line2]}
25+
return printf("%s %s --write-mode=overwrite --file-lines '[%s]'", g:rustfmt_command, g:rustfmt_options, json_encode(l:arg))
26+
endfunction
2727

28-
let command = g:rustfmt_command . " --write-mode=overwrite "
28+
function! s:RustfmtCommand(filename)
29+
return g:rustfmt_command . " --write-mode=overwrite " . g:rustfmt_options . " " . shellescape(a:filename)
30+
endfunction
2931

32+
function! s:RunRustfmt(command, curw, tmpname)
3033
if exists("*systemlist")
31-
let out = systemlist(command . g:rustfmt_options . " " . shellescape(l:tmpname))
34+
let out = systemlist(a:command)
3235
else
33-
let out = split(system(command . g:rustfmt_options . " " . shellescape(l:tmpname)), '\r\?\n')
36+
let out = split(system(a:command), '\r\?\n')
3437
endif
3538

3639
if v:shell_error == 0 || v:shell_error == 3
3740
" remove undo point caused via BufWritePre
3841
try | silent undojoin | catch | endtry
3942

4043
" Replace current file with temp file, then reload buffer
41-
call rename(l:tmpname, expand('%'))
44+
call rename(a:tmpname, expand('%'))
4245
silent edit!
4346
let &syntax = &syntax
4447

@@ -76,8 +79,28 @@ function! rustfmt#Format()
7679
let s:got_fmt_error = 1
7780
lwindow
7881
" We didn't use the temp file, so clean up
79-
call delete(l:tmpname)
82+
call delete(a:tmpname)
8083
endif
8184

82-
call winrestview(l:curw)
85+
call winrestview(a:curw)
86+
endfunction
87+
88+
function! rustfmt#FormatRange(line1, line2)
89+
let l:curw = winsaveview()
90+
let l:tmpname = expand("%:p:h") . "/." . expand("%:p:t") . ".rustfmt"
91+
call writefile(getline(1, '$'), l:tmpname)
92+
93+
let command = s:RustfmtCommandRange(l:tmpname, a:line1, a:line2)
94+
95+
call s:RunRustfmt(command, l:curw, l:tmpname)
96+
endfunction
97+
98+
function! rustfmt#Format()
99+
let l:curw = winsaveview()
100+
let l:tmpname = expand("%:p:h") . "/." . expand("%:p:t") . ".rustfmt"
101+
call writefile(getline(1, '$'), l:tmpname)
102+
103+
let command = s:RustfmtCommand(l:tmpname)
104+
105+
call s:RunRustfmt(command, l:curw, l:tmpname)
83106
endfunction

doc/rust.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ g:rustfmt_fail_silently~
116116
*g:rustfmt_options*
117117
g:rustfmt_options~
118118
Set this option to a string of options to pass to 'rustfmt'. The
119-
write-mode is already set to 'overwrite'. If not specified it
119+
write-mode is already set to 'overwrite'. If not specified it
120120
defaults to '' : >
121121
let g:rustfmt_options = ''
122122
<
@@ -219,6 +219,10 @@ COMMANDS *rust-commands*
219219
|g:rustfmt_command|. If |g:rustfmt_fail_silently| is set to 1
220220
then it will not populate the |location-list|.
221221

222+
:RustFmtRange *:RustFmtRange*
223+
Runs |g:rustfmt_command| with selected range. See
224+
|:RustFmt| for any other information.
225+
222226
==============================================================================
223227
MAPPINGS *rust-mappings*
224228

ftplugin/rust.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ command! -range=% RustPlay :call rust#Play(<count>, <line1>, <line2>, <f-args>)
130130
" See |:RustFmt| for docs
131131
command! -buffer RustFmt call rustfmt#Format()
132132

133+
" See |:RustFmtRange| for docs
134+
command! -range -buffer RustFmtRange call rustfmt#FormatRange(<line1>, <line2>)
135+
133136
" Mappings {{{1
134137

135138
" Bind ⌘R in MacVim to :RustRun

0 commit comments

Comments
 (0)