Skip to content

Commit c5abcbd

Browse files
committed
Filter ri method list. This is probably not the best solution
1 parent db119ed commit c5abcbd

File tree

3 files changed

+95
-4
lines changed

3 files changed

+95
-4
lines changed

History.txt

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* Restored parsing of block comments. RubyForge bug #28668 by Stefano Crocco.
2424
* Metaprogrammed methods defined with blocks no longer confuse the ruby
2525
parser. RubyForge bug #28370 by Erik Hollensbe.
26+
* ri no longer displays all methods in the inheritance chain.
2627

2728
=== 3.2 / 2010-12-29
2829

lib/rdoc/ri/driver.rb

+36-4
Original file line numberDiff line numberDiff line change
@@ -651,12 +651,14 @@ def display_method name
651651

652652
raise NotFoundError, name if found.empty?
653653

654+
filtered = filter_methods found, name
655+
654656
out = RDoc::Markup::Document.new
655657

656658
out << RDoc::Markup::Heading.new(1, name)
657659
out << RDoc::Markup::BlankLine.new
658660

659-
found.each do |store, methods|
661+
filtered.each do |store, methods|
660662
methods.each do |method|
661663
out << RDoc::Markup::Paragraph.new("(from #{store.friendly_path})")
662664

@@ -753,6 +755,21 @@ def expand_name name
753755
"#{expand_class klass}#{selector}#{method}"
754756
end
755757

758+
##
759+
# Filters the methods in +found+ trying to find a match for +name+.
760+
761+
def filter_methods found, name
762+
regexp = name_regexp name
763+
764+
filtered = found.find_all do |store, methods|
765+
methods.any? { |method| method.full_name =~ regexp }
766+
end
767+
768+
return filtered unless filtered.empty?
769+
770+
found
771+
end
772+
756773
##
757774
# Yields items matching +name+ including the store they were found in, the
758775
# class being searched for, the class they were found in (an ancestor) the
@@ -948,10 +965,10 @@ def load_methods_matching name
948965
methods = []
949966

950967
methods << load_method(store, :class_methods, ancestor, '::', method) if
951-
types == :class or types == :both
968+
[:class, :both].include? types
952969

953970
methods << load_method(store, :instance_methods, ancestor, '#', method) if
954-
types == :instance or types == :both
971+
[:instance, :both].include? types
955972

956973
found << [store, methods.compact]
957974
end
@@ -970,6 +987,21 @@ def method_type selector
970987
end
971988
end
972989

990+
##
991+
# Returns a regular expression for +name+ that will match an
992+
# RDoc::AnyMethod's name.
993+
994+
def name_regexp name
995+
klass, type, name = parse_name name
996+
997+
case type
998+
when '#', '::' then
999+
/^#{klass}#{type}#{name}$/
1000+
else
1001+
/^#{klass}(#|::)#{name}$/
1002+
end
1003+
end
1004+
9731005
##
9741006
# Paginates output through a pager program.
9751007

@@ -996,7 +1028,7 @@ def paging?
9961028
end
9971029

9981030
##
999-
# Extract the class, selector and method name parts from +name+ like
1031+
# Extracts the class, selector and method name parts from +name+ like
10001032
# Foo::Bar#baz.
10011033
#
10021034
# NOTE: Given Foo::Bar, Bar is considered a class even though it may be a

test/test_rdoc_ri_driver.rb

+58
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,16 @@ def test_display_method_inherited
390390
assert_match %r%^=== Implementation from Foo%, out
391391
end
392392

393+
def test_display_method_overriden
394+
util_multi_store
395+
396+
out, = capture_io do
397+
@driver.display_method 'Bar#override'
398+
end
399+
400+
refute_match %r%must not be displayed%, out
401+
end
402+
393403
def test_display_name_not_found_class
394404
util_store
395405

@@ -495,6 +505,32 @@ def test_find_methods_method
495505
assert_equal expected, items
496506
end
497507

508+
def test_filter_methods
509+
util_multi_store
510+
511+
name = 'Bar#override'
512+
513+
found = @driver.load_methods_matching name
514+
515+
sorted = @driver.filter_methods found, name
516+
517+
expected = [[@store2, [@override]]]
518+
519+
assert_equal expected, sorted
520+
end
521+
522+
def test_filter_methods_not_found
523+
util_multi_store
524+
525+
name = 'Bar#inherit'
526+
527+
found = @driver.load_methods_matching name
528+
529+
sorted = @driver.filter_methods found, name
530+
531+
assert_equal found, sorted
532+
end
533+
498534
def test_formatter
499535
tty = Object.new
500536
def tty.tty?() true; end
@@ -533,6 +569,16 @@ def test_method_type
533569
assert_equal :class, @driver.method_type('::')
534570
end
535571

572+
def test_name_regexp
573+
assert_equal /^RDoc::AnyMethod#new$/,
574+
@driver.name_regexp('RDoc::AnyMethod#new')
575+
assert_equal /^RDoc::AnyMethod::new$/,
576+
@driver.name_regexp('RDoc::AnyMethod::new')
577+
578+
assert_equal /^RDoc::AnyMethod(#|::)new$/,
579+
@driver.name_regexp('RDoc::AnyMethod.new')
580+
end
581+
536582
def test_list_known_classes
537583
util_store
538584

@@ -766,6 +812,7 @@ def util_multi_store
766812
@mAmbiguous = RDoc::NormalModule.new 'Ambiguous'
767813

768814
@cFoo = RDoc::NormalClass.new 'Foo'
815+
769816
@cBar = RDoc::NormalClass.new 'Bar'
770817
@cBar.superclass = 'Foo'
771818
@cFoo_Baz = RDoc::NormalClass.new 'Baz'
@@ -774,10 +821,15 @@ def util_multi_store
774821
@baz = RDoc::AnyMethod.new nil, 'baz'
775822
@cBar.add_method @baz
776823

824+
@override = RDoc::AnyMethod.new nil, 'override'
825+
@override.comment = 'must be displayed'
826+
@cBar.add_method @override
827+
777828
@store2.save_class @mAmbiguous
778829
@store2.save_class @cBar
779830
@store2.save_class @cFoo_Baz
780831

832+
@store2.save_method @cBar, @override
781833
@store2.save_method @cBar, @baz
782834

783835
@store2.save_cache
@@ -824,6 +876,11 @@ def util_store
824876
@inherit = RDoc::AnyMethod.new nil, 'inherit'
825877
@cFoo.add_method @inherit
826878

879+
# overriden by Bar in multi_store
880+
@overriden = RDoc::AnyMethod.new nil, 'override'
881+
@overriden.comment = 'must not be displayed'
882+
@cFoo.add_method @overriden
883+
827884
@store.save_class @cFoo
828885
@store.save_class @cFoo_Bar
829886
@store.save_class @cFoo_Baz
@@ -836,6 +893,7 @@ def util_store
836893
@store.save_method @cFoo_Bar, @attr
837894

838895
@store.save_method @cFoo, @inherit
896+
@store.save_method @cFoo, @overriden
839897

840898
@store.save_cache
841899

0 commit comments

Comments
 (0)