Skip to content

Commit

Permalink
Filter ri method list. This is probably not the best solution
Browse files Browse the repository at this point in the history
  • Loading branch information
drbrain committed Jan 3, 2011
1 parent db119ed commit c5abcbd
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 4 deletions.
1 change: 1 addition & 0 deletions History.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* Restored parsing of block comments. RubyForge bug #28668 by Stefano Crocco.
* Metaprogrammed methods defined with blocks no longer confuse the ruby
parser. RubyForge bug #28370 by Erik Hollensbe.
* ri no longer displays all methods in the inheritance chain.

=== 3.2 / 2010-12-29

Expand Down
40 changes: 36 additions & 4 deletions lib/rdoc/ri/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -651,12 +651,14 @@ def display_method name

raise NotFoundError, name if found.empty?

filtered = filter_methods found, name

out = RDoc::Markup::Document.new

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

found.each do |store, methods|
filtered.each do |store, methods|
methods.each do |method|
out << RDoc::Markup::Paragraph.new("(from #{store.friendly_path})")

Expand Down Expand Up @@ -753,6 +755,21 @@ def expand_name name
"#{expand_class klass}#{selector}#{method}"
end

##
# Filters the methods in +found+ trying to find a match for +name+.

def filter_methods found, name
regexp = name_regexp name

filtered = found.find_all do |store, methods|
methods.any? { |method| method.full_name =~ regexp }
end

return filtered unless filtered.empty?

found
end

##
# Yields items matching +name+ including the store they were found in, the
# class being searched for, the class they were found in (an ancestor) the
Expand Down Expand Up @@ -948,10 +965,10 @@ def load_methods_matching name
methods = []

methods << load_method(store, :class_methods, ancestor, '::', method) if
types == :class or types == :both
[:class, :both].include? types

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

found << [store, methods.compact]
end
Expand All @@ -970,6 +987,21 @@ def method_type selector
end
end

##
# Returns a regular expression for +name+ that will match an
# RDoc::AnyMethod's name.

def name_regexp name
klass, type, name = parse_name name

case type
when '#', '::' then
/^#{klass}#{type}#{name}$/
else
/^#{klass}(#|::)#{name}$/
end
end

##
# Paginates output through a pager program.

Expand All @@ -996,7 +1028,7 @@ def paging?
end

##
# Extract the class, selector and method name parts from +name+ like
# Extracts the class, selector and method name parts from +name+ like
# Foo::Bar#baz.
#
# NOTE: Given Foo::Bar, Bar is considered a class even though it may be a
Expand Down
58 changes: 58 additions & 0 deletions test/test_rdoc_ri_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,16 @@ def test_display_method_inherited
assert_match %r%^=== Implementation from Foo%, out
end

def test_display_method_overriden
util_multi_store

out, = capture_io do
@driver.display_method 'Bar#override'
end

refute_match %r%must not be displayed%, out
end

def test_display_name_not_found_class
util_store

Expand Down Expand Up @@ -495,6 +505,32 @@ def test_find_methods_method
assert_equal expected, items
end

def test_filter_methods
util_multi_store

name = 'Bar#override'

found = @driver.load_methods_matching name

sorted = @driver.filter_methods found, name

expected = [[@store2, [@override]]]

assert_equal expected, sorted
end

def test_filter_methods_not_found
util_multi_store

name = 'Bar#inherit'

found = @driver.load_methods_matching name

sorted = @driver.filter_methods found, name

assert_equal found, sorted
end

def test_formatter
tty = Object.new
def tty.tty?() true; end
Expand Down Expand Up @@ -533,6 +569,16 @@ def test_method_type
assert_equal :class, @driver.method_type('::')
end

def test_name_regexp
assert_equal /^RDoc::AnyMethod#new$/,
@driver.name_regexp('RDoc::AnyMethod#new')
assert_equal /^RDoc::AnyMethod::new$/,
@driver.name_regexp('RDoc::AnyMethod::new')

assert_equal /^RDoc::AnyMethod(#|::)new$/,
@driver.name_regexp('RDoc::AnyMethod.new')
end

def test_list_known_classes
util_store

Expand Down Expand Up @@ -766,6 +812,7 @@ def util_multi_store
@mAmbiguous = RDoc::NormalModule.new 'Ambiguous'

@cFoo = RDoc::NormalClass.new 'Foo'

@cBar = RDoc::NormalClass.new 'Bar'
@cBar.superclass = 'Foo'
@cFoo_Baz = RDoc::NormalClass.new 'Baz'
Expand All @@ -774,10 +821,15 @@ def util_multi_store
@baz = RDoc::AnyMethod.new nil, 'baz'
@cBar.add_method @baz

@override = RDoc::AnyMethod.new nil, 'override'
@override.comment = 'must be displayed'
@cBar.add_method @override

@store2.save_class @mAmbiguous
@store2.save_class @cBar
@store2.save_class @cFoo_Baz

@store2.save_method @cBar, @override
@store2.save_method @cBar, @baz

@store2.save_cache
Expand Down Expand Up @@ -824,6 +876,11 @@ def util_store
@inherit = RDoc::AnyMethod.new nil, 'inherit'
@cFoo.add_method @inherit

# overriden by Bar in multi_store
@overriden = RDoc::AnyMethod.new nil, 'override'
@overriden.comment = 'must not be displayed'
@cFoo.add_method @overriden

@store.save_class @cFoo
@store.save_class @cFoo_Bar
@store.save_class @cFoo_Baz
Expand All @@ -836,6 +893,7 @@ def util_store
@store.save_method @cFoo_Bar, @attr

@store.save_method @cFoo, @inherit
@store.save_method @cFoo, @overriden

@store.save_cache

Expand Down

0 comments on commit c5abcbd

Please sign in to comment.