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