blob: 7efca5985bb12f4e510f33b0dfe9885568a53f6f [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>
4" Maintainer: Kevin Ballard <kevin@sb.org>
5" Last Change: June 08, 2016
6" For bugs, patches and license go to https://github.com/rust-lang/rust.vim
7
8if exists("b:did_ftplugin")
9 finish
10endif
11let b:did_ftplugin = 1
12
13let s:save_cpo = &cpo
14set cpo&vim
15
16augroup rust.vim
17autocmd!
18
19" Variables {{{1
20
21" The rust source code at present seems to typically omit a leader on /*!
22" comments, so we'll use that as our default, but make it easy to switch.
23" This does not affect indentation at all (I tested it with and without
24" leader), merely whether a leader is inserted by default or not.
25if exists("g:rust_bang_comment_leader") && g:rust_bang_comment_leader != 0
26 " Why is the `,s0:/*,mb:\ ,ex:*/` there, you ask? I don't understand why,
27 " but without it, */ gets indented one space even if there were no
28 " leaders. I'm fairly sure that's a Vim bug.
29 setlocal comments=s1:/*,mb:*,ex:*/,s0:/*,mb:\ ,ex:*/,:///,://!,://
30else
31 setlocal comments=s0:/*!,m:\ ,ex:*/,s1:/*,mb:*,ex:*/,:///,://!,://
32endif
33setlocal commentstring=//%s
34setlocal formatoptions-=t formatoptions+=croqnl
35" j was only added in 7.3.541, so stop complaints about its nonexistence
36silent! setlocal formatoptions+=j
37
38" smartindent will be overridden by indentexpr if filetype indent is on, but
39" otherwise it's better than nothing.
40setlocal smartindent nocindent
41
42if !exists("g:rust_recommended_style") || g:rust_recommended_style != 0
43 setlocal tabstop=4 shiftwidth=4 softtabstop=4 expandtab
44 setlocal textwidth=99
45endif
46
47" This includeexpr isn't perfect, but it's a good start
48setlocal includeexpr=substitute(v:fname,'::','/','g')
49
50setlocal suffixesadd=.rs
51
52if exists("g:ftplugin_rust_source_path")
53 let &l:path=g:ftplugin_rust_source_path . ',' . &l:path
54endif
55
56if exists("g:loaded_delimitMate")
57 if exists("b:delimitMate_excluded_regions")
58 let b:rust_original_delimitMate_excluded_regions = b:delimitMate_excluded_regions
59 endif
60
61 let s:delimitMate_extra_excluded_regions = ',rustLifetimeCandidate,rustGenericLifetimeCandidate'
62
63 " For this buffer, when delimitMate issues the `User delimitMate_map`
64 " event in the autocommand system, add the above-defined extra excluded
65 " regions to delimitMate's state, if they have not already been added.
66 autocmd User <buffer>
67 \ if expand('<afile>') ==# 'delimitMate_map' && match(
68 \ delimitMate#Get("excluded_regions"),
69 \ s:delimitMate_extra_excluded_regions) == -1
70 \| let b:delimitMate_excluded_regions =
71 \ delimitMate#Get("excluded_regions")
72 \ . s:delimitMate_extra_excluded_regions
73 \|endif
74
75 " For this buffer, when delimitMate issues the `User delimitMate_unmap`
76 " event in the autocommand system, delete the above-defined extra excluded
77 " regions from delimitMate's state (the deletion being idempotent and
78 " having no effect if the extra excluded regions are not present in the
79 " targeted part of delimitMate's state).
80 autocmd User <buffer>
81 \ if expand('<afile>') ==# 'delimitMate_unmap'
82 \| let b:delimitMate_excluded_regions = substitute(
83 \ delimitMate#Get("excluded_regions"),
84 \ '\C\V' . s:delimitMate_extra_excluded_regions,
85 \ '', 'g')
86 \|endif
87endif
88
89if has("folding") && exists('g:rust_fold') && g:rust_fold != 0
90 let b:rust_set_foldmethod=1
91 setlocal foldmethod=syntax
92 if g:rust_fold == 2
93 setlocal foldlevel<
94 else
95 setlocal foldlevel=99
96 endif
97endif
98
99if has('conceal') && exists('g:rust_conceal') && g:rust_conceal != 0
100 let b:rust_set_conceallevel=1
101 setlocal conceallevel=2
102endif
103
104" Motion Commands {{{1
105
106" Bind motion commands to support hanging indents
107nnoremap <silent> <buffer> [[ :call rust#Jump('n', 'Back')<CR>
108nnoremap <silent> <buffer> ]] :call rust#Jump('n', 'Forward')<CR>
109xnoremap <silent> <buffer> [[ :call rust#Jump('v', 'Back')<CR>
110xnoremap <silent> <buffer> ]] :call rust#Jump('v', 'Forward')<CR>
111onoremap <silent> <buffer> [[ :call rust#Jump('o', 'Back')<CR>
112onoremap <silent> <buffer> ]] :call rust#Jump('o', 'Forward')<CR>
113
114" Commands {{{1
115
116" See |:RustRun| for docs
117command! -nargs=* -complete=file -bang -buffer RustRun call rust#Run(<bang>0, <q-args>)
118
119" See |:RustExpand| for docs
120command! -nargs=* -complete=customlist,rust#CompleteExpand -bang -buffer RustExpand call rust#Expand(<bang>0, <q-args>)
121
122" See |:RustEmitIr| for docs
123command! -nargs=* -buffer RustEmitIr call rust#Emit("llvm-ir", <q-args>)
124
125" See |:RustEmitAsm| for docs
126command! -nargs=* -buffer RustEmitAsm call rust#Emit("asm", <q-args>)
127
128" See |:RustPlay| for docs
129command! -range=% RustPlay :call rust#Play(<count>, <line1>, <line2>, <f-args>)
130
131" See |:RustFmt| for docs
132command! -buffer RustFmt call rustfmt#Format()
133
134" See |:RustFmtRange| for docs
135command! -range -buffer RustFmtRange call rustfmt#FormatRange(<line1>, <line2>)
136
137" Mappings {{{1
138
139" Bind ⌘R in MacVim to :RustRun
140nnoremap <silent> <buffer> <D-r> :RustRun<CR>
141" Bind ⌘⇧R in MacVim to :RustRun! pre-filled with the last args
142nnoremap <buffer> <D-R> :RustRun! <C-r>=join(b:rust_last_rustc_args)<CR><C-\>erust#AppendCmdLine(' -- ' . join(b:rust_last_args))<CR>
143
144if !exists("b:rust_last_rustc_args") || !exists("b:rust_last_args")
145 let b:rust_last_rustc_args = []
146 let b:rust_last_args = []
147endif
148
149" Cleanup {{{1
150
151let b:undo_ftplugin = "
152 \ setlocal formatoptions< comments< commentstring< includeexpr< suffixesadd<
153 \|setlocal tabstop< shiftwidth< softtabstop< expandtab< textwidth<
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 RustRun
170 \|delcommand RustExpand
171 \|delcommand RustEmitIr
172 \|delcommand RustEmitAsm
173 \|delcommand RustPlay
174 \|nunmap <buffer> <D-r>
175 \|nunmap <buffer> <D-R>
176 \|nunmap <buffer> [[
177 \|nunmap <buffer> ]]
178 \|xunmap <buffer> [[
179 \|xunmap <buffer> ]]
180 \|ounmap <buffer> [[
181 \|ounmap <buffer> ]]
182 \|set matchpairs-=<:>
183 \"
184
185" }}}1
186
187" Code formatting on save
188if get(g:, "rustfmt_autosave", 0)
189 autocmd BufWritePre *.rs silent! call rustfmt#Format()
190endif
191
192augroup END
193
194let &cpo = s:save_cpo
195unlet s:save_cpo
196
197" vim: set noet sw=8 ts=8: