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 locationForOffset(CompilationUnit unit, Uri uri, int offset) { |
| 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 // TODO(sigmund): change to show the span from the beginning of the line (#73) |
| 248 SourceSpan spanForNode(CompilationUnit unit, Source source, AstNode node) { |
| 249 var currentToken = node is AnnotatedNode |
| 250 ? node.firstTokenAfterCommentAndMetadata |
| 251 : node.beginToken; |
| 252 var begin = currentToken.offset; |
| 253 var end = node.end; |
| 254 var text = source.contents.data.substring(begin, end); |
| 255 var uri = source.uri; |
| 256 return new SourceSpan(locationForOffset(unit, uri, begin), |
| 257 locationForOffset(unit, uri, end), '$text'); |
| 258 } |
OLD | NEW |