Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 /// Holds a couple utility functions used at various places in the system. | 5 /// Holds a couple utility functions used at various places in the system. |
| 6 library ddc.src.utils; | 6 library ddc.src.utils; |
| 7 | 7 |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:path/path.dart' as path; | 10 import 'package:path/path.dart' as path; |
| 11 import 'package:analyzer/src/generated/ast.dart' | 11 import 'package:analyzer/src/generated/ast.dart' |
| 12 show | 12 show |
| 13 ImportDirective, | 13 ImportDirective, |
| 14 ExportDirective, | 14 ExportDirective, |
| 15 PartDirective, | 15 PartDirective, |
| 16 CompilationUnit, | 16 CompilationUnit, |
| 17 Identifier; | 17 Identifier, |
| 18 AnnotatedNode, | |
| 19 AstNode; | |
| 18 import 'package:analyzer/src/generated/engine.dart' | 20 import 'package:analyzer/src/generated/engine.dart' |
| 19 show ParseDartTask, AnalysisContext; | 21 show ParseDartTask, AnalysisContext; |
| 20 import 'package:analyzer/src/generated/source.dart' show Source; | 22 import 'package:analyzer/src/generated/source.dart' show Source; |
| 21 import 'package:analyzer/src/generated/element.dart'; | 23 import 'package:analyzer/src/generated/element.dart'; |
| 22 import 'package:analyzer/analyzer.dart' show parseDirectives; | 24 import 'package:analyzer/analyzer.dart' show parseDirectives; |
| 25 import 'package:source_span/source_span.dart'; | |
| 23 | 26 |
| 24 bool isDartPrivateLibrary(LibraryElement library) { | 27 bool isDartPrivateLibrary(LibraryElement library) { |
| 25 var uri = library.source.uri; | 28 var uri = library.source.uri; |
| 26 if (uri.scheme != "dart") return false; | 29 if (uri.scheme != "dart") return false; |
| 27 return Identifier.isPrivateName(uri.path); | 30 return Identifier.isPrivateName(uri.path); |
| 28 } | 31 } |
| 29 | 32 |
| 30 /// Choose a canonical name from the library element. This is safe to use as a | 33 /// Choose a canonical name from the library element. This is safe to use as a |
| 31 /// namespace in JS and Dart code generation. This never uses the library's | 34 /// namespace in JS and Dart code generation. This never uses the library's |
| 32 /// name (the identifier in the `library` declaration) as it doesn't have any | 35 /// name (the identifier in the `library` declaration) as it doesn't have any |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 void dec([int n = 2]) { | 228 void dec([int n = 2]) { |
| 226 _indent = _indent - n; | 229 _indent = _indent - n; |
| 227 assert(_indent >= 0); | 230 assert(_indent >= 0); |
| 228 _prefix = "".padRight(_indent); | 231 _prefix = "".padRight(_indent); |
| 229 } | 232 } |
| 230 | 233 |
| 231 void close() { | 234 void close() { |
| 232 new File(_path).writeAsStringSync('$_sb'); | 235 new File(_path).writeAsStringSync('$_sb'); |
| 233 } | 236 } |
| 234 } | 237 } |
| 238 | |
| 239 SourceLocation locationFor(CompilationUnit unit, Uri uri, int offset) { | |
|
Jennifer Messerly
2015/03/03 02:24:22
not sure the "For" adds much. "getSourceLocation"?
Siggi Cherem (dart-lang)
2015/03/04 04:44:24
What do you think about locationForOffset?
Jennifer Messerly
2015/03/04 15:35:10
yeah that works too.
Just curious, is it intention
Siggi Cherem (dart-lang)
2015/03/04 17:38:16
funny - technically no, but every time I start nam
| |
| 240 var lineInfo = unit.lineInfo.getLocation(offset); | |
| 241 return new SourceLocation(offset, | |
| 242 sourceUrl: uri, | |
| 243 line: lineInfo.lineNumber - 1, | |
| 244 column: lineInfo.columnNumber - 1); | |
| 245 } | |
| 246 | |
| 247 SourceSpan spanFor(CompilationUnit unit, Uri uri, AstNode node) { | |
| 248 var currentToken = node is AnnotatedNode | |
| 249 ? node.firstTokenAfterCommentAndMetadata | |
| 250 : node.beginToken; | |
| 251 var begin = currentToken.offset; | |
| 252 var endToken = node.endToken; | |
| 253 var text = new StringBuffer(); | |
|
Jennifer Messerly
2015/03/03 02:24:22
I presume there is a method to this madness :-)
g
Siggi Cherem (dart-lang)
2015/03/04 04:44:24
yeah, this was more twisted than it needed to be :
| |
| 254 append(tok) { | |
| 255 var offset = tok.offset; | |
| 256 var diff = offset - begin - text.length; | |
| 257 text.write(' ' * diff); | |
| 258 text.write(tok.lexeme); | |
| 259 } | |
| 260 while (currentToken != endToken) { | |
| 261 append(currentToken); | |
| 262 currentToken = currentToken.next; | |
| 263 } | |
| 264 append(currentToken); | |
| 265 return new SourceSpan( | |
| 266 locationFor(unit, uri, begin), locationFor(unit, uri, node.end), '$text'); | |
| 267 } | |
| OLD | NEW |