Skip to content

Commit e935763

Browse files
committed
Updating Object in an ri data store now removes methods, etc. properly.
RDoc::Parser::Ruby lets the object model take care of document_self (mostly).
1 parent 01275ae commit e935763

File tree

9 files changed

+495
-43
lines changed

9 files changed

+495
-43
lines changed

History.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Minor enhancements
44
* RDoc::Parser::C can now discover methods on ENV and ARGF.
55
* RDoc::Parser::C now knows about rb_cSocket and rb_mDL.
6+
* Bug fixes
7+
* Updating Object in an ri data store with new data now removes methods,
8+
includes, constants and aliases.
69

710
=== 3.7 / 2011-06-27
811

lib/rdoc/class_module.rb

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def initialize(name, superclass = nil)
115115
# across multiple runs.
116116

117117
def add_comment comment, location
118-
return if comment.empty?
118+
return if comment.empty? or not document_self
119119

120120
original = comment
121121

@@ -328,7 +328,10 @@ def merge class_module
328328
@comment = @comment_location = document
329329
end
330330

331-
merge_collections attributes, class_module.attributes do |add, attr|
331+
cm = class_module
332+
other_files = cm.in_files
333+
334+
merge_collections attributes, cm.attributes, other_files do |add, attr|
332335
if add then
333336
add_attribute attr
334337
else
@@ -337,7 +340,7 @@ def merge class_module
337340
end
338341
end
339342

340-
merge_collections constants, class_module.constants do |add, const|
343+
merge_collections constants, cm.constants, other_files do |add, const|
341344
if add then
342345
add_constant const
343346
else
@@ -346,15 +349,15 @@ def merge class_module
346349
end
347350
end
348351

349-
merge_collections includes, class_module.includes do |add, incl|
352+
merge_collections includes, cm.includes, other_files do |add, incl|
350353
if add then
351354
add_include incl
352355
else
353356
@includes.delete incl
354357
end
355358
end
356359

357-
merge_collections method_list, class_module.method_list do |add, meth|
360+
merge_collections method_list, cm.method_list, other_files do |add, meth|
358361
if add then
359362
add_method meth
360363
else
@@ -367,15 +370,37 @@ def merge class_module
367370
end
368371

369372
##
370-
# Merges collection +mine+ with +other+ preferring other.
371-
372-
def merge_collections mine, other, &block # :nodoc:
373+
# Merges collection +mine+ with +other+ preferring other. +other_files+ is
374+
# used to help determine which items should be deleted.
375+
#
376+
# Yields whether the item should be added or removed (true or false) and the
377+
# item to be added or removed.
378+
#
379+
# merge_collections things, other.things, other.in_files do |add, thing|
380+
# if add then
381+
# # add the thing
382+
# else
383+
# # remove the thing
384+
# end
385+
# end
386+
387+
def merge_collections mine, other, other_files, &block # :nodoc:
373388
my_things = mine. group_by { |thing| thing.file }
374389
other_things = other.group_by { |thing| thing.file }
375390

391+
my_things.delete_if do |file, things|
392+
next false unless other_files.include? file
393+
394+
things.each do |thing|
395+
yield false, thing
396+
end
397+
398+
true
399+
end
400+
376401
other_things.each do |file, things|
377402
my_things[file].each { |thing| yield false, thing } if
378-
my_things.include? file
403+
my_things.include?(file)
379404

380405
things.each do |thing|
381406
yield true, thing

lib/rdoc/parser/ruby.rb

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ def parse_attr(context, single, tk, comment)
478478

479479
read_documentation_modifiers att, RDoc::ATTR_MODIFIERS
480480

481-
context.add_attribute att if att.document_self
481+
context.add_attribute att
482482

483483
@stats.add_attribute att
484484
else
@@ -499,6 +499,8 @@ def parse_attr_accessor(context, single, tk, comment)
499499

500500
tmp = RDoc::CodeObject.new
501501
read_documentation_modifiers tmp, RDoc::ATTR_MODIFIERS
502+
# TODO In most other places we let the context keep track of document_self
503+
# and add found items appropriately but here we do not. I'm not sure why.
502504
return unless tmp.document_self
503505

