@@ -1390,190 +1390,126 @@ module Crystal
1390
1390
end
1391
1391
1392
1392
def format_def_args (args : Array , block_arg, splat_index, variadic, double_splat)
1393
- to_skip = 0
1394
-
1395
- # If there are no args, remove extra "()", if any
1396
- if args.empty?
1393
+ # If there are no args, remove extra "()"
1394
+ if args.empty? && ! block_arg && ! double_splat && ! variadic
1397
1395
if @token .type == :"("
1398
- prefix_size = @column + 1
1399
-
1400
- write " (" if block_arg || double_splat || variadic
1401
- next_token
1402
-
1403
- skip_space
1404
- if @token .type == :NEWLINE
1405
- write_line
1406
- write_indent(prefix_size)
1407
- skip_space_or_newline(prefix_size)
1408
- end
1409
-
1410
- if double_splat
1411
- write_token :"**"
1412
- double_splat.accept self
1413
- skip_space_or_newline
1414
- end
1415
-
1416
- if block_arg
1417
- if double_splat
1418
- write_token :","
1419
- found_comment = skip_space_or_newline
1420
- if found_comment
1421
- write_indent(prefix_size)
1422
- else
1423
- write " "
1424
- end
1425
- end
1426
- write_token :"&"
1427
- skip_space
1428
- to_skip += 1 if at_skip?
1429
- accept block_arg
1430
- skip_space_or_newline
1431
- end
1432
-
1433
- if variadic
1434
- skip_space_or_newline
1435
- write_token :"..."
1436
- skip_space_or_newline
1437
- end
1438
-
1396
+ next_token_skip_space_or_newline
1439
1397
check :")"
1440
1398
next_token
1441
- write " )" if block_arg || double_splat || variadic
1442
1399
end
1443
- else
1444
- prefix_size = @column + 1
1400
+ return 0
1401
+ end
1445
1402
1446
- old_indent = @indent
1447
- next_needs_indent = false
1448
- has_parentheses = false
1449
- found_comment = false
1403
+ # Count instance variable arguments. See `at_skip?`.
1404
+ to_skip = 0
1450
1405
1451
- if @token .type == :"("
1452
- has_parentheses = true
1453
- write " ("
1454
- next_token_skip_space
1455
- if @token .type == :NEWLINE
1456
- write_line
1457
- skip_space_or_newline(prefix_size)
1458
- next_needs_indent = true
1459
- end
1460
- skip_space_or_newline
1461
- else
1462
- write " ("
1463
- end
1406
+ wrote_newline = false
1407
+ found_first_newline = false
1464
1408
1465
- comma_written = false
1409
+ old_indent = @indent
1410
+ @indent = @column + 1
1466
1411
1467
- args.each_with_index do |arg , i |
1468
- if next_needs_indent
1469
- write_indent(prefix_size)
1470
- end
1412
+ write_token :"("
1413
+ skip_space
1414
+
1415
+ # When "(" follows newline, it turns on two spaces indentation mode.
1416
+ if @token .type == :NEWLINE
1417
+ @indent = old_indent + 2
1418
+ found_first_newline = true
1419
+ wrote_newline = true
1471
1420
1421
+ write_line
1422
+ next_token_skip_space_or_newline
1423
+ end
1424
+
1425
+ args.each_with_index do |arg , i |
1426
+ has_more = ! last?(i, args) || double_splat || block_arg || variadic
1427
+ wrote_newline = format_def_arg(wrote_newline, has_more) do
1472
1428
if i == splat_index
1473
1429
write_token :"*"
1474
1430
skip_space_or_newline
1431
+ next if arg.external_name.empty? # skip empty splat argument.
1475
1432
end
1476
1433
1477
- if i == splat_index && arg.external_name.empty?
1478
- # Nothing
1479
- else
1480
- indent(prefix_size, arg)
1481
- to_skip += 1 if @last_arg_is_skip
1482
- end
1483
-
1484
- skip_space
1485
-
1486
- if @token .type == :","
1487
- has_more = ! last?(i, args) || double_splat || block_arg
1488
-
1489
- if has_more
1490
- write " ,"
1491
- comma_written = true
1492
- end
1493
- next_token
1494
- found_comment = skip_space
1495
- if @token .type == :NEWLINE
1496
- if has_more
1497
- indent(prefix_size) { consume_newlines }
1498
- next_needs_indent = true
1499
- end
1500
- elsif found_comment
1501
- next_needs_indent = true
1502
- else
1503
- next_needs_indent = false
1504
- write " " if has_more
1505
- end
1506
- skip_space_or_newline
1507
- else
1508
- comma_written = false
1509
- end
1434
+ arg.accept self
1435
+ to_skip += 1 if @last_arg_is_skip
1510
1436
end
1437
+ end
1511
1438
1512
- if double_splat
1513
- if next_needs_indent
1514
- write_indent(prefix_size)
1515
- next_needs_indent = false
1516
- end
1517
- unless comma_written
1518
- write " , "
1519
- comma_written = true
1520
- end
1439
+ if double_splat
1440
+ wrote_newline = format_def_arg(wrote_newline, block_arg) do
1521
1441
write_token :"**"
1522
- double_splat.accept self
1523
1442
skip_space_or_newline
1524
- if block_arg
1525
- check :","
1526
- write " ,"
1527
- comma_written = true
1528
- found_comment = next_token_skip_space
1529
- if found_comment
1530
- next_needs_indent = true
1531
- found_comment = false
1532
- else
1533
- if @token .type == :NEWLINE
1534
- indent(prefix_size) { consume_newlines }
1535
- next_needs_indent = true
1536
- else
1537
- write " "
1538
- end
1539
- end
1540
- end
1443
+
1444
+ to_skip += 1 if at_skip?
1445
+ double_splat.accept self
1541
1446
end
1447
+ end
1542
1448
1543
- if block_arg
1544
- if next_needs_indent
1545
- write_indent(prefix_size)
1546
- next_needs_indent = false
1547
- end
1548
- unless comma_written
1549
- write " , "
1550
- comma_written = true
1551
- end
1449
+ if block_arg
1450
+ wrote_newline = format_def_arg(wrote_newline, false ) do
1552
1451
write_token :"&"
1553
- skip_space
1452
+ skip_space_or_newline
1453
+
1554
1454
to_skip += 1 if at_skip?
1555
- accept block_arg
1556
- skip_space
1455
+ block_arg.accept self
1557
1456
end
1457
+ end
1558
1458
1559
- if variadic
1560
- write_token " , " , :"..."
1561
- skip_space_or_newline
1459
+ if variadic
1460
+ wrote_newline = format_def_arg(wrote_newline, false ) do
1461
+ write_token :"..."
1562
1462
end
1463
+ end
1563
1464
1564
- if has_parentheses
1565
- skip_space_or_newline
1566
- write_indent(prefix_size) if found_comment
1567
- write_token :")"
1465
+ if found_first_newline && ! wrote_newline
1466
+ write_line
1467
+ wrote_newline = true
1468
+ end
1469
+ write_indent(found_first_newline ? old_indent : @indent ) if wrote_newline
1470
+ write_token :")"
1471
+
1472
+ @indent = old_indent
1473
+
1474
+ to_skip
1475
+ end
1476
+
1477
+ def format_def_arg (wrote_newline, has_more)
1478
+ write_indent if wrote_newline
1479
+
1480
+ yield
1481
+
1482
+ # Write "," before skipping spaces to prevent inserting comment between argument and comma.
1483
+ write " ," if has_more
1484
+
1485
+ just_wrote_newline = skip_space
1486
+ if @token .type == :NEWLINE
1487
+ if has_more
1488
+ consume_newlines
1489
+ just_wrote_newline = true
1568
1490
else
1569
- write_indent(prefix_size) if found_comment
1570
- write " ) "
1491
+ # `last: true` is needed to write newline and comment only if comment is found.
1492
+ just_wrote_newline = skip_space_or_newline( last: true )
1571
1493
end
1494
+ end
1572
1495
1573
- @indent = old_indent
1496
+ if @token .type == :","
1497
+ found_comment = next_token_skip_space
1498
+ if found_comment
1499
+ just_wrote_newline = true
1500
+ elsif @token .type == :NEWLINE
1501
+ if has_more && ! just_wrote_newline
1502
+ consume_newlines
1503
+ just_wrote_newline = true
1504
+ else
1505
+ just_wrote_newline |= skip_space_or_newline(last: true )
1506
+ end
1507
+ else
1508
+ write " " if has_more && ! just_wrote_newline
1509
+ end
1574
1510
end
1575
1511
1576
- to_skip
1512
+ just_wrote_newline
1577
1513
end
1578
1514
1579
1515
# The parser transforms `def foo(@x); end` to `def foo(x); @x = x; end` so if we
0 commit comments