robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
cbe142d3a4
master
1" Author: Stephen Sugden <stephen@stephensugden.com> 2" Last Modified: 2023-09-11 3" Last Change: 4" 2025 Oct 27 by Vim project: don't use rustfmt as 'formatprg' by default 5" 2026 Jan 25 by Vim project: don't hide rustfmt errors, restore default var 6" 2026 Mar 30 by Vim project: use fnameescape for :chdir commands 7" 8" Adapted from https://github.com/fatih/vim-go 9" For bugs, patches and license go to https://github.com/rust-lang/rust.vim 10 11if ! exists ( "g:rustfmt_autosave" ) 12let g: rustfmt_autosave = 0 13endif 14 15if ! exists ( "g:rustfmt_command" ) 16let g: rustfmt_command = "rustfmt" 17endif 18 19if ! exists ( "g:rustfmt_options" ) 20let g: rustfmt_options = "" 21endif 22 23if ! exists ( "g:rustfmt_fail_silently" ) 24let g: rustfmt_fail_silently = 0 25endif 26 27function ! rustfmt#DetectVersion () 28let s: rustfmt_version = "0" 29let s: rustfmt_help = "" 30let s: rustfmt_unstable_features = "" 31if ! get ( g:, 'rustfmt_detect_version' , 0 ) 32return s: rustfmt_version 33endif 34" Save rustfmt '--help' for feature inspection 35silent let s: rustfmt_help = system ( g: rustfmt_command . " --help" ) 36let s: rustfmt_unstable_features = s: rustfmt_help =~# "--unstable-features" 37 38" Build a comparable rustfmt version variable out of its `--version` output: 39silent let l: rustfmt_version_full = system ( g: rustfmt_command . " --version" ) 40let l: rustfmt_version_list = matchlist ( l: rustfmt_version_full , 41 \'\vrustfmt ([0-9]+[.][0-9]+[.][0-9]+)' ) 42if len ( l: rustfmt_version_list ) >= 3 43let s: rustfmt_version = l: rustfmt_version_list [ 1 ] 44endif 45return s: rustfmt_version 46endfunction 47 48call rustfmt#DetectVersion () 49 50if ! exists ( "g:rustfmt_emit_files" ) 51let g: rustfmt_emit_files = s: rustfmt_version >= "0.8.2" 52endif 53 54if ! exists ( "g:rustfmt_file_lines" ) 55let g: rustfmt_file_lines = s: rustfmt_help =~# "--file-lines JSON" 56endif 57 58let s: got_fmt_error = 0 59 60function ! rustfmt#Load () 61" Utility call to get this script loaded, for debugging 62endfunction 63 64function ! s: RustfmtWriteMode () 65if g: rustfmt_emit_files 66return "--emit=files" 67else 68return "--write-mode=overwrite" 69endif 70endfunction 71 72function ! s: RustfmtConfigOptions () 73let default = '--edition 2018' 74 75if ! get ( g:, 'rustfmt_find_toml' , 0 ) 76return default 77endif 78 79let l: rustfmt_toml = findfile ( 'rustfmt.toml' , expand ( '%:p:h' ) . ';' ) 80if l: rustfmt_toml !=# '' 81return '--config-path ' . shellescape ( fnamemodify ( l: rustfmt_toml , ":p" )) 82endif 83 84let l: _rustfmt_toml = findfile ( '.rustfmt.toml' , expand ( '%:p:h' ) . ';' ) 85if l: _rustfmt_toml !=# '' 86return '--config-path ' . shellescape ( fnamemodify ( l: _rustfmt_toml , ":p" )) 87endif 88 89" Default to edition 2018 in case no rustfmt.toml was found. 90return default 91endfunction 92 93function ! s: RustfmtCommandRange ( filename , line1 , line2 ) 94if g: rustfmt_file_lines == 0 95echo "--file-lines is not supported in the installed `rustfmt` executable" 96return 97endif 98 99let l: arg = { "file" : shellescape ( a: filename ), "range" : [ a: line1 , a: line2 ]} 100let l: write_mode = s: RustfmtWriteMode () 101let l: rustfmt_config = s: RustfmtConfigOptions () 102 103" FIXME: When --file-lines gets to be stable, add version range checking 104" accordingly. 105let l: unstable_features = s: rustfmt_unstable_features ? '--unstable-features' : '' 106 107let l: cmd = printf ( "%s %s %s %s %s --file-lines '[%s]' %s" , g: rustfmt_command , 108 \l: write_mode , g: rustfmt_options , 109 \l: unstable_features , l: rustfmt_config , 110 \json_encode ( l: arg ), shellescape ( a: filename )) 111return l: cmd 112endfunction 113 114function ! s: RustfmtCommand () 115let write_mode = g: rustfmt_emit_files ? '--emit=stdout' : '--write-mode=display' 116let config = s: RustfmtConfigOptions () 117return join ([ g: rustfmt_command , write_mode , config , g: rustfmt_options ]) 118endfunction 119 120function ! s: DeleteLines ( start , end ) abort 121silent ! execute a: start . ',' . a: end . 'delete _' 122endfunction 123 124function ! s: RunRustfmt ( command , tmpname , from_writepre ) 125let l: view = winsaveview () 126 127let l: stderr_tmpname = tempname () 128call writefile ([], l: stderr_tmpname ) 129 130let l: command = a: command . ' 2> ' . l: stderr_tmpname 131 132if a: tmpname ==# '' 133" Rustfmt in stdin/stdout mode 134 135" chdir to the directory of the file 136let l: has_lcd = haslocaldir () 137let l: prev_cd = getcwd () 138execute 'lchdir! ' . fnameescape ( expand ( '%:h' )) 139 140let l: buffer = getline ( 1 , '$' ) 141if exists ( "*systemlist" ) 142silent let out = systemlist ( l: command , l: buffer ) 143else 144silent let out = split ( system ( l: command , 145 \join ( l: buffer , "\n" )), '\r\?\n' ) 146endif 147else 148if exists ( "*systemlist" ) 149silent let out = systemlist ( l: command ) 150else 151silent let out = split ( system ( l: command ), '\r\?\n' ) 152endif 153endif 154 155let l: stderr = readfile ( l: stderr_tmpname ) 156 157call delete ( l: stderr_tmpname ) 158 159let l: open_lwindow = 0 160if v: shell_error == 0 161if a: from_writepre 162" remove undo point caused via BufWritePre 163try |silent undojoin |catch |endtry 164endif 165 166if a: tmpname ==# '' 167let l: content = l: out 168else 169" take the tmpfile's content, this is better than rename 170" because it preserves file modes. 171let l: content = readfile ( a: tmpname ) 172endif 173 174call s: DeleteLines ( len ( l: content ), line ( '$' )) 175call setline ( 1 , l: content ) 176 177" only clear location list if it was previously filled to prevent 178" clobbering other additions 179if s: got_fmt_error 180let s: got_fmt_error = 0 181call setloclist ( 0 , []) 182let l: open_lwindow = 1 183endif 184elseif g: rustfmt_fail_silently == 0 && ! a: from_writepre 185" otherwise get the errors and put them in the location list 186let l: errors = [] 187 188let l: prev_line = "" 189for l: line in l: stderr 190" error: expected one of `;` or `as`, found `extern` 191" --> src/main.rs:2:1 192let tokens = matchlist ( l: line , '^\s\+-->\s\(.\{-}\):\(\d\+\):\(\d\+\)$' ) 193if ! empty ( tokens ) 194call add ( l: errors , { "filename" : @%, 195 \"lnum" : tokens [ 2 ], 196 \"col" : tokens [ 3 ], 197 \"text" : l: prev_line }) 198endif 199let l: prev_line = l: line 200endfor 201 202if ! empty ( l: errors ) 203call setloclist ( 0 , l: errors , 'r' ) 204echohl Error |echomsg "rustfmt returned error" |echohl None 205else 206echo "rust.vim: was not able to parse rustfmt messages. Here is the raw output:" 207echo "\n" 208for l: line in l: stderr 209echomsg l: line 210endfor 211endif 212 213let s: got_fmt_error = 1 214let l: open_lwindow = 1 215endif 216 217" Restore the current directory if needed 218let l: prev_cd = fnameescape ( l: prev_cd ) 219if a: tmpname ==# '' 220if l: has_lcd 221execute 'lchdir! ' . l: prev_cd 222else 223execute 'chdir! ' . l: prev_cd 224endif 225endif 226 227" Open lwindow after we have changed back to the previous directory 228if l: open_lwindow == 1 229try 230lwindow 231catch /^Vim\%((\S\+)\)\=:E776:/ 232endtry 233endif 234 235call winrestview ( l: view ) 236endfunction 237 238function ! rustfmt#FormatRange ( line1 , line2 ) 239let l: tmpname = tempname () 240call writefile ( getline ( 1 , '$' ), l: tmpname ) 241let command = s: RustfmtCommandRange ( l: tmpname , a: line1 , a: line2 ) 242call s: RunRustfmt ( command , l: tmpname , v: false ) 243call delete ( l: tmpname ) 244endfunction 245 246function ! rustfmt#Format () 247call s: RunRustfmt ( s: RustfmtCommand (), '' , v: false ) 248endfunction 249 250function ! rustfmt#Cmd () 251" Mainly for debugging 252return s: RustfmtCommand () 253endfunction 254 255function ! rustfmt#PreWrite () 256if ! filereadable ( expand ( "%@" )) 257return 258endif 259if rust#GetConfigVar ( 'rustfmt_autosave_if_config_present' , 0 ) 260if findfile ( 'rustfmt.toml' , '.;' ) !=# '' || findfile ( '.rustfmt.toml' , '.;' ) !=# '' 261let b: rustfmt_autosave = 1 262let b: _rustfmt_autosave_because_of_config = 1 263endif 264else 265if has_key ( b:, '_rustfmt_autosave_because_of_config' ) 266unlet b: _rustfmt_autosave_because_of_config 267unlet b: rustfmt_autosave 268endif 269endif 270 271if ! rust#GetConfigVar ( "rustfmt_autosave" , 0 ) 272return 273endif 274 275call s: RunRustfmt ( s: RustfmtCommand (), '' , v: true ) 276endfunction 277 278 279" vim: set et sw=4 sts=4 ts=8: