blob: f325df2fda493be0115d4558e79ae0e924c20bce [file] [log] [blame]
Bram Moolenaar3c2881d2017-03-21 19:18:29 +01001" Author: Stephen Sugden <stephen@stephensugden.com>
Gregory Andersfc935942023-09-12 13:23:38 -05002" Last Modified: 2023-09-11
Konfekt4ac995b2025-03-31 20:45:26 +02003" Last Change: 2025 Mar 31 by Vim project (rename s:RustfmtConfigOptions())
Bram Moolenaar3c2881d2017-03-21 19:18:29 +01004"
5" Adapted from https://github.com/fatih/vim-go
6" For bugs, patches and license go to https://github.com/rust-lang/rust.vim
7
8if !exists("g:rustfmt_autosave")
Gregory Andersfc935942023-09-12 13:23:38 -05009 let g:rustfmt_autosave = 0
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010010endif
11
12if !exists("g:rustfmt_command")
Gregory Andersfc935942023-09-12 13:23:38 -050013 let g:rustfmt_command = "rustfmt"
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010014endif
15
16if !exists("g:rustfmt_options")
Gregory Andersfc935942023-09-12 13:23:38 -050017 let g:rustfmt_options = ""
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010018endif
19
20if !exists("g:rustfmt_fail_silently")
Gregory Andersfc935942023-09-12 13:23:38 -050021 let g:rustfmt_fail_silently = 0
22endif
23
24function! rustfmt#DetectVersion()
25 " Save rustfmt '--help' for feature inspection
26 silent let s:rustfmt_help = system(g:rustfmt_command . " --help")
27 let s:rustfmt_unstable_features = s:rustfmt_help =~# "--unstable-features"
28
Viktor Szépedbf749b2023-10-16 09:53:37 +020029 " Build a comparable rustfmt version variable out of its `--version` output:
Gregory Andersfc935942023-09-12 13:23:38 -050030 silent let l:rustfmt_version_full = system(g:rustfmt_command . " --version")
31 let l:rustfmt_version_list = matchlist(l:rustfmt_version_full,
32 \ '\vrustfmt ([0-9]+[.][0-9]+[.][0-9]+)')
33 if len(l:rustfmt_version_list) < 3
34 let s:rustfmt_version = "0"
35 else
36 let s:rustfmt_version = l:rustfmt_version_list[1]
37 endif
38 return s:rustfmt_version
39endfunction
40
41call rustfmt#DetectVersion()
42
43if !exists("g:rustfmt_emit_files")
44 let g:rustfmt_emit_files = s:rustfmt_version >= "0.8.2"
45endif
46
47if !exists("g:rustfmt_file_lines")
48 let g:rustfmt_file_lines = s:rustfmt_help =~# "--file-lines JSON"
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010049endif
50
51let s:got_fmt_error = 0
52
Gregory Andersfc935942023-09-12 13:23:38 -050053function! rustfmt#Load()
54 " Utility call to get this script loaded, for debugging
55endfunction
56
57function! s:RustfmtWriteMode()
58 if g:rustfmt_emit_files
59 return "--emit=files"
60 else
61 return "--write-mode=overwrite"
62 endif
63endfunction
64
Konfekt4ac995b2025-03-31 20:45:26 +020065function! rustfmt#RustfmtConfigOptions()
Gregory Andersfc935942023-09-12 13:23:38 -050066 let l:rustfmt_toml = findfile('rustfmt.toml', expand('%:p:h') . ';')
67 if l:rustfmt_toml !=# ''
68 return '--config-path '.shellescape(fnamemodify(l:rustfmt_toml, ":p"))
69 endif
70
71 let l:_rustfmt_toml = findfile('.rustfmt.toml', expand('%:p:h') . ';')
72 if l:_rustfmt_toml !=# ''
73 return '--config-path '.shellescape(fnamemodify(l:_rustfmt_toml, ":p"))
74 endif
75
76 " Default to edition 2018 in case no rustfmt.toml was found.
77 return '--edition 2018'
78endfunction
79
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010080function! s:RustfmtCommandRange(filename, line1, line2)
Gregory Andersfc935942023-09-12 13:23:38 -050081 if g:rustfmt_file_lines == 0
82 echo "--file-lines is not supported in the installed `rustfmt` executable"
83 return
84 endif
85
86 let l:arg = {"file": shellescape(a:filename), "range": [a:line1, a:line2]}
87 let l:write_mode = s:RustfmtWriteMode()
Konfekt4ac995b2025-03-31 20:45:26 +020088 let l:rustfmt_config = rustfmt#RustfmtConfigOptions()
Gregory Andersfc935942023-09-12 13:23:38 -050089
90 " FIXME: When --file-lines gets to be stable, add version range checking
91 " accordingly.
92 let l:unstable_features = s:rustfmt_unstable_features ? '--unstable-features' : ''
93
94 let l:cmd = printf("%s %s %s %s %s --file-lines '[%s]' %s", g:rustfmt_command,
95 \ l:write_mode, g:rustfmt_options,
96 \ l:unstable_features, l:rustfmt_config,
97 \ json_encode(l:arg), shellescape(a:filename))
98 return l:cmd
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010099endfunction
100
Gregory Andersfc935942023-09-12 13:23:38 -0500101function! s:RustfmtCommand()
102 let write_mode = g:rustfmt_emit_files ? '--emit=stdout' : '--write-mode=display'
Konfekt4ac995b2025-03-31 20:45:26 +0200103 let config = rustfmt#RustfmtConfigOptions()
Gregory Andersfc935942023-09-12 13:23:38 -0500104 return join([g:rustfmt_command, write_mode, config, g:rustfmt_options])
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100105endfunction
106
Gregory Andersfc935942023-09-12 13:23:38 -0500107function! s:DeleteLines(start, end) abort
108 silent! execute a:start . ',' . a:end . 'delete _'
109endfunction
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100110
Gregory Andersfc935942023-09-12 13:23:38 -0500111function! s:RunRustfmt(command, tmpname, from_writepre)
112 let l:view = winsaveview()
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100113
Gregory Andersfc935942023-09-12 13:23:38 -0500114 let l:stderr_tmpname = tempname()
115 call writefile([], l:stderr_tmpname)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100116
Gregory Andersfc935942023-09-12 13:23:38 -0500117 let l:command = a:command . ' 2> ' . l:stderr_tmpname
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100118
Gregory Andersfc935942023-09-12 13:23:38 -0500119 if a:tmpname ==# ''
120 " Rustfmt in stdin/stdout mode
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100121
Gregory Andersfc935942023-09-12 13:23:38 -0500122 " chdir to the directory of the file
123 let l:has_lcd = haslocaldir()
124 let l:prev_cd = getcwd()
125 execute 'lchdir! '.expand('%:h')
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100126
Gregory Andersfc935942023-09-12 13:23:38 -0500127 let l:buffer = getline(1, '$')
128 if exists("*systemlist")
129 silent let out = systemlist(l:command, l:buffer)
130 else
131 silent let out = split(system(l:command,
132 \ join(l:buffer, "\n")), '\r\?\n')
133 endif
134 else
135 if exists("*systemlist")
136 silent let out = systemlist(l:command)
137 else
138 silent let out = split(system(l:command), '\r\?\n')
139 endif
140 endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100141
Gregory Andersfc935942023-09-12 13:23:38 -0500142 let l:stderr = readfile(l:stderr_tmpname)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100143
Gregory Andersfc935942023-09-12 13:23:38 -0500144 call delete(l:stderr_tmpname)
145
146 let l:open_lwindow = 0
147 if v:shell_error == 0
148 if a:from_writepre
149 " remove undo point caused via BufWritePre
150 try | silent undojoin | catch | endtry
151 endif
152
153 if a:tmpname ==# ''
154 let l:content = l:out
155 else
156 " take the tmpfile's content, this is better than rename
157 " because it preserves file modes.
158 let l:content = readfile(a:tmpname)
159 endif
160
161 call s:DeleteLines(len(l:content), line('$'))
162 call setline(1, l:content)
163
164 " only clear location list if it was previously filled to prevent
165 " clobbering other additions
166 if s:got_fmt_error
167 let s:got_fmt_error = 0
168 call setloclist(0, [])
169 let l:open_lwindow = 1
170 endif
171 elseif g:rustfmt_fail_silently == 0 && !a:from_writepre
172 " otherwise get the errors and put them in the location list
173 let l:errors = []
174
175 let l:prev_line = ""
176 for l:line in l:stderr
177 " error: expected one of `;` or `as`, found `extern`
178 " --> src/main.rs:2:1
179 let tokens = matchlist(l:line, '^\s\+-->\s\(.\{-}\):\(\d\+\):\(\d\+\)$')
180 if !empty(tokens)
181 call add(l:errors, {"filename": @%,
182 \"lnum": tokens[2],
183 \"col": tokens[3],
184 \"text": l:prev_line})
185 endif
186 let l:prev_line = l:line
187 endfor
188
189 if !empty(l:errors)
190 call setloclist(0, l:errors, 'r')
191 echohl Error | echomsg "rustfmt returned error" | echohl None
192 else
193 echo "rust.vim: was not able to parse rustfmt messages. Here is the raw output:"
194 echo "\n"
195 for l:line in l:stderr
196 echo l:line
197 endfor
198 endif
199
200 let s:got_fmt_error = 1
201 let l:open_lwindow = 1
202 endif
203
204 " Restore the current directory if needed
205 if a:tmpname ==# ''
206 if l:has_lcd
207 execute 'lchdir! '.l:prev_cd
208 else
209 execute 'chdir! '.l:prev_cd
210 endif
211 endif
212
213 " Open lwindow after we have changed back to the previous directory
214 if l:open_lwindow == 1
215 lwindow
216 endif
217
218 call winrestview(l:view)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100219endfunction
220
221function! rustfmt#FormatRange(line1, line2)
Gregory Andersfc935942023-09-12 13:23:38 -0500222 let l:tmpname = tempname()
223 call writefile(getline(1, '$'), l:tmpname)
224 let command = s:RustfmtCommandRange(l:tmpname, a:line1, a:line2)
225 call s:RunRustfmt(command, l:tmpname, v:false)
226 call delete(l:tmpname)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100227endfunction
228
229function! rustfmt#Format()
Gregory Andersfc935942023-09-12 13:23:38 -0500230 call s:RunRustfmt(s:RustfmtCommand(), '', v:false)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100231endfunction
Gregory Andersfc935942023-09-12 13:23:38 -0500232
233function! rustfmt#Cmd()
234 " Mainly for debugging
235 return s:RustfmtCommand()
236endfunction
237
238function! rustfmt#PreWrite()
239 if !filereadable(expand("%@"))
240 return
241 endif
242 if rust#GetConfigVar('rustfmt_autosave_if_config_present', 0)
243 if findfile('rustfmt.toml', '.;') !=# '' || findfile('.rustfmt.toml', '.;') !=# ''
244 let b:rustfmt_autosave = 1
245 let b:_rustfmt_autosave_because_of_config = 1
246 endif
247 else
248 if has_key(b:, '_rustfmt_autosave_because_of_config')
249 unlet b:_rustfmt_autosave_because_of_config
250 unlet b:rustfmt_autosave
251 endif
252 endif
253
254 if !rust#GetConfigVar("rustfmt_autosave", 0)
255 return
256 endif
257
258 call s:RunRustfmt(s:RustfmtCommand(), '', v:true)
259endfunction
260
261
262" vim: set et sw=4 sts=4 ts=8: