Skip to content

Commit f9ffe66

Browse files
committed
Improve expand class name
Make it return the class name when it can identify the class with given child name even if it cannot identify ancestors, return the class name.
1 parent 762ae73 commit f9ffe66

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

lib/rdoc/ri/driver.rb

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -907,22 +907,9 @@ def display_page_list store, pages = store.cache[:pages], search = nil
907907
# will be expanded to Zlib::DataError.
908908

909909
def expand_class klass
910-
klass.split('::').inject '' do |expanded, klass_part|
911-
expanded << '::' unless expanded.empty?
912-
short = expanded << klass_part
913-
914-
subset = classes.keys.select do |klass_name|
915-
klass_name =~ /^#{expanded}[^:]*$/
916-
end
917-
918-
abbrevs = Abbrev.abbrev subset
919-
920-
expanded = abbrevs[short]
921-
922-
raise NotFoundError, short unless expanded
923-
924-
expanded.dup
925-
end
910+
ary = classes.keys.grep(Regexp.new("\\A#{klass.gsub(/(?=::|\z)/, '[^:]*')}\\z"))
911+
raise NotFoundError, klass if ary.length != 1
912+
ary.first
926913
end
927914

928915
##

test/test_rdoc_ri_driver.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,24 @@ def test_expand_class
834834
end
835835
end
836836

837+
def test_expand_class_2
838+
@store1 = RDoc::RI::Store.new @home_ri, :home
839+
840+
@top_level = @store1.add_file 'file.rb'
841+
842+
@cFoo = @top_level.add_class RDoc::NormalClass, 'Foo'
843+
@mFox = @top_level.add_module RDoc::NormalModule, 'Fox'
844+
@cFoo_Bar = @cFoo.add_class RDoc::NormalClass, 'Bar'
845+
@store1.save
846+
847+
@driver.stores = [@store1]
848+
assert_raises RDoc::RI::Driver::NotFoundError do
849+
@driver.expand_class 'F'
850+
end
851+
assert_equal 'Foo::Bar', @driver.expand_class('F::Bar')
852+
assert_equal 'Foo::Bar', @driver.expand_class('F::B')
853+
end
854+
837855
def test_expand_name
838856
util_store
839857

0 commit comments

Comments
 (0)