Skip to content

Commit 2d9be0f

Browse files
committed
Extract normalizing comment indent to a method
1 parent da0dff8 commit 2d9be0f

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

lib/rdoc/comment.rb

+19-9
Original file line numberDiff line numberDiff line change
@@ -353,16 +353,26 @@ def parse(text, filename, line_no, type, &include_callback)
353353
end
354354
line_no += read_lines
355355
end
356-
# normalize comment
357-
min_spaces = nil
358-
comment_lines.each do |l|
359-
next if l.match?(/\A\s*\z/)
360-
n = l[/\A */].size
361-
min_spaces = n if !min_spaces || n < min_spaces
356+
357+
normalized_comment = String.new(encoding: text.encoding) << normalize_comment_lines(comment_lines).join("\n")
358+
[normalized_comment, directives]
359+
end
360+
361+
# Remove preceding indent spaces and blank lines from the comment lines
362+
363+
private def normalize_comment_lines(lines)
364+
blank_line_regexp = /\A\s*\z/
365+
min_spaces = lines.map do |l|
366+
l[/\A */].size unless l.match?(blank_line_regexp)
367+
end.compact.min
368+
if min_spaces && min_spaces > 0
369+
lines = lines.map { |l| l[min_spaces..] || '' }
370+
else
371+
lines = lines.dup
362372
end
363-
comment_lines.map! { |l| l[min_spaces..] || '' } if min_spaces
364-
comment_lines.shift while comment_lines.first&.match?(/\A\s*\z/)
365-
[String.new(encoding: text.encoding) << comment_lines.join("\n"), directives]
373+
lines.shift while lines.first&.match?(blank_line_regexp)
374+
lines.pop while lines.last&.match?(blank_line_regexp)
375+
lines
366376
end
367377

368378
# Take value lines of multiline directive

0 commit comments

Comments
 (0)