Skip to content

Commit

Permalink
Handling for multiline splats
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewRadev committed Aug 8, 2012
1 parent 5990bf1 commit 15ae2d7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
30 changes: 30 additions & 0 deletions etc/examples/indent/splat.rb
@@ -0,0 +1,30 @@
x = Foo[*
y do
z
end
]

x = Foo[* # with a comment
y do
z
end
]

x = *
array.map do
3
end

x = Foo[*
y {
z
}
]

x = Foo(*y do
z
end)

foo(1,
2,
*)
20 changes: 19 additions & 1 deletion indent/ruby.vim
Expand Up @@ -90,6 +90,9 @@ let s:continuation_regex =
" Regex that defines bracket continuations
let s:bracket_continuation_regex = '%\@<!\%([({[]\)\s*\%(#.*\)\=$'

" Regex that defines the first part of a splat pattern
let s:splat_regex = '[[,(]\s*\*\s*\%(#.*\)\=$'

" Regex that defines blocks.
"
" Note that there's a slight problem with this regex and s:continuation_regex.
Expand Down Expand Up @@ -156,7 +159,17 @@ function s:GetMSL(lnum)
" Otherwise, terminate search as we have found our MSL already.
let line = getline(lnum)

if line =~ s:non_bracket_continuation_regex && msl_body =~ s:non_bracket_continuation_regex
if line =~ s:splat_regex
" If the above line looks like the "*" of a splat, use the current one's
" indentation.
"
" Example:
" Hash[*
" method_call do
" something
"
return msl
elseif line =~ s:non_bracket_continuation_regex && msl_body =~ s:non_bracket_continuation_regex
" If the current line is a non-bracket continuation and so is the
" previous one, keep its indent and continue looking for an MSL.
"
Expand Down Expand Up @@ -351,6 +364,11 @@ function GetRubyIndent(...)
return indent(s:GetMSL(lnum)) + &sw
endif

" If the previous line ended with the "*" of a splat, add a level of indent
if line =~ s:splat_regex
return indent(lnum) + &sw
endif

" If the previous line contained an opening bracket, and we are still in it,
" add indent depending on the bracket type.
if line =~ '[[({]'
Expand Down

0 comments on commit 15ae2d7

Please sign in to comment.