504506
case tk.name
@@ -557,7 +559,7 @@ def parse_alias(context, single, tk, comment)
557559
al.line = line_no
558560

559561
read_documentation_modifiers al, RDoc::ATTR_MODIFIERS
560-
context.add_alias al if al.document_self
562+
context.add_alias al
561563
@stats.add_alias al
562564

563565
al
@@ -633,7 +635,7 @@ def parse_class(container, single, tk, comment)
633635
cls.offset = offset
634636
cls.line = line_no
635637

636-
cls.add_comment comment, @top_level if cls.document_self
638+
cls.add_comment comment, @top_level
637639

638640
@top_level.add_to_classes_or_modules cls
639641
@stats.add_class cls
@@ -657,7 +659,7 @@ def parse_class(container, single, tk, comment)
657659

658660
# notify :nodoc: all if not a constant-named class/module
659661
# (and remove any comment)
660-
unless name =~ /\A(::)?[A-Z]/
662+
unless name =~ /\A(::)?[A-Z]/ then
661663
other.document_self = nil
662664
other.document_children = false
663665
other.clear_comment
@@ -758,7 +760,7 @@ def parse_constant(container, tk, comment)
758760
read_documentation_modifiers con, RDoc::CONSTANT_MODIFIERS
759761

760762
@stats.add_constant con
761-
container.add_constant con if con.document_self
763+
container.add_constant con
762764
true
763765
end
764766

@@ -797,7 +799,7 @@ def parse_comment(container, tk, comment)
797799

798800
return unless meth.name
799801

800-
container.add_method meth if meth.document_self
802+
container.add_method meth
801803

802804
meth.comment = comment
803805

@@ -818,7 +820,6 @@ def parse_comment(container, tk, comment)
818820
att.line = line_no
819821

820822
container.add_attribute att
821-
822823
@stats.add_attribute att
823824
end
824825

@@ -882,7 +883,6 @@ def parse_meta_attr(context, single, tk, comment)
882883

883884
tmp = RDoc::CodeObject.new
884885
read_documentation_modifiers tmp, RDoc::ATTR_MODIFIERS
885-
return unless tmp.document_self
886886

