Skip to content

Commit 859183e

Browse files
committed
Add support for the public access modifier keyword.
1 parent addba7e commit 859183e

File tree

4 files changed

+206
-139
lines changed

4 files changed

+206
-139
lines changed

doc/ft-ruby-syntax.txt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ There are a number of options to the Ruby syntax highlighting.
66
2. Whitespace errors |ruby_space_errors|
77
3. Folds |ruby_fold|
88
4. Reducing expensive operations |ruby_no_expensive| |ruby_minlines|
9+
5. Reducing expensive operations |ruby_indent_access_modifier_style|
910

1011

1112
1. Ruby operators *ruby_operators*
@@ -61,4 +62,47 @@ the "ruby_minlines" variable to a value larger than 50: >
6162
Ideally, this value should be a number of lines large enough to embrace your
6263
largest class or module.
6364

65+
5. Access modifier indentation *ruby_indent_access_modifier_style*
66+
67+
Different access modifier indentation styles can be used by setting: >
68+
69+
:let g:ruby_indent_access_modifier_style
70+
:let g:ruby_indent_access_modifier_style = 'indent'
71+
:let g:ruby_indent_access_modifier_style = 'outdent'
72+
<
73+
By default, the normal access modifier style is used.
74+
Access modifier style 'normal':
75+
class Indent
76+
private :method
77+
protected :method
78+
private
79+
def method; end
80+
protected
81+
def method; end
82+
public
83+
def method; end
84+
end
85+
Access modifier style 'indent':
86+
class Indent
87+
private :method
88+
protected :method
89+
private
90+
def method; end
91+
protected
92+
def method; end
93+
public
94+
def method; end
95+
end
96+
Access modifier style 'outdent':
97+
class Indent
98+
private :method
99+
protected :method
100+
private
101+
def method; end
102+
protected
103+
def method; end
104+
public
105+
def method; end
106+
end
107+
64108
vim:tw=78:sw=4:ts=8:ft=help:norl:

indent/ruby.vim

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ if exists("b:did_indent")
1313
endif
1414
let b:did_indent = 1
1515

16-
if !exists('g:ruby_indent_private_protected_style')
16+
if !exists('g:ruby_indent_access_modifier_style')
1717
" Possible values: "normal", "indent", "outdent"
18-
let g:ruby_indent_private_protected_style = 'normal'
18+
let g:ruby_indent_access_modifier_style = 'normal'
1919
endif
2020

2121
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
27-
setlocal indentkeys+==private,=protected,:=private,:=protected
27+
setlocal indentkeys+==private,=protected,=public
2828

2929
" Only define the function once.
3030
if exists("*GetRubyIndent")
@@ -98,8 +98,11 @@ let s:bracket_continuation_regex = '%\@<!\%([({[]\)\s*\%(#.*\)\=$'
9898
" Regex that defines the first part of a splat pattern
9999
let s:splat_regex = '[[,(]\s*\*\s*\%(#.*\)\=$'
100100

101-
" Regex that describes a private/protected access modifier
102-
let s:private_protected_regex = '\C^\s*\%(private\|protected\)\s*\%(#.*\)\=$'
101+
" Regex that describes all indent access modifiers
102+
let s:access_modifier_regex = '\C^\s*\%(private\|public\|protected\)\s*\%(#.*\)\=$'
103+
104+
" Regex that describes the indent access modifiers (excludes public)
105+
let s:indent_access_modifier_regex = '\C^\s*\%(private\|protected\)\s*\%(#.*\)\=$'
103106

104107
" Regex that defines blocks.
105108
"
@@ -363,17 +366,17 @@ function GetRubyIndent(...)
363366
let line = getline(clnum)
364367
let ind = -1
365368

