OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library dart_style.src.source_visitor; | 5 library dart_style.src.source_visitor; |
6 | 6 |
7 import 'package:analyzer/analyzer.dart'; | 7 import 'package:analyzer/analyzer.dart'; |
8 import 'package:analyzer/src/generated/scanner.dart'; | 8 import 'package:analyzer/src/generated/scanner.dart'; |
9 import 'package:analyzer/src/generated/source.dart'; | 9 import 'package:analyzer/src/generated/source.dart'; |
10 | 10 |
(...skipping 700 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
711 space(); | 711 space(); |
712 | 712 |
713 // The "async" or "sync" keyword. | 713 // The "async" or "sync" keyword. |
714 token(node.keyword, after: space); | 714 token(node.keyword, after: space); |
715 | 715 |
716 // Try to keep the "(...) => " with the start of the body for anonymous | 716 // Try to keep the "(...) => " with the start of the body for anonymous |
717 // functions. | 717 // functions. |
718 if (_isInLambda(node)) builder.startSpan(); | 718 if (_isInLambda(node)) builder.startSpan(); |
719 | 719 |
720 token(node.functionDefinition); // "=>". | 720 token(node.functionDefinition); // "=>". |
721 soloSplit(Cost.arrow); | 721 |
| 722 // Split after the "=>", using the rule created before the parameters |
| 723 // by _visitBody(). |
| 724 split(); |
| 725 builder.endRule(); |
722 | 726 |
723 if (_isInLambda(node)) builder.endSpan(); | 727 if (_isInLambda(node)) builder.endSpan(); |
724 | 728 |
725 builder.startBlockArgumentNesting(); | 729 builder.startBlockArgumentNesting(); |
726 builder.startSpan(); | 730 builder.startSpan(); |
727 visit(node.expression); | 731 visit(node.expression); |
728 builder.endSpan(); | 732 builder.endSpan(); |
729 builder.endBlockArgumentNesting(); | 733 builder.endBlockArgumentNesting(); |
730 }); | 734 }); |
731 } | 735 } |
(...skipping 828 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1560 } | 1564 } |
1561 | 1565 |
1562 /// Visit the given function [parameters] followed by its [body], printing a | 1566 /// Visit the given function [parameters] followed by its [body], printing a |
1563 /// space before it if it's not empty. | 1567 /// space before it if it's not empty. |
1564 /// | 1568 /// |
1565 /// If [afterParameters] is provided, it is invoked between the parameters | 1569 /// If [afterParameters] is provided, it is invoked between the parameters |
1566 /// and body. (It's used for constructor initialization lists.) | 1570 /// and body. (It's used for constructor initialization lists.) |
1567 void _visitBody(FormalParameterList parameters, FunctionBody body, | 1571 void _visitBody(FormalParameterList parameters, FunctionBody body, |
1568 [afterParameters()]) { | 1572 [afterParameters()]) { |
1569 // If the body is "=>", add an extra level of indentation around the | 1573 // If the body is "=>", add an extra level of indentation around the |
1570 // parameters and the body. This ensures that if the parameters wrap, they | 1574 // parameters and a rule that spans the parameters and the "=>". This |
1571 // wrap more deeply than the "=>" does, as in: | 1575 // ensures that if the parameters wrap, they wrap more deeply than the "=>" |
| 1576 // does, as in: |
1572 // | 1577 // |
1573 // someFunction(parameter, | 1578 // someFunction(parameter, |
1574 // parameter, parameter) => | 1579 // parameter, parameter) => |
1575 // "the body"; | 1580 // "the body"; |
1576 if (body is ExpressionFunctionBody) builder.nestExpression(); | 1581 // |
| 1582 // Also, it ensures that if the parameters wrap, we split at the "=>" too |
| 1583 // to avoid: |
| 1584 // |
| 1585 // someFunction(parameter, |
| 1586 // parameter) => function( |
| 1587 // argument); |
| 1588 // |
| 1589 // This is confusing because it looks like those two lines are at the same |
| 1590 // level when they are actually unrelated. Splitting at "=>" forces: |
| 1591 // |
| 1592 // someFunction(parameter, |
| 1593 // parameter) => |
| 1594 // function( |
| 1595 // argument); |
| 1596 if (body is ExpressionFunctionBody) { |
| 1597 builder.nestExpression(); |
| 1598 |
| 1599 // This rule is ended by visitExpressionFunctionBody(). |
| 1600 builder.startRule(new SimpleRule(cost: Cost.arrow)); |
| 1601 } |
1577 | 1602 |
1578 if (parameters != null) { | 1603 if (parameters != null) { |
1579 builder.nestExpression(); | 1604 builder.nestExpression(); |
1580 | 1605 |
1581 visit(parameters); | 1606 visit(parameters); |
1582 if (afterParameters != null) afterParameters(); | 1607 if (afterParameters != null) afterParameters(); |
1583 | 1608 |
1584 builder.unnest(); | 1609 builder.unnest(); |
1585 } | 1610 } |
1586 | 1611 |
(...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2056 /// Gets the 1-based line number that the beginning of [token] lies on. | 2081 /// Gets the 1-based line number that the beginning of [token] lies on. |
2057 int _startLine(Token token) => _lineInfo.getLocation(token.offset).lineNumber; | 2082 int _startLine(Token token) => _lineInfo.getLocation(token.offset).lineNumber; |
2058 | 2083 |
2059 /// Gets the 1-based line number that the end of [token] lies on. | 2084 /// Gets the 1-based line number that the end of [token] lies on. |
2060 int _endLine(Token token) => _lineInfo.getLocation(token.end).lineNumber; | 2085 int _endLine(Token token) => _lineInfo.getLocation(token.end).lineNumber; |
2061 | 2086 |
2062 /// Gets the 1-based column number that the beginning of [token] lies on. | 2087 /// Gets the 1-based column number that the beginning of [token] lies on. |
2063 int _startColumn(Token token) => | 2088 int _startColumn(Token token) => |
2064 _lineInfo.getLocation(token.offset).columnNumber; | 2089 _lineInfo.getLocation(token.offset).columnNumber; |
2065 } | 2090 } |
OLD | NEW |