robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqMerge pull request #40472 from zeertzjq/vim-1f337327945732ed7

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