Skip to content

Commit 49b2f4e

Browse files
committed
Parse ruby 2.1 <visibility> def
fixes #355
1 parent 4bc25f6 commit 49b2f4e

File tree

6 files changed

+63
-3
lines changed

6 files changed

+63
-3
lines changed

lib/rdoc/context.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ class RDoc::Context < RDoc::CodeObject
9898

9999
attr_accessor :visibility
100100

101+
##
102+
# Current visibility of this line
103+
104+
attr_writer :current_line_visibility
105+
101106
##
102107
# Hash of registered methods. Attributes are also registered here,
103108
# twice if they are RW.
@@ -148,6 +153,7 @@ def initialize_methods_etc
148153
@extends = []
149154
@constants = []
150155
@external_aliases = []
156+
@current_line_visibility = nil
151157

152158
# This Hash maps a method name to a list of unmatched aliases (aliases of
153159
# a method not yet encountered).
@@ -478,7 +484,11 @@ def add_method method
478484
end
479485
else
480486
@methods_hash[key] = method
481-
method.visibility = @visibility
487+
if @current_line_visibility
488+
method.visibility, @current_line_visibility = @current_line_visibility, nil
489+
else
490+
method.visibility = @visibility
491+
end
482492
add_to @method_list, method
483493
resolve_aliases method
484494
end

lib/rdoc/parser/ruby.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,7 @@ def parse_statements(container, single = NORMAL, current_method = nil,
16661666

16671667
unget_tk tk
16681668
keep_comment = true
1669+
container.current_line_visibility = nil
16691670

16701671
when TkCLASS then
16711672
parse_class container, single, tk, comment
@@ -1888,6 +1889,8 @@ def parse_visibility(container, single, tk)
18881889
#
18891890
when TkNL, TkUNLESS_MOD, TkIF_MOD, TkSEMICOLON then
18901891
container.ongoing_visibility = vis
1892+
when TkDEF
1893+
container.current_line_visibility = vis
18911894
else
18921895
update_visibility container, vis_type, vis, singleton
18931896
end

test/test_rdoc_context.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,27 @@ def test_sort_sections_tomdoc_missing
866866
assert_equal [nil, 'Public', 'Internal'], titles
867867
end
868868

869+
def test_visibility_def
870+
assert_equal :private, @c6.find_method_named('priv1').visibility
871+
assert_equal :protected, @c6.find_method_named('prot1').visibility
872+
assert_equal :public, @c6.find_method_named('pub1').visibility
873+
assert_equal :private, @c6.find_method_named('priv2').visibility
874+
assert_equal :protected, @c6.find_method_named('prot2').visibility
875+
assert_equal :public, @c6.find_method_named('pub2').visibility
876+
assert_equal :private, @c6.find_method_named('priv3').visibility
877+
assert_equal :protected, @c6.find_method_named('prot3').visibility
878+
assert_equal :public, @c6.find_method_named('pub3').visibility
879+
assert_equal :private, @c6.find_method_named('priv4').visibility
880+
assert_equal :protected, @c6.find_method_named('prot4').visibility
881+
assert_equal :public, @c6.find_method_named('pub4').visibility
882+
assert_equal :private, @c6.find_method_named('priv5').visibility
883+
assert_equal :protected, @c6.find_method_named('prot5').visibility
884+
assert_equal :public, @c6.find_method_named('pub5').visibility
885+
assert_equal :private, @c6.find_method_named('priv6').visibility
886+
assert_equal :protected, @c6.find_method_named('prot6').visibility
887+
assert_equal :public, @c6.find_method_named('pub6').visibility
888+
end
889+
869890
def util_visibilities
870891
@pub = RDoc::AnyMethod.new nil, 'pub'
871892
@prot = RDoc::AnyMethod.new nil, 'prot'

test/test_rdoc_store.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def test_add_file_relative
162162

163163
def test_all_classes_and_modules
164164
expected = %w[
165-
C1 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1
165+
C1 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1 C6
166166
Child
167167
M1 M1::M2
168168
Parent
@@ -213,7 +213,7 @@ def test_class_path
213213

214214
def test_classes
215215
expected = %w[
216-
C1 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1
216+
C1 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1 C6
217217
Child
218218
Parent
219219
]

test/xref_data.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,31 @@ class C1
5757
end
5858
end
5959
60+
class C6
61+
private def priv1() end
62+
def pub1() end
63+
protected def prot1() end
64+
def pub2() end
65+
public def pub3() end
66+
def pub4() end
67+
68+
private
69+
private def priv2() end
70+
def priv3() end
71+
protected def prot2() end
72+
def priv4() end
73+
public def pub5() end
74+
def priv5() end
75+
76+
protected
77+
private def priv6() end
78+
def prot3() end
79+
protected def prot4() end
80+
def prot5() end
81+
public def pub6() end
82+
def prot6() end
83+
end
84+
6085
module M1
6186
def m
6287
end

test/xref_test_case.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def generator.file_dir() nil end
5151
@c5_c1 = @xref_data.find_module_named 'C5::C1'
5252
@c3_h1 = @xref_data.find_module_named 'C3::H1'
5353
@c3_h2 = @xref_data.find_module_named 'C3::H2'
54+
@c6 = @xref_data.find_module_named 'C6'
5455

5556
@m1 = @xref_data.find_module_named 'M1'
5657
@m1_m = @m1.method_list.first

0 commit comments

Comments
 (0)