Skip to content

Link to a class method when using dot #629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions lib/rdoc/cross_reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,41 @@ def resolve name, text

if /#{CLASS_REGEXP_STR}([.#]|::)#{METHOD_REGEXP_STR}/o =~ name then
type = $2
type = '' if type == '.' # will find either #method or ::method
method = "#{type}#{$3}"
if '.' == type # will find either #method or ::method
method = $3
else
method = "#{type}#{$3}"
end
container = @context.find_symbol_module($1)
elsif /^([.#]|::)#{METHOD_REGEXP_STR}/o =~ name then
type = $1
type = '' if type == '.'
method = "#{type}#{$2}"
if '.' == type
method = $2
else
method = "#{type}#{$2}"
end
container = @context
else
type = nil
container = nil
end

if container then
ref = container.find_local_symbol method

unless ref || RDoc::TopLevel === container then
ref = container.find_ancestor_local_symbol method
unless RDoc::TopLevel === container then
if '.' == type then
if 'new' == method then # AnyClassName.new will be class method
ref = container.find_local_symbol method
ref = container.find_ancestor_local_symbol method unless ref
else
ref = container.find_local_symbol "::#{method}"
ref = container.find_ancestor_local_symbol "::#{method}" unless ref
ref = container.find_local_symbol "##{method}" unless ref
ref = container.find_ancestor_local_symbol "##{method}" unless ref
end
else
ref = container.find_local_symbol method
ref = container.find_ancestor_local_symbol method unless ref
end
end
end

Expand Down
9 changes: 9 additions & 0 deletions test/test_rdoc_cross_reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ def test_resolve_method
assert_ref @c2_c3_m, '::C2::C3#m(*)'
end

def test_resolve_the_same_name_in_instance_and_class_method
assert_ref @c9_a_i_foo, 'C9::A#foo'
assert_ref @c9_a_c_bar, 'C9::A::bar'
assert_ref @c9_b_c_foo, 'C9::B::foo'
assert_ref @c9_b_i_bar, 'C9::B#bar'
assert_ref @c9_b_c_foo, 'C9::B.foo'
assert_ref @c9_a_c_bar, 'C9::B.bar'
end

def test_resolve_method_equals3
m = RDoc::AnyMethod.new '', '==='
@c1.add_method m
Expand Down
4 changes: 2 additions & 2 deletions test/test_rdoc_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def test_add_file_relative

def test_all_classes_and_modules
expected = %w[
C1 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1 C6 C7 C8 C8::S1
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
Child
M1 M1::M2
Parent
Expand Down Expand Up @@ -213,7 +213,7 @@ def test_class_path

def test_classes
expected = %w[
C1 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1 C6 C7 C8 C8::S1
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
Child
Parent
]
Expand Down
12 changes: 12 additions & 0 deletions test/xref_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ class S1
end
end

class C9
class A
def foo() end
def self.bar() end
end

class B < A
def self.foo() end
def bar() end
end
end

module M1
def m
end
Expand Down
8 changes: 8 additions & 0 deletions test/xref_test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ def generator.file_dir() nil end
@c8 = @xref_data.find_module_named 'C8'
@c8_s1 = @xref_data.find_module_named 'C8::S1'

@c9 = @xref_data.find_module_named 'C9'
@c9_a = @xref_data.find_module_named 'C9::A'
@c9_a_i_foo = @c9_a.method_list.first
@c9_a_c_bar = @c9_a.method_list.last
@c9_b = @xref_data.find_module_named 'C9::B'
@c9_b_c_foo = @c9_b.method_list.first
@c9_b_i_bar = @c9_b.method_list.last

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

Expand Down