FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Leading dots for method calls · vim-ruby/vim-ruby@a7b0b38 · GitHub

Commit a7b0b38

Browse files
committed
Leading dots for method calls
See #189
1 parent 89e46f3 commit a7b0b38

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎indent/ruby.vim‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ setlocal nosmartindent
2222

2323
" Now, set up our indentation expression and keys that trigger it.
2424
setlocal indentexpr=GetRubyIndent(v:lnum)
25-
setlocal indentkeys=0{,0},0),0],!^F,o,O,e,:
25+
setlocal indentkeys=0{,0},0),0],!^F,o,O,e,:,.
2626
setlocal indentkeys+==end,=else,=elsif,=when,=ensure,=rescue,==begin,==end
2727
setlocal indentkeys+==private,=protected,=public
2828

@@ -120,6 +120,9 @@ let s:block_regex =
120120

121121
let s:block_continuation_regex = '^\s*[^])}\t ].*'.s:block_regex
122122

123+
" Regex that describes a leading operator (only a method call's dot for now)
124+
let s:leading_operator_regex = '^\s*[.]'
125+
123126
" 2. Auxiliary Functions {{{1
124127
" ======================
125128

@@ -179,7 +182,11 @@ function s:GetMSL(lnum)
179182
" Otherwise, terminate search as we have found our MSL already.
180183
let line = getline(lnum)
181184

182-
if s:Match(lnum, s:splat_regex)
185+
if s:Match(msl, s:leading_operator_regex)
186+
" If the current line starts with a leading operator, keep its indent
187+
" and keep looking for an MSL.
188+
let msl = lnum
189+
elseif s:Match(lnum, s:splat_regex)
183190
" If the above line looks like the "*" of a splat, use the current one's
184191
" indentation.
185192
"
@@ -442,6 +449,11 @@ function GetRubyIndent(...)
442449
return 0
443450
endif
444451

452+
" If the current line starts with a leading operator, add a level of indent.
453+
if s:Match(clnum, s:leading_operator_regex)
454+
return indent(s:GetMSL(clnum)) + &sw
455+
endif
456+
445457
" 3.3. Work on the previous line. {{{2
446458
" -------------------------------
447459

@@ -481,6 +493,12 @@ function GetRubyIndent(...)
481493
return indent(s:GetMSL(lnum)) + &sw
482494
endif
483495

496+
" If the previous line started with a leading operator, use its MSL's level
497+
" of indent
498+
if s:Match(lnum, s:leading_operator_regex)
499+
return indent(s:GetMSL(lnum))
500+
endif
501+
484502
" If the previous line ended with the "*" of a splat, add a level of indent
485503
if line =~ s:splat_regex
486504
return indent(lnum) + &sw

‎spec/indent/continuations_spec.rb‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
require 'spec_helper'
22

33
describe "Indenting" do
4+
specify "method chaining" do
5+
assert_correct_indenting <<-EOF
6+
some_object.
7+
method_one.
8+
method_two.
9+
method_three
10+
EOF
11+
12+
assert_correct_indenting <<-EOF
13+
some_object
14+
.method_one
15+
.method_two
16+
.method_three
17+
EOF
18+
end
19+
420
specify "arrays" do
521
assert_correct_indenting <<-EOF
622
foo = [one,

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL