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

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

Issue 24180004: Source selection API abstraction first cut. (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 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_experimental/analyzer.dart'; 9 import 'package:analyzer_experimental/analyzer.dart';
10 import 'package:analyzer_experimental/src/generated/parser.dart'; 10 import 'package:analyzer_experimental/src/generated/parser.dart';
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 } 71 }
72 72
73 /// Dart source code formatter. 73 /// Dart source code formatter.
74 abstract class CodeFormatter { 74 abstract class CodeFormatter {
75 75
76 factory CodeFormatter([FormatterOptions options = const FormatterOptions()]) 76 factory CodeFormatter([FormatterOptions options = const FormatterOptions()])
77 => new CodeFormatterImpl(options); 77 => new CodeFormatterImpl(options);
78 78
79 /// Format the specified portion (from [offset] with [length]) of the given 79 /// Format the specified portion (from [offset] with [length]) of the given
80 /// [source] string, optionally providing an [indentationLevel]. 80 /// [source] string, optionally providing an [indentationLevel].
81 String format(CodeKind kind, String source, {int offset, int end, 81 FormattedSource format(CodeKind kind, String source, {int offset, int end,
82 int indentationLevel: 0}); 82 int indentationLevel: 0, Selection selection: null});
83 83
84 } 84 }
85 85
86 /// Source selection state information.
87 class Selection {
88
89 /// The offset of the source selection.
90 final int offset;
91
92 /// The length of the selection.
93 final int length;
94
95 Selection(this.offset, this.length);
96 }
97
98 /// Formatted source.
99 class FormattedSource {
100
101 /// Selection state or null if unspecified.
102 final Selection selection;
103
104 /// Formatted source string.
105 final String source;
106
107 /// Create a formatted [source] result, with optional [selection] information.
108 FormattedSource(this.source, [this.selection = null]);
109 }
110
111
86 class CodeFormatterImpl implements CodeFormatter, AnalysisErrorListener { 112 class CodeFormatterImpl implements CodeFormatter, AnalysisErrorListener {
87 113
88 final FormatterOptions options; 114 final FormatterOptions options;
89 final errors = <AnalysisError>[]; 115 final errors = <AnalysisError>[];
90 final whitespace = new RegExp(r'[\s]+'); 116 final whitespace = new RegExp(r'[\s]+');
91 117
92 LineInfo lineInfo; 118 LineInfo lineInfo;
93 119
94 CodeFormatterImpl(this.options); 120 CodeFormatterImpl(this.options);
95 121
96 String format(CodeKind kind, String source, {int offset, int end, 122 FormattedSource format(CodeKind kind, String source, {int offset, int end,
97 int indentationLevel: 0}) { 123 int indentationLevel: 0, Selection selection: null}) {
98 124
99 var startToken = tokenize(source); 125 var startToken = tokenize(source);
100 checkForErrors(); 126 checkForErrors();
101 127
102 var node = parse(kind, startToken); 128 var node = parse(kind, startToken);
103 checkForErrors(); 129 checkForErrors();
104 130
105 var formatter = new SourceVisitor(options, lineInfo); 131 var formatter = new SourceVisitor(options, lineInfo);
106 node.accept(formatter); 132 node.accept(formatter);
107 133
108 var formattedSource = formatter.writer.toString(); 134 var formattedSource = formatter.writer.toString();
109 135
110 checkTokenStreams(startToken, tokenize(formattedSource)); 136 checkTokenStreams(startToken, tokenize(formattedSource));
111 137
112 return formattedSource; 138 return new FormattedSource(formattedSource);
113 } 139 }
114 140
115 checkTokenStreams(Token t1, Token t2) => 141 checkTokenStreams(Token t1, Token t2) =>
116 new TokenStreamComparator(lineInfo, t1, t2).verifyEquals(); 142 new TokenStreamComparator(lineInfo, t1, t2).verifyEquals();
117 143
118 ASTNode parse(CodeKind kind, Token start) { 144 ASTNode parse(CodeKind kind, Token start) {
119 145
120 var parser = new Parser(null, this); 146 var parser = new Parser(null, this);
121 147
122 switch (kind) { 148 switch (kind) {
(...skipping 1236 matching lines...) Expand 10 before | Expand all | Expand 10 after
1359 var lastLine = 1385 var lastLine =
1360 lineInfo.getLocation(lastOffset).lineNumber; 1386 lineInfo.getLocation(lastOffset).lineNumber;
1361 var currentLine = 1387 var currentLine =
1362 lineInfo.getLocation(currentOffset).lineNumber; 1388 lineInfo.getLocation(currentOffset).lineNumber;
1363 return currentLine - lastLine; 1389 return currentLine - lastLine;
1364 } 1390 }
1365 1391
1366 String toString() => writer.toString(); 1392 String toString() => writer.toString();
1367 1393
1368 } 1394 }
OLDNEW
« 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