rust.vim 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. " Vim indent file
  2. " Language: Rust
  3. " Author: Chris Morgan <me@chrismorgan.info>
  4. " Last Change: 2023-09-11
  5. " 2024 Jul 04 by Vim Project: use shiftwidth() instead of hard-coding shifted values (#15138)
  6. " For bugs, patches and license go to https://github.com/rust-lang/rust.vim
  7. " Note: upstream seems umaintained: https://github.com/rust-lang/rust.vim/issues/502
  8. " Only load this indent file when no other was loaded.
  9. if exists("b:did_indent")
  10. finish
  11. endif
  12. let b:did_indent = 1
  13. setlocal cindent
  14. setlocal cinoptions=L0,(s,Ws,J1,j1,m1
  15. setlocal cinkeys=0{,0},!^F,o,O,0[,0],0(,0)
  16. " Don't think cinwords will actually do anything at all... never mind
  17. setlocal cinwords=for,if,else,while,loop,impl,mod,unsafe,trait,struct,enum,fn,extern,macro
  18. " Some preliminary settings
  19. setlocal nolisp " Make sure lisp indenting doesn't supersede us
  20. setlocal autoindent " indentexpr isn't much help otherwise
  21. " Also do indentkeys, otherwise # gets shoved to column 0 :-/
  22. setlocal indentkeys=0{,0},!^F,o,O,0[,0],0(,0)
  23. setlocal indentexpr=GetRustIndent(v:lnum)
  24. let b:undo_indent = "setlocal cindent< cinoptions< cinkeys< cinwords< lisp< autoindent< indentkeys< indentexpr<"
  25. " Only define the function once.
  26. if exists("*GetRustIndent")
  27. finish
  28. endif
  29. " vint: -ProhibitAbbreviationOption
  30. let s:save_cpo = &cpo
  31. set cpo&vim
  32. " vint: +ProhibitAbbreviationOption
  33. " Come here when loading the script the first time.
  34. function! s:get_line_trimmed(lnum)
  35. " Get the line and remove a trailing comment.
  36. " Use syntax highlighting attributes when possible.
  37. " NOTE: this is not accurate; /* */ or a line continuation could trick it
  38. let line = getline(a:lnum)
  39. let line_len = strlen(line)
  40. if has('syntax_items')
  41. " If the last character in the line is a comment, do a binary search for
  42. " the start of the comment. synID() is slow, a linear search would take
  43. " too long on a long line.
  44. if synIDattr(synID(a:lnum, line_len, 1), "name") =~? 'Comment\|Todo'
  45. let min = 1
  46. let max = line_len
  47. while min < max
  48. let col = (min + max) / 2
  49. if synIDattr(synID(a:lnum, col, 1), "name") =~? 'Comment\|Todo'
  50. let max = col
  51. else
  52. let min = col + 1
  53. endif
  54. endwhile
  55. let line = strpart(line, 0, min - 1)
  56. endif
  57. return substitute(line, "\s*$", "", "")
  58. else
  59. " Sorry, this is not complete, nor fully correct (e.g. string "//").
  60. " Such is life.
  61. return substitute(line, "\s*//.*$", "", "")
  62. endif
  63. endfunction
  64. function! s:is_string_comment(lnum, col)
  65. if has('syntax_items')
  66. for id in synstack(a:lnum, a:col)
  67. let synname = synIDattr(id, "name")
  68. if synname ==# "rustString" || synname =~# "^rustComment"
  69. return 1
  70. endif
  71. endfor
  72. else
  73. " without syntax, let's not even try
  74. return 0
  75. endif
  76. endfunction
  77. if exists('*shiftwidth')
  78. function! s:shiftwidth()
  79. return shiftwidth()
  80. endfunc
  81. else
  82. function! s:shiftwidth()
  83. return &shiftwidth
  84. endfunc
  85. endif
  86. function GetRustIndent(lnum)
  87. " Starting assumption: cindent (called at the end) will do it right
  88. " normally. We just want to fix up a few cases.
  89. let line = getline(a:lnum)
  90. if has('syntax_items')
  91. let synname = synIDattr(synID(a:lnum, 1, 1), "name")
  92. if synname ==# "rustString"
  93. " If the start of the line is in a string, don't change the indent
  94. return -1
  95. elseif synname =~? '\(Comment\|Todo\)'
  96. \ && line !~# '^\s*/\*' " not /* opening line
  97. if synname =~? "CommentML" " multi-line
  98. if line !~# '^\s*\*' && getline(a:lnum - 1) =~# '^\s*/\*'
  99. " This is (hopefully) the line after a /*, and it has no
  100. " leader, so the correct indentation is that of the
  101. " previous line.
  102. return GetRustIndent(a:lnum - 1)
  103. endif
  104. endif
  105. " If it's in a comment, let cindent take care of it now. This is
  106. " for cases like "/*" where the next line should start " * ", not
  107. " "* " as the code below would otherwise cause for module scope
  108. " Fun fact: " /*\n*\n*/" takes two calls to get right!
  109. return cindent(a:lnum)
  110. endif
  111. endif
  112. " cindent gets second and subsequent match patterns/struct members wrong,
  113. " as it treats the comma as indicating an unfinished statement::
  114. "
  115. " match a {
  116. " b => c,
  117. " d => e,
  118. " f => g,
  119. " };
  120. " Search backwards for the previous non-empty line.
  121. let prevlinenum = prevnonblank(a:lnum - 1)
  122. let prevline = s:get_line_trimmed(prevlinenum)
  123. while prevlinenum > 1 && prevline !~# '[^[:blank:]]'
  124. let prevlinenum = prevnonblank(prevlinenum - 1)
  125. let prevline = s:get_line_trimmed(prevlinenum)
  126. endwhile
  127. " A standalone '{', '}', or 'where'
  128. let l:standalone_open = line =~# '\V\^\s\*{\s\*\$'
  129. let l:standalone_close = line =~# '\V\^\s\*}\s\*\$'
  130. let l:standalone_where = line =~# '\V\^\s\*where\s\*\$'
  131. if l:standalone_open || l:standalone_close || l:standalone_where
  132. " ToDo: we can search for more items than 'fn' and 'if'.
  133. let [l:found_line, l:col, l:submatch] =
  134. \ searchpos('\<\(fn\)\|\(if\)\>', 'bnWp')
  135. if l:found_line !=# 0
  136. " Now we count the number of '{' and '}' in between the match
  137. " locations and the current line (there is probably a better
  138. " way to compute this).
  139. let l:i = l:found_line
  140. let l:search_line = strpart(getline(l:i), l:col - 1)
  141. let l:opens = 0
  142. let l:closes = 0
  143. while l:i < a:lnum
  144. let l:search_line2 = substitute(l:search_line, '\V{', '', 'g')
  145. let l:opens += strlen(l:search_line) - strlen(l:search_line2)
  146. let l:search_line3 = substitute(l:search_line2, '\V}', '', 'g')
  147. let l:closes += strlen(l:search_line2) - strlen(l:search_line3)
  148. let l:i += 1
  149. let l:search_line = getline(l:i)
  150. endwhile
  151. if l:standalone_open || l:standalone_where
  152. if l:opens ==# l:closes
  153. return indent(l:found_line)
  154. endif
  155. else
  156. " Expect to find just one more close than an open
  157. if l:opens ==# l:closes + 1
  158. return indent(l:found_line)
  159. endif
  160. endif
  161. endif
  162. endif
  163. " A standalone 'where' adds a shift.
  164. let l:standalone_prevline_where = prevline =~# '\V\^\s\*where\s\*\$'
  165. if l:standalone_prevline_where
  166. return indent(prevlinenum) + shiftwidth()
  167. endif
  168. " Handle where clauses nicely: subsequent values should line up nicely.
  169. if prevline[len(prevline) - 1] ==# ","
  170. \ && prevline =~# '^\s*where\s'
  171. return indent(prevlinenum) + 6
  172. endif
  173. let l:last_prevline_character = prevline[len(prevline) - 1]
  174. " A line that ends with '.<expr>;' is probably an end of a long list
  175. " of method operations.
  176. if prevline =~# '\V\^\s\*.' && l:last_prevline_character ==# ';'
  177. call cursor(a:lnum - 1, 1)
  178. let l:scope_start = searchpair('{\|(', '', '}\|)', 'nbW',
  179. \ 's:is_string_comment(line("."), col("."))')
  180. if l:scope_start != 0 && l:scope_start < a:lnum
  181. return indent(l:scope_start) + shiftwidth()
  182. endif
  183. endif
  184. if l:last_prevline_character ==# ","
  185. \ && s:get_line_trimmed(a:lnum) !~# '^\s*[\[\]{})]'
  186. \ && prevline !~# '^\s*fn\s'
  187. \ && prevline !~# '([^()]\+,$'
  188. \ && s:get_line_trimmed(a:lnum) !~# '^\s*\S\+\s*=>'
  189. " Oh ho! The previous line ended in a comma! I bet cindent will try to
  190. " take this too far... For now, let's normally use the previous line's
  191. " indent.
  192. " One case where this doesn't work out is where *this* line contains
  193. " square or curly brackets; then we normally *do* want to be indenting
  194. " further.
  195. "
  196. " Another case where we don't want to is one like a function
  197. " definition with arguments spread over multiple lines:
  198. "
  199. " fn foo(baz: Baz,
  200. " baz: Baz) // <-- cindent gets this right by itself
  201. "
  202. " Another case is similar to the previous, except calling a function
  203. " instead of defining it, or any conditional expression that leaves
  204. " an open paren:
  205. "
  206. " foo(baz,
  207. " baz);
  208. "
  209. " if baz && (foo ||
  210. " bar) {
  211. "
  212. " Another case is when the current line is a new match arm.
  213. "
  214. " There are probably other cases where we don't want to do this as
  215. " well. Add them as needed.
  216. return indent(prevlinenum)
  217. endif
  218. if !has("patch-7.4.355")
  219. " cindent before 7.4.355 doesn't do the module scope well at all; e.g.::
  220. "
  221. " static FOO : &'static [bool] = [
  222. " true,
  223. " false,
  224. " false,
  225. " true,
  226. " ];
  227. "
  228. " uh oh, next statement is indented further!
  229. " Note that this does *not* apply the line continuation pattern properly;
  230. " that's too hard to do correctly for my liking at present, so I'll just
  231. " start with these two main cases (square brackets and not returning to
  232. " column zero)
  233. call cursor(a:lnum, 1)
  234. if searchpair('{\|(', '', '}\|)', 'nbW',
  235. \ 's:is_string_comment(line("."), col("."))') == 0
  236. if searchpair('\[', '', '\]', 'nbW',
  237. \ 's:is_string_comment(line("."), col("."))') == 0
  238. " Global scope, should be zero
  239. return 0
  240. else
  241. " At the module scope, inside square brackets only
  242. "if getline(a:lnum)[0] == ']' || search('\[', '', '\]', 'nW') == a:lnum
  243. if line =~# "^\\s*]"
  244. " It's the closing line, dedent it
  245. return 0
  246. else
  247. return shiftwidth()
  248. endif
  249. endif
  250. endif
  251. endif
  252. " Fall back on cindent, which does it mostly right
  253. return cindent(a:lnum)
  254. endfunction
  255. " vint: -ProhibitAbbreviationOption
  256. let &cpo = s:save_cpo
  257. unlet s:save_cpo
  258. " vint: +ProhibitAbbreviationOption
  259. " vim: set et sw=4 sts=4 ts=8: