Simple image host.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

rust.vim 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. " Vim indent file
  2. " Language: Rust
  3. " Author: Chris Morgan <me@chrismorgan.info>
  4. " Last Change: 2014 Sep 13
  5. " Only load this indent file when no other was loaded.
  6. if exists("b:did_indent")
  7. finish
  8. endif
  9. let b:did_indent = 1
  10. setlocal cindent
  11. setlocal cinoptions=L0,(0,Ws,J1,j1
  12. setlocal cinkeys=0{,0},!^F,o,O,0[,0]
  13. " Don't think cinwords will actually do anything at all... never mind
  14. setlocal cinwords=for,if,else,while,loop,impl,mod,unsafe,trait,struct,enum,fn,extern
  15. " Some preliminary settings
  16. setlocal nolisp " Make sure lisp indenting doesn't supersede us
  17. setlocal autoindent " indentexpr isn't much help otherwise
  18. " Also do indentkeys, otherwise # gets shoved to column 0 :-/
  19. setlocal indentkeys=0{,0},!^F,o,O,0[,0]
  20. setlocal indentexpr=GetRustIndent(v:lnum)
  21. " Only define the function once.
  22. if exists("*GetRustIndent")
  23. finish
  24. endif
  25. " Come here when loading the script the first time.
  26. function! s:get_line_trimmed(lnum)
  27. " Get the line and remove a trailing comment.
  28. " Use syntax highlighting attributes when possible.
  29. " NOTE: this is not accurate; /* */ or a line continuation could trick it
  30. let line = getline(a:lnum)
  31. let line_len = strlen(line)
  32. if has('syntax_items')
  33. " If the last character in the line is a comment, do a binary search for
  34. " the start of the comment. synID() is slow, a linear search would take
  35. " too long on a long line.
  36. if synIDattr(synID(a:lnum, line_len, 1), "name") =~ 'Comment\|Todo'
  37. let min = 1
  38. let max = line_len
  39. while min < max
  40. let col = (min + max) / 2
  41. if synIDattr(synID(a:lnum, col, 1), "name") =~ 'Comment\|Todo'
  42. let max = col
  43. else
  44. let min = col + 1
  45. endif
  46. endwhile
  47. let line = strpart(line, 0, min - 1)
  48. endif
  49. return substitute(line, "\s*$", "", "")
  50. else
  51. " Sorry, this is not complete, nor fully correct (e.g. string "//").
  52. " Such is life.
  53. return substitute(line, "\s*//.*$", "", "")
  54. endif
  55. endfunction
  56. function! s:is_string_comment(lnum, col)
  57. if has('syntax_items')
  58. for id in synstack(a:lnum, a:col)
  59. let synname = synIDattr(id, "name")
  60. if synname == "rustString" || synname =~ "^rustComment"
  61. return 1
  62. endif
  63. endfor
  64. else
  65. " without syntax, let's not even try
  66. return 0
  67. endif
  68. endfunction
  69. function GetRustIndent(lnum)
  70. " Starting assumption: cindent (called at the end) will do it right
  71. " normally. We just want to fix up a few cases.
  72. let line = getline(a:lnum)
  73. if has('syntax_items')
  74. let synname = synIDattr(synID(a:lnum, 1, 1), "name")
  75. if synname == "rustString"
  76. " If the start of the line is in a string, don't change the indent
  77. return -1
  78. elseif synname =~ '\(Comment\|Todo\)'
  79. \ && line !~ '^\s*/\*' " not /* opening line
  80. if synname =~ "CommentML" " multi-line
  81. if line !~ '^\s*\*' && getline(a:lnum - 1) =~ '^\s*/\*'
  82. " This is (hopefully) the line after a /*, and it has no
  83. " leader, so the correct indentation is that of the
  84. " previous line.
  85. return GetRustIndent(a:lnum - 1)
  86. endif
  87. endif
  88. " If it's in a comment, let cindent take care of it now. This is
  89. " for cases like "/*" where the next line should start " * ", not
  90. " "* " as the code below would otherwise cause for module scope
  91. " Fun fact: " /*\n*\n*/" takes two calls to get right!
  92. return cindent(a:lnum)
  93. endif
  94. endif
  95. " cindent gets second and subsequent match patterns/struct members wrong,
  96. " as it treats the comma as indicating an unfinished statement::
  97. "
  98. " match a {
  99. " b => c,
  100. " d => e,
  101. " f => g,
  102. " };
  103. " Search backwards for the previous non-empty line.
  104. let prevlinenum = prevnonblank(a:lnum - 1)
  105. let prevline = s:get_line_trimmed(prevlinenum)
  106. while prevlinenum > 1 && prevline !~ '[^[:blank:]]'
  107. let prevlinenum = prevnonblank(prevlinenum - 1)
  108. let prevline = s:get_line_trimmed(prevlinenum)
  109. endwhile
  110. if prevline[len(prevline) - 1] == ","
  111. \ && s:get_line_trimmed(a:lnum) !~ '^\s*[\[\]{}]'
  112. \ && prevline !~ '^\s*fn\s'
  113. \ && prevline !~ '([^()]\+,$'
  114. " Oh ho! The previous line ended in a comma! I bet cindent will try to
  115. " take this too far... For now, let's normally use the previous line's
  116. " indent.
  117. " One case where this doesn't work out is where *this* line contains
  118. " square or curly brackets; then we normally *do* want to be indenting
  119. " further.
  120. "
  121. " Another case where we don't want to is one like a function
  122. " definition with arguments spread over multiple lines:
  123. "
  124. " fn foo(baz: Baz,
  125. " baz: Baz) // <-- cindent gets this right by itself
  126. "
  127. " Another case is similar to the previous, except calling a function
  128. " instead of defining it, or any conditional expression that leaves
  129. " an open paren:
  130. "
  131. " foo(baz,
  132. " baz);
  133. "
  134. " if baz && (foo ||
  135. " bar) {
  136. "
  137. " There are probably other cases where we don't want to do this as
  138. " well. Add them as needed.
  139. return indent(prevlinenum)
  140. endif
  141. if !has("patch-7.4.355")
  142. " cindent before 7.4.355 doesn't do the module scope well at all; e.g.::
  143. "
  144. " static FOO : &'static [bool] = [
  145. " true,
  146. " false,
  147. " false,
  148. " true,
  149. " ];
  150. "
  151. " uh oh, next statement is indented further!
  152. " Note that this does *not* apply the line continuation pattern properly;
  153. " that's too hard to do correctly for my liking at present, so I'll just
  154. " start with these two main cases (square brackets and not returning to
  155. " column zero)
  156. call cursor(a:lnum, 1)
  157. if searchpair('{\|(', '', '}\|)', 'nbW',
  158. \ 's:is_string_comment(line("."), col("."))') == 0
  159. if searchpair('\[', '', '\]', 'nbW',
  160. \ 's:is_string_comment(line("."), col("."))') == 0
  161. " Global scope, should be zero
  162. return 0
  163. else
  164. " At the module scope, inside square brackets only
  165. "if getline(a:lnum)[0] == ']' || search('\[', '', '\]', 'nW') == a:lnum
  166. if line =~ "^\\s*]"
  167. " It's the closing line, dedent it
  168. return 0
  169. else
  170. return &shiftwidth
  171. endif
  172. endif
  173. endif
  174. endif
  175. " Fall back on cindent, which does it mostly right
  176. return cindent(a:lnum)
  177. endfunction