Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1446)

Side by Side Diff: pkg/analyzer/lib/src/services/formatter_impl.dart

Issue 105613003: Fix for map and list literal indentation (dartbug.com/15453). (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/analyzer/test/services/formatter_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 formatter_impl; 5 library formatter_impl;
6 6
7 import 'dart:math'; 7 import 'dart:math';
8 8
9 import 'package:analyzer/analyzer.dart'; 9 import 'package:analyzer/analyzer.dart';
10 import 'package:analyzer/src/generated/java_core.dart' show CharSequence; 10 import 'package:analyzer/src/generated/java_core.dart' show CharSequence;
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 348
349 /// Cached previous token for calculating preceding whitespace. 349 /// Cached previous token for calculating preceding whitespace.
350 Token previousToken; 350 Token previousToken;
351 351
352 /// A flag to indicate that a newline should be emitted before the next token. 352 /// A flag to indicate that a newline should be emitted before the next token.
353 bool needsNewline = false; 353 bool needsNewline = false;
354 354
355 /// A counter for spaces that should be emitted preceding the next token. 355 /// A counter for spaces that should be emitted preceding the next token.
356 int leadingSpaces = 0; 356 int leadingSpaces = 0;
357 357
358 /// A flag to specify whether line-leading spaces should be preserved (and
359 /// addded to the indent level).
360 bool allowLineLeadingSpaces;
361
358 /// Used for matching EOL comments 362 /// Used for matching EOL comments
359 final twoSlashes = new RegExp(r'//[^/]'); 363 final twoSlashes = new RegExp(r'//[^/]');
360 364
361 /// Original pre-format selection information (may be null). 365 /// Original pre-format selection information (may be null).
362 final Selection preSelection; 366 final Selection preSelection;
363 367
364 final bool codeTransforms; 368 final bool codeTransforms;
365 369
366 /// Post format selection information. 370 /// Post format selection information.
367 Selection selection; 371 Selection selection;
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
591 visitConstructorInitializers(ConstructorDeclaration node) { 595 visitConstructorInitializers(ConstructorDeclaration node) {
592 newlines(); 596 newlines();
593 indent(2); 597 indent(2);
594 token(node.separator /* : */); 598 token(node.separator /* : */);
595 space(); 599 space();
596 for (var i = 0; i < node.initializers.length; i++) { 600 for (var i = 0; i < node.initializers.length; i++) {
597 if (i > 0) { 601 if (i > 0) {
598 // preceding comma 602 // preceding comma
599 token(node.initializers[i].beginToken.previous); 603 token(node.initializers[i].beginToken.previous);
600 newlines(); 604 newlines();
601 space(2); 605 space(n: 2, allowLineLeading: true);
602 } 606 }
603 node.initializers[i].accept(this); 607 node.initializers[i].accept(this);
604 } 608 }
605 unindent(2); 609 unindent(2);
606 } 610 }
607 611
608 visitConstructorRedirects(ConstructorDeclaration node) { 612 visitConstructorRedirects(ConstructorDeclaration node) {
609 token(node.separator /* = */, precededBy: space, followedBy: space); 613 token(node.separator /* = */, precededBy: space, followedBy: space);
610 visitCommaSeparatedNodes(node.initializers); 614 visitCommaSeparatedNodes(node.initializers);
611 visit(node.redirectedConstructor); 615 visit(node.redirectedConstructor);
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after
929 } 933 }
930 934
931 visitLibraryIdentifier(LibraryIdentifier node) { 935 visitLibraryIdentifier(LibraryIdentifier node) {
932 append(node.name); 936 append(node.name);
933 } 937 }
934 938
935 visitListLiteral(ListLiteral node) { 939 visitListLiteral(ListLiteral node) {
936 modifier(node.constKeyword); 940 modifier(node.constKeyword);
937 visit(node.typeArguments); 941 visit(node.typeArguments);
938 token(node.leftBracket); 942 token(node.leftBracket);
943 indent();
939 visitCommaSeparatedNodes(node.elements); 944 visitCommaSeparatedNodes(node.elements);
940 optionalTrailingComma(node.rightBracket); 945 optionalTrailingComma(node.rightBracket);
946 unindent();
941 token(node.rightBracket); 947 token(node.rightBracket);
942 } 948 }
943 949
944 visitMapLiteral(MapLiteral node) { 950 visitMapLiteral(MapLiteral node) {
945 modifier(node.constKeyword); 951 modifier(node.constKeyword);
946 visitNode(node.typeArguments, followedBy: space); 952 visitNode(node.typeArguments, followedBy: space);
947 token(node.leftBracket); 953 token(node.leftBracket);
954 indent();
948 visitCommaSeparatedNodes(node.entries); 955 visitCommaSeparatedNodes(node.entries);
949 optionalTrailingComma(node.rightBracket); 956 optionalTrailingComma(node.rightBracket);
957 unindent();
950 token(node.rightBracket); 958 token(node.rightBracket);
951 } 959 }
952 960
953 visitMapLiteralEntry(MapLiteralEntry node) { 961 visitMapLiteralEntry(MapLiteralEntry node) {
954 visit(node.key); 962 visit(node.key);
955 token(node.separator); 963 token(node.separator);
956 space(); 964 space();
957 visit(node.value); 965 visit(node.value);
958 } 966 }
959 967
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after
1344 append(token.lexeme); 1352 append(token.lexeme);
1345 if (followedBy != null) { 1353 if (followedBy != null) {
1346 followedBy(); 1354 followedBy();
1347 } 1355 }
1348 previousToken = token; 1356 previousToken = token;
1349 } 1357 }
1350 } 1358 }
1351 1359
1352 emitSpaces() { 1360 emitSpaces() {
1353 while (leadingSpaces > 0) { 1361 while (leadingSpaces > 0) {
1354 writer.print(' '); 1362 if (!writer.currentLine.isWhitespace() || allowLineLeadingSpaces) {
1363 writer.print(' ');
1364 }
1365 allowLineLeadingSpaces = false;
1355 leadingSpaces--; 1366 leadingSpaces--;
1356 } 1367 }
1357 } 1368 }
1358 1369
1359 checkForSelectionUpdate(Token token) { 1370 checkForSelectionUpdate(Token token) {
1360 // Cache the first token on or AFTER the selection offset 1371 // Cache the first token on or AFTER the selection offset
1361 if (preSelection != null && selection == null) { 1372 if (preSelection != null && selection == null) {
1362 // Check for overshots 1373 // Check for overshots
1363 var overshot = token.offset - preSelection.offset; 1374 var overshot = token.offset - preSelection.offset;
1364 if (overshot >= 0) { 1375 if (overshot >= 0) {
1365 //TODO(pquitslund): update length (may need truncating) 1376 //TODO(pquitslund): update length (may need truncating)
1366 selection = new Selection( 1377 selection = new Selection(
1367 writer.toString().length + leadingSpaces - overshot, 1378 writer.toString().length + leadingSpaces - overshot,
1368 preSelection.length); 1379 preSelection.length);
1369 } 1380 }
1370 } 1381 }
1371 } 1382 }
1372 1383
1373 /// Emit a non-breakable space. 1384 /// Emit a non-breakable space. If [allowLineLeading] is specified, spaces
1374 space([n = 1]) { 1385 /// will be preserved at the start of a line (in addition to the
1386 /// indent-level), otherwise line-leading spaces will be ignored.
1387 space({n: 1, allowLineLeading: false}) {
1375 //TODO(pquitslund): replace with a proper space token 1388 //TODO(pquitslund): replace with a proper space token
1376 leadingSpaces+=n; 1389 leadingSpaces+=n;
1390 allowLineLeadingSpaces = allowLineLeading;
1377 } 1391 }
1378 1392
1379 /// Emit a breakable space 1393 /// Emit a breakable space
1380 breakableSpace() { 1394 breakableSpace() {
1381 //Implement 1395 //Implement
1382 } 1396 }
1383 1397
1384 /// Append the given [string] to the source writer if it's non-null. 1398 /// Append the given [string] to the source writer if it's non-null.
1385 append(String string) { 1399 append(String string) {
1386 if (string != null && !string.isEmpty) { 1400 if (string != null && !string.isEmpty) {
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
1551 var lastLine = 1565 var lastLine =
1552 lineInfo.getLocation(lastOffset).lineNumber; 1566 lineInfo.getLocation(lastOffset).lineNumber;
1553 var currentLine = 1567 var currentLine =
1554 lineInfo.getLocation(currentOffset).lineNumber; 1568 lineInfo.getLocation(currentOffset).lineNumber;
1555 return currentLine - lastLine; 1569 return currentLine - lastLine;
1556 } 1570 }
1557 1571
1558 String toString() => writer.toString(); 1572 String toString() => writer.toString();
1559 1573
1560 } 1574 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/services/formatter_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698