blob: c2bcdd34bc13dde48a065814db41273e901c5aaa [file] [log] [blame]
Riley Bruins0a083062024-06-03 20:40:45 +02001" 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 Andersfc935942023-09-12 13:23:38 -05006" For bugs, patches and license go to https://github.com/rust-lang/rust.vim
Bram Moolenaar3c2881d2017-03-21 19:18:29 +01007
8if exists("b:did_ftplugin")
Gregory Andersfc935942023-09-12 13:23:38 -05009 finish
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010010endif
11let b:did_ftplugin = 1
12
Gregory Andersfc935942023-09-12 13:23:38 -050013" vint: -ProhibitAbbreviationOption
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010014let s:save_cpo = &cpo
15set cpo&vim
Gregory Andersfc935942023-09-12 13:23:38 -050016" vint: +ProhibitAbbreviationOption
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010017
Gregory Andersfc935942023-09-12 13:23:38 -050018if get(b:, 'current_compiler', '') ==# ''
19 if strlen(findfile('Cargo.toml', '.;')) > 0
20 compiler cargo
21 else
22 compiler rustc
23 endif
24endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010025
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 Andersfc935942023-09-12 13:23:38 -050032if 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 Moolenaar3c2881d2017-03-21 19:18:29 +010037else
Gregory Andersfc935942023-09-12 13:23:38 -050038 setlocal comments=s0:/*!,ex:*/,s1:/*,mb:*,ex:*/,:///,://!,://
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010039endif
Riley Bruins0a083062024-06-03 20:40:45 +020040setlocal commentstring=//\ %s
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010041setlocal formatoptions-=t formatoptions+=croqnl
42" j was only added in 7.3.541, so stop complaints about its nonexistence
43silent! setlocal formatoptions+=j
44
45" smartindent will be overridden by indentexpr if filetype indent is on, but
46" otherwise it's better than nothing.
47setlocal smartindent nocindent
48
Gregory Andersfc935942023-09-12 13:23:38 -050049if 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 Moolenaar3c2881d2017-03-21 19:18:29 +010053endif
54
Gregory Andersfc935942023-09-12 13:23:38 -050055setlocal include=\\v^\\s*(pub\\s+)?use\\s+\\zs(\\f\|:)+
56setlocal includeexpr=rust#IncludeExpr(v:fname)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010057
58setlocal suffixesadd=.rs
59
60if exists("g:ftplugin_rust_source_path")
61 let &l:path=g:ftplugin_rust_source_path . ',' . &l:path
62endif
63
64if exists("g:loaded_delimitMate")
Gregory Andersfc935942023-09-12 13:23:38 -050065 if exists("b:delimitMate_excluded_regions")
66 let b:rust_original_delimitMate_excluded_regions = b:delimitMate_excluded_regions
67 endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010068
Gregory Andersfc935942023-09-12 13:23:38 -050069 augroup rust.vim.DelimitMate
70 autocmd!
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010071
Gregory Andersfc935942023-09-12 13:23:38 -050072 autocmd User delimitMate_map :call rust#delimitmate#onMap()
73 autocmd User delimitMate_unmap :call rust#delimitmate#onUnmap()
74 augroup END
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010075endif
76
Gregory Andersfc935942023-09-12 13:23:38 -050077" Integration with auto-pairs (https://github.com/jiangmiao/auto-pairs)
78if exists("g:AutoPairsLoaded") && !get(g:, 'rust_keep_autopairs_default', 0)
79 let b:AutoPairs = {'(':')', '[':']', '{':'}','"':'"', '`':'`'}
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010080endif
81
Gregory Andersfc935942023-09-12 13:23:38 -050082if 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
90endif
91
92if has('conceal') && get(g:, 'rust_conceal', 0)
93 let b:rust_set_conceallevel=1
94 setlocal conceallevel=2
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010095endif
96
97" Motion Commands {{{1
MyyPoef21bca2024-03-18 20:38:09 +020098if !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>
106endif
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100107
108" Commands {{{1
109
110" See |:RustRun| for docs
111command! -nargs=* -complete=file -bang -buffer RustRun call rust#Run(<bang>0, <q-args>)
112
113" See |:RustExpand| for docs
114command! -nargs=* -complete=customlist,rust#CompleteExpand -bang -buffer RustExpand call rust#Expand(<bang>0, <q-args>)
115
116" See |:RustEmitIr| for docs
117command! -nargs=* -buffer RustEmitIr call rust#Emit("llvm-ir", <q-args>)
118
119" See |:RustEmitAsm| for docs
120command! -nargs=* -buffer RustEmitAsm call rust#Emit("asm", <q-args>)
121
122" See |:RustPlay| for docs
Gregory Andersfc935942023-09-12 13:23:38 -0500123command! -range=% -buffer RustPlay :call rust#Play(<count>, <line1>, <line2>, <f-args>)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100124
125" See |:RustFmt| for docs
Gregory Andersfc935942023-09-12 13:23:38 -0500126command! -bar -buffer RustFmt call rustfmt#Format()
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100127
128" See |:RustFmtRange| for docs
129command! -range -buffer RustFmtRange call rustfmt#FormatRange(<line1>, <line2>)
130
Gregory Andersfc935942023-09-12 13:23:38 -0500131" See |:RustInfo| for docs
132command! -bar -buffer RustInfo call rust#debugging#Info()
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100133
Gregory Andersfc935942023-09-12 13:23:38 -0500134" See |:RustInfoToClipboard| for docs
135command! -bar -buffer RustInfoToClipboard call rust#debugging#InfoToClipboard()
136
137" See |:RustInfoToFile| for docs
138command! -bar -nargs=1 -buffer RustInfoToFile call rust#debugging#InfoToFile(<f-args>)
139
140" See |:RustTest| for docs
141command! -buffer -nargs=* -count -bang RustTest call rust#Test(<q-mods>, <count>, <bang>0, <q-args>)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100142
143if !exists("b:rust_last_rustc_args") || !exists("b:rust_last_args")
Gregory Andersfc935942023-09-12 13:23:38 -0500144 let b:rust_last_rustc_args = []
145 let b:rust_last_args = []
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100146endif
147
148" Cleanup {{{1
149
150let b:undo_ftplugin = "
Gregory Andersfc935942023-09-12 13:23:38 -0500151 \ setlocal formatoptions< comments< commentstring< include< includeexpr< suffixesadd<
152 \|if exists('b:rust_set_style')
153 \|setlocal tabstop< shiftwidth< softtabstop< expandtab< textwidth<
154 \|endif
155 \|if exists('b:rust_original_delimitMate_excluded_regions')
156 \|let b:delimitMate_excluded_regions = b:rust_original_delimitMate_excluded_regions
157 \|unlet b:rust_original_delimitMate_excluded_regions
158 \|else
159 \|unlet! b:delimitMate_excluded_regions
160 \|endif
161 \|if exists('b:rust_set_foldmethod')
162 \|setlocal foldmethod< foldlevel<
163 \|unlet b:rust_set_foldmethod
164 \|endif
165 \|if exists('b:rust_set_conceallevel')
166 \|setlocal conceallevel<
167 \|unlet b:rust_set_conceallevel
168 \|endif
169 \|unlet! b:rust_last_rustc_args b:rust_last_args
170 \|delcommand -buffer RustRun
171 \|delcommand -buffer RustExpand
172 \|delcommand -buffer RustEmitIr
173 \|delcommand -buffer RustEmitAsm
174 \|delcommand -buffer RustPlay
175 \|delcommand -buffer RustFmt
176 \|delcommand -buffer RustFmtRange
177 \|delcommand -buffer RustInfo
178 \|delcommand -buffer RustInfoToClipboard
179 \|delcommand -buffer RustInfoToFile
180 \|delcommand -buffer RustTest
MyyPoef21bca2024-03-18 20:38:09 +0200181 \|silent! nunmap <buffer> [[
182 \|silent! nunmap <buffer> ]]
183 \|silent! xunmap <buffer> [[
184 \|silent! xunmap <buffer> ]]
185 \|silent! ounmap <buffer> [[
186 \|silent! ounmap <buffer> ]]
Gregory Andersfc935942023-09-12 13:23:38 -0500187 \|setlocal matchpairs-=<:>
188 \|unlet b:match_skip
189 \"
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100190
191" }}}1
192
193" Code formatting on save
Gregory Andersfc935942023-09-12 13:23:38 -0500194augroup rust.vim.PreWrite
195 autocmd!
196 autocmd BufWritePre *.rs silent! call rustfmt#PreWrite()
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100197augroup END
198
Gregory Andersfc935942023-09-12 13:23:38 -0500199setlocal matchpairs+=<:>
200" For matchit.vim (rustArrow stops `Fn() -> X` messing things up)
201let b:match_skip = 's:comment\|string\|rustCharacter\|rustArrow'
202
203command! -buffer -nargs=+ Cargo call cargo#cmd(<q-args>)
204command! -buffer -nargs=* Cbuild call cargo#build(<q-args>)
205command! -buffer -nargs=* Ccheck call cargo#check(<q-args>)
206command! -buffer -nargs=* Cclean call cargo#clean(<q-args>)
207command! -buffer -nargs=* Cdoc call cargo#doc(<q-args>)
208command! -buffer -nargs=+ Cnew call cargo#new(<q-args>)
209command! -buffer -nargs=* Cinit call cargo#init(<q-args>)
210command! -buffer -nargs=* Crun call cargo#run(<q-args>)
211command! -buffer -nargs=* Ctest call cargo#test(<q-args>)
212command! -buffer -nargs=* Cbench call cargo#bench(<q-args>)
213command! -buffer -nargs=* Cupdate call cargo#update(<q-args>)
214command! -buffer -nargs=* Csearch call cargo#search(<q-args>)
215command! -buffer -nargs=* Cpublish call cargo#publish(<q-args>)
216command! -buffer -nargs=* Cinstall call cargo#install(<q-args>)
217command! -buffer -nargs=* Cruntarget call cargo#runtarget(<q-args>)
218
219let b:undo_ftplugin .= '
220 \|delcommand -buffer Cargo
221 \|delcommand -buffer Cbuild
222 \|delcommand -buffer Ccheck
223 \|delcommand -buffer Cclean
224 \|delcommand -buffer Cdoc
225 \|delcommand -buffer Cnew
226 \|delcommand -buffer Cinit
227 \|delcommand -buffer Crun
228 \|delcommand -buffer Ctest
229 \|delcommand -buffer Cbench
230 \|delcommand -buffer Cupdate
231 \|delcommand -buffer Csearch
232 \|delcommand -buffer Cpublish
233 \|delcommand -buffer Cinstall
234 \|delcommand -buffer Cruntarget'
235
236" vint: -ProhibitAbbreviationOption
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100237let &cpo = s:save_cpo
238unlet s:save_cpo
Gregory Andersfc935942023-09-12 13:23:38 -0500239" vint: +ProhibitAbbreviationOption
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100240
Gregory Andersfc935942023-09-12 13:23:38 -0500241" vim: set et sw=4 sts=4 ts=8: