@@ -20,25 +20,28 @@ endif
20
20
21
21
let s: got_fmt_error = 0
22
22
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
27
27
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
29
31
32
+ function ! s: RunRustfmt (command , curw, tmpname)
30
33
if exists (" *systemlist" )
31
- let out = systemlist (command . g: rustfmt_options . " " . shellescape ( l: tmpname ) )
34
+ let out = systemlist (a: command )
32
35
else
33
- let out = split (system (command . g: rustfmt_options . " " . shellescape ( l: tmpname ) ), ' \r\?\n' )
36
+ let out = split (system (a: command ), ' \r\?\n' )
34
37
endif
35
38
36
39
if v: shell_error == 0 || v: shell_error == 3
37
40
" remove undo point caused via BufWritePre
38
41
try | silent undojoin | catch | endtry
39
42
40
43
" Replace current file with temp file, then reload buffer
41
- call rename (l : tmpname , expand (' %' ))
44
+ call rename (a : tmpname , expand (' %' ))
42
45
silent edit !
43
46
let &syntax = &syntax
44
47
@@ -76,8 +79,28 @@ function! rustfmt#Format()
76
79
let s: got_fmt_error = 1
77
80
lwindow
78
81
" We didn't use the temp file, so clean up
79
- call delete (l : tmpname )
82
+ call delete (a : tmpname )
80
83
endif
81
84
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 )
83
106
endfunction
0 commit comments