Skip to content

Commit 5b80134

Browse files
committed
Fix inline comments on loop and conditional predicates
1 parent 2a28cd9 commit 5b80134

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

lib/syntax_tree/node.rb

+2
Original file line numberDiff line numberDiff line change
@@ -6004,6 +6004,8 @@ def format(q)
60046004
q.group(0, "->") do
60056005
if params.is_a?(Paren)
60066006
q.format(params) unless params.contents.empty?
6007+
elsif params.empty? && params.comments.any?
6008+
q.format(params)
60076009
elsif !params.empty?
60086010
q.group do
60096011
q.text("(")

lib/syntax_tree/parser.rb

+25-18
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,8 @@ def find_next_statement_start(position)
302302
def on_BEGIN(statements)
303303
lbrace = find_token(LBrace)
304304
rbrace = find_token(RBrace)
305-
start_char = find_next_statement_start(lbrace.location.end_char)
306305

306+
start_char = find_next_statement_start(lbrace.location.end_char)
307307
statements.bind(
308308
start_char,
309309
start_char - line_counts[lbrace.location.start_line - 1].start,
@@ -340,8 +340,8 @@ def on_CHAR(value)
340340
def on_END(statements)
341341
lbrace = find_token(LBrace)
342342
rbrace = find_token(RBrace)
343-
start_char = find_next_statement_start(lbrace.location.end_char)
344343

344+
start_char = find_next_statement_start(lbrace.location.end_char)
345345
statements.bind(
346346
start_char,
347347
start_char - line_counts[lbrace.location.start_line - 1].start,
@@ -831,8 +831,8 @@ def on_brace_block(block_var, statements)
831831
lbrace = find_token(LBrace)
832832
rbrace = find_token(RBrace)
833833
location = (block_var || lbrace).location
834-
start_char = find_next_statement_start(location.end_char)
835834

835+
start_char = find_next_statement_start(location.end_char)
836836
statements.bind(
837837
start_char,
838838
start_char - line_counts[location.start_line - 1].start,
@@ -1329,8 +1329,8 @@ def on_else(statements)
13291329

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

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

1358+
start_char = find_next_statement_start(predicate.location.end_char)
13581359
statements.bind(
1359-
predicate.location.end_char,
1360-
predicate.location.end_column,
1360+
start_char,
1361+
start_char - line_counts[predicate.location.start_line - 1].start,
13611362
ending.location.start_char,
13621363
ending.location.start_column
13631364
)
@@ -1598,9 +1599,10 @@ def on_for(index, collection, statements)
15981599
tokens.delete(keyword)
15991600
end
16001601

1602+
start_char = find_next_statement_start((keyword || collection).location.end_char)
16011603
statements.bind(
1602-
(keyword || collection).location.end_char,
1603-
(keyword || collection).location.end_column,
1604+
start_char,
1605+
start_char - line_counts[(keyword || collection).location.end_line - 1].start,
16041606
ending.location.start_char,
16051607
ending.location.start_column
16061608
)
@@ -1778,9 +1780,10 @@ def on_if(predicate, statements, consequent)
17781780
beginning = find_token(Kw, "if")
17791781
ending = consequent || find_token(Kw, "end")
17801782

1783+
start_char = find_next_statement_start(predicate.location.end_char)
17811784
statements.bind(
1782-
predicate.location.end_char,
1783-
predicate.location.end_column,
1785+
start_char,
1786+
start_char - line_counts[predicate.location.end_line - 1].start,
17841787
ending.location.start_char,
17851788
ending.location.start_column
17861789
)
@@ -2024,9 +2027,10 @@ def on_lambda(params, statements)
20242027
closing = find_token(Kw, "end")
20252028
end
20262029

2030+
start_char = find_next_statement_start(opening.location.end_char)
20272031
statements.bind(
2028-
opening.location.end_char,
2029-
opening.location.end_column,
2032+
start_char,
2033+
start_char - line_counts[opening.location.end_line - 1].start,
20302034
closing.location.start_char,
20312035
closing.location.start_column
20322036
)
@@ -3456,9 +3460,10 @@ def on_unless(predicate, statements, consequent)
34563460
beginning = find_token(Kw, "unless")
34573461
ending = consequent || find_token(Kw, "end")
34583462

3463+
start_char = find_next_statement_start(predicate.location.end_char)
34593464
statements.bind(
3460-
predicate.location.end_char,
3461-
predicate.location.end_column,
3465+
start_char,
3466+
start_char - line_counts[predicate.location.end_line - 1].start,
34623467
ending.location.start_char,
34633468
ending.location.start_column
34643469
)
@@ -3498,9 +3503,10 @@ def on_until(predicate, statements)
34983503
end
34993504

35003505
# Update the Statements location information
3506+
start_char = find_next_statement_start(predicate.location.end_char)
35013507
statements.bind(
3502-
predicate.location.end_char,
3503-
predicate.location.end_column,
3508+
start_char,
3509+
start_char - line_counts[predicate.location.end_line - 1].start,
35043510
ending.location.start_char,
35053511
ending.location.start_column
35063512
)
@@ -3633,9 +3639,10 @@ def on_while(predicate, statements)
36333639
end
36343640

36353641
# Update the Statements location information
3642+
start_char = find_next_statement_start(predicate.location.end_char)
36363643
statements.bind(
3637-
predicate.location.end_char,
3638-
predicate.location.end_column,
3644+
start_char,
3645+
start_char - line_counts[predicate.location.end_line - 1].start,
36393646
ending.location.start_char,
36403647
ending.location.start_column
36413648
)

0 commit comments

Comments
 (0)