rust.vim 9.1 KB

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