Riley Bruins | 0a08306 | 2024-06-03 20:40:45 +0200 | [diff] [blame] | 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') |
Konfekt | 4ac995b | 2025-03-31 20:45:26 +0200 | [diff] [blame] | 6 | " 2025 Mar 31 by Vim project (set 'formatprg' option) |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 7 | " For bugs, patches and license go to https://github.com/rust-lang/rust.vim |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 8 | |
| 9 | if exists("b:did_ftplugin") |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 10 | finish |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 11 | endif |
| 12 | let b:did_ftplugin = 1 |
| 13 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 14 | " vint: -ProhibitAbbreviationOption |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 15 | let s:save_cpo = &cpo |
| 16 | set cpo&vim |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 17 | " vint: +ProhibitAbbreviationOption |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 18 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 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 |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 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. |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 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:*/,:///,://!,:// |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 38 | else |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 39 | setlocal comments=s0:/*!,ex:*/,s1:/*,mb:*,ex:*/,:///,://!,:// |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 40 | endif |
Riley Bruins | 0a08306 | 2024-06-03 20:40:45 +0200 | [diff] [blame] | 41 | setlocal commentstring=//\ %s |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 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 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 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=99 |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 54 | endif |
| 55 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 56 | setlocal include=\\v^\\s*(pub\\s+)?use\\s+\\zs(\\f\|:)+ |
| 57 | setlocal includeexpr=rust#IncludeExpr(v:fname) |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 58 | |
| 59 | setlocal suffixesadd=.rs |
| 60 | |
Konfekt | 4ac995b | 2025-03-31 20:45:26 +0200 | [diff] [blame] | 61 | if executable(get(g:, 'rustfmt_command', 'rustfmt')) |
| 62 | if get(g:, "rustfmt_fail_silently", 0) |
| 63 | augroup rust.vim.FailSilently |
| 64 | autocmd! * <buffer> |
| 65 | autocmd ShellFilterPost <buffer> if v:shell_error | execute 'echom "shell filter returned error " . v:shell_error . ", undoing changes"' | undo | endif |
| 66 | augroup END |
| 67 | endif |
| 68 | |
| 69 | let &l:formatprg = get(g:, 'rustfmt_command', 'rustfmt') . ' ' . |
| 70 | \ get(g:, 'rustfmt_options', '') . ' ' . |
| 71 | \ rustfmt#RustfmtConfigOptions() |
| 72 | endif |
| 73 | |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 74 | if exists("g:ftplugin_rust_source_path") |
| 75 | let &l:path=g:ftplugin_rust_source_path . ',' . &l:path |
| 76 | endif |
| 77 | |
| 78 | if exists("g:loaded_delimitMate") |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 79 | if exists("b:delimitMate_excluded_regions") |
| 80 | let b:rust_original_delimitMate_excluded_regions = b:delimitMate_excluded_regions |
| 81 | endif |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 82 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 83 | augroup rust.vim.DelimitMate |
| 84 | autocmd! |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 85 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 86 | autocmd User delimitMate_map :call rust#delimitmate#onMap() |
| 87 | autocmd User delimitMate_unmap :call rust#delimitmate#onUnmap() |
| 88 | augroup END |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 89 | endif |
| 90 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 91 | " Integration with auto-pairs (https://github.com/jiangmiao/auto-pairs) |
| 92 | if exists("g:AutoPairsLoaded") && !get(g:, 'rust_keep_autopairs_default', 0) |
| 93 | let b:AutoPairs = {'(':')', '[':']', '{':'}','"':'"', '`':'`'} |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 94 | endif |
| 95 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 96 | if has("folding") && get(g:, 'rust_fold', 0) |
| 97 | let b:rust_set_foldmethod=1 |
| 98 | setlocal foldmethod=syntax |
| 99 | if g:rust_fold == 2 |
| 100 | setlocal foldlevel< |
| 101 | else |
| 102 | setlocal foldlevel=99 |
| 103 | endif |
| 104 | endif |
| 105 | |
| 106 | if has('conceal') && get(g:, 'rust_conceal', 0) |
| 107 | let b:rust_set_conceallevel=1 |
| 108 | setlocal conceallevel=2 |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 109 | endif |
| 110 | |
| 111 | " Motion Commands {{{1 |
MyyPo | ef21bca | 2024-03-18 20:38:09 +0200 | [diff] [blame] | 112 | if !exists("g:no_plugin_maps") && !exists("g:no_rust_maps") |
| 113 | " Bind motion commands to support hanging indents |
| 114 | nnoremap <silent> <buffer> [[ :call rust#Jump('n', 'Back')<CR> |
| 115 | nnoremap <silent> <buffer> ]] :call rust#Jump('n', 'Forward')<CR> |
| 116 | xnoremap <silent> <buffer> [[ :call rust#Jump('v', 'Back')<CR> |
| 117 | xnoremap <silent> <buffer> ]] :call rust#Jump('v', 'Forward')<CR> |
| 118 | onoremap <silent> <buffer> [[ :call rust#Jump('o', 'Back')<CR> |
| 119 | onoremap <silent> <buffer> ]] :call rust#Jump('o', 'Forward')<CR> |
| 120 | endif |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 121 | |
| 122 | " Commands {{{1 |
| 123 | |
| 124 | " See |:RustRun| for docs |
| 125 | command! -nargs=* -complete=file -bang -buffer RustRun call rust#Run(<bang>0, <q-args>) |
| 126 | |
| 127 | " See |:RustExpand| for docs |
| 128 | command! -nargs=* -complete=customlist,rust#CompleteExpand -bang -buffer RustExpand call rust#Expand(<bang>0, <q-args>) |
| 129 | |
| 130 | " See |:RustEmitIr| for docs |
| 131 | command! -nargs=* -buffer RustEmitIr call rust#Emit("llvm-ir", <q-args>) |
| 132 | |
| 133 | " See |:RustEmitAsm| for docs |
| 134 | command! -nargs=* -buffer RustEmitAsm call rust#Emit("asm", <q-args>) |
| 135 | |
| 136 | " See |:RustPlay| for docs |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 137 | command! -range=% -buffer RustPlay :call rust#Play(<count>, <line1>, <line2>, <f-args>) |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 138 | |
| 139 | " See |:RustFmt| for docs |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 140 | command! -bar -buffer RustFmt call rustfmt#Format() |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 141 | |
| 142 | " See |:RustFmtRange| for docs |
| 143 | command! -range -buffer RustFmtRange call rustfmt#FormatRange(<line1>, <line2>) |
| 144 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 145 | " See |:RustInfo| for docs |
| 146 | command! -bar -buffer RustInfo call rust#debugging#Info() |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 147 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 148 | " See |:RustInfoToClipboard| for docs |
| 149 | command! -bar -buffer RustInfoToClipboard call rust#debugging#InfoToClipboard() |
| 150 | |
| 151 | " See |:RustInfoToFile| for docs |
| 152 | command! -bar -nargs=1 -buffer RustInfoToFile call rust#debugging#InfoToFile(<f-args>) |
| 153 | |
| 154 | " See |:RustTest| for docs |
| 155 | command! -buffer -nargs=* -count -bang RustTest call rust#Test(<q-mods>, <count>, <bang>0, <q-args>) |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 156 | |
| 157 | if !exists("b:rust_last_rustc_args") || !exists("b:rust_last_args") |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 158 | let b:rust_last_rustc_args = [] |
| 159 | let b:rust_last_args = [] |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 160 | endif |
| 161 | |
| 162 | " Cleanup {{{1 |
| 163 | |
| 164 | let b:undo_ftplugin = " |
Konfekt | 7c3f9af | 2024-10-05 17:26:46 +0200 | [diff] [blame] | 165 | \ compiler make | |
Konfekt | 4ac995b | 2025-03-31 20:45:26 +0200 | [diff] [blame] | 166 | \ setlocal formatoptions< comments< commentstring< include< includeexpr< suffixesadd< formatprg< |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 167 | \|if exists('b:rust_set_style') |
| 168 | \|setlocal tabstop< shiftwidth< softtabstop< expandtab< textwidth< |
| 169 | \|endif |
| 170 | \|if exists('b:rust_original_delimitMate_excluded_regions') |
| 171 | \|let b:delimitMate_excluded_regions = b:rust_original_delimitMate_excluded_regions |
| 172 | \|unlet b:rust_original_delimitMate_excluded_regions |
| 173 | \|else |
| 174 | \|unlet! b:delimitMate_excluded_regions |
| 175 | \|endif |
| 176 | \|if exists('b:rust_set_foldmethod') |
| 177 | \|setlocal foldmethod< foldlevel< |
| 178 | \|unlet b:rust_set_foldmethod |
| 179 | \|endif |
| 180 | \|if exists('b:rust_set_conceallevel') |
| 181 | \|setlocal conceallevel< |
| 182 | \|unlet b:rust_set_conceallevel |
| 183 | \|endif |
| 184 | \|unlet! b:rust_last_rustc_args b:rust_last_args |
| 185 | \|delcommand -buffer RustRun |
| 186 | \|delcommand -buffer RustExpand |
| 187 | \|delcommand -buffer RustEmitIr |
| 188 | \|delcommand -buffer RustEmitAsm |
| 189 | \|delcommand -buffer RustPlay |
| 190 | \|delcommand -buffer RustFmt |
| 191 | \|delcommand -buffer RustFmtRange |
| 192 | \|delcommand -buffer RustInfo |
| 193 | \|delcommand -buffer RustInfoToClipboard |
| 194 | \|delcommand -buffer RustInfoToFile |
| 195 | \|delcommand -buffer RustTest |
MyyPo | ef21bca | 2024-03-18 20:38:09 +0200 | [diff] [blame] | 196 | \|silent! nunmap <buffer> [[ |
| 197 | \|silent! nunmap <buffer> ]] |
| 198 | \|silent! xunmap <buffer> [[ |
| 199 | \|silent! xunmap <buffer> ]] |
| 200 | \|silent! ounmap <buffer> [[ |
| 201 | \|silent! ounmap <buffer> ]] |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 202 | \|setlocal matchpairs-=<:> |
| 203 | \|unlet b:match_skip |
| 204 | \" |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 205 | |
| 206 | " }}}1 |
| 207 | |
| 208 | " Code formatting on save |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 209 | augroup rust.vim.PreWrite |
| 210 | autocmd! |
| 211 | autocmd BufWritePre *.rs silent! call rustfmt#PreWrite() |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 212 | augroup END |
| 213 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 214 | setlocal matchpairs+=<:> |
| 215 | " For matchit.vim (rustArrow stops `Fn() -> X` messing things up) |
| 216 | let b:match_skip = 's:comment\|string\|rustCharacter\|rustArrow' |
| 217 | |
| 218 | command! -buffer -nargs=+ Cargo call cargo#cmd(<q-args>) |
| 219 | command! -buffer -nargs=* Cbuild call cargo#build(<q-args>) |
| 220 | command! -buffer -nargs=* Ccheck call cargo#check(<q-args>) |
| 221 | command! -buffer -nargs=* Cclean call cargo#clean(<q-args>) |
| 222 | command! -buffer -nargs=* Cdoc call cargo#doc(<q-args>) |
| 223 | command! -buffer -nargs=+ Cnew call cargo#new(<q-args>) |
| 224 | command! -buffer -nargs=* Cinit call cargo#init(<q-args>) |
| 225 | command! -buffer -nargs=* Crun call cargo#run(<q-args>) |
| 226 | command! -buffer -nargs=* Ctest call cargo#test(<q-args>) |
| 227 | command! -buffer -nargs=* Cbench call cargo#bench(<q-args>) |
| 228 | command! -buffer -nargs=* Cupdate call cargo#update(<q-args>) |
| 229 | command! -buffer -nargs=* Csearch call cargo#search(<q-args>) |
| 230 | command! -buffer -nargs=* Cpublish call cargo#publish(<q-args>) |
| 231 | command! -buffer -nargs=* Cinstall call cargo#install(<q-args>) |
| 232 | command! -buffer -nargs=* Cruntarget call cargo#runtarget(<q-args>) |
| 233 | |
| 234 | let b:undo_ftplugin .= ' |
| 235 | \|delcommand -buffer Cargo |
| 236 | \|delcommand -buffer Cbuild |
| 237 | \|delcommand -buffer Ccheck |
| 238 | \|delcommand -buffer Cclean |
| 239 | \|delcommand -buffer Cdoc |
| 240 | \|delcommand -buffer Cnew |
| 241 | \|delcommand -buffer Cinit |
| 242 | \|delcommand -buffer Crun |
| 243 | \|delcommand -buffer Ctest |
| 244 | \|delcommand -buffer Cbench |
| 245 | \|delcommand -buffer Cupdate |
| 246 | \|delcommand -buffer Csearch |
| 247 | \|delcommand -buffer Cpublish |
| 248 | \|delcommand -buffer Cinstall |
| 249 | \|delcommand -buffer Cruntarget' |
| 250 | |
| 251 | " vint: -ProhibitAbbreviationOption |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 252 | let &cpo = s:save_cpo |
| 253 | unlet s:save_cpo |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 254 | " vint: +ProhibitAbbreviationOption |
Bram Moolenaar | 3c2881d | 2017-03-21 19:18:29 +0100 | [diff] [blame] | 255 | |
Gregory Anders | fc93594 | 2023-09-12 13:23:38 -0500 | [diff] [blame] | 256 | " vim: set et sw=4 sts=4 ts=8: |