blob: 652e6af33ab7f3d99565cf0e5df4b289c912bd63 [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
Bram Moolenaar3c2881d2017-03-21 19:18:29 +01003"
4" Adapted from https://github.com/fatih/vim-go
5" For bugs, patches and license go to https://github.com/rust-lang/rust.vim
6
7if !exists("g:rustfmt_autosave")
Gregory Andersfc935942023-09-12 13:23:38 -05008 let g:rustfmt_autosave = 0
Bram Moolenaar3c2881d2017-03-21 19:18:29 +01009endif
10
11if !exists("g:rustfmt_command")
Gregory Andersfc935942023-09-12 13:23:38 -050012 let g:rustfmt_command = "rustfmt"
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010013endif
14
15if !exists("g:rustfmt_options")
Gregory Andersfc935942023-09-12 13:23:38 -050016 let g:rustfmt_options = ""
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010017endif
18
19if !exists("g:rustfmt_fail_silently")
Gregory Andersfc935942023-09-12 13:23:38 -050020 let g:rustfmt_fail_silently = 0
21endif
22
23function! rustfmt#DetectVersion()
24 " Save rustfmt '--help' for feature inspection
25 silent let s:rustfmt_help = system(g:rustfmt_command . " --help")
26 let s:rustfmt_unstable_features = s:rustfmt_help =~# "--unstable-features"
27
28 " Build a comparable rustfmt version varible out of its `--version` output:
29 silent let l:rustfmt_version_full = system(g:rustfmt_command . " --version")
30 let l:rustfmt_version_list = matchlist(l:rustfmt_version_full,
31 \ '\vrustfmt ([0-9]+[.][0-9]+[.][0-9]+)')
32 if len(l:rustfmt_version_list) < 3
33 let s:rustfmt_version = "0"
34 else
35 let s:rustfmt_version = l:rustfmt_version_list[1]
36 endif
37 return s:rustfmt_version
38endfunction
39
40call rustfmt#DetectVersion()
41
42if !exists("g:rustfmt_emit_files")
43 let g:rustfmt_emit_files = s:rustfmt_version >= "0.8.2"
44endif
45
46if !exists("g:rustfmt_file_lines")
47 let g:rustfmt_file_lines = s:rustfmt_help =~# "--file-lines JSON"
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010048endif
49
50let s:got_fmt_error = 0
51
Gregory Andersfc935942023-09-12 13:23:38 -050052function! rustfmt#Load()
53 " Utility call to get this script loaded, for debugging
54endfunction
55
56function! s:RustfmtWriteMode()
57 if g:rustfmt_emit_files
58 return "--emit=files"
59 else
60 return "--write-mode=overwrite"
61 endif
62endfunction
63
64function! s:RustfmtConfigOptions()
65 let l:rustfmt_toml = findfile('rustfmt.toml', expand('%:p:h') . ';')
66 if l:rustfmt_toml !=# ''
67 return '--config-path '.shellescape(fnamemodify(l:rustfmt_toml, ":p"))
68 endif
69
70 let l:_rustfmt_toml = findfile('.rustfmt.toml', expand('%:p:h') . ';')
71 if l:_rustfmt_toml !=# ''
72 return '--config-path '.shellescape(fnamemodify(l:_rustfmt_toml, ":p"))
73 endif
74
75 " Default to edition 2018 in case no rustfmt.toml was found.
76 return '--edition 2018'
77endfunction
78
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010079function! s:RustfmtCommandRange(filename, line1, line2)
Gregory Andersfc935942023-09-12 13:23:38 -050080 if g:rustfmt_file_lines == 0
81 echo "--file-lines is not supported in the installed `rustfmt` executable"
82 return
83 endif
84
85 let l:arg = {"file": shellescape(a:filename), "range": [a:line1, a:line2]}
86 let l:write_mode = s:RustfmtWriteMode()
87 let l:rustfmt_config = s:RustfmtConfigOptions()
88
89 " FIXME: When --file-lines gets to be stable, add version range checking
90 " accordingly.
91 let l:unstable_features = s:rustfmt_unstable_features ? '--unstable-features' : ''
92
93 let l:cmd = printf("%s %s %s %s %s --file-lines '[%s]' %s", g:rustfmt_command,
94 \ l:write_mode, g:rustfmt_options,
95 \ l:unstable_features, l:rustfmt_config,
96 \ json_encode(l:arg), shellescape(a:filename))
97 return l:cmd
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010098endfunction
99
Gregory Andersfc935942023-09-12 13:23:38 -0500100function! s:RustfmtCommand()
101 let write_mode = g:rustfmt_emit_files ? '--emit=stdout' : '--write-mode=display'
102 let config = s:RustfmtConfigOptions()
103 return join([g:rustfmt_command, write_mode, config, g:rustfmt_options])
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100104endfunction
105
Gregory Andersfc935942023-09-12 13:23:38 -0500106function! s:DeleteLines(start, end) abort
107 silent! execute a:start . ',' . a:end . 'delete _'
108endfunction
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100109
Gregory Andersfc935942023-09-12 13:23:38 -0500110function! s:RunRustfmt(command, tmpname, from_writepre)
111 let l:view = winsaveview()
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100112
Gregory Andersfc935942023-09-12 13:23:38 -0500113 let l:stderr_tmpname = tempname()
114 call writefile([], l:stderr_tmpname)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100115
Gregory Andersfc935942023-09-12 13:23:38 -0500116 let l:command = a:command . ' 2> ' . l:stderr_tmpname
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100117
Gregory Andersfc935942023-09-12 13:23:38 -0500118 if a:tmpname ==# ''
119 " Rustfmt in stdin/stdout mode
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100120
Gregory Andersfc935942023-09-12 13:23:38 -0500121 " chdir to the directory of the file
122 let l:has_lcd = haslocaldir()
123 let l:prev_cd = getcwd()
124 execute 'lchdir! '.expand('%:h')
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100125
Gregory Andersfc935942023-09-12 13:23:38 -0500126 let l:buffer = getline(1, '$')
127 if exists("*systemlist")
128 silent let out = systemlist(l:command, l:buffer)
129 else
130 silent let out = split(system(l:command,
131 \ join(l:buffer, "\n")), '\r\?\n')
132 endif
133 else
134 if exists("*systemlist")
135 silent let out = systemlist(l:command)
136 else
137 silent let out = split(system(l:command), '\r\?\n')
138 endif
139 endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100140
Gregory Andersfc935942023-09-12 13:23:38 -0500141 let l:stderr = readfile(l:stderr_tmpname)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100142
Gregory Andersfc935942023-09-12 13:23:38 -0500143 call delete(l:stderr_tmpname)
144
145 let l:open_lwindow = 0
146 if v:shell_error == 0
147 if a:from_writepre
148 " remove undo point caused via BufWritePre
149 try | silent undojoin | catch | endtry
150 endif
151
152 if a:tmpname ==# ''
153 let l:content = l:out
154 else
155 " take the tmpfile's content, this is better than rename
156 " because it preserves file modes.
157 let l:content = readfile(a:tmpname)
158 endif
159
160 call s:DeleteLines(len(l:content), line('$'))
161 call setline(1, l:content)
162
163 " only clear location list if it was previously filled to prevent
164 " clobbering other additions
165 if s:got_fmt_error
166 let s:got_fmt_error = 0
167 call setloclist(0, [])
168 let l:open_lwindow = 1
169 endif
170 elseif g:rustfmt_fail_silently == 0 && !a:from_writepre
171 " otherwise get the errors and put them in the location list
172 let l:errors = []
173
174 let l:prev_line = ""
175 for l:line in l:stderr
176 " error: expected one of `;` or `as`, found `extern`
177 " --> src/main.rs:2:1
178 let tokens = matchlist(l:line, '^\s\+-->\s\(.\{-}\):\(\d\+\):\(\d\+\)$')
179 if !empty(tokens)
180 call add(l:errors, {"filename": @%,
181 \"lnum": tokens[2],
182 \"col": tokens[3],
183 \"text": l:prev_line})
184 endif
185 let l:prev_line = l:line
186 endfor
187
188 if !empty(l:errors)
189 call setloclist(0, l:errors, 'r')
190 echohl Error | echomsg "rustfmt returned error" | echohl None
191 else
192 echo "rust.vim: was not able to parse rustfmt messages. Here is the raw output:"
193 echo "\n"
194 for l:line in l:stderr
195 echo l:line
196 endfor
197 endif
198
199 let s:got_fmt_error = 1
200 let l:open_lwindow = 1
201 endif
202
203 " Restore the current directory if needed
204 if a:tmpname ==# ''
205 if l:has_lcd
206 execute 'lchdir! '.l:prev_cd
207 else
208 execute 'chdir! '.l:prev_cd
209 endif
210 endif
211
212 " Open lwindow after we have changed back to the previous directory
213 if l:open_lwindow == 1
214 lwindow
215 endif
216
217 call winrestview(l:view)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100218endfunction
219
220function! rustfmt#FormatRange(line1, line2)
Gregory Andersfc935942023-09-12 13:23:38 -0500221 let l:tmpname = tempname()
222 call writefile(getline(1, '$'), l:tmpname)
223 let command = s:RustfmtCommandRange(l:tmpname, a:line1, a:line2)
224 call s:RunRustfmt(command, l:tmpname, v:false)
225 call delete(l:tmpname)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100226endfunction
227
228function! rustfmt#Format()
Gregory Andersfc935942023-09-12 13:23:38 -0500229 call s:RunRustfmt(s:RustfmtCommand(), '', v:false)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100230endfunction
Gregory Andersfc935942023-09-12 13:23:38 -0500231
232function! rustfmt#Cmd()
233 " Mainly for debugging
234 return s:RustfmtCommand()
235endfunction
236
237function! rustfmt#PreWrite()
238 if !filereadable(expand("%@"))
239 return
240 endif
241 if rust#GetConfigVar('rustfmt_autosave_if_config_present', 0)
242 if findfile('rustfmt.toml', '.;') !=# '' || findfile('.rustfmt.toml', '.;') !=# ''
243 let b:rustfmt_autosave = 1
244 let b:_rustfmt_autosave_because_of_config = 1
245 endif
246 else
247 if has_key(b:, '_rustfmt_autosave_because_of_config')
248 unlet b:_rustfmt_autosave_because_of_config
249 unlet b:rustfmt_autosave
250 endif
251 endif
252
253 if !rust#GetConfigVar("rustfmt_autosave", 0)
254 return
255 endif
256
257 call s:RunRustfmt(s:RustfmtCommand(), '', v:true)
258endfunction
259
260
261" vim: set et sw=4 sts=4 ts=8: