Skip to content

Commit cba66bc

Browse files
committed
auto merge of #13925 : kballard/rust/vim_indent_bracket_fix, r=thestinger
If an unbalanced [ exists in a string or comment, this should not be considered when calculating the indent at the top level. Similarly, when testing for ({/}) to see if we're at the top level to begin with, strings and comments should be skipped.
2 parents a1ca572 + 91e61ad commit cba66bc

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/etc/vim/indent/rust.vim

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ endif
3030

3131
" Come here when loading the script the first time.
3232

33-
function s:get_line_trimmed(lnum)
33+
function! s:get_line_trimmed(lnum)
3434
" Get the line and remove a trailing comment.
3535
" Use syntax highlighting attributes when possible.
3636
" NOTE: this is not accurate; /* */ or a line continuation could trick it
@@ -61,6 +61,20 @@ function s:get_line_trimmed(lnum)
6161
endif
6262
endfunction
6363

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+
6478
function GetRustIndent(lnum)
6579

6680
" Starting assumption: cindent (called at the end) will do it right
@@ -152,8 +166,10 @@ function GetRustIndent(lnum)
152166
" column zero)
153167

154168
call cursor(a:lnum, 1)
155-
if searchpair('{\|(', '', '}\|)', 'nbW') == 0
156-
if searchpair('\[', '', '\]', 'nbW') == 0
169+
if searchpair('{\|(', '', '}\|)', 'nbW'
170+
\ 's:is_string_comment(line("."), col("."))') == 0
171+
if searchpair('\[', '', '\]', 'nbW',
172+
\ 's:is_string_comment(line("."), col("."))') == 0
157173
" Global scope, should be zero
158174
return 0
159175
else

0 commit comments

Comments
 (0)