Skip to content

Commit

Permalink
Simplify switch statement in parse_statements.
Browse files Browse the repository at this point in the history
`:on_nl`, `:on_ignored_nl`, `:on_comment`, `:on_embdoc` are handled by a
single `when` branch. In this branch there are multiple `if` branches
handling `:on_nl` and `:on_ignored_nl` separately from `:on_comment` and
`:on_embdoc`.

By having separate `when` branches for `:on_nl`/`:on_ignored_nl` and
`:on_comment`/`:on_embdoc` we can remove the `if` statements.
  • Loading branch information
p8 committed Dec 28, 2024
1 parent 265d572 commit 2960f64
Showing 1 changed file with 46 additions and 49 deletions.
95 changes: 46 additions & 49 deletions lib/rdoc/parser/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1795,67 +1795,64 @@ def parse_statements(container, single = NORMAL, current_method = nil,
non_comment_seen = true unless (:on_comment == tk[:kind] or :on_embdoc == tk[:kind])

case tk[:kind]
when :on_nl, :on_ignored_nl, :on_comment, :on_embdoc then
if :on_nl == tk[:kind] or :on_ignored_nl == tk[:kind]
skip_tkspace
tk = get_tk
else
past_tokens = @read.size > 1 ? @read[0..-2] : []
nl_position = 0
past_tokens.reverse.each_with_index do |read_tk, i|
if read_tk =~ /^\n$/ then
nl_position = (past_tokens.size - 1) - i
break
elsif read_tk =~ /^#.*\n$/ then
nl_position = ((past_tokens.size - 1) - i) + 1
break
end
end
comment_only_line = past_tokens[nl_position..-1].all?{ |c| c =~ /^\s+$/ }
unless comment_only_line then
tk = get_tk
when :on_nl, :on_ignored_nl then
skip_tkspace

keep_comment = true
container.current_line_visibility = nil

when :on_comment, :on_embdoc then
past_tokens = @read.size > 1 ? @read[0..-2] : []
nl_position = 0
past_tokens.reverse.each_with_index do |read_tk, i|
if read_tk =~ /^\n$/ then
nl_position = (past_tokens.size - 1) - i
break
elsif read_tk =~ /^#.*\n$/ then
nl_position = ((past_tokens.size - 1) - i) + 1
break
end
end
comment_only_line = past_tokens[nl_position..-1].all?{ |c| c =~ /^\s+$/ }
unless comment_only_line then
tk = get_tk
end

if tk and (:on_comment == tk[:kind] or :on_embdoc == tk[:kind]) then
if non_comment_seen then
# Look for RDoc in a comment about to be thrown away
non_comment_seen = parse_comment container, tk, comment unless
comment.empty?
if non_comment_seen then
# Look for RDoc in a comment about to be thrown away
non_comment_seen = parse_comment container, tk, comment unless
comment.empty?

comment = ''
comment = RDoc::Encoding.change_encoding comment, @encoding if @encoding
end
comment = ''
comment = RDoc::Encoding.change_encoding comment, @encoding if @encoding
end

line_no = nil
while tk and (:on_comment == tk[:kind] or :on_embdoc == tk[:kind]) do
comment_body = retrieve_comment_body(tk)
line_no = tk[:line_no] if comment.empty?
comment += comment_body
comment << "\n" unless comment_body =~ /\n\z/

line_no = nil
while tk and (:on_comment == tk[:kind] or :on_embdoc == tk[:kind]) do
comment_body = retrieve_comment_body(tk)
line_no = tk[:line_no] if comment.empty?
comment += comment_body
comment << "\n" unless comment_body =~ /\n\z/

if comment_body.size > 1 && comment_body =~ /\n\z/ then
skip_tkspace_without_nl # leading spaces
end
tk = get_tk
if comment_body.size > 1 && comment_body =~ /\n\z/ then
skip_tkspace_without_nl # leading spaces
end
tk = get_tk
end

comment = new_comment comment, line_no
comment = new_comment comment, line_no

unless comment.empty? then
look_for_directives_in container, comment
unless comment.empty? then
look_for_directives_in container, comment

if container.done_documenting then
throw :eof if RDoc::TopLevel === container
container.ongoing_visibility = save_visibility
end
if container.done_documenting then
throw :eof if RDoc::TopLevel === container
container.ongoing_visibility = save_visibility
end

keep_comment = true
else
non_comment_seen = true
end

keep_comment = true

unget_tk tk
keep_comment = true
container.current_line_visibility = nil
Expand Down

0 comments on commit 2960f64

Please sign in to comment.