887887
if comment.sub!(/^# +:?(attr(_reader|_writer|_accessor)?): *(\S*).*?\n/i, '') then
888888
rw = case $1
@@ -969,7 +969,7 @@ def parse_meta_method(container, single, tk, comment)
969969

970970
extract_call_seq comment, meth
971971

972-
container.add_method meth if meth.document_self
972+
container.add_method meth
973973

974974
last_tk = tk
975975

@@ -1238,7 +1238,7 @@ def parse_module(container, single, tk, comment)
12381238
mod.record_location @top_level
12391239

12401240
read_documentation_modifiers mod, RDoc::CLASS_MODIFIERS
1241-
mod.add_comment comment, @top_level if mod.document_self
1241+
mod.add_comment comment, @top_level
12421242
parse_statements(mod)
12431243

12441244
@top_level.add_to_classes_or_modules mod
@@ -1341,23 +1341,15 @@ def parse_statements(container, single = NORMAL, current_method = nil,
13411341
end
13421342

13431343
when TkDEF then
1344-
if container.document_self then
1345-
parse_method container, single, tk, comment
1346-
else
1347-
nest += 1
1348-
end
1344+
parse_method container, single, tk, comment
13491345

13501346
when TkCONSTANT then
1351-
if container.document_self then
1352-
if not parse_constant container, tk, comment then
1353-
try_parse_comment = true
1354-
end
1347+
unless parse_constant container, tk, comment then
1348+
try_parse_comment = true
13551349
end
13561350

13571351
when TkALIAS then
1358-
if container.document_self and not current_method then
1359-
parse_alias container, single, tk, comment
1360-
end
1352+
parse_alias container, single, tk, comment unless current_method
13611353

13621354
when TkYIELD then
13631355
if current_method.nil? then
@@ -1395,12 +1387,11 @@ def parse_statements(container, single = NORMAL, current_method = nil,
13951387
when /^attr_(reader|writer|accessor)$/ then
13961388
parse_attr_accessor container, single, tk, comment
13971389
when 'alias_method' then
1398-
parse_alias container, single, tk, comment if
1399-
container.document_self
1390+
parse_alias container, single, tk, comment
14001391
when 'require', 'include' then
14011392
# ignore
14021393
else
1403-
if container.document_self and comment =~ /\A#\#$/ then
1394+
if comment =~ /\A#\#$/ then
14041395
case comment
14051396
when /^# +:?attr(_reader|_writer|_accessor)?:/ then
14061397
parse_meta_attr container, single, tk, comment
@@ -1523,11 +1514,12 @@ def parse_symbol_in_arg
15231514
end
15241515

15251516
##
1526-
# Parses statements at the toplevel in +container+
1517+
# Parses statements in the top-level +container+
15271518

15281519
def parse_top_level_statements(container)
15291520
comment = collect_first_comment
15301521
look_for_directives_in(container, comment)
1522+
# HACK move if to RDoc::Context#comment=
15311523
container.comment = comment if container.document_self unless comment.empty?
15321524
parse_statements container, NORMAL, nil, comment
15331525
end

lib/rdoc/ri/store.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,7 @@ def save_class klass
268268
path = class_file full_name
269269

270270
begin
271-
disk_klass = nil
272-
273-
open path, 'rb' do |io|
274-
disk_klass = Marshal.load io.read
275-
end
271+
disk_klass = load_class full_name
276272

277273
klass = disk_klass.merge klass
278274
rescue Errno::ENOENT

lib/rdoc/top_level.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ def == other
322322
# Adds +an_alias+ to +Object+ instead of +self+.
323323

324324
def add_alias(an_alias)
325+
object_class.record_location self
325326
return an_alias unless @document_self
326327
object_class.add_alias an_alias
327328
end
@@ -330,6 +331,7 @@ def add_alias(an_alias)
330331
# Adds +constant+ to +Object+ instead of +self+.
331332

332333
def add_constant(constant)
334+
object_class.record_location self
333335
return constant unless @document_self
334336
object_class.add_constant constant
335337
end
@@ -338,6 +340,7 @@ def add_constant(constant)
338340
# Adds +include+ to +Object+ instead of +self+.
339341

340342
def add_include(include)
343+
object_class.record_location self
341344
return include unless @document_self
342345
object_class.add_include include
343346
end
@@ -346,6 +349,7 @@ def add_include(include)
346349
# Adds +method+ to +Object+ instead of +self+.
347350

348351
def add_method(method)
352+
object_class.record_location self
349353
return method unless @document_self
350354
object_class.add_method method
351355
end

test/test_rdoc_class_module.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ def test_add_comment
4040
assert_equal "comment 1\n---\ncomment 2\n---\n* comment 3", cm.comment
4141
end
4242

43+
def test_add_comment_stopdoc
44+
tl = RDoc::TopLevel.new 'file.rb'
45+
46+
cm = RDoc::ClassModule.new 'Klass'
47+
cm.stop_doc
48+
49+
cm.add_comment '# comment 1', tl
50+
51+
assert_empty cm.comment
52+
end
53+
4354
def test_ancestors
4455
assert_equal [@parent], @child.ancestors
4556
end
@@ -258,6 +269,33 @@ def test_merge_attributes_version_0
258269
assert_equal expected, cm1.attributes.sort
259270
end
260271

272+
def test_merge_collections_drop
273+
tl = RDoc::TopLevel.new 'file'
274+
275+
cm1 = RDoc::ClassModule.new 'C'
276+
cm1.record_location tl
277+
278+
const = cm1.add_constant RDoc::Constant.new('CONST', nil, nil)
279+
const.record_location tl
280+
281+
cm2 = RDoc::ClassModule.new 'C'
282+
cm2.record_location tl
283+
284+
added = []
285+
removed = []
286+
287+
cm1.merge_collections cm1.constants, cm2.constants, cm2.in_files do |add, c|
288+
if add then
289+
added << c
290+
else
291+
removed << c
292+
end
293+
end
294+
295+
assert_empty added
296+
assert_equal [const], removed
297+
end
298+
261299
def test_merge_comment
262300
tl1 = RDoc::TopLevel.new 'one.rb'
263301
tl2 = RDoc::TopLevel.new 'two.rb'

0 commit comments

Comments
 (0)