Skip to content

Fix inline comments on loop and conditional predicates #165

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
Oct 4, 2022
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
2 changes: 2 additions & 0 deletions lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6004,6 +6004,8 @@ def format(q)
q.group(0, "->") do
if params.is_a?(Paren)
q.format(params) unless params.contents.empty?
elsif params.empty? && params.comments.any?
q.format(params)
elsif !params.empty?
q.group do
q.text("(")
Expand Down
45 changes: 27 additions & 18 deletions lib/syntax_tree/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ def find_next_statement_start(position)
def on_BEGIN(statements)
lbrace = find_token(LBrace)
rbrace = find_token(RBrace)
start_char = find_next_statement_start(lbrace.location.end_char)

start_char = find_next_statement_start(lbrace.location.end_char)
statements.bind(
start_char,
start_char - line_counts[lbrace.location.start_line - 1].start,
Expand Down Expand Up @@ -340,8 +340,8 @@ def on_CHAR(value)
def on_END(statements)
lbrace = find_token(LBrace)
rbrace = find_token(RBrace)
start_char = find_next_statement_start(lbrace.location.end_char)

start_char = find_next_statement_start(lbrace.location.end_char)
statements.bind(
start_char,
start_char - line_counts[lbrace.location.start_line - 1].start,
Expand Down Expand Up @@ -831,8 +831,8 @@ def on_brace_block(block_var, statements)
lbrace = find_token(LBrace)
rbrace = find_token(RBrace)
location = (block_var || lbrace).location
start_char = find_next_statement_start(location.end_char)

start_char = find_next_statement_start(location.end_char)
statements.bind(
start_char,
start_char - line_counts[location.start_line - 1].start,
Expand Down Expand Up @@ -1329,8 +1329,8 @@ def on_else(statements)

node = tokens[index]
ending = node.value == "end" ? tokens.delete_at(index) : node
start_char = find_next_statement_start(keyword.location.end_char)

start_char = find_next_statement_start(keyword.location.end_char)
statements.bind(
start_char,
start_char - line_counts[keyword.location.start_line - 1].start,
Expand All @@ -1355,9 +1355,10 @@ def on_elsif(predicate, statements, consequent)
beginning = find_token(Kw, "elsif")
ending = consequent || find_token(Kw, "end")

start_char = find_next_statement_start(predicate.location.end_char)
statements.bind(
predicate.location.end_char,
predicate.location.end_column,
start_char,
start_char - line_counts[predicate.location.start_line - 1].start,
ending.location.start_char,
ending.location.start_column
)
Expand Down Expand Up @@ -1598,9 +1599,12 @@ def on_for(index, collection, statements)
tokens.delete(keyword)
end

start_char =
find_next_statement_start((keyword || collection).location.end_char)
statements.bind(
(keyword || collection).location.end_char,
(keyword || collection).location.end_column,
start_char,
start_char -
line_counts[(keyword || collection).location.end_line - 1].start,
ending.location.start_char,
ending.location.start_column
)
Expand Down Expand Up @@ -1778,9 +1782,10 @@ def on_if(predicate, statements, consequent)
beginning = find_token(Kw, "if")
ending = consequent || find_token(Kw, "end")

start_char = find_next_statement_start(predicate.location.end_char)
statements.bind(
predicate.location.end_char,
predicate.location.end_column,
start_char,
start_char - line_counts[predicate.location.end_line - 1].start,
ending.location.start_char,
ending.location.start_column
)
Expand Down Expand Up @@ -2024,9 +2029,10 @@ def on_lambda(params, statements)
closing = find_token(Kw, "end")
end

start_char = find_next_statement_start(opening.location.end_char)
statements.bind(
opening.location.end_char,
opening.location.end_column,
start_char,
start_char - line_counts[opening.location.end_line - 1].start,
closing.location.start_char,
closing.location.start_column
)
Expand Down Expand Up @@ -3456,9 +3462,10 @@ def on_unless(predicate, statements, consequent)
beginning = find_token(Kw, "unless")
ending = consequent || find_token(Kw, "end")

start_char = find_next_statement_start(predicate.location.end_char)
statements.bind(
predicate.location.end_char,
predicate.location.end_column,
start_char,
start_char - line_counts[predicate.location.end_line - 1].start,
ending.location.start_char,
ending.location.start_column
)
Expand Down Expand Up @@ -3498,9 +3505,10 @@ def on_until(predicate, statements)
end

# Update the Statements location information
start_char = find_next_statement_start(predicate.location.end_char)
statements.bind(
predicate.location.end_char,
predicate.location.end_column,
start_char,
start_char - line_counts[predicate.location.end_line - 1].start,
ending.location.start_char,
ending.location.start_column
)
Expand Down Expand Up @@ -3633,9 +3641,10 @@ def on_while(predicate, statements)
end

# Update the Statements location information
start_char = find_next_statement_start(predicate.location.end_char)
statements.bind(
predicate.location.end_char,
predicate.location.end_column,
start_char,
start_char - line_counts[predicate.location.end_line - 1].start,
ending.location.start_char,
ending.location.start_column
)
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/elsif.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@
else
qyz
end
%
if true
elsif false # comment1
# comment2
end
4 changes: 4 additions & 0 deletions test/fixtures/for.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@
for foo, in [[foo, bar]]
foo
end
%
for foo in bar # comment1
# comment2
end
4 changes: 4 additions & 0 deletions test/fixtures/if.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@
if (x = x + 1).to_i
x
end
%
if true # comment1
# comment2
end
4 changes: 4 additions & 0 deletions test/fixtures/lambda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@
)
) do
end
%
-> do # comment1
# comment2
end
4 changes: 4 additions & 0 deletions test/fixtures/unless.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@
unless foo
a ? b : c
end
%
unless true # comment1
# comment2
end
4 changes: 4 additions & 0 deletions test/fixtures/until.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@
until (foo += 1)
foo
end
%
until true # comment1
# comment2
end
4 changes: 4 additions & 0 deletions test/fixtures/while.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@
while (foo += 1)
foo
end
%
while true # comment1
# comment2
end