ERuby indenting

Tim Pope vim-ruby-devel at tpope.info
Wed Nov 8 19:00:20 EST 2006


On Wed, Nov 08, 2006 at 10:31:10PM +0100, Nikolai Weibull wrote:
> On 11/8/06, Tim Pope <vim-ruby-devel at tpope.info> wrote:
> > I have a couple of questions regarding your indent/ruby.vim myself.
> > Are you particularly attached to the method of lining up parentheses?
> 
> ?  Do you mean as in
> 
> def some_method(param1,
>                 param2)
>   <
> 
> or what?  Writing
> 
> def some_method(param1,
>                 param2
>                )
>                <
> 
> is bugged though, because it's assumed that the ) closes something.
> This is stupid, though, and the ) shouldn't be considered special.
> I'll check what's causing this some day or other.

The second example is one of the problem cases.  Here is another:

ActiveRecord::Base.establish_connection(
                                        :adapter => 'sqlite3',
                                        :database => 'foo.sqlite3')
                                       <

Notice that it also breaks if the opening parentheses ends the first
line.

My ideal outcome would actually be something like:

ActiveRecord::Base.establish_connection(
  :adapter => 'sqlite3',
  :database => 'foo.sqlite3'
)
<

This is a common case I run into.  With a very long method call, I
want to start the arguments on a separate line so I can have more
room.  Instead, I am treated to being in the exact same column that I
was already in.  I have to manually correct for this, and then
manually correct again when closing the parentheses.  Very tedious
with such a large indent.

After seeing your example it's apparent there are two different use
cases here.

very_long_method_name(
  bar,
  baz
)

short(foo,
      bar)

Perhaps the ideal solution is to optimize for my case when the line
ends with an opening parenthesis, and your case when it is followed
with further characters.  I have no idea how difficult this is to
implement but I think it would leave all parties happy.  (Further
input from others on this is welcome.)

> > I have altered my local copy to make it possible to disable
> > (g:rubyindent_match_parentheses) and instead use the brace/bracket
> > behavior.  Given that the indent doesn't restore properly on the line
> > after the closing parenthesis, I'd be quite satisfied to just see the
> > behavior eliminated altogether.  I could furnish a patch for this as
> > well (or just alter and commit directly) if you are open to it.
> 
> Please show the problem and the solution.

I have attached a patch.  While it would pacify me, I think it would
probably be better to instead pursue the goal I outlined above.

> > On a related note, ) and ] probably belong in indentkeys.
> 
> They already are.

Err, apparently I should be sending that to myself, as they are
missing from eruby.vim, not yours.

Cheers,
Tim
-------------- next part --------------
--- ruby.vim	2006-04-25 02:05:42.000000000 -0500
+++ ruby.vim.new	2006-08-21 14:49:52.000000000 -0500
@@ -30,6 +30,10 @@
 let s:cpo_save = &cpo
 set cpo&vim
 
+if !exists("g:rubyindent_match_parentheses")
+    let g:rubyindent_match_parentheses = 1
+endif
+
 " 1. Variables {{{1
 " ============
 
@@ -217,7 +221,11 @@
     call cursor(v:lnum, col)
     let bs = strpart('(){}[]', stridx(')}]', line[col - 1]) * 2, 2)
     if searchpair(escape(bs[0], '\['), '', bs[1], 'bW', s:skip_expr) > 0
-      let ind = line[col-1]==')' ? virtcol('.')-1 : indent(s:GetMSL(line('.')))
+      if line[col-1]==')' && g:rubyindent_match_parentheses
+          let ind = virtcol('.')-1
+      else
+          let ind = indent(s:GetMSL(line('.')))
+      endif
     endif
     return ind
   endif
@@ -273,10 +281,11 @@
   " add indent depending on the bracket type.
   if line =~ '[[({]'
     let counts = s:LineHasOpeningBrackets(lnum)
-    if counts[0] == '1' && searchpair('(', '', ')', 'bW', s:skip_expr) > 0
-      return virtcol('.')
-    elseif counts[1] == '1' || counts[2] == '1'
+    if counts[1] == '1' || counts[2] == '1' || 
+                \ (counts[0] == '1' && !g:rubyindent_match_parentheses)
       return ind + &sw
+    elseif counts[0] == '1' && searchpair('(', '', ')', 'bW', s:skip_expr) > 0
+      return virtcol('.')
     else
       call cursor(v:lnum, vcol)
     end


More information about the vim-ruby-devel mailing list