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 1862 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1873 // Force all of the surrounding collections to split. | 1873 // Force all of the surrounding collections to split. |
1874 for (var i = 0; i < _collectionSplits.length; i++) { | 1874 for (var i = 0; i < _collectionSplits.length; i++) { |
1875 _collectionSplits[i] = true; | 1875 _collectionSplits[i] = true; |
1876 } | 1876 } |
1877 | 1877 |
1878 // Add this collection to the stack. | 1878 // Add this collection to the stack. |
1879 _collectionSplits.add(false); | 1879 _collectionSplits.add(false); |
1880 | 1880 |
1881 _startLiteralBody(leftBracket); | 1881 _startLiteralBody(leftBracket); |
1882 | 1882 |
1883 // Always use a hard rule to split the elements. The parent chunk of | |
1884 // the collection will handle the unsplit case, so this only comes | |
1885 // into play when the collection is split. | |
1886 var rule = new Rule.hard(); | |
1887 builder.startRule(rule); | |
1888 | |
1889 // If a collection contains a line comment, we assume it's a big complex | 1883 // If a collection contains a line comment, we assume it's a big complex |
1890 // blob of data with some documented structure. In that case, the user | 1884 // blob of data with some documented structure. In that case, the user |
1891 // probably broke the elements into lines deliberately, so preserve those. | 1885 // probably broke the elements into lines deliberately, so preserve those. |
1892 var preserveNewlines = _containsLineComments(elements, rightBracket); | 1886 var preserveNewlines = _containsLineComments(elements, rightBracket); |
1893 | 1887 |
| 1888 var rule; |
| 1889 var lineRule; |
| 1890 if (preserveNewlines) { |
| 1891 // Newlines are significant, so we'll explicitly write those. Elements |
| 1892 // on the same line all share an argument-list-like rule that allows |
| 1893 // splitting between zero, one, or all of them. This is faster in long |
| 1894 // lists than using individual splits after each element. |
| 1895 lineRule = new TypeArgumentRule(); |
| 1896 builder.startLazyRule(lineRule); |
| 1897 } else { |
| 1898 // Newlines aren't significant, so use a hard rule to split the elements. |
| 1899 // The parent chunk of the collection will handle the unsplit case, so |
| 1900 // this only comes into play when the collection is split. |
| 1901 rule = new Rule.hard(); |
| 1902 builder.startRule(rule); |
| 1903 } |
| 1904 |
1894 for (var element in elements) { | 1905 for (var element in elements) { |
1895 if (element != elements.first) { | 1906 if (element != elements.first) { |
1896 if (preserveNewlines) { | 1907 if (preserveNewlines) { |
| 1908 // See if the next element is on the next line. |
1897 if (_endLine(element.beginToken.previous) != | 1909 if (_endLine(element.beginToken.previous) != |
1898 _startLine(element.beginToken)) { | 1910 _startLine(element.beginToken)) { |
1899 oneOrTwoNewlines(); | 1911 oneOrTwoNewlines(); |
| 1912 |
| 1913 // Start a new rule for the new line. |
| 1914 builder.endRule(); |
| 1915 lineRule = new TypeArgumentRule(); |
| 1916 builder.startLazyRule(lineRule); |
1900 } else { | 1917 } else { |
1901 soloSplit(); | 1918 lineRule.beforeArgument(split()); |
1902 } | 1919 } |
1903 } else { | 1920 } else { |
1904 builder.split(nest: false, space: true); | 1921 builder.split(nest: false, space: true); |
1905 } | 1922 } |
1906 } | 1923 } |
1907 | 1924 |
1908 builder.nestExpression(); | 1925 builder.nestExpression(); |
1909 visit(element); | 1926 visit(element); |
1910 | 1927 |
1911 // The comma after the element. | 1928 // The comma after the element. |
(...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2397 /// Gets the 1-based line number that the beginning of [token] lies on. | 2414 /// Gets the 1-based line number that the beginning of [token] lies on. |
2398 int _startLine(Token token) => _lineInfo.getLocation(token.offset).lineNumber; | 2415 int _startLine(Token token) => _lineInfo.getLocation(token.offset).lineNumber; |
2399 | 2416 |
2400 /// Gets the 1-based line number that the end of [token] lies on. | 2417 /// Gets the 1-based line number that the end of [token] lies on. |
2401 int _endLine(Token token) => _lineInfo.getLocation(token.end).lineNumber; | 2418 int _endLine(Token token) => _lineInfo.getLocation(token.end).lineNumber; |
2402 | 2419 |
2403 /// Gets the 1-based column number that the beginning of [token] lies on. | 2420 /// Gets the 1-based column number that the beginning of [token] lies on. |
2404 int _startColumn(Token token) => | 2421 int _startColumn(Token token) => |
2405 _lineInfo.getLocation(token.offset).columnNumber; | 2422 _lineInfo.getLocation(token.offset).columnNumber; |
2406 } | 2423 } |
OLD | NEW |