neovim

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

rust.vim (9397B)


      1 " Language:	Rust
      2 " Description:	Vim ftplugin for Rust
      3 " Maintainer:	Chris Morgan <me@chrismorgan.info>
      4 " Last Change:	2024 Mar 17
      5 "		2024 May 23 by Riley Bruins <ribru17@gmail.com ('commentstring')
      6 "		2025 Dec 09 update 'textwidth# to 100 #18892
      7 " For bugs, patches and license go to https://github.com/rust-lang/rust.vim
      8 
      9 if exists("b:did_ftplugin")
     10    finish
     11 endif
     12 let b:did_ftplugin = 1
     13 
     14 " vint: -ProhibitAbbreviationOption
     15 let s:save_cpo = &cpo
     16 set cpo&vim
     17 " vint: +ProhibitAbbreviationOption
     18 
     19 if get(b:, 'current_compiler', '') ==# ''
     20    if strlen(findfile('Cargo.toml', '.;')) > 0
     21        compiler cargo
     22    else
     23        compiler rustc
     24    endif
     25 endif
     26 
     27 " Variables {{{1
     28 
     29 " The rust source code at present seems to typically omit a leader on /*!
     30 " comments, so we'll use that as our default, but make it easy to switch.
     31 " This does not affect indentation at all (I tested it with and without
     32 " leader), merely whether a leader is inserted by default or not.
     33 if get(g:, 'rust_bang_comment_leader', 0)
     34    " Why is the `,s0:/*,mb:\ ,ex:*/` there, you ask? I don't understand why,
     35    " but without it, */ gets indented one space even if there were no
     36    " leaders. I'm fairly sure that's a Vim bug.
     37    setlocal comments=s1:/*,mb:*,ex:*/,s0:/*,mb:\ ,ex:*/,:///,://!,://
     38 else
     39    setlocal comments=s0:/*!,ex:*/,s1:/*,mb:*,ex:*/,:///,://!,://
     40 endif
     41 setlocal commentstring=//\ %s
     42 setlocal formatoptions-=t formatoptions+=croqnl
     43 " j was only added in 7.3.541, so stop complaints about its nonexistence
     44 silent! setlocal formatoptions+=j
     45 
     46 " smartindent will be overridden by indentexpr if filetype indent is on, but
     47 " otherwise it's better than nothing.
     48 setlocal smartindent nocindent
     49 
     50 if get(g:, 'rust_recommended_style', 1)
     51    let b:rust_set_style = 1
     52    setlocal shiftwidth=4 softtabstop=4 expandtab
     53    setlocal textwidth=100
     54 endif
     55 
     56 setlocal include=\\v^\\s*(pub\\s+)?use\\s+\\zs(\\f\|:)+
     57 setlocal includeexpr=rust#IncludeExpr(v:fname)
     58 
     59 setlocal suffixesadd=.rs
     60 
     61 if exists("g:ftplugin_rust_source_path")
     62    let &l:path=g:ftplugin_rust_source_path . ',' . &l:path
     63 endif
     64 
     65 if exists("g:loaded_delimitMate")
     66    if exists("b:delimitMate_excluded_regions")
     67        let b:rust_original_delimitMate_excluded_regions = b:delimitMate_excluded_regions
     68    endif
     69 
     70    augroup rust.vim.DelimitMate
     71        autocmd!
     72 
     73        autocmd User delimitMate_map   :call rust#delimitmate#onMap()
     74        autocmd User delimitMate_unmap :call rust#delimitmate#onUnmap()
     75    augroup END
     76 endif
     77 
     78 " Integration with auto-pairs (https://github.com/jiangmiao/auto-pairs)
     79 if exists("g:AutoPairsLoaded") && !get(g:, 'rust_keep_autopairs_default', 0)
     80    let b:AutoPairs = {'(':')', '[':']', '{':'}','"':'"', '`':'`'}
     81 endif
     82 
     83 if has("folding") && get(g:, 'rust_fold', 0)
     84    let b:rust_set_foldmethod=1
     85    setlocal foldmethod=syntax
     86    if g:rust_fold == 2
     87        setlocal foldlevel<
     88    else
     89        setlocal foldlevel=99
     90    endif
     91 endif
     92 
     93 if has('conceal') && get(g:, 'rust_conceal', 0)
     94    let b:rust_set_conceallevel=1
     95    setlocal conceallevel=2
     96 endif
     97 
     98 " Motion Commands {{{1
     99 if !exists("g:no_plugin_maps") && !exists("g:no_rust_maps")
    100    " Bind motion commands to support hanging indents
    101    nnoremap <silent> <buffer> [[ :call rust#Jump('n', 'Back')<CR>
    102    nnoremap <silent> <buffer> ]] :call rust#Jump('n', 'Forward')<CR>
    103    xnoremap <silent> <buffer> [[ :call rust#Jump('v', 'Back')<CR>
    104    xnoremap <silent> <buffer> ]] :call rust#Jump('v', 'Forward')<CR>
    105    onoremap <silent> <buffer> [[ :call rust#Jump('o', 'Back')<CR>
    106    onoremap <silent> <buffer> ]] :call rust#Jump('o', 'Forward')<CR>
    107 endif
    108 
    109 " Commands {{{1
    110 
    111 " See |:RustRun| for docs
    112 command! -nargs=* -complete=file -bang -buffer RustRun call rust#Run(<bang>0, <q-args>)
    113 
    114 " See |:RustExpand| for docs
    115 command! -nargs=* -complete=customlist,rust#CompleteExpand -bang -buffer RustExpand call rust#Expand(<bang>0, <q-args>)
    116 
    117 " See |:RustEmitIr| for docs
    118 command! -nargs=* -buffer RustEmitIr call rust#Emit("llvm-ir", <q-args>)
    119 
    120 " See |:RustEmitAsm| for docs
    121 command! -nargs=* -buffer RustEmitAsm call rust#Emit("asm", <q-args>)
    122 
    123 " See |:RustPlay| for docs
    124 command! -range=% -buffer RustPlay :call rust#Play(<count>, <line1>, <line2>, <f-args>)
    125 
    126 " See |:RustFmt| for docs
    127 command! -bar -buffer RustFmt call rustfmt#Format()
    128 
    129 " See |:RustFmtRange| for docs
    130 command! -range -buffer RustFmtRange call rustfmt#FormatRange(<line1>, <line2>)
    131 
    132 " See |:RustInfo| for docs
    133 command! -bar -buffer RustInfo call rust#debugging#Info()
    134 
    135 " See |:RustInfoToClipboard| for docs
    136 command! -bar -buffer RustInfoToClipboard call rust#debugging#InfoToClipboard()
    137 
    138 " See |:RustInfoToFile| for docs
    139 command! -bar -nargs=1 -buffer RustInfoToFile call rust#debugging#InfoToFile(<f-args>)
    140 
    141 " See |:RustTest| for docs
    142 command! -buffer -nargs=* -count -bang RustTest call rust#Test(<q-mods>, <count>, <bang>0, <q-args>)
    143 
    144 if !exists("b:rust_last_rustc_args") || !exists("b:rust_last_args")
    145    let b:rust_last_rustc_args = []
    146    let b:rust_last_args = []
    147 endif
    148 
    149 " Cleanup {{{1
    150 
    151 let b:undo_ftplugin = "
    152            \ compiler make |
    153            \ setlocal formatoptions< comments< commentstring< include< includeexpr< suffixesadd<
    154            \|if exists('b:rust_set_style')
    155                \|setlocal tabstop< shiftwidth< softtabstop< expandtab< textwidth<
    156                \|endif
    157                \|if exists('b:rust_original_delimitMate_excluded_regions')
    158                    \|let b:delimitMate_excluded_regions = b:rust_original_delimitMate_excluded_regions
    159                    \|unlet b:rust_original_delimitMate_excluded_regions
    160                    \|else
    161                        \|unlet! b:delimitMate_excluded_regions
    162                        \|endif
    163                        \|if exists('b:rust_set_foldmethod')
    164                            \|setlocal foldmethod< foldlevel<
    165                            \|unlet b:rust_set_foldmethod
    166                            \|endif
    167                            \|if exists('b:rust_set_conceallevel')
    168                                \|setlocal conceallevel<
    169                                \|unlet b:rust_set_conceallevel
    170                                \|endif
    171                                \|unlet! b:rust_last_rustc_args b:rust_last_args
    172                                \|delcommand -buffer RustRun
    173                                \|delcommand -buffer RustExpand
    174                                \|delcommand -buffer RustEmitIr
    175                                \|delcommand -buffer RustEmitAsm
    176                                \|delcommand -buffer RustPlay
    177                                \|delcommand -buffer RustFmt
    178                                \|delcommand -buffer RustFmtRange
    179                                \|delcommand -buffer RustInfo
    180                                \|delcommand -buffer RustInfoToClipboard
    181                                \|delcommand -buffer RustInfoToFile
    182                                \|delcommand -buffer RustTest
    183                                \|silent! nunmap <buffer> [[
    184                                \|silent! nunmap <buffer> ]]
    185                                \|silent! xunmap <buffer> [[
    186                                \|silent! xunmap <buffer> ]]
    187                                \|silent! ounmap <buffer> [[
    188                                \|silent! ounmap <buffer> ]]
    189                                \|setlocal matchpairs-=<:>
    190                                \|unlet b:match_skip
    191                                \"
    192 
    193 " }}}1
    194 
    195 " Code formatting on save
    196 augroup rust.vim.PreWrite
    197    autocmd!
    198    autocmd BufWritePre *.rs silent! call rustfmt#PreWrite()
    199 augroup END
    200 
    201 setlocal matchpairs+=<:>
    202 " For matchit.vim (rustArrow stops `Fn() -> X` messing things up)
    203 let b:match_skip = 's:comment\|string\|rustCharacter\|rustArrow'
    204 
    205 command! -buffer -nargs=+ Cargo call cargo#cmd(<q-args>)
    206 command! -buffer -nargs=* Cbuild call cargo#build(<q-args>)
    207 command! -buffer -nargs=* Ccheck call cargo#check(<q-args>)
    208 command! -buffer -nargs=* Cclean call cargo#clean(<q-args>)
    209 command! -buffer -nargs=* Cdoc call cargo#doc(<q-args>)
    210 command! -buffer -nargs=+ Cnew call cargo#new(<q-args>)
    211 command! -buffer -nargs=* Cinit call cargo#init(<q-args>)
    212 command! -buffer -nargs=* Crun call cargo#run(<q-args>)
    213 command! -buffer -nargs=* Ctest call cargo#test(<q-args>)
    214 command! -buffer -nargs=* Cbench call cargo#bench(<q-args>)
    215 command! -buffer -nargs=* Cupdate call cargo#update(<q-args>)
    216 command! -buffer -nargs=* Csearch  call cargo#search(<q-args>)
    217 command! -buffer -nargs=* Cpublish call cargo#publish(<q-args>)
    218 command! -buffer -nargs=* Cinstall call cargo#install(<q-args>)
    219 command! -buffer -nargs=* Cruntarget call cargo#runtarget(<q-args>)
    220 
    221 let b:undo_ftplugin .= '
    222            \|delcommand -buffer Cargo
    223            \|delcommand -buffer Cbuild
    224            \|delcommand -buffer Ccheck
    225            \|delcommand -buffer Cclean
    226            \|delcommand -buffer Cdoc
    227            \|delcommand -buffer Cnew
    228            \|delcommand -buffer Cinit
    229            \|delcommand -buffer Crun
    230            \|delcommand -buffer Ctest
    231            \|delcommand -buffer Cbench
    232            \|delcommand -buffer Cupdate
    233            \|delcommand -buffer Csearch
    234            \|delcommand -buffer Cpublish
    235            \|delcommand -buffer Cinstall
    236            \|delcommand -buffer Cruntarget'
    237 
    238 " vint: -ProhibitAbbreviationOption
    239 let &cpo = s:save_cpo
    240 unlet s:save_cpo
    241 " vint: +ProhibitAbbreviationOption
    242 
    243 " vim: set et sw=4 sts=4 ts=8: