Skip to content

Commit d7d58e6

Browse files
authored
Fix section comments with the prism parser (#1639)
The section expects a non-formatted comment. Closes #1638 Can be simplified once the ripper parser is removed. It currently passes the comment text as is and transforms it in the section class itself. The prism parser now does it outside of that class.
1 parent 05ed61c commit d7d58e6

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

lib/rdoc/code_object/context/section.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ def legacy_aref
106106
#
107107
# # :section: The title
108108
# # The body
109+
#
110+
#--
111+
# TODO Remove when the ripper parser has been removed
109112

110113
def extract_comment(comment)
111114
case comment

lib/rdoc/parser/prism_ruby.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,15 +473,28 @@ def parse_comment_text_to_directives(comment_text, start_line) # :nodoc:
473473
comment.line = start_line
474474
markup, = directives['markup']
475475
comment.format = markup&.downcase || @markup
476-
if (section, = directives['section'])
476+
if (section, directive_line = directives['section'])
477477
# If comment has :section:, it is not a documentable comment for a code object
478-
@container.set_current_section(section, comment.dup)
478+
comment.text = extract_section_comment(comment_text, directive_line - start_line)
479+
@container.set_current_section(section, comment)
479480
return
480481
end
481482
@preprocess.run_post_processes(comment, @container)
482483
[comment, directives]
483484
end
484485

486+
# Extracts the comment for this section from the normalized comment block.
487+
# Removes all lines before the line that contains :section:
488+
# If the comment also ends with the same content, remove it as well
489+
490+
def extract_section_comment(comment_text, prefix_line_count) # :nodoc:
491+
prefix = comment_text.lines[0...prefix_line_count].join
492+
comment_text.delete_prefix!(prefix)
493+
# Comment is already normalized and doesn't end with a newline
494+
comment_text.delete_suffix!(prefix.chomp)
495+
comment_text
496+
end
497+
485498
def slice_tokens(start_pos, end_pos) # :nodoc:
486499
start_index = @tokens.bsearch_index { |t| ([t.line_no, t.char_no] <=> start_pos) >= 0 }
487500
end_index = @tokens.bsearch_index { |t| ([t.line_no, t.char_no] <=> end_pos) >= 0 }

test/rdoc/parser/prism_ruby_test.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,41 @@ def test_look_for_directives_in_section
3636
assert_equal 'new section', section.title
3737
end
3838

39+
def test_section_with_divider
40+
util_parser <<~RUBY
41+
# DIVIDER
42+
# :section: section 1
43+
# foo
44+
# DIVIDER
45+
46+
# DIVIDER 1
47+
# DIVIDER 2
48+
# :section: section 2
49+
# foo
50+
# DIVIDER 1
51+
# DIVIDER 2
52+
53+
# DIVIDER TOP ONLY
54+
# :section: section 3
55+
# foo
56+
57+
# DIVIDER TOP ONLY
58+
# :section: section 4
59+
RUBY
60+
61+
section = @top_level.sections_hash['section 1']
62+
assert_equal "\n<p>foo</p>\n", section.description
63+
64+
section = @top_level.sections_hash['section 2']
65+
assert_equal "\n<p>foo</p>\n", section.description
66+
67+
section = @top_level.sections_hash['section 3']
68+
assert_equal "\n<p>foo</p>\n", section.description
69+
70+
section = @top_level.sections_hash['section 4']
71+
assert_equal '', section.description
72+
end
73+
3974
def test_look_for_directives_in_commented
4075
util_parser <<~RUBY
4176
# how to make a section:

0 commit comments

Comments
 (0)