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