Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 1 | " Author: Stephen Sugden <stephen@stephensugden.com> |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 2 | " Last Modified: 2023-09-11 |
Konfekt | 4ac995b | 2025-03-31 20:45:26 +0200 | [diff] [blame] | 3 | " Last Change: 2025 Mar 31 by Vim project (rename s:RustfmtConfigOptions()) |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 4 | " |
| 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 | |
| 8 | if !exists("g:rustfmt_autosave") |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 9 | let g:rustfmt_autosave = 0 |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 10 | endif |
| 11 | |
| 12 | if !exists("g:rustfmt_command") |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 13 | let g:rustfmt_command = "rustfmt" |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 14 | endif |
| 15 | |
| 16 | if !exists("g:rustfmt_options") |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 17 | let g:rustfmt_options = "" |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 18 | endif |
| 19 | |
| 20 | if !exists("g:rustfmt_fail_silently") |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 21 | let g:rustfmt_fail_silently = 0 |
| 22 | endif |
| 23 | |
| 24 | function! 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épe | dbf749b | 2023-10-16 09:53:37 +0200 | [diff] [blame] | 29 | " Build a comparable rustfmt version variable out of its `--version` output: |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 30 | 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 |
| 39 | endfunction |
| 40 | |
| 41 | call rustfmt#DetectVersion() |
| 42 | |
| 43 | if !exists("g:rustfmt_emit_files") |
| 44 | let g:rustfmt_emit_files = s:rustfmt_version >= "0.8.2" |
| 45 | endif |
| 46 | |
| 47 | if !exists("g:rustfmt_file_lines") |
| 48 | let g:rustfmt_file_lines = s:rustfmt_help =~# "--file-lines JSON" |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 49 | endif |
| 50 | |
| 51 | let s:got_fmt_error = 0 |
| 52 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 53 | function! rustfmt#Load() |
| 54 | " Utility call to get this script loaded, for debugging |
| 55 | endfunction |
| 56 | |
| 57 | function! s:RustfmtWriteMode() |
| 58 | if g:rustfmt_emit_files |
| 59 | return "--emit=files" |
| 60 | else |
| 61 | return "--write-mode=overwrite" |
| 62 | endif |
| 63 | endfunction |
| 64 | |
Konfekt | 4ac995b | 2025-03-31 20:45:26 +0200 | [diff] [blame] | 65 | function! rustfmt#RustfmtConfigOptions() |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 66 | 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' |
| 78 | endfunction |
| 79 | |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 80 | function! s:RustfmtCommandRange(filename, line1, line2) |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 81 | 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() |
Konfekt | 4ac995b | 2025-03-31 20:45:26 +0200 | [diff] [blame] | 88 | let l:rustfmt_config = rustfmt#RustfmtConfigOptions() |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 89 | |
| 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 Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 99 | endfunction |
| 100 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 101 | function! s:RustfmtCommand() |
| 102 | let write_mode = g:rustfmt_emit_files ? '--emit=stdout' : '--write-mode=display' |
Konfekt | 4ac995b | 2025-03-31 20:45:26 +0200 | [diff] [blame] | 103 | let config = rustfmt#RustfmtConfigOptions() |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 104 | return join([g:rustfmt_command, write_mode, config, g:rustfmt_options]) |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 105 | endfunction |
| 106 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 107 | function! s:DeleteLines(start, end) abort |
| 108 | silent! execute a:start . ',' . a:end . 'delete _' |
| 109 | endfunction |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 110 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 111 | function! s:RunRustfmt(command, tmpname, from_writepre) |
| 112 | let l:view = winsaveview() |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 113 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 114 | let l:stderr_tmpname = tempname() |
| 115 | call writefile([], l:stderr_tmpname) |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 116 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 117 | let l:command = a:command . ' 2> ' . l:stderr_tmpname |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 118 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 119 | if a:tmpname ==# '' |
| 120 | " Rustfmt in stdin/stdout mode |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 121 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 122 | " 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 Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 126 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 127 | 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 Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 141 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 142 | let l:stderr = readfile(l:stderr_tmpname) |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 143 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 144 | 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 Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 219 | endfunction |
| 220 | |
| 221 | function! rustfmt#FormatRange(line1, line2) |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 222 | 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 Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 227 | endfunction |
| 228 | |
| 229 | function! rustfmt#Format() |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 230 | call s:RunRustfmt(s:RustfmtCommand(), '', v:false) |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 231 | endfunction |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 232 | |
| 233 | function! rustfmt#Cmd() |
| 234 | " Mainly for debugging |
| 235 | return s:RustfmtCommand() |
| 236 | endfunction |
| 237 | |
| 238 | function! 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) |
| 259 | endfunction |
| 260 | |
| 261 | |
| 262 | " vim: set et sw=4 sts=4 ts=8: |