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