diff --git a/lib/rdoc/code_object/context.rb b/lib/rdoc/code_object/context.rb index 3a4dd0ec68..cdee0486b5 100644 --- a/lib/rdoc/code_object/context.rb +++ b/lib/rdoc/code_object/context.rb @@ -779,9 +779,7 @@ def find_class_method_named(name) # Finds a constant with +name+ in this context def find_constant_named(name) - @constants.find do |m| - m.name == name || m.full_name == name - end + @constants.find { |m| m.name == name || m.full_name == name } end ## diff --git a/lib/rdoc/cross_reference.rb b/lib/rdoc/cross_reference.rb index 1f33538b73..3dcf9b0ea7 100644 --- a/lib/rdoc/cross_reference.rb +++ b/lib/rdoc/cross_reference.rb @@ -132,47 +132,56 @@ def initialize(context) end ## - # Returns a method reference to +name+. + # Returns a method, attribute or constant reference to +name+ + # if it exists in the containing context object. It returns + # nil otherwise. + # + # For example, this method would decompose name = 'A::CONSTANT' into a + # container object A and a symbol 'CONSTANT', and it would try to find + # 'CONSTANT' in A. - def resolve_method(name) + def resolve_local_symbol(name) ref = nil + type = nil + container = nil - if /#{CLASS_REGEXP_STR}([.#]|::)#{METHOD_REGEXP_STR}/o =~ name then + case name + when /#{CLASS_REGEXP_STR}::([A-Z]\w*)\z/o then + symbol = $2 + container = @context.find_symbol_module($1) + when /#{CLASS_REGEXP_STR}([.#]|::)#{METHOD_REGEXP_STR}/o then type = $2 if '.' == type # will find either #method or ::method - method = $3 + symbol = $3 else - method = "#{type}#{$3}" + symbol = "#{type}#{$3}" end container = @context.find_symbol_module($1) - elsif /^([.#]|::)#{METHOD_REGEXP_STR}/o =~ name then + when /^([.#]|::)#{METHOD_REGEXP_STR}/o then type = $1 if '.' == type - method = $2 + symbol = $2 else - method = "#{type}#{$2}" + symbol = "#{type}#{$2}" end container = @context - else - type = nil - container = nil end if container then 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 + if 'new' == symbol then # AnyClassName.new will be class method + ref = container.find_local_symbol symbol + ref = container.find_ancestor_local_symbol symbol 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 + ref = container.find_local_symbol "::#{symbol}" + ref = container.find_ancestor_local_symbol "::#{symbol}" unless ref + ref = container.find_local_symbol "##{symbol}" unless ref + ref = container.find_ancestor_local_symbol "##{symbol}" unless ref end else - ref = container.find_local_symbol method - ref = container.find_ancestor_local_symbol method unless ref + ref = container.find_local_symbol symbol + ref = container.find_ancestor_local_symbol symbol unless ref end end end @@ -197,7 +206,7 @@ def resolve(name, text) @context.find_symbol name end - ref = resolve_method name unless ref + ref = resolve_local_symbol name unless ref # Try a page name ref = @store.page name if not ref and name =~ /^[\w.\/]+$/ diff --git a/test/rdoc/rdoc_cross_reference_test.rb b/test/rdoc/rdoc_cross_reference_test.rb index e536a67a89..ebb70bb564 100644 --- a/test/rdoc/rdoc_cross_reference_test.rb +++ b/test/rdoc/rdoc_cross_reference_test.rb @@ -157,6 +157,12 @@ def test_resolve_method assert_ref @c2_c3_m, '::C2::C3#m(*)' end + def test_resolve_constant + assert_ref @c1_const, 'CONST' + assert_ref @c1_const, 'C1::CONST' + assert_ref @c1_const, 'C12::CONST' + 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' diff --git a/test/rdoc/rdoc_store_test.rb b/test/rdoc/rdoc_store_test.rb index 7bd1d48902..51708c8e9b 100644 --- a/test/rdoc/rdoc_store_test.rb +++ b/test/rdoc/rdoc_store_test.rb @@ -163,7 +163,7 @@ def test_add_file_relative def test_all_classes_and_modules expected = %w[ - C1 C10 C10::C11 C11 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1 C6 C7 C8 C9 C9::A C9::B + C1 C10 C10::C11 C11 C12 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1 C6 C7 C8 C9 C9::A C9::B Child M1 M1::M2 Object @@ -219,7 +219,7 @@ def test_class_path def test_classes expected = %w[ - C1 C10 C10::C11 C11 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1 C6 C7 C8 C9 C9::A C9::B + C1 C10 C10::C11 C11 C12 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1 C6 C7 C8 C9 C9::A C9::B Child Object Parent diff --git a/test/rdoc/xref_data.rb b/test/rdoc/xref_data.rb index acfc0ce3ff..616b0eba81 100644 --- a/test/rdoc/xref_data.rb +++ b/test/rdoc/xref_data.rb @@ -145,6 +145,9 @@ class C11 def C11 end +class C12 < C1 +end + module M1 def m end diff --git a/test/rdoc/xref_test_case.rb b/test/rdoc/xref_test_case.rb index 4c3e3166b2..1c7a8804e4 100644 --- a/test/rdoc/xref_test_case.rb +++ b/test/rdoc/xref_test_case.rb @@ -33,6 +33,7 @@ def setup @c1__m = @c1.find_class_method_named 'm' # C1::m @c1_m = @c1.find_instance_method_named 'm' # C1#m @c1_plus = @c1.find_instance_method_named '+' + @c1_const = @c1.find_constant_named 'CONST' @c2 = @xref_data.find_module_named 'C2' @c2_a = @c2.method_list.last