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.
This commit is contained in:
bors 2014-05-09 23:51:30 -07:00
commit a7d877ce62
1 changed files with 16 additions and 11 deletions

View File

@ -13,7 +13,7 @@ setlocal cindent
setlocal cinoptions=L0,(0,Ws,JN,j1 setlocal cinoptions=L0,(0,Ws,JN,j1
setlocal cinkeys=0{,0},!^F,o,O,0[,0] setlocal cinkeys=0{,0},!^F,o,O,0[,0]
" Don't think cinwords will actually do anything at all... never mind " Don't think cinwords will actually do anything at all... never mind
setlocal cinwords=do,for,if,else,while,loop,impl,mod,unsafe,trait,struct,enum,fn,extern setlocal cinwords=for,if,else,while,loop,impl,mod,unsafe,trait,struct,enum,fn,extern
" Some preliminary settings " Some preliminary settings
setlocal nolisp " Make sure lisp indenting doesn't supersede us setlocal nolisp " Make sure lisp indenting doesn't supersede us
@ -40,12 +40,12 @@ function! s:get_line_trimmed(lnum)
" If the last character in the line is a comment, do a binary search for " If the last character in the line is a comment, do a binary search for
" the start of the comment. synID() is slow, a linear search would take " the start of the comment. synID() is slow, a linear search would take
" too long on a long line. " too long on a long line.
if synIDattr(synID(a:lnum, line_len, 1), "name") =~ "Comment\|Todo" if synIDattr(synID(a:lnum, line_len, 1), "name") =~ 'Comment\|Todo'
let min = 1 let min = 1
let max = line_len let max = line_len
while min < max while min < max
let col = (min + max) / 2 let col = (min + max) / 2
if synIDattr(synID(a:lnum, col, 1), "name") =~ "Comment\|Todo" if synIDattr(synID(a:lnum, col, 1), "name") =~ 'Comment\|Todo'
let max = col let max = col
else else
let min = col + 1 let min = col + 1
@ -87,10 +87,10 @@ function GetRustIndent(lnum)
if synname == "rustString" if synname == "rustString"
" If the start of the line is in a string, don't change the indent " If the start of the line is in a string, don't change the indent
return -1 return -1
elseif synname =~ "\\(Comment\\|Todo\\)" elseif synname =~ '\(Comment\|Todo\)'
\ && line !~ "^\\s*/\\*" " not /* opening line \ && line !~ '^\s*/\*' " not /* opening line
if synname =~ "CommentML" " multi-line if synname =~ "CommentML" " multi-line
if line !~ "^\\s*\\*" && getline(a:lnum - 1) =~ "^\\s*/\\*" if line !~ '^\s*\*' && getline(a:lnum - 1) =~ '^\s*/\*'
" This is (hopefully) the line after a /*, and it has no " This is (hopefully) the line after a /*, and it has no
" leader, so the correct indentation is that of the " leader, so the correct indentation is that of the
" previous line. " previous line.
@ -115,11 +115,16 @@ function GetRustIndent(lnum)
" }; " };
" Search backwards for the previous non-empty line. " Search backwards for the previous non-empty line.
let prevline = s:get_line_trimmed(prevnonblank(a:lnum - 1)) let prevlinenum = prevnonblank(a:lnum - 1)
let prevline = s:get_line_trimmed(prevlinenum)
while prevlinenum > 1 && prevline !~ '[^[:blank:]]'
let prevlinenum = prevnonblank(prevlinenum - 1)
let prevline = s:get_line_trimmed(prevlinenum)
endwhile
if prevline[len(prevline) - 1] == "," if prevline[len(prevline) - 1] == ","
\ && s:get_line_trimmed(a:lnum) !~ "^\\s*[\\[\\]{}]" \ && s:get_line_trimmed(a:lnum) !~ '^\s*[\[\]{}]'
\ && prevline !~ "^\\s*fn\\s" \ && prevline !~ '^\s*fn\s'
\ && prevline !~ "([^()]\\+,$" \ && prevline !~ '([^()]\+,$'
" Oh ho! The previous line ended in a comma! I bet cindent will try to " Oh ho! The previous line ended in a comma! I bet cindent will try to
" take this too far... For now, let's normally use the previous line's " take this too far... For now, let's normally use the previous line's
" indent. " indent.
@ -166,7 +171,7 @@ function GetRustIndent(lnum)
" column zero) " column zero)
call cursor(a:lnum, 1) call cursor(a:lnum, 1)
if searchpair('{\|(', '', '}\|)', 'nbW' if searchpair('{\|(', '', '}\|)', 'nbW',
\ 's:is_string_comment(line("."), col("."))') == 0 \ 's:is_string_comment(line("."), col("."))') == 0
if searchpair('\[', '', '\]', 'nbW', if searchpair('\[', '', '\]', 'nbW',
\ 's:is_string_comment(line("."), col("."))') == 0 \ 's:is_string_comment(line("."), col("."))') == 0