Skip to content

Commit e139174

Browse files
authored
Merge pull request #165 from ruby-syntax-tree/fix-164
Fix inline comments on loop and conditional predicates
2 parents 2a28cd9 + 791120c commit e139174

File tree

9 files changed

+58
-18
lines changed

9 files changed

+58
-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

+27-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,12 @@ def on_for(index, collection, statements)
15981599
tokens.delete(keyword)
15991600
end
16001601

1602+
start_char =
1603+
find_next_statement_start((keyword || collection).location.end_char)
16011604
statements.bind(
1602-
(keyword || collection).location.end_char,
1603-
(keyword || collection).location.end_column,
1605+
start_char,
1606+
start_char -
1607+
line_counts[(keyword || collection).location.end_line - 1].start,
16041608
ending.location.start_char,
16051609
ending.location.start_column
16061610
)
@@ -1778,9 +1782,10 @@ def on_if(predicate, statements, consequent)
17781782
beginning = find_token(Kw, "if")
17791783
ending = consequent || find_token(Kw, "end")
17801784

1785+
start_char = find_next_statement_start(predicate.location.end_char)
17811786
statements.bind(
1782-
predicate.location.end_char,
1783-
predicate.location.end_column,
1787+
start_char,
1788+
start_char - line_counts[predicate.location.end_line - 1].start,
17841789
ending.location.start_char,
17851790
ending.location.start_column
17861791
)
@@ -2024,9 +2029,10 @@ def on_lambda(params, statements)
20242029
closing = find_token(Kw, "end")
20252030
end
20262031

2032+
start_char = find_next_statement_start(opening.location.end_char)
20272033
statements.bind(
2028-
opening.location.end_char,
2029-
opening.location.end_column,
2034+
start_char,
2035+
start_char - line_counts[opening.location.end_line - 1].start,
20302036
closing.location.start_char,
20312037
closing.location.start_column
20322038
)
@@ -3456,9 +3462,10 @@ def on_unless(predicate, statements, consequent)
34563462
beginning = find_token(Kw, "unless")
34573463
ending = consequent || find_token(Kw, "end")
34583464

3465+
start_char = find_next_statement_start(predicate.location.end_char)
34593466
statements.bind(
3460-
predicate.location.end_char,
3461-
predicate.location.end_column,
3467+
start_char,
3468+
start_char - line_counts[predicate.location.end_line - 1].start,
34623469
ending.location.start_char,
34633470
ending.location.start_column
34643471
)
@@ -3498,9 +3505,10 @@ def on_until(predicate, statements)
34983505
end
34993506

35003507
# Update the Statements location information
3508+
start_char = find_next_statement_start(predicate.location.end_char)
35013509
statements.bind(
3502-
predicate.location.end_char,
3503-
predicate.location.end_column,
3510+
start_char,
3511+
start_char - line_counts[predicate.location.end_line - 1].start,
35043512
ending.location.start_char,
35053513
ending.location.start_column
35063514
)
@@ -3633,9 +3641,10 @@ def on_while(predicate, statements)
36333641
end
36343642

36353643
# Update the Statements location information
3644+
start_char = find_next_statement_start(predicate.location.end_char)
36363645
statements.bind(
3637-
predicate.location.end_char,
3638-
predicate.location.end_column,
3646+
start_char,
3647+
start_char - line_counts[predicate.location.end_line - 1].start,
36393648
ending.location.start_char,
36403649
ending.location.start_column
36413650
)

test/fixtures/elsif.rb

+5
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@
1717
else
1818
qyz
1919
end
20+
%
21+
if true
22+
elsif false # comment1
23+
# comment2
24+
end

test/fixtures/for.rb

+4
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@
3838
for foo, in [[foo, bar]]
3939
foo
4040
end
41+
%
42+
for foo in bar # comment1
43+
# comment2
44+
end

test/fixtures/if.rb

+4
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,7 @@
6363
if (x = x + 1).to_i
6464
x
6565
end
66+
%
67+
if true # comment1
68+
# comment2
69+
end

test/fixtures/lambda.rb

+4
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,7 @@
7676
)
7777
) do
7878
end
79+
%
80+
-> do # comment1
81+
# comment2
82+
end

test/fixtures/unless.rb

+4
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@
3232
unless foo
3333
a ? b : c
3434
end
35+
%
36+
unless true # comment1
37+
# comment2
38+
end

test/fixtures/until.rb

+4
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@
2323
until (foo += 1)
2424
foo
2525
end
26+
%
27+
until true # comment1
28+
# comment2
29+
end

test/fixtures/while.rb

+4
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@
2323
while (foo += 1)
2424
foo
2525
end
26+
%
27+
while true # comment1
28+
# comment2
29+
end

0 commit comments

Comments
 (0)