Skip to content

Commit

Permalink
Fix indentation for closing parentheses
Browse files Browse the repository at this point in the history
To help explain this problem, consider this input (where | is the
cursor):

    foo(|)

Prior to this commit, pressing Enter would result in the following:

    foo(
        |)

That is, the closing parenthesis is indented by one level. This is the
case regardless of the cindent options/keys chosen.

To fix this, we manually indent lines that start with a ")" (ignoring
leading whitespace). Such lines are indented according to the
indentation of the line that contained the opening parenthesis.

Fixes rust-lang#443
  • Loading branch information
yorickpeterse committed May 20, 2021
1 parent 87c745d commit d4beeb5
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions indent/rust.vim
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,31 @@ function GetRustIndent(lnum)
endif
endif

" cindent by messes up indentation of parentheses. It does so regardless of
" the cindent options.
"
" Consider this input example, where | is the cursor:
"
" foo(|)
"
" When pressing Enter, the indentation will be this:
"
" foo(
" |)
"
" But that's not what we want, we want it to be this instead:
"
" foo(
" |)
if line =~# '\V\^\s\*)'
let l:paren_start = searchpair('(', '', ')', 'nbW',
\ 's:is_string_comment(line("."), col("."))')

if l:paren_start != 0 && l:paren_start < a:lnum
return indent(l:paren_start)
endif
endif

" Fall back on cindent, which does it mostly right
return cindent(a:lnum)
endfunction
Expand Down

0 comments on commit d4beeb5

Please sign in to comment.