Skip to content

Commit 759d1e4

Browse files
committed
Walk the ancestors of a class looking for a method in ri
1 parent 805494d commit 759d1e4

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

History.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
* N Major Enhancements:
44
* RDoc now knows about meta-programmed methods, see RDoc::Parser::Ruby
55
* Reorganized parsers under RDoc::Parser base class
6-
* ri now walks the ancestors of a class looking for a method
6+
* ri now walks the ancestors of a class looking for a method e.g. ri
7+
File#read displays documentation for IO#read (may require regeneration of
8+
ri data)
79
* N Minor Enhancements:
810
* Allow links to files
911
* Default options now taken from RDOCOPT environment variable

lib/rdoc/code_objects.rb

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,9 @@ def add_require(a_require)
315315

316316
def add_class_or_module(collection, class_type, name, superclass=nil)
317317
cls = collection[name]
318-
if cls
318+
319+
if cls then
320+
cls.superclass = superclass unless cls.module?
319321
puts "Reusing class/module #{name}" if $DEBUG_RDOC
320322
else
321323
cls = class_type.new(name, superclass)
@@ -551,7 +553,8 @@ def add_class_or_module(collection, class_type, name, superclass)
551553
cls = collection[name]
552554

553555
if cls then
554-
puts "Reusing class/module #{name}" #if $DEBUG_RDOC
556+
cls.superclass = superclass unless cls.module?
557+
puts "Reusing class/module #{cls.full_name}" if $DEBUG_RDOC
555558
else
556559
if class_type == NormalModule then
557560
all = @@all_modules
@@ -562,15 +565,11 @@ def add_class_or_module(collection, class_type, name, superclass)
562565
cls = all[name]
563566

564567
unless cls then
565-
cls = class_type.new(name, superclass)
568+
cls = class_type.new name, superclass
566569
all[name] = cls unless @done_documenting
567570
end
568571

569-
if NormalClass === cls and cls.superclass.nil? then
570-
cls.superclass = superclass
571-
end
572-
573-
puts "Adding class/module #{name} to #{@name}" if $DEBUG_RDOC
572+
puts "Adding class/module #{name} to #{full_name}" if $DEBUG_RDOC
574573

575574
collection[name] = cls unless @done_documenting
576575

@@ -632,8 +631,6 @@ class ClassModule < Context
632631

633632
attr_accessor :diagram
634633

635-
attr_writer :superclass
636-
637634
def initialize(name, superclass = nil)
638635
@name = name
639636
@diagram = nil
@@ -672,7 +669,8 @@ def module?
672669
end
673670

674671
##
675-
# The superclass of this class
672+
# Get the superclass of this class. Attempts to retrieve the superclass'
673+
# real name by following module nesting.
676674

677675
def superclass
678676
raise NoMethodError, "#{full_name} is a module" if module?
@@ -689,6 +687,17 @@ def superclass
689687
@superclass
690688
end
691689

690+
##
691+
# Set the superclass of this class
692+
693+
def superclass=(superclass)
694+
raise NoMethodError, "#{full_name} is a module" if module?
695+
696+
if @superclass.nil? or @superclass == 'Object' then
697+
@superclass = superclass
698+
end
699+
end
700+
692701
def to_s
693702
"#{self.class}: #{@name} #{@comment} #{super}"
694703
end
@@ -806,23 +815,24 @@ def inspect
806815
end
807816

808817
def param_seq
809-
p = params.gsub(/\s*\#.*/, '')
810-
p = p.tr("\n", " ").squeeze(" ")
811-
p = "(" + p + ")" unless p[0] == ?(
818+
params = params.gsub(/\s*\#.*/, '')
819+
params = params.tr("\n", " ").squeeze(" ")
820+
params = "(#{params})" unless p[0] == ?(
812821

813-
if (block = block_params)
814-
# If this method has explicit block parameters, remove any
815-
# explicit &block
816-
p.sub!(/,?\s*&\w+/)
822+
if block = block_params then # yes, =
823+
# If this method has explicit block parameters, remove any explicit
824+
# &block
825+
params.sub!(/,?\s*&\w+/)
817826

818827
block.gsub!(/\s*\#.*/, '')
819828
block = block.tr("\n", " ").squeeze(" ")
820829
if block[0] == ?(
821830
block.sub!(/^\(/, '').sub!(/\)/, '')
822831
end
823-
p << " {|#{block}| ...}"
832+
params << " { |#{block}| ... }"
824833
end
825-
p
834+
835+
params
826836
end
827837

828838
def to_s

lib/rdoc/ri/driver.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ def class_cache
303303
populate_class_cache class_cache, classes, true
304304
write_cache class_cache, class_cache_file_path
305305
end
306+
307+
@class_cache = RDoc::RI::Driver::Hash.convert @class_cache
308+
@class_cache
306309
end
307310

308311
def class_cache_file_path
@@ -375,12 +378,12 @@ def load_cache_for(klassname)
375378
##
376379
# Finds the next ancestor of +orig_klass+ after +klass+.
377380

378-
def lookup_ancestor(klass, orig_klass, ancestor = nil)
381+
def lookup_ancestor(klass, orig_klass)
379382
cache = class_cache[orig_klass]
380383

381384
return nil unless cache
382385

383-
ancestors ||= [orig_klass]
386+
ancestors = [orig_klass]
384387
ancestors.push(*cache.includes.map { |inc| inc['name'] })
385388
ancestors << cache.superclass
386389

@@ -452,6 +455,8 @@ def populate_class_cache(class_cache, classes, extension = false)
452455
desc["class_method_extensions"] = desc.delete "class_methods"
453456
end
454457

458+
klass = RDoc::RI::Driver::Hash.convert klass
459+
455460
klass.merge_enums desc
456461
klass["sources"] << cdesc
457462
end

0 commit comments

Comments
 (0)