Skip to content

Commit a7d877c

Browse files
committed
auto merge of #14056 : kballard/rust/vim_indent_fix, r=huonw
cindent handles the following case incorrectly: impl X { b: int, // c: int, } if you try and insert a new line after the `c` declaration. To fix this, fix the get_line_trimmed() function to work properly, and then extend GetRustIndent to keep searching backwards until it finds a non-blank line after trimming. This lets it handle the trailing comma case properly, as if the comment were never there. Fixes #14041.
2 parents 5b4774d + 0381ad4 commit a7d877c

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/etc/vim/indent/rust.vim

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ setlocal cindent
1313
setlocal cinoptions=L0,(0,Ws,JN,j1
1414
setlocal cinkeys=0{,0},!^F,o,O,0[,0]
1515
" Don't think cinwords will actually do anything at all... never mind
16-
setlocal cinwords=do,for,if,else,while,loop,impl,mod,unsafe,trait,struct,enum,fn,extern
16+
setlocal cinwords=for,if,else,while,loop,impl,mod,unsafe,trait,struct,enum,fn,extern
1717

1818
" Some preliminary settings
1919
setlocal nolisp " Make sure lisp indenting doesn't supersede us
@@ -40,12 +40,12 @@ function! s:get_line_trimmed(lnum)
4040
" If the last character in the line is a comment, do a binary search for
4141
" the start of the comment. synID() is slow, a linear search would take
4242
" too long on a long line.
43-
if synIDattr(synID(a:lnum, line_len, 1), "name") =~ "Comment\|Todo"
43+
if synIDattr(synID(a:lnum, line_len, 1), "name") =~ 'Comment\|Todo'
4444
let min = 1
4545
let max = line_len
4646
while min < max
4747
let col = (min + max) / 2
48-
if synIDattr(synID(a:lnum, col, 1), "name") =~ "Comment\|Todo"
48+
if synIDattr(synID(a:lnum, col, 1), "name") =~ 'Comment\|Todo'
4949
let max = col
5050
else
5151
let min = col + 1
@@ -87,10 +87,10 @@ function GetRustIndent(lnum)
8787
if synname == "rustString"
8888
" If the start of the line is in a string, don't change the indent
8989
return -1
90-
elseif synname =~ "\\(Comment\\|Todo\\)"
91-
\ && line !~ "^\\s*/\\*" " not /* opening line
90+
elseif synname =~ '\(Comment\|Todo\)'
91+
\ && line !~ '^\s*/\*' " not /* opening line
9292
if synname =~ "CommentML" " multi-line
93-
if line !~ "^\\s*\\*" && getline(a:lnum - 1) =~ "^\\s*/\\*"
93+
if line !~ '^\s*\*' && getline(a:lnum - 1) =~ '^\s*/\*'
9494
" This is (hopefully) the line after a /*, and it has no
9595
" leader, so the correct indentation is that of the
9696
" previous line.
@@ -115,11 +115,16 @@ function GetRustIndent(lnum)
115115
" };
116116

117117
" Search backwards for the previous non-empty line.
118-
let prevline = s:get_line_trimmed(prevnonblank(a:lnum - 1))
118+
let prevlinenum = prevnonblank(a:lnum - 1)
119+
let prevline = s:get_line_trimmed(prevlinenum)
120+
while prevlinenum > 1 && prevline !~ '[^[:blank:]]'
121+
let prevlinenum = prevnonblank(prevlinenum - 1)
122+
let prevline = s:get_line_trimmed(prevlinenum)
123+
endwhile
119124
if prevline[len(prevline) - 1] == ","
120-
\ && s:get_line_trimmed(a:lnum) !~ "^\\s*[\\[\\]{}]"
121-
\ && prevline !~ "^\\s*fn\\s"
122-
\ && prevline !~ "([^()]\\+,$"
125+
\ && s:get_line_trimmed(a:lnum) !~ '^\s*[\[\]{}]'
126+
\ && prevline !~ '^\s*fn\s'
127+
\ && prevline !~ '([^()]\+,$'
123128
" Oh ho! The previous line ended in a comma! I bet cindent will try to
124129
" take this too far... For now, let's normally use the previous line's
125130
" indent.
@@ -166,7 +171,7 @@ function GetRustIndent(lnum)
166171
" column zero)
167172

168173
call cursor(a:lnum, 1)
169-
if searchpair('{\|(', '', '}\|)', 'nbW'
174+
if searchpair('{\|(', '', '}\|)', 'nbW',
170175
\ 's:is_string_comment(line("."), col("."))') == 0
171176
if searchpair('\[', '', '\]', 'nbW',
172177
\ 's:is_string_comment(line("."), col("."))') == 0

0 commit comments

Comments
 (0)