Skip to content

Commit

Permalink
Distinguish between class methods and instance methods when using Doc…
Browse files Browse the repository at this point in the history
…ument-method. Issue ruby#36
  • Loading branch information
drbrain committed Jun 9, 2011
1 parent f79d2a2 commit b0ad845
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
2 changes: 2 additions & 0 deletions History.txt
Expand Up @@ -17,6 +17,8 @@
* RDoc::Markup::Formatter and subclasses now allow an optional +markup+
parameter for adding custom markup. The example in
RDoc::Markup::Formatter will now work. Issue #38 by tsilen.
* RDoc::Parser::C can now distinguish between class methods and instance
methods in Document-method. Issue #36 by Vincent Batts.

=== 3.6.1 / 2011-05-15

Expand Down
4 changes: 3 additions & 1 deletion lib/rdoc/generator/markup.rb
Expand Up @@ -42,7 +42,9 @@ def formatter
show_hash = RDoc::RDoc.current.options.show_hash
hyperlink_all = RDoc::RDoc.current.options.hyperlink_all
this = RDoc::Context === self ? self : @parent
@formatter = RDoc::Markup::ToHtmlCrossref.new this.path, this, show_hash, hyperlink_all

@formatter = RDoc::Markup::ToHtmlCrossref.new(this.path, this, show_hash,
hyperlink_all)
end

##
Expand Down
26 changes: 18 additions & 8 deletions lib/rdoc/parser/c.rb
Expand Up @@ -64,8 +64,17 @@
# [Document-variable: +name+]
# Documentation for the named +rb_define_variable+
#
# [Document-method: +name+]
# Documentation for the named method.
# [Document-method: +method_name+]
# Documentation for the named method. Use this when the method name is
# unambiguous.
#
# [Document-method: <tt>ClassName::method_name<tt>]
# Documentation for a singleton method in the given class. Use this when
# the method name alone is ambiguous.
#
# [Document-method: <tt>ClassName#method_name<tt>]
# Documentation for a instance method in the given class. Use this when the
# method name alone is ambiguous.
#
# [Document-attr: +name+]
# Documentation for the named attribute.
Expand Down Expand Up @@ -432,7 +441,7 @@ def find_body class_name, meth_name, meth_obj, file_content, quiet = false
# distinct (for example Kernel.hash and Kernel.object_id share the same
# implementation

override_comment = find_override_comment class_name, meth_obj.name
override_comment = find_override_comment class_name, meth_obj
comment = override_comment if override_comment

find_modifiers comment, meth_obj if comment
Expand Down Expand Up @@ -475,7 +484,7 @@ def find_body class_name, meth_name, meth_obj, file_content, quiet = false
warn "No definition for #{meth_name}" if @options.verbosity > 1
false
else # No body, but might still have an override comment
comment = find_override_comment class_name, meth_obj.name
comment = find_override_comment class_name, meth_obj

if comment then
find_modifiers comment, meth_obj
Expand Down Expand Up @@ -643,12 +652,13 @@ def find_modifiers comment, meth_obj
end

##
# Finds a <tt>Document-method</tt> override for +meth_name+ in +class_name+
# Finds a <tt>Document-method</tt> override for +meth_obj+ on +class_name+

def find_override_comment(class_name, meth_name)
name = Regexp.escape(meth_name)
def find_override_comment class_name, meth_obj
name = Regexp.escape meth_obj.name
prefix = Regexp.escape meth_obj.name_prefix

if @content =~ %r%Document-method:\s+#{class_name}(?:\.|::|#)#{name}\s*?\n((?>.*?\*/))%m then
if @content =~ %r%Document-method:\s+#{class_name}#{prefix}#{name}\s*?\n((?>.*?\*/))%m then
$1
elsif @content =~ %r%Document-method:\s#{name}\s*?\n((?>.*?\*/))%m then
$1
Expand Down
45 changes: 45 additions & 0 deletions test/test_rdoc_parser_c.rb
Expand Up @@ -923,6 +923,51 @@ def test_find_body_document_method_equals
assert_equal 'A comment', bar.comment
end

def test_find_body_document_method_same
content = <<-EOF
VALUE
s_bar() {
}
VALUE
bar() {
}
/*
* Document-method: Foo::bar
*
* a comment for Foo::bar
*/
/*
* Document-method: Foo#bar
*
* a comment for Foo#bar
*/
void
Init_Foo(void) {
VALUE foo = rb_define_class("Foo", rb_cObject);
rb_define_singleton_method(foo, "bar", s_bar, 0);
rb_define_method(foo, "bar", bar, 0);
}
EOF

klass = util_get_class content, 'foo'
assert_equal 2, klass.method_list.length

methods = klass.method_list.sort

s_bar = methods.first
assert_equal 'Foo::bar', s_bar.full_name
assert_equal "a comment for Foo::bar", s_bar.comment

bar = methods.last
assert_equal 'Foo#bar', bar.full_name
assert_equal "a comment for Foo#bar", bar.comment
end

def test_find_modifiers_call_seq
comment = <<-COMMENT
/* call-seq:
Expand Down

0 comments on commit b0ad845

Please sign in to comment.