blob: fb15b444d008c878ba9fdab1f6950e7e0d034e7f [file] [log] [blame]
Bram Moolenaar3c2881d2017-03-21 19:18:29 +01001" Language: Rust
2" Description: Vim ftplugin for Rust
3" Maintainer: Chris Morgan <me@chrismorgan.info>
MyyPoef21bca2024-03-18 20:38:09 +02004" Last Change: 2024-03-17
Gregory Andersfc935942023-09-12 13:23:38 -05005" For bugs, patches and license go to https://github.com/rust-lang/rust.vim
Bram Moolenaar3c2881d2017-03-21 19:18:29 +01006
7if exists("b:did_ftplugin")
Gregory Andersfc935942023-09-12 13:23:38 -05008 finish
Bram Moolenaar3c2881d2017-03-21 19:18:29 +01009endif
10let b:did_ftplugin = 1
11
Gregory Andersfc935942023-09-12 13:23:38 -050012" vint: -ProhibitAbbreviationOption
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010013let s:save_cpo = &cpo
14set cpo&vim
Gregory Andersfc935942023-09-12 13:23:38 -050015" vint: +ProhibitAbbreviationOption
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010016
Gregory Andersfc935942023-09-12 13:23:38 -050017if get(b:, 'current_compiler', '') ==# ''
18 if strlen(findfile('Cargo.toml', '.;')) > 0
19 compiler cargo
20 else
21 compiler rustc
22 endif
23endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010024
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 Andersfc935942023-09-12 13:23:38 -050031if 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 Moolenaar3c2881d2017-03-21 19:18:29 +010036else
Gregory Andersfc935942023-09-12 13:23:38 -050037 setlocal comments=s0:/*!,ex:*/,s1:/*,mb:*,ex:*/,:///,://!,://
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010038endif
39setlocal commentstring=//%s
40setlocal formatoptions-=t formatoptions+=croqnl
41" j was only added in 7.3.541, so stop complaints about its nonexistence
42silent! setlocal formatoptions+=j
43
44" smartindent will be overridden by indentexpr if filetype indent is on, but
45" otherwise it's better than nothing.
46setlocal smartindent nocindent
47
Gregory Andersfc935942023-09-12 13:23:38 -050048if 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 Moolenaar3c2881d2017-03-21 19:18:29 +010052endif
53
Gregory Andersfc935942023-09-12 13:23:38 -050054setlocal include=\\v^\\s*(pub\\s+)?use\\s+\\zs(\\f\|:)+
55setlocal includeexpr=rust#IncludeExpr(v:fname)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010056
57setlocal suffixesadd=.rs
58
59if exists("g:ftplugin_rust_source_path")
60 let &l:path=g:ftplugin_rust_source_path . ',' . &l:path
61endif
62
63if exists("g:loaded_delimitMate")
Gregory Andersfc935942023-09-12 13:23:38 -050064 if exists("b:delimitMate_excluded_regions")
65 let b:rust_original_delimitMate_excluded_regions = b:delimitMate_excluded_regions
66 endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010067
Gregory Andersfc935942023-09-12 13:23:38 -050068 augroup rust.vim.DelimitMate
69 autocmd!
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010070
Gregory Andersfc935942023-09-12 13:23:38 -050071 autocmd User delimitMate_map :call rust#delimitmate#onMap()
72 autocmd User delimitMate_unmap :call rust#delimitmate#onUnmap()
73 augroup END
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010074endif
75
Gregory Andersfc935942023-09-12 13:23:38 -050076" Integration with auto-pairs (https://github.com/jiangmiao/auto-pairs)
77if exists("g:AutoPairsLoaded") && !get(g:, 'rust_keep_autopairs_default', 0)
78 let b:AutoPairs = {'(':')', '[':']', '{':'}','"':'"', '`':'`'}
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010079endif
80
Gregory Andersfc935942023-09-12 13:23:38 -050081if 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
89endif
90
91if has('conceal') && get(g:, 'rust_conceal', 0)
92 let b:rust_set_conceallevel=1
93 setlocal conceallevel=2
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010094endif
95
96" Motion Commands {{{1
MyyPoef21bca2024-03-18 20:38:09 +020097if !exists("g:no_plugin_maps") && !exists("g:no_rust_maps")
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>
105endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100106
107" Commands {{{1
108
109" See |:RustRun| for docs
110command! -nargs=* -complete=file -bang -buffer RustRun call rust#Run(<bang>0, <q-args>)
111
112" See |:RustExpand| for docs
113command! -nargs=* -complete=customlist,rust#CompleteExpand -bang -buffer RustExpand call rust#Expand(<bang>0, <q-args>)
114
115" See |:RustEmitIr| for docs
116command! -nargs=* -buffer RustEmitIr call rust#Emit("llvm-ir", <q-args>)
117
118" See |:RustEmitAsm| for docs
119command! -nargs=* -buffer RustEmitAsm call rust#Emit("asm", <q-args>)
120
121" See |:RustPlay| for docs
Gregory Andersfc935942023-09-12 13:23:38 -0500122command! -range=% -buffer RustPlay :call rust#Play(<count>, <line1>, <line2>, <f-args>)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100123
124" See |:RustFmt| for docs
Gregory Andersfc935942023-09-12 13:23:38 -0500125command! -bar -buffer RustFmt call rustfmt#Format()
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100126
127" See |:RustFmtRange| for docs
128command! -range -buffer RustFmtRange call rustfmt#FormatRange(<line1>, <line2>)
129
Gregory Andersfc935942023-09-12 13:23:38 -0500130" See |:RustInfo| for docs
131command! -bar -buffer RustInfo call rust#debugging#Info()
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100132
Gregory Andersfc935942023-09-12 13:23:38 -0500133" See |:RustInfoToClipboard| for docs
134command! -bar -buffer RustInfoToClipboard call rust#debugging#InfoToClipboard()
135
136" See |:RustInfoToFile| for docs
137command! -bar -nargs=1 -buffer RustInfoToFile call rust#debugging#InfoToFile(<f-args>)
138
139" See |:RustTest| for docs
140command! -buffer -nargs=* -count -bang RustTest call rust#Test(<q-mods>, <count>, <bang>0, <q-args>)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100141
142if !exists("b:rust_last_rustc_args") || !exists("b:rust_last_args")
Gregory Andersfc935942023-09-12 13:23:38 -0500143 let b:rust_last_rustc_args = []
144 let b:rust_last_args = []
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100145endif
146
147" Cleanup {{{1
148
149let b:undo_ftplugin = "
Gregory Andersfc935942023-09-12 13:23:38 -0500150 \ setlocal formatoptions< comments< commentstring< include< includeexpr< suffixesadd<
151 \|if exists('b:rust_set_style')
152 \|setlocal tabstop< shiftwidth< softtabstop< expandtab< textwidth<
153 \|endif
154 \|if exists('b:rust_original_delimitMate_excluded_regions')
155 \|let b:delimitMate_excluded_regions = b:rust_original_delimitMate_excluded_regions
156 \|unlet b:rust_original_delimitMate_excluded_regions
157 \|else
158 \|unlet! b:delimitMate_excluded_regions
159 \|endif
160 \|if exists('b:rust_set_foldmethod')
161 \|setlocal foldmethod< foldlevel<
162 \|unlet b:rust_set_foldmethod
163 \|endif
164 \|if exists('b:rust_set_conceallevel')
165 \|setlocal conceallevel<
166 \|unlet b:rust_set_conceallevel
167 \|endif
168 \|unlet! b:rust_last_rustc_args b:rust_last_args
169 \|delcommand -buffer RustRun
170 \|delcommand -buffer RustExpand
171 \|delcommand -buffer RustEmitIr
172 \|delcommand -buffer RustEmitAsm
173 \|delcommand -buffer RustPlay
174 \|delcommand -buffer RustFmt
175 \|delcommand -buffer RustFmtRange
176 \|delcommand -buffer RustInfo
177 \|delcommand -buffer RustInfoToClipboard
178 \|delcommand -buffer RustInfoToFile
179 \|delcommand -buffer RustTest
MyyPoef21bca2024-03-18 20:38:09 +0200180 \|silent! nunmap <buffer> [[
181 \|silent! nunmap <buffer> ]]
182 \|silent! xunmap <buffer> [[
183 \|silent! xunmap <buffer> ]]
184 \|silent! ounmap <buffer> [[
185 \|silent! ounmap <buffer> ]]
Gregory Andersfc935942023-09-12 13:23:38 -0500186 \|setlocal matchpairs-=<:>
187 \|unlet b:match_skip
188 \"
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100189
190" }}}1
191
192" Code formatting on save
Gregory Andersfc935942023-09-12 13:23:38 -0500193augroup rust.vim.PreWrite
194 autocmd!
195 autocmd BufWritePre *.rs silent! call rustfmt#PreWrite()
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100196augroup END
197
Gregory Andersfc935942023-09-12 13:23:38 -0500198setlocal matchpairs+=<:>
199" For matchit.vim (rustArrow stops `Fn() -> X` messing things up)
200let b:match_skip = 's:comment\|string\|rustCharacter\|rustArrow'
201
202command! -buffer -nargs=+ Cargo call cargo#cmd(<q-args>)
203command! -buffer -nargs=* Cbuild call cargo#build(<q-args>)
204command! -buffer -nargs=* Ccheck call cargo#check(<q-args>)
205command! -buffer -nargs=* Cclean call cargo#clean(<q-args>)
206command! -buffer -nargs=* Cdoc call cargo#doc(<q-args>)
207command! -buffer -nargs=+ Cnew call cargo#new(<q-args>)
208command! -buffer -nargs=* Cinit call cargo#init(<q-args>)
209command! -buffer -nargs=* Crun call cargo#run(<q-args>)
210command! -buffer -nargs=* Ctest call cargo#test(<q-args>)
211command! -buffer -nargs=* Cbench call cargo#bench(<q-args>)
212command! -buffer -nargs=* Cupdate call cargo#update(<q-args>)
213command! -buffer -nargs=* Csearch call cargo#search(<q-args>)
214command! -buffer -nargs=* Cpublish call cargo#publish(<q-args>)
215command! -buffer -nargs=* Cinstall call cargo#install(<q-args>)
216command! -buffer -nargs=* Cruntarget call cargo#runtarget(<q-args>)
217
218let b:undo_ftplugin .= '
219 \|delcommand -buffer Cargo
220 \|delcommand -buffer Cbuild
221 \|delcommand -buffer Ccheck
222 \|delcommand -buffer Cclean
223 \|delcommand -buffer Cdoc
224 \|delcommand -buffer Cnew
225 \|delcommand -buffer Cinit
226 \|delcommand -buffer Crun
227 \|delcommand -buffer Ctest
228 \|delcommand -buffer Cbench
229 \|delcommand -buffer Cupdate
230 \|delcommand -buffer Csearch
231 \|delcommand -buffer Cpublish
232 \|delcommand -buffer Cinstall
233 \|delcommand -buffer Cruntarget'
234
235" vint: -ProhibitAbbreviationOption
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100236let &cpo = s:save_cpo
237unlet s:save_cpo
Gregory Andersfc935942023-09-12 13:23:38 -0500238" vint: +ProhibitAbbreviationOption
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100239
Gregory Andersfc935942023-09-12 13:23:38 -0500240" vim: set et sw=4 sts=4 ts=8: