Skip to content

Retrieve embdoc body #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions lib/rdoc/parser/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,11 @@ def collect_first_comment
tk = get_tk

while tk && (:on_comment == tk[:kind] or :on_embdoc == tk[:kind])
if first_line and tk[:text] =~ /\A#!/ then
comment_body = retrieve_comment_body(tk)
if first_line and comment_body =~ /\A#!/ then
skip_tkspace
tk = get_tk
elsif first_line and tk[:text] =~ /\A#\s*-\*-/ then
elsif first_line and comment_body =~ /\A#\s*-\*-/ then
first_line = false
skip_tkspace
tk = get_tk
Expand All @@ -261,7 +262,7 @@ def collect_first_comment
first_comment_tk_kind = tk[:kind]

first_line = false
comment << tk[:text]
comment << comment_body
tk = get_tk

if :on_nl === tk then
Expand Down Expand Up @@ -1654,6 +1655,17 @@ def parse_rescue
end
end

##
# Retrieve comment body without =begin/=end

def retrieve_comment_body(tk)
if :on_embdoc == tk[:kind]
tk[:text].gsub(/\A=begin.*\n/, '').gsub(/=end\n?\z/, '')
else
tk[:text]
end
end

##
# The core of the Ruby parser.

Expand Down Expand Up @@ -1707,10 +1719,11 @@ def parse_statements(container, single = NORMAL, current_method = nil,
end

while tk and (:on_comment == tk[:kind] or :on_embdoc == tk[:kind]) do
comment += tk[:text]
comment += "\n" unless "\n" == tk[:text].chars.to_a.last
comment_body = retrieve_comment_body(tk)
comment += comment_body
comment += "\n" unless "\n" == comment_body.chars.to_a.last

if tk[:text].size > 1 && "\n" == tk[:text].chars.to_a.last then
if comment_body.size > 1 && "\n" == comment_body.chars.to_a.last then
skip_tkspace false # leading spaces
end
tk = get_tk
Expand Down
12 changes: 6 additions & 6 deletions test/test_rdoc_parser_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class C; end

comment = parser.collect_first_comment

assert_equal RDoc::Comment.new("=begin\nfirst\n=end\n", @top_level), comment
assert_equal RDoc::Comment.new("first\n", @top_level), comment
end

def test_get_class_or_module
Expand Down Expand Up @@ -3349,11 +3349,11 @@ def m() end

foo = @top_level.classes.first

assert_equal "=begin rdoc\nFoo comment\n=end", foo.comment.text
assert_equal 'Foo comment', foo.comment.text

m = foo.method_list.first

assert_equal "=begin\nm comment\n=end", m.comment.text
assert_equal 'm comment', m.comment.text
end

def test_scan_block_comment_nested # Issue #41
Expand All @@ -3375,7 +3375,7 @@ class Bar
foo = @top_level.modules.first

assert_equal 'Foo', foo.full_name
assert_equal "=begin rdoc\nfindmeindoc\n=end", foo.comment.text
assert_equal 'findmeindoc', foo.comment.text

bar = foo.classes.first

Expand Down Expand Up @@ -3423,12 +3423,12 @@ def lauren

foo = @top_level.classes.first

assert_equal "=begin rdoc\n\n= DESCRIPTION\n\nThis is a simple test class\n\n= RUMPUS\n\nIs a silly word\n\n=end",
assert_equal "= DESCRIPTION\n\nThis is a simple test class\n\n= RUMPUS\n\nIs a silly word",
foo.comment.text

m = foo.method_list.first

assert_equal "=begin rdoc\nA nice girl\n=end", m.comment.text
assert_equal 'A nice girl', m.comment.text
end

def test_scan_class_nested_nodoc
Expand Down