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

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

Issue 132383002: Line-breaking plumbing and bits and pieces. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 11 months 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
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 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 /// A counter for spaces that should be emitted preceding the next token. 359 /// A counter for spaces that should be emitted preceding the next token.
360 int leadingSpaces = 0; 360 int leadingSpaces = 0;
361 361
362 /// A flag to specify whether line-leading spaces should be preserved (and 362 /// A flag to specify whether line-leading spaces should be preserved (and
363 /// addded to the indent level). 363 /// addded to the indent level).
364 bool allowLineLeadingSpaces; 364 bool allowLineLeadingSpaces;
365 365
366 /// Used for matching EOL comments 366 /// Used for matching EOL comments
367 final twoSlashes = new RegExp(r'//[^/]'); 367 final twoSlashes = new RegExp(r'//[^/]');
368 368
369 /// A weight for potential breakpoints.
370 int breakWeight = null;
371
369 /// Original pre-format selection information (may be null). 372 /// Original pre-format selection information (may be null).
370 final Selection preSelection; 373 final Selection preSelection;
371 374
372 final bool codeTransforms; 375 final bool codeTransforms;
373 376
374 /// Post format selection information. 377 /// Post format selection information.
375 Selection selection; 378 Selection selection;
376 379
377 380
378 /// Initialize a newly created visitor to write source code representing 381 /// Initialize a newly created visitor to write source code representing
(...skipping 609 matching lines...) Expand 10 before | Expand all | Expand 10 after
988 991
989 visitLibraryIdentifier(LibraryIdentifier node) { 992 visitLibraryIdentifier(LibraryIdentifier node) {
990 append(node.name); 993 append(node.name);
991 } 994 }
992 995
993 visitListLiteral(ListLiteral node) { 996 visitListLiteral(ListLiteral node) {
994 modifier(node.constKeyword); 997 modifier(node.constKeyword);
995 visit(node.typeArguments); 998 visit(node.typeArguments);
996 token(node.leftBracket); 999 token(node.leftBracket);
997 indent(); 1000 indent();
998 visitCommaSeparatedNodes(node.elements); 1001 visitCommaSeparatedNodes(node.elements /*, followedBy: breakableSpace*/);
999 optionalTrailingComma(node.rightBracket); 1002 optionalTrailingComma(node.rightBracket);
1000 unindent(); 1003 unindent();
1001 token(node.rightBracket); 1004 token(node.rightBracket);
1002 } 1005 }
1003 1006
1004 visitMapLiteral(MapLiteral node) { 1007 visitMapLiteral(MapLiteral node) {
1005 modifier(node.constKeyword); 1008 modifier(node.constKeyword);
1006 visitNode(node.typeArguments, followedBy: space); 1009 visitNode(node.typeArguments, followedBy: space);
1007 token(node.leftBracket); 1010 token(node.leftBracket);
1008 newlines(); 1011 newlines();
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after
1448 checkForSelectionUpdate(token); 1451 checkForSelectionUpdate(token);
1449 append(token.lexeme); 1452 append(token.lexeme);
1450 if (followedBy != null) { 1453 if (followedBy != null) {
1451 followedBy(); 1454 followedBy();
1452 } 1455 }
1453 previousToken = token; 1456 previousToken = token;
1454 } 1457 }
1455 } 1458 }
1456 1459
1457 emitSpaces() { 1460 emitSpaces() {
1458 while (leadingSpaces > 0) { 1461 if (leadingSpaces > 0) {
1459 if (!writer.currentLine.isWhitespace() || allowLineLeadingSpaces) { 1462 if (allowLineLeadingSpaces || !writer.currentLine.isWhitespace()) {
1460 writer.print(' '); 1463 writer.spaces(leadingSpaces, breakWeight: breakWeight);
1461 } 1464 }
1465 leadingSpaces = 0;
1462 allowLineLeadingSpaces = false; 1466 allowLineLeadingSpaces = false;
1463 leadingSpaces--; 1467 breakWeight = null;
1464 } 1468 }
1465 } 1469 }
1466 1470
1467 checkForSelectionUpdate(Token token) { 1471 checkForSelectionUpdate(Token token) {
1468 // Cache the first token on or AFTER the selection offset 1472 // Cache the first token on or AFTER the selection offset
1469 if (preSelection != null && selection == null) { 1473 if (preSelection != null && selection == null) {
1470 // Check for overshots 1474 // Check for overshots
1471 var overshot = token.offset - preSelection.offset; 1475 var overshot = token.offset - preSelection.offset;
1472 if (overshot >= 0) { 1476 if (overshot >= 0) {
1473 //TODO(pquitslund): update length (may need truncating) 1477 //TODO(pquitslund): update length (may need truncating)
1474 selection = new Selection( 1478 selection = new Selection(
1475 writer.toString().length + leadingSpaces - overshot, 1479 writer.toString().length + leadingSpaces - overshot,
1476 preSelection.length); 1480 preSelection.length);
1477 } 1481 }
1478 } 1482 }
1479 } 1483 }
1480 1484
1481 /// Emit a non-breakable space. If [allowLineLeading] is specified, spaces 1485 /// Emit a non-breakable space. If [allowLineLeading] is specified, spaces
1482 /// will be preserved at the start of a line (in addition to the 1486 /// will be preserved at the start of a line (in addition to the
1483 /// indent-level), otherwise line-leading spaces will be ignored. 1487 /// indent-level), otherwise line-leading spaces will be ignored.
1484 space({n: 1, allowLineLeading: false}) { 1488 space({n: 1, allowLineLeading: false}) {
1485 //TODO(pquitslund): replace with a proper space token 1489 //TODO(pquitslund): replace with a proper space token
1486 leadingSpaces+=n; 1490 leadingSpaces+=n;
1487 allowLineLeadingSpaces = allowLineLeading; 1491 allowLineLeadingSpaces = allowLineLeading;
1488 } 1492 }
1489 1493
1490 /// Emit a breakable space 1494 /// Mark a breakable space
1491 breakableSpace() { 1495 breakableSpace() {
1492 //Implement 1496 space();
1497 breakWeight =
1498 countNewlinesBetween(previousToken, previousToken.next) > 0 ? 1 : 0;
1493 } 1499 }
1494 1500
1495 /// Append the given [string] to the source writer if it's non-null. 1501 /// Append the given [string] to the source writer if it's non-null.
1496 append(String string) { 1502 append(String string) {
1497 if (string != null && !string.isEmpty) { 1503 if (string != null && !string.isEmpty) {
1498 emitSpaces(); 1504 emitSpaces();
1499 writer.print(string); 1505 writer.print(string);
1500 } 1506 }
1501 } 1507 }
1502 1508
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
1672 var lastLine = 1678 var lastLine =
1673 lineInfo.getLocation(lastOffset).lineNumber; 1679 lineInfo.getLocation(lastOffset).lineNumber;
1674 var currentLine = 1680 var currentLine =
1675 lineInfo.getLocation(currentOffset).lineNumber; 1681 lineInfo.getLocation(currentOffset).lineNumber;
1676 return currentLine - lastLine; 1682 return currentLine - lastLine;
1677 } 1683 }
1678 1684
1679 String toString() => writer.toString(); 1685 String toString() => writer.toString();
1680 1686
1681 } 1687 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/lib/src/services/writer.dart » ('j') | pkg/analyzer/lib/src/services/writer.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698