366-
" If this line is a private/protected keyword, align according to the
367-
" closest class declaration.
368-
if g:ruby_indent_private_protected_style == 'indent'
369-
if s:Match(clnum, s:private_protected_regex)
369+
" If this line is an access modifier keyword, align according to the closest
370+
" class declaration.
371+
if g:ruby_indent_access_modifier_style == 'indent'
372+
if s:Match(clnum, s:access_modifier_regex)
370373
let class_line = s:FindContainingClass()
371374
if class_line > 0
372375
return indent(class_line) + &sw
373376
endif
374377
endif
375-
elseif g:ruby_indent_private_protected_style == 'outdent'
376-
if s:Match(clnum, s:private_protected_regex)
378+
elseif g:ruby_indent_access_modifier_style == 'outdent'
379+
if s:Match(clnum, s:access_modifier_regex)
377380
let class_line = s:FindContainingClass()
378381
if class_line > 0
379382
return indent(class_line)
@@ -457,9 +460,16 @@ function GetRubyIndent(...)
457460
let line = getline(lnum)
458461
let ind = indent(lnum)
459462

460-
if g:ruby_indent_private_protected_style == 'indent' || g:ruby_indent_private_protected_style == 'outdent'
461-
" If the previous line was a private/protected keyword, add a level of indent
462-
if s:Match(lnum, s:private_protected_regex)
463+
if g:ruby_indent_access_modifier_style == 'indent'
464+
" If the previous line was a private/protected keyword, add a
465+
" level of indent
466+
if s:Match(lnum, s:indent_access_modifier_regex)
467+
return indent(s:GetMSL(lnum)) + &sw
468+
endif
469+
elseif g:ruby_indent_access_modifier_style == 'outdent'
470+
" If the previous line was a private/protected/public keyword, remove
471+
" a level of indent
472+
if s:Match(lnum, s:access_modifier_regex)
463473
return indent(s:GetMSL(lnum)) + &sw
464474
endif
465475
endif
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
require 'spec_helper'
2+
3+
describe "Indenting" do
4+
after :each do
5+
vim.command 'let g:ruby_indent_access_modifier_style = "normal"'
6+
end
7+
8+
specify "default indented access modifiers" do
9+
assert_correct_indenting <<-EOF
10+
class OuterClass
11+
12+
private :method
13+
protected :method
14+
def method; end
15+
protected
16+
def method; end
17+
private
18+
def method; end
19+
public
20+
def method; end
21+
22+
class InnerClass
23+
24+
private :method
25+
protected :method
26+
def method; end
27+
protected
28+
def method; end
29+
private
30+
def method; end
31+
public
32+
def method; end
33+
34+
end
35+
36+
private :method
37+
protected :method
38+
def method; end
39+
protected
40+
def method; end
41+
private
42+
def method; end
43+
public
44+
def method; end
45+
46+
end
47+
EOF
48+
end
49+
50+
specify "indented access modifiers" do
51+
vim.command 'let g:ruby_indent_access_modifier_style = "indent"'
52+
53+
assert_correct_indenting <<-EOF
54+
class OuterClass
55+
56+
private :method
57+
protected :method
58+
def method; end
59+
protected
60+
def method; end
61+
private
62+
def method; end
63+
public
64+
def method; end
65+
66+
class InnerClass
67+
68+
private :method
69+
protected :method
70+
def method; end
71+
protected
72+
def method; end
73+
private
74+
def method; end
75+
public
76+
def method; end
77+
78+
end
79+
80+
private :method
81+
protected :method
82+
def method; end
83+
protected
84+
def method; end
85+
private
86+
def method; end
87+
public
88+
def method; end
89+
90+
end
91+
EOF
92+
end
93+
94+
specify "outdented access modifiers" do
95+
vim.command 'let g:ruby_indent_access_modifier_style = "outdent"'
96+
97+
assert_correct_indenting <<-EOF
98+
class OuterClass
99+
100+
private :method
101+
protected :method
102+
def method; end
103+
protected
104+
def method; end
105+
private
106+
def method; end
107+
public
108+
def method; end
109+
110+
class InnerClass
111+
112+
private :method
113+
protected :method
114+
def method; end
115+
protected
116+
def method; end
117+
private
118+
def method; end
119+
public
120+
def method; end
121+
122+
end
123+
124+
private :method
125+
protected :method
126+
def method; end
127+
protected
128+
def method; end
129+
private
130+
def method; end
131+
public
132+
def method; end
133+
134+
end
135+
EOF
136+
end
137+
end

spec/indent/private_protected_spec.rb

Lines changed: 0 additions & 124 deletions
This file was deleted.

0 commit comments

Comments
 (0)