Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 22 matching lines...) Expand all Loading... | |
| 33 final int tabSize; | 33 final int tabSize; |
| 34 final bool tabsForIndent; | 34 final bool tabsForIndent; |
| 35 final int pageWidth; | 35 final int pageWidth; |
| 36 } | 36 } |
| 37 | 37 |
| 38 | 38 |
| 39 /// Thrown when an error occurs in formatting. | 39 /// Thrown when an error occurs in formatting. |
| 40 class FormatterException implements Exception { | 40 class FormatterException implements Exception { |
| 41 | 41 |
| 42 /// A message describing the error. | 42 /// A message describing the error. |
| 43 final message; | 43 final String message; |
|
pquitslund
2013/08/08 21:20:23
Hmmmm. The type seems gratuitous here. Or?
kevmoo-old
2013/08/08 21:23:34
Nope. It's great for the analyzer and doc generati
| |
| 44 | 44 |
| 45 /// Creates a new FormatterException with an optional error [message]. | 45 /// Creates a new FormatterException with an optional error [message]. |
| 46 const FormatterException([this.message = '']); | 46 const FormatterException([this.message = '']); |
| 47 | 47 |
| 48 FormatterException.forError(List<AnalysisError> errors) : | 48 FormatterException.forError(List<AnalysisError> errors) : |
| 49 // TODO(pquitslund): add descriptive message based on errors | 49 // TODO(pquitslund): add descriptive message based on errors |
| 50 message = 'an analysis error occured during format'; | 50 message = 'an analysis error occured during format'; |
| 51 | 51 |
| 52 String toString() => 'FormatterException: $message'; | 52 String toString() => 'FormatterException: $message'; |
| 53 } | 53 } |
| 54 | 54 |
| 55 /// Specifies the kind of code snippet to format. | 55 /// Specifies the kind of code snippet to format. |
| 56 class CodeKind { | 56 class CodeKind { |
| 57 | 57 |
| 58 final index; | 58 final int _index; |
| 59 | 59 |
| 60 const CodeKind(this.index); | 60 const CodeKind._(this._index); |
| 61 | 61 |
| 62 /// A compilation unit snippet. | 62 /// A compilation unit snippet. |
| 63 static const COMPILATION_UNIT = const CodeKind(0); | 63 static const COMPILATION_UNIT = const CodeKind._(0); |
| 64 | 64 |
| 65 /// A statement snippet. | 65 /// A statement snippet. |
| 66 static const STATEMENT = const CodeKind(1); | 66 static const STATEMENT = const CodeKind._(1); |
| 67 | 67 |
| 68 } | 68 } |
| 69 | 69 |
| 70 /// Dart source code formatter. | 70 /// Dart source code formatter. |
| 71 abstract class CodeFormatter { | 71 abstract class CodeFormatter { |
| 72 | 72 |
| 73 factory CodeFormatter([FormatterOptions options = const FormatterOptions()]) | 73 factory CodeFormatter([FormatterOptions options = const FormatterOptions()]) |
| 74 => new CodeFormatterImpl(options); | 74 => new CodeFormatterImpl(options); |
| 75 | 75 |
| 76 /// Format the specified portion (from [offset] with [length]) of the given | 76 /// Format the specified portion (from [offset] with [length]) of the given |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 111 switch (kind) { | 111 switch (kind) { |
| 112 case CodeKind.COMPILATION_UNIT: | 112 case CodeKind.COMPILATION_UNIT: |
| 113 return parser.parseCompilationUnit(start); | 113 return parser.parseCompilationUnit(start); |
| 114 case CodeKind.STATEMENT: | 114 case CodeKind.STATEMENT: |
| 115 return parser.parseStatement(start); | 115 return parser.parseStatement(start); |
| 116 } | 116 } |
| 117 | 117 |
| 118 throw new FormatterException('Unsupported format kind: $kind'); | 118 throw new FormatterException('Unsupported format kind: $kind'); |
| 119 } | 119 } |
| 120 | 120 |
| 121 checkForErrors() { | 121 void checkForErrors() { |
|
pquitslund
2013/08/08 21:20:23
What's your thinking on when to specify a void ret
kevmoo-old
2013/08/08 21:23:34
If a public member returns nothing, flag it void.
| |
| 122 if (errors.length > 0) { | 122 if (errors.length > 0) { |
| 123 throw new FormatterException.forError(errors); | 123 throw new FormatterException.forError(errors); |
| 124 } | 124 } |
| 125 } | 125 } |
| 126 | 126 |
| 127 void onError(AnalysisError error) { | 127 void onError(AnalysisError error) { |
| 128 errors.add(error); | 128 errors.add(error); |
| 129 } | 129 } |
| 130 | 130 |
| 131 Token tokenize(String source) { | 131 Token tokenize(String source) { |
| 132 var scanner = new StringScanner(null, source, this); | 132 var scanner = new StringScanner(null, source, this); |
| 133 var token = scanner.tokenize(); | 133 var token = scanner.tokenize(); |
| 134 lineInfo = new LineInfo(scanner.lineStarts); | 134 lineInfo = new LineInfo(scanner.lineStarts); |
| 135 return token; | 135 return token; |
| 136 } | 136 } |
| 137 | 137 |
| 138 } | 138 } |
| 139 | 139 |
| 140 | 140 |
| 141 /// An AST visitor that drives formatting heuristics. | 141 /// An AST visitor that drives formatting heuristics. |
| 142 class SourceVisitor implements ASTVisitor { | 142 class SourceVisitor implements ASTVisitor { |
| 143 | 143 |
| 144 /// The writer to which the source is to be written. | 144 /// The writer to which the source is to be written. |
| 145 SourceWriter writer; | 145 final SourceWriter writer; |
| 146 | 146 |
| 147 /// Cached line info for calculating blank lines. | 147 /// Cached line info for calculating blank lines. |
| 148 LineInfo lineInfo; | 148 LineInfo lineInfo; |
| 149 | 149 |
| 150 /// Cached previous token for calculating preceding whitespace. | 150 /// Cached previous token for calculating preceding whitespace. |
| 151 Token previousToken; | 151 Token previousToken; |
| 152 | 152 |
| 153 /// Initialize a newly created visitor to write source code representing | 153 /// Initialize a newly created visitor to write source code representing |
| 154 /// the visited nodes to the given [writer]. | 154 /// the visited nodes to the given [writer]. |
| 155 SourceVisitor(FormatterOptions options, this.lineInfo) : | 155 SourceVisitor(FormatterOptions options, this.lineInfo) : |
| (...skipping 854 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1010 return 0; | 1010 return 0; |
| 1011 } | 1011 } |
| 1012 var lastLine = | 1012 var lastLine = |
| 1013 lineInfo.getLocation(last.offset).lineNumber; | 1013 lineInfo.getLocation(last.offset).lineNumber; |
| 1014 var currentLine = | 1014 var currentLine = |
| 1015 lineInfo.getLocation(current.offset).lineNumber; | 1015 lineInfo.getLocation(current.offset).lineNumber; |
| 1016 return currentLine - lastLine; | 1016 return currentLine - lastLine; |
| 1017 } | 1017 } |
| 1018 | 1018 |
| 1019 } | 1019 } |
| OLD | NEW |