]> git.lizzy.rs Git - rust.git/blob - src/etc/vim/ftplugin/rust.vim
6e373b2ddc4712359b65722c17a0aa201f158dcc
[rust.git] / src / etc / vim / ftplugin / rust.vim
1 " Language:     Rust
2 " Description:  Vim syntax file for Rust
3 " Maintainer:   Chris Morgan <me@chrismorgan.info>
4 " Maintainer:   Kevin Ballard <kevin@sb.org>
5 " Last Change:  May 27, 2014
6
7 if exists("b:did_ftplugin")
8         finish
9 endif
10 let b:did_ftplugin = 1
11
12 let s:save_cpo = &cpo
13 set cpo&vim
14
15 " Variables {{{1
16
17 " The rust source code at present seems to typically omit a leader on /*!
18 " comments, so we'll use that as our default, but make it easy to switch.
19 " This does not affect indentation at all (I tested it with and without
20 " leader), merely whether a leader is inserted by default or not.
21 if exists("g:rust_bang_comment_leader") && g:rust_bang_comment_leader == 1
22         " Why is the `,s0:/*,mb:\ ,ex:*/` there, you ask? I don't understand why,
23         " but without it, */ gets indented one space even if there were no
24         " leaders. I'm fairly sure that's a Vim bug.
25         setlocal comments=s1:/*,mb:*,ex:*/,s0:/*,mb:\ ,ex:*/,:///,://!,://
26 else
27         setlocal comments=s0:/*!,m:\ ,ex:*/,s1:/*,mb:*,ex:*/,:///,://!,://
28 endif
29 setlocal commentstring=//%s
30 setlocal formatoptions-=t formatoptions+=croqnl
31 " j was only added in 7.3.541, so stop complaints about its nonexistence
32 silent! setlocal formatoptions+=j
33
34 " This includeexpr isn't perfect, but it's a good start
35 setlocal includeexpr=substitute(v:fname,'::','/','g')
36
37 " NOT adding .rc as it's being phased out (0.7)
38 setlocal suffixesadd=.rs
39
40 if exists("g:ftplugin_rust_source_path")
41     let &l:path=g:ftplugin_rust_source_path . ',' . &l:path
42 endif
43
44 if exists("g:loaded_delimitMate")
45         if exists("b:delimitMate_excluded_regions")
46                 let b:rust_original_delimitMate_excluded_regions = b:delimitMate_excluded_regions
47         endif
48         let b:delimitMate_excluded_regions = delimitMate#Get("excluded_regions") . ',rustLifetimeCandidate,rustGenericLifetimeCandidate'
49 endif
50
51 " Motion Commands {{{1
52
53 " Bind motion commands to support hanging indents
54 nnoremap <silent> <buffer> [[ :call rust#Jump('n', 'Back')<CR>
55 nnoremap <silent> <buffer> ]] :call rust#Jump('n', 'Forward')<CR>
56 xnoremap <silent> <buffer> [[ :call rust#Jump('v', 'Back')<CR>
57 xnoremap <silent> <buffer> ]] :call rust#Jump('v', 'Forward')<CR>
58 onoremap <silent> <buffer> [[ :call rust#Jump('o', 'Back')<CR>
59 onoremap <silent> <buffer> ]] :call rust#Jump('o', 'Forward')<CR>
60
61 " Commands {{{1
62
63 " :RustRun will compile and run the current file. If it has unsaved changes,
64 " they will be saved first. If it has no path, it will be written to a
65 " temporary file first. The generated binary is always placed in a temporary
66 " directory, but run from the current directory.
67 "
68 " The arguments passed to :RustRun will be passed to the generated binary.
69 "
70 " If ! is specified, the arguments are given to rustc as well. A -- argument
71 " separates rustc args from the args passed to the binary.
72 "
73 " If g:rustc_path is defined, it is used as the path to rustc. Otherwise it is
74 " assumed that rustc is in $PATH.
75 command! -nargs=* -complete=file -bang -bar -buffer RustRun call rust#Run(<bang>0, [<f-args>])
76
77 " :RustExpand will expand the current file using --pretty.
78 "
79 " Any arguments given to :RustExpand will be passed to rustc. This is largely
80 " so you can pass various --cfg configurations.
81 "
82 " If ! is specified, the first argument will be interpreted as the --pretty
83 " type. Otherwise it will default to 'expanded'.
84 "
85 " If the current file has unsaved changes, it will be saved first. If it's an
86 " unnamed buffer, it will be written to a temporary file.
87 "
88 " If g:rustc_path is defined, it is used as the path to rustc. Otherwise it is
89 " assumed that rustc is in $PATH.
90 command! -nargs=* -complete=customlist,rust#CompleteExpand -bang -bar -buffer RustExpand call rust#Expand(<bang>0, [<f-args>])
91
92 " Mappings {{{1
93
94 " Bind ⌘R in MacVim to :RustRun
95 nnoremap <silent> <buffer> <D-r> :RustRun<CR>
96 " Bind ⌘⇧R in MacVim to :RustRun! pre-filled with the last args
97 nnoremap <buffer> <D-R> :RustRun! <C-r>=join(b:rust_last_rustc_args)<CR><C-\>erust#AppendCmdLine(' -- ' . join(b:rust_last_args))<CR>
98
99 if !exists("b:rust_last_rustc_args") || !exists("b:rust_last_args")
100         let b:rust_last_rustc_args = []
101         let b:rust_last_args = []
102 endif
103
104 " Cleanup {{{1
105
106 let b:undo_ftplugin = "
107                 \setlocal formatoptions< comments< commentstring< includeexpr< suffixesadd<
108                 \|if exists('b:rust_original_delimitMate_excluded_regions')
109                   \|let b:delimitMate_excluded_regions = b:rust_original_delimitMate_excluded_regions
110                   \|unlet b:rust_original_delimitMate_excluded_regions
111                 \|else
112                   \|unlet! b:delimitMate_excluded_regions
113                 \|endif
114                 \|unlet! b:rust_last_rustc_args b:rust_last_args
115                 \|delcommand RustRun
116                 \|delcommand RustExpand
117                 \|nunmap <buffer> <D-r>
118                 \|nunmap <buffer> <D-R>
119                 \|nunmap <buffer> [[
120                 \|nunmap <buffer> ]]
121                 \|xunmap <buffer> [[
122                 \|xunmap <buffer> ]]
123                 \|ounmap <buffer> [[
124                 \|ounmap <buffer> ]]
125                 \"
126
127 " }}}1
128
129 let &cpo = s:save_cpo
130 unlet s:save_cpo
131
132 " vim: set noet sw=4 ts=4: