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

Unified Diff: pkg/analyzer_experimental/lib/src/services/formatter_impl.dart

Issue 24371003: Formatter selection preservation support. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: pkg/analyzer_experimental/lib/src/services/formatter_impl.dart
===================================================================
--- pkg/analyzer_experimental/lib/src/services/formatter_impl.dart (revision 27780)
+++ pkg/analyzer_experimental/lib/src/services/formatter_impl.dart (working copy)
@@ -93,6 +93,8 @@
final int length;
Selection(this.offset, this.length);
+
+ String toString() => 'Selection (offset: $offset, length: $length)';
}
/// Formatted source.
@@ -128,14 +130,14 @@
var node = parse(kind, startToken);
checkForErrors();
- var formatter = new SourceVisitor(options, lineInfo);
+ var formatter = new SourceVisitor(options, lineInfo, selection);
node.accept(formatter);
var formattedSource = formatter.writer.toString();
checkTokenStreams(startToken, tokenize(formattedSource));
- return new FormattedSource(formattedSource);
+ return new FormattedSource(formattedSource, formatter.selection);
}
checkTokenStreams(Token t1, Token t2) =>
@@ -309,10 +311,16 @@
/// Used for matching EOL comments
final twoSlashes = new RegExp(r'//[^/]');
+
+ /// Original pre-format selection information (may be null).
+ final Selection preSelection;
+
+ /// Post format selection information.
+ Selection selection;
/// Initialize a newly created visitor to write source code representing
/// the visited nodes to the given [writer].
- SourceVisitor(FormatterOptions options, this.lineInfo) :
+ SourceVisitor(FormatterOptions options, this.lineInfo, this.preSelection) :
writer = new SourceWriter(indentCount: options.initialIndentationLevel,
lineSeparator: options.lineSeparator);
@@ -1209,6 +1217,7 @@
if (precededBy != null) {
precededBy();
}
+ checkForSelectionUpdate(token);
append(token.lexeme);
if (followedBy != null) {
followedBy();
@@ -1216,6 +1225,19 @@
previousToken = token;
}
}
+
+ checkForSelectionUpdate(Token token) {
+ // Cache the first token on or AFTER the selection offset
+ if (preSelection != null && selection == null) {
+ // Check for overshots
+ var overshot = token.offset - preSelection.offset;
+ if (overshot >= 0) {
+ //TODO(pquitslund): update length (may need truncating)
+ selection = new Selection(writer.toString().length - overshot,
+ preSelection.length);
+ }
+ }
+ }
commaSeperator() {
comma();
« no previous file with comments | « pkg/analyzer_experimental/bin/formatter.dart ('k') | pkg/analyzer_experimental/test/services/formatter_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698