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

Unified Diff: lib/src/codegen/dart_codegen.dart

Issue 959073002: Fix new line breaks on } (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 10 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
« no previous file with comments | « lib/devc.dart ('k') | lib/src/options.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/codegen/dart_codegen.dart
diff --git a/lib/src/codegen/dart_codegen.dart b/lib/src/codegen/dart_codegen.dart
index 0ad79bd2377cde064c42c943ab3960d4c8d6f0b5..8bb7c0edc98a1fd233674951cbd487f878e7a9c6 100644
--- a/lib/src/codegen/dart_codegen.dart
+++ b/lib/src/codegen/dart_codegen.dart
@@ -17,6 +17,7 @@ import 'package:path/path.dart' as path;
import 'package:dev_compiler/src/info.dart';
import 'package:dev_compiler/src/checker/rules.dart';
+import 'package:dev_compiler/src/options.dart';
import 'package:dev_compiler/src/report.dart';
import 'package:dev_compiler/src/utils.dart' as utils;
import 'ast_builder.dart';
@@ -64,32 +65,53 @@ class DdcRuntime {
// TODO(leafp) This is kind of a hack, but it works for now.
class FileWriter extends java_core.PrintStringWriter {
- bool _format;
+ final CompilerOptions options;
String _path;
- FileWriter(this._format, this._path);
+ FileWriter(this.options, this._path);
int indent = 0;
+ int withinInterpolationExpression = 0;
+ bool insideForLoop = false;
void print(x) {
- if (_format) {
+ if (!options.cheapTestFormat) {
super.print(x);
return;
}
- if (x == '{') {
- indent++;
- x = '{\n${" " * indent}';
- } else if (x == ';') {
- x = ';\n${" " * indent}';
- } else if (x == '}') {
- indent--;
- x = '}\n${" " * indent}';
+ switch (x) {
+ case '{':
+ indent++;
+ x = '{\n${" " * indent}';
+ break;
+ case ';':
+ if (!insideForLoop) {
+ x = ';\n${" " * indent}';
+ }
+ break;
+ case 'for (':
+ insideForLoop = true;
+ break;
+ case ') ':
+ insideForLoop = false;
+ break;
+ case r'${':
+ withinInterpolationExpression++;
+ break;
+ case '}':
+ if (withinInterpolationExpression > 0) {
+ withinInterpolationExpression--;
+ } else {
+ indent--;
+ x = '}\n${" " * indent}';
+ }
+ break;
}
super.print(x);
}
void finalize() {
String s = toString();
- if (_format) {
+ if (options.formatOutput && !options.cheapTestFormat) {
DartFormatter d = new DartFormatter();
try {
_log.fine("Formatting file $_path ");
@@ -390,12 +412,12 @@ class UnitGenerator extends UnitGeneratorCommon with ConversionVisitor<Object> {
}
class DartGenerator extends codegenerator.CodeGenerator {
- bool _format;
+ final CompilerOptions options;
reifier.VariableManager _vm;
Set<LibraryElement> _extraImports;
TypeRules _rules;
- DartGenerator(String outDir, Uri root, TypeRules rules, this._format)
+ DartGenerator(String outDir, Uri root, TypeRules rules, this.options)
: _rules = rules,
super(outDir, root, rules);
@@ -403,7 +425,7 @@ class DartGenerator extends codegenerator.CodeGenerator {
var uri = unit.element.source.uri;
_log.fine("Generating unit " + uri.toString());
FileWriter out = new FileWriter(
- _format, path.join(libraryDir, '${uri.pathSegments.last}'));
+ options, path.join(libraryDir, '${uri.pathSegments.last}'));
var tm = new reifier.TypeManager(_vm);
var r = new reifier.UnitCoercionReifier(tm, _vm, _rules);
r.reify(unit);
@@ -458,16 +480,16 @@ class EmptyUnitGenerator extends UnitGeneratorCommon {
// This class emits the code unchanged, for comparison purposes.
class EmptyDartGenerator extends codegenerator.CodeGenerator {
- bool _format;
+ final CompilerOptions options;
- EmptyDartGenerator(String outDir, Uri root, TypeRules rules, this._format)
+ EmptyDartGenerator(String outDir, Uri root, TypeRules rules, this.options)
: super(outDir, root, rules);
void generateUnit(CompilationUnit unit, LibraryInfo info, String libraryDir) {
var uri = unit.element.source.uri;
_log.fine("Emitting original unit " + uri.toString());
FileWriter out = new FileWriter(
- _format, path.join(libraryDir, '${uri.pathSegments.last}'));
+ options, path.join(libraryDir, '${uri.pathSegments.last}'));
var unitGen = new EmptyUnitGenerator(unit, out);
unitGen.generate();
out.finalize();
« no previous file with comments | « lib/devc.dart ('k') | lib/src/options.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698