| 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/dart/ast/token.dart'; | 8 import 'package:analyzer/dart/ast/token.dart'; |
| 9 import 'package:analyzer/src/generated/source.dart'; | 9 import 'package:analyzer/src/generated/source.dart'; |
| 10 | 10 |
| (...skipping 2019 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2030 /// Parameter lists with trailing commas are formatted differently from | 2030 /// Parameter lists with trailing commas are formatted differently from |
| 2031 /// regular parameter lists. They are treated more like collection literals. | 2031 /// regular parameter lists. They are treated more like collection literals. |
| 2032 /// | 2032 /// |
| 2033 /// We don't reuse [_visitCollectionLiteral] here because there are enough | 2033 /// We don't reuse [_visitCollectionLiteral] here because there are enough |
| 2034 /// weird differences around optional parameters that it's easiest just to | 2034 /// weird differences around optional parameters that it's easiest just to |
| 2035 /// give them their own method. | 2035 /// give them their own method. |
| 2036 void _visitTrailingCommaParameterList(FormalParameterList parameters) { | 2036 void _visitTrailingCommaParameterList(FormalParameterList parameters) { |
| 2037 // Can't have a trailing comma if there are no parameters. | 2037 // Can't have a trailing comma if there are no parameters. |
| 2038 assert(parameters.parameters.isNotEmpty); | 2038 assert(parameters.parameters.isNotEmpty); |
| 2039 | 2039 |
| 2040 _metadataRules.add(new MetadataRule()); |
| 2041 |
| 2040 // Always split the parameters. | 2042 // Always split the parameters. |
| 2041 builder.startRule(new Rule.hard()); | 2043 builder.startRule(new Rule.hard()); |
| 2042 | 2044 |
| 2043 token(parameters.leftParenthesis); | 2045 token(parameters.leftParenthesis); |
| 2044 | 2046 |
| 2045 // Find the parameter immediately preceding the optional parameters (if | 2047 // Find the parameter immediately preceding the optional parameters (if |
| 2046 // there are any). | 2048 // there are any). |
| 2047 FormalParameter lastRequired; | 2049 FormalParameter lastRequired; |
| 2048 for (var i = 0; i < parameters.parameters.length; i++) { | 2050 for (var i = 0; i < parameters.parameters.length; i++) { |
| 2049 if (parameters.parameters[i] is DefaultFormalParameter) { | 2051 if (parameters.parameters[i] is DefaultFormalParameter) { |
| 2050 if (i > 0) lastRequired = parameters.parameters[i - 1]; | 2052 if (i > 0) lastRequired = parameters.parameters[i - 1]; |
| 2051 break; | 2053 break; |
| 2052 } | 2054 } |
| 2053 } | 2055 } |
| 2054 | 2056 |
| 2055 // If all parameters are optional, put the "[" or "{" right after "(". | 2057 // If all parameters are optional, put the "[" or "{" right after "(". |
| 2056 if (parameters.parameters.first is DefaultFormalParameter) { | 2058 if (parameters.parameters.first is DefaultFormalParameter) { |
| 2057 token(parameters.leftDelimiter); | 2059 token(parameters.leftDelimiter); |
| 2058 } | 2060 } |
| 2059 | 2061 |
| 2060 // Process the parameters as a separate set of chunks. | 2062 // Process the parameters as a separate set of chunks. |
| 2061 builder = builder.startBlock(null); | 2063 builder = builder.startBlock(null); |
| 2062 | 2064 |
| 2063 for (var parameter in parameters.parameters) { | 2065 for (var parameter in parameters.parameters) { |
| 2064 builder.nestExpression(); | |
| 2065 visit(parameter); | 2066 visit(parameter); |
| 2066 | 2067 |
| 2067 // The comma after the parameter. | 2068 // The comma after the parameter. |
| 2068 if (parameter.endToken.next.type == TokenType.COMMA) { | 2069 if (parameter.endToken.next.type == TokenType.COMMA) { |
| 2069 token(parameter.endToken.next); | 2070 token(parameter.endToken.next); |
| 2070 } | 2071 } |
| 2071 | 2072 |
| 2072 // If the optional parameters start after this one, put the delimiter | 2073 // If the optional parameters start after this one, put the delimiter |
| 2073 // at the end of its line. | 2074 // at the end of its line. |
| 2074 if (parameter == lastRequired) { | 2075 if (parameter == lastRequired) { |
| 2075 space(); | 2076 space(); |
| 2076 token(parameters.leftDelimiter); | 2077 token(parameters.leftDelimiter); |
| 2077 lastRequired = null; | 2078 lastRequired = null; |
| 2078 } | 2079 } |
| 2079 | 2080 |
| 2080 builder.unnest(); | |
| 2081 newline(); | 2081 newline(); |
| 2082 } | 2082 } |
| 2083 | 2083 |
| 2084 // Put comments before the closing ")", "]", or "}" inside the block. | 2084 // Put comments before the closing ")", "]", or "}" inside the block. |
| 2085 var firstDelimiter = | 2085 var firstDelimiter = |
| 2086 parameters.rightDelimiter ?? parameters.rightParenthesis; | 2086 parameters.rightDelimiter ?? parameters.rightParenthesis; |
| 2087 writePrecedingCommentsAndNewlines(firstDelimiter); | 2087 writePrecedingCommentsAndNewlines(firstDelimiter); |
| 2088 builder = builder.endBlock(null, forceSplit: true); | 2088 builder = builder.endBlock(null, forceSplit: true); |
| 2089 builder.endRule(); | 2089 builder.endRule(); |
| 2090 | 2090 |
| 2091 _metadataRules.removeLast(); |
| 2092 |
| 2091 // Now write the delimiter itself. | 2093 // Now write the delimiter itself. |
| 2092 _writeText(firstDelimiter.lexeme, firstDelimiter.offset); | 2094 _writeText(firstDelimiter.lexeme, firstDelimiter.offset); |
| 2093 if (firstDelimiter != parameters.rightParenthesis) { | 2095 if (firstDelimiter != parameters.rightParenthesis) { |
| 2094 token(parameters.rightParenthesis); | 2096 token(parameters.rightParenthesis); |
| 2095 } | 2097 } |
| 2096 } | 2098 } |
| 2097 | 2099 |
| 2098 /// Gets the cost to split at an assignment (or `:` in the case of a named | 2100 /// Gets the cost to split at an assignment (or `:` in the case of a named |
| 2099 /// default value) with the given [rightHandSide]. | 2101 /// default value) with the given [rightHandSide]. |
| 2100 /// | 2102 /// |
| (...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2566 /// Gets the 1-based line number that the beginning of [token] lies on. | 2568 /// Gets the 1-based line number that the beginning of [token] lies on. |
| 2567 int _startLine(Token token) => _lineInfo.getLocation(token.offset).lineNumber; | 2569 int _startLine(Token token) => _lineInfo.getLocation(token.offset).lineNumber; |
| 2568 | 2570 |
| 2569 /// Gets the 1-based line number that the end of [token] lies on. | 2571 /// Gets the 1-based line number that the end of [token] lies on. |
| 2570 int _endLine(Token token) => _lineInfo.getLocation(token.end).lineNumber; | 2572 int _endLine(Token token) => _lineInfo.getLocation(token.end).lineNumber; |
| 2571 | 2573 |
| 2572 /// Gets the 1-based column number that the beginning of [token] lies on. | 2574 /// Gets the 1-based column number that the beginning of [token] lies on. |
| 2573 int _startColumn(Token token) => | 2575 int _startColumn(Token token) => |
| 2574 _lineInfo.getLocation(token.offset).columnNumber; | 2576 _lineInfo.getLocation(token.offset).columnNumber; |
| 2575 } | 2577 } |
| OLD | NEW |