Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Special rules for `def` as an indent keyword

See #190

Ruby 2.1 makes method definitions return the method name as a symbol.
This means that you could prefix the `def` line with a `private`
and it would make the method private upon definition.

Technically, any method call could be placed before the `def`, but
that's no different than before. The private/protected use case seems
like the most likely one, so this is what we'll handle for now. If it
gets popular to use different class-level methods for method
definitions, we could put a \k\+ instead.
  • Loading branch information...
commit 89e46f380177c7e6bf53e84f1e4c56d8e8a2d431 1 parent 2597860
@AndrewRadev AndrewRadev authored
Showing with 24 additions and 6 deletions.
  1. +8 −6 indent/ruby.vim
  2. +16 −0 spec/indent/method_definitions_spec.rb
View
14 indent/ruby.vim
@@ -55,9 +55,10 @@ let s:skip_expr =
\ "synIDattr(synID(line('.'),col('.'),1),'name') =~ '".s:syng_strcom."'"
" Regex used for words that, at the start of a line, add a level of indent.
-let s:ruby_indent_keywords = '^\s*\zs\<\%(module\|class\|def\|if\|for' .
- \ '\|while\|until\|else\|elsif\|case\|when\|unless\|begin\|ensure' .
- \ '\|rescue\):\@!\>' .
+let s:ruby_indent_keywords =
+ \ '^\s*\zs\<\%(module\|class\|if\|for' .
+ \ '\|while\|until\|else\|elsif\|case\|when\|unless\|begin\|ensure\|rescue' .
+ \ '\|\%(public\|protected\|private\)\=\s*def\):\@!\>' .
\ '\|\%([=,*/%+-]\|<<\|>>\|:\s\)\s*\zs' .
\ '\<\%(if\|for\|while\|until\|case\|unless\|begin\):\@!\>'
@@ -70,7 +71,8 @@ let s:ruby_deindent_keywords =
" TODO: the do here should be restricted somewhat (only at end of line)?
let s:end_start_regex =
\ '\C\%(^\s*\|[=,*/%+\-|;{]\|<<\|>>\|:\s\)\s*\zs' .
- \ '\<\%(module\|class\|def\|if\|for\|while\|until\|case\|unless\|begin\):\@!\>' .
+ \ '\<\%(module\|class\|if\|for\|while\|until\|case\|unless\|begin' .
+ \ '\|\%(public\|protected\|private\)\=\s*def\):\@!\>' .
\ '\|\%(^\|[^.:@$]\)\@<=\<do:\@!\>'
" Regex that defines the middle-match for the 'end' keyword.
@@ -99,10 +101,10 @@ let s:bracket_continuation_regex = '%\@<!\%([({[]\)\s*\%(#.*\)\=$'
let s:splat_regex = '[[,(]\s*\*\s*\%(#.*\)\=$'
" Regex that describes all indent access modifiers
-let s:access_modifier_regex = '\C^\s*\%(private\|public\|protected\)\s*\%(#.*\)\=$'
+let s:access_modifier_regex = '\C^\s*\%(public\|protected\|private\)\s*\%(#.*\)\=$'
" Regex that describes the indent access modifiers (excludes public)
-let s:indent_access_modifier_regex = '\C^\s*\%(private\|protected\)\s*\%(#.*\)\=$'
+let s:indent_access_modifier_regex = '\C^\s*\%(protected\|private\)\s*\%(#.*\)\=$'
" Regex that defines blocks.
"
View
16 spec/indent/method_definitions_spec.rb
@@ -0,0 +1,16 @@
+require 'spec_helper'
+
+describe "Indenting" do
+ specify "method definitions prefixed with access modifiers" do
+ assert_correct_indenting <<-EOF
+ class Foo
+ public def one(x)
+ end
+
+ private def two(y)
+ code
+ end
+ end
+ EOF
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.