Repository: ruby/rdoc (Ruby)

Issue:
rdoc only parses one definition per file
I was trying to move lib/rubygems.rb module `Gem` definition to the top module definition, it's defined twice in the file. Only adding the docs to the second definition will get parsed by rdoc.

Here's a contrived example:

``` ruby
# omg
module Foo
end

# wtf
module Foo
end
```

If you generate the documentation for this file, you will only see the "wtf".

![double module](http://screencloud.net/img/screenshots/4e9dbffe9d456dd7f165fc2902859b7e.png)



Discussion:
This bug conflicts with updating an ri data store, see commit comment in @e033904




Patch:
diff --git a/lib/rdoc/class_module.rb b/lib/rdoc/class_module.rb
index 5881d6cf24..33a8baa6a6 100644
--- a/lib/rdoc/class_module.rb
+++ b/lib/rdoc/class_module.rb
@@ -136,7 +136,9 @@ def add_comment comment, location
                 normalize_comment comment
               end
 
-    @comment_location.delete_if { |(_, l)| l == location }
+    if location.parser == RDoc::Parser::C
+      @comment_location.delete_if { |(_, l)| l == location }
+    end
 
     @comment_location << [comment, location]
 


Test patch:
diff --git a/test/test_rdoc_class_module.rb b/test/test_rdoc_class_module.rb
index 7cbbcbcc52..503492fca5 100644
--- a/test/test_rdoc_class_module.rb
+++ b/test/test_rdoc_class_module.rb
@@ -42,7 +42,8 @@ def test_add_comment_duplicate
     cm.add_comment '# comment 1', tl1
     cm.add_comment '# comment 2', tl1
 
-    assert_equal [['comment 2', tl1]], cm.comment_location
+    assert_equal [['comment 1', tl1],
+                  ['comment 2', tl1]], cm.comment_location
   end
 
   def test_add_comment_stopdoc
diff --git a/test/test_rdoc_parser_c.rb b/test/test_rdoc_parser_c.rb
index 5341da16e2..77089a0ae4 100644
--- a/test/test_rdoc_parser_c.rb
+++ b/test/test_rdoc_parser_c.rb
@@ -357,6 +357,25 @@ def test_do_classes_class
     assert_equal "this is the Foo class", klass.comment.text
   end
 
+  def test_do_classes_duplicate_class
+    content = <<-EOF
+/* Document-class: Foo
+ * first
+ */
+VALUE cFoo = rb_define_class("Foo", rb_cObject);
+/* Document-class: Foo
+ * second
+ */
+VALUE cFoo = rb_define_class("Foo", rb_cObject);
+    EOF
+
+    klass = util_get_class content, 'cFoo'
+    assert_equal 1, klass.comment_location.size
+    first = klass.comment_location.first
+    first_comment = first[0]
+    assert_equal 'first', first_comment.text
+  end
+
   def test_do_classes_struct
     content = <<-EOF
 /* Document-class: Foo
diff --git a/test/test_rdoc_parser_ruby.rb b/test/test_rdoc_parser_ruby.rb
index e9e3fcf246..5d3cac66af 100644
--- a/test/test_rdoc_parser_ruby.rb
+++ b/test/test_rdoc_parser_ruby.rb
@@ -681,6 +681,32 @@ class Foo
     assert_equal @top_level, blah.file
   end
 
+  def test_parse_class_in_a_file_repeatedly
+    @filename = 'a.rb'
+    comment_a = RDoc::Comment.new "# aaa\n", @top_level
+    util_parser "class Foo\nend"
+    tk = @parser.get_tk
+    @parser.parse_class @top_level, RDoc::Parser::Ruby::NORMAL, tk, comment_a
+    comment_b = RDoc::Comment.new "# bbb\n", @top_level
+    util_parser "class Foo\nend"
+    tk = @parser.get_tk
+    @parser.parse_class @top_level, RDoc::Parser::Ruby::NORMAL, tk, comment_b
+
+    @filename = 'b.rb'
+    comment_c = RDoc::Comment.new "# ccc\n", @top_level
+    util_parser "class Foo\nend"
+    tk = @parser.get_tk
+    @parser.parse_class @top_level, RDoc::Parser::Ruby::NORMAL, tk, comment_c
+
+    foo = @top_level.classes.first
+    assert_equal 'Foo', foo.full_name
+    assert_equal [[comment_a, @top_level],
+                  [comment_b, @top_level],
+                  [comment_c, @top_level]], foo.comment_location
+    assert_equal [@top_level], foo.in_files
+    assert_equal 1, foo.line
+  end
+
   def test_parse_class_ghost_method_yields
     util_parser <<-CLASS
 class Foo
@@ -3393,6 +3419,7 @@ module Foo
     foo = @top_level.modules.first
 
     expected = [
+      RDoc::Comment.new('comment a', @top_level),
       RDoc::Comment.new('comment b', @top_level)
     ]