neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

rustfmt.vim (8472B)


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