Skip to content

Commit d9f7809

Browse files
committed
Link to a class method when using dot
Some classes of standard library have the same name in class method and instance method. For example, Random::bytes and Random#bytes. In most cases of using dot in documents, it intends class method.
1 parent 31b2a75 commit d9f7809

File tree

5 files changed

+57
-10
lines changed

5 files changed

+57
-10
lines changed

lib/rdoc/cross_reference.rb

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,23 +127,41 @@ def resolve name, text
127127

128128
if /#{CLASS_REGEXP_STR}([.#]|::)#{METHOD_REGEXP_STR}/o =~ name then
129129
type = $2
130-
type = '' if type == '.' # will find either #method or ::method
131-
method = "#{type}#{$3}"
130+
if '.' == type # will find either #method or ::method
131+
method = $3
132+
else
133+
method = "#{type}#{$3}"
134+
end
132135
container = @context.find_symbol_module($1)
133136
elsif /^([.#]|::)#{METHOD_REGEXP_STR}/o =~ name then
134137
type = $1
135-
type = '' if type == '.'
136-
method = "#{type}#{$2}"
138+
if '.' == type
139+
method = $2
140+
else
141+
method = "#{type}#{$2}"
142+
end
137143
container = @context
138144
else
145+
type = nil
139146
container = nil
140147
end
141148

142149
if container then
143-
ref = container.find_local_symbol method
144-
145-
unless ref || RDoc::TopLevel === container then
146-
ref = container.find_ancestor_local_symbol method
150+
unless RDoc::TopLevel === container then
151+
if '.' == type then
152+
if 'new' == method then # AnyClassName.new will be class method
153+
ref = container.find_local_symbol method
154+
ref = container.find_ancestor_local_symbol method unless ref
155+
else
156+
ref = container.find_local_symbol "::#{method}"
157+
ref = container.find_ancestor_local_symbol "::#{method}" unless ref
158+
ref = container.find_local_symbol "##{method}" unless ref
159+
ref = container.find_ancestor_local_symbol "##{method}" unless ref
160+
end
161+
else
162+
ref = container.find_local_symbol method
163+
ref = container.find_ancestor_local_symbol method unless ref
164+
end
147165
end
148166
end
149167

test/test_rdoc_cross_reference.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ def test_resolve_method
139139
assert_ref @c2_c3_m, '::C2::C3#m(*)'
140140
end
141141

142+
def test_resolve_the_same_name_in_instance_and_class_method
143+
assert_ref @c9_a_i_foo, 'C9::A#foo'
144+
assert_ref @c9_a_c_bar, 'C9::A::bar'
145+
assert_ref @c9_b_c_foo, 'C9::B::foo'
146+
assert_ref @c9_b_i_bar, 'C9::B#bar'
147+
assert_ref @c9_b_c_foo, 'C9::B.foo'
148+
assert_ref @c9_a_c_bar, 'C9::B.bar'
149+
end
150+
142151
def test_resolve_method_equals3
143152
m = RDoc::AnyMethod.new '', '==='
144153
@c1.add_method m

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 C6 C7 C8 C8::S1
165+
C1 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1 C6 C7 C8 C8::S1 C9 C9::A C9::B
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 C6 C7 C8 C8::S1
216+
C1 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1 C6 C7 C8 C8::S1 C9 C9::A C9::B
217217
Child
218218
Parent
219219
]

test/xref_data.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ class S1
101101
end
102102
end
103103
104+
class C9
105+
class A
106+
def foo() end
107+
def self.bar() end
108+
end
109+
110+
class B < A
111+
def self.foo() end
112+
def bar() end
113+
end
114+
end
115+
104116
module M1
105117
def m
106118
end

test/xref_test_case.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ def generator.file_dir() nil end
5656
@c8 = @xref_data.find_module_named 'C8'
5757
@c8_s1 = @xref_data.find_module_named 'C8::S1'
5858

59+
@c9 = @xref_data.find_module_named 'C9'
60+
@c9_a = @xref_data.find_module_named 'C9::A'
61+
@c9_a_i_foo = @c9_a.method_list.first
62+
@c9_a_c_bar = @c9_a.method_list.last
63+
@c9_b = @xref_data.find_module_named 'C9::B'
64+
@c9_b_c_foo = @c9_b.method_list.first
65+
@c9_b_i_bar = @c9_b.method_list.last
66+
5967
@m1 = @xref_data.find_module_named 'M1'
6068
@m1_m = @m1.method_list.first
6169

0 commit comments

Comments
 (0)