Skip to content
Browse files

Handling for multiline splats

  • Loading branch information...
1 parent 5990bf1 commit 15ae2d70f30196d08c33eb40639ff52ccc469406 @AndrewRadev AndrewRadev committed Aug 8, 2012
Showing with 49 additions and 1 deletion.
  1. +30 −0 etc/examples/indent/splat.rb
  2. +19 −1 indent/ruby.vim
View
30 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,
+ *)
View
20 indent/ruby.vim
@@ -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.
@@ -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.
"
@@ -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 =~ '[[({]'

0 comments on commit 15ae2d7

Please sign in to comment.
Something went wrong with that request. Please try again.