OLD | NEW |
---|---|
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 code_transformer.src.resolver_impl; | 5 library code_transformer.src.resolver_impl; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'package:analyzer/analyzer.dart' show parseDirectives; | 8 import 'package:analyzer/analyzer.dart' show parseDirectives; |
9 import 'package:analyzer/src/generated/ast.dart' hide ConstantEvaluator; | 9 import 'package:analyzer/src/generated/ast.dart' hide ConstantEvaluator; |
10 import 'package:analyzer/src/generated/constant.dart' show ConstantEvaluator, | 10 import 'package:analyzer/src/generated/constant.dart' show ConstantEvaluator, |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 var visited = new Set<AssetId>(); | 109 var visited = new Set<AssetId>(); |
110 var visiting = new FutureGroup(); | 110 var visiting = new FutureGroup(); |
111 var toUpdate = []; | 111 var toUpdate = []; |
112 | 112 |
113 void processAsset(AssetId assetId) { | 113 void processAsset(AssetId assetId) { |
114 visited.add(assetId); | 114 visited.add(assetId); |
115 | 115 |
116 visiting.add(transform.readInputAsString(assetId).then((contents) { | 116 visiting.add(transform.readInputAsString(assetId).then((contents) { |
117 var source = sources[assetId]; | 117 var source = sources[assetId]; |
118 if (source == null) { | 118 if (source == null) { |
119 source = new _AssetBasedSource(assetId, this); | 119 // TODO(scheglov) how to get Uri here? |
Siggi Cherem (dart-lang)
2014/08/04 22:43:55
You might be able to basically use the logic we ha
| |
120 Uri uri = null; | |
121 source = new _AssetBasedSource(assetId, this, uri); | |
120 sources[assetId] = source; | 122 sources[assetId] = source; |
121 } | 123 } |
122 source.updateDependencies(contents); | 124 source.updateDependencies(contents); |
123 toUpdate.add(new _PendingUpdate(source, contents)); | 125 toUpdate.add(new _PendingUpdate(source, contents)); |
124 source.dependentAssets.where((id) => !visited.contains(id)) | 126 source.dependentAssets.where((id) => !visited.contains(id)) |
125 .forEach(processAsset); | 127 .forEach(processAsset); |
126 }, onError: (e) { | 128 }, onError: (e) { |
127 var source = sources[assetId]; | 129 var source = sources[assetId]; |
128 if (source != null && source.exists()) { | 130 if (source != null && source.exists()) { |
129 _context.applyChanges( | 131 _context.applyChanges( |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
291 | 293 |
292 /// Implementation of Analyzer's Source for Barback based assets. | 294 /// Implementation of Analyzer's Source for Barback based assets. |
293 class _AssetBasedSource extends Source { | 295 class _AssetBasedSource extends Source { |
294 | 296 |
295 /// Asset ID where this source can be found. | 297 /// Asset ID where this source can be found. |
296 final AssetId assetId; | 298 final AssetId assetId; |
297 | 299 |
298 /// The resolver this is being used in. | 300 /// The resolver this is being used in. |
299 final ResolverImpl _resolver; | 301 final ResolverImpl _resolver; |
300 | 302 |
303 /// The URI from which this source was originally derived. | |
304 final Uri uri; | |
305 | |
301 /// Cache of dependent asset IDs, to avoid re-parsing the AST. | 306 /// Cache of dependent asset IDs, to avoid re-parsing the AST. |
302 Iterable<AssetId> _dependentAssets; | 307 Iterable<AssetId> _dependentAssets; |
303 | 308 |
304 /// The current revision of the file, incremented only when file changes. | 309 /// The current revision of the file, incremented only when file changes. |
305 int _revision = 0; | 310 int _revision = 0; |
306 | 311 |
307 /// The file contents. | 312 /// The file contents. |
308 String _contents; | 313 String _contents; |
309 | 314 |
310 _AssetBasedSource(this.assetId, this._resolver); | 315 _AssetBasedSource(this.assetId, this._resolver, this.uri); |
311 | 316 |
312 /// Update the dependencies of this source. This parses [contents] but avoids | 317 /// Update the dependencies of this source. This parses [contents] but avoids |
313 /// any analyzer resolution. | 318 /// any analyzer resolution. |
314 void updateDependencies(String contents) { | 319 void updateDependencies(String contents) { |
315 if (contents == _contents) return; | 320 if (contents == _contents) return; |
316 var unit = parseDirectives(contents, suppressErrors: true); | 321 var unit = parseDirectives(contents, suppressErrors: true); |
317 _dependentAssets = unit.directives | 322 _dependentAssets = unit.directives |
318 .where((d) => (d is ImportDirective || d is PartDirective || | 323 .where((d) => (d is ImportDirective || d is PartDirective || |
319 d is ExportDirective)) | 324 d is ExportDirective)) |
320 .map((d) => _resolve(assetId, d.uri.stringValue, _logger, | 325 .map((d) => _resolve(assetId, d.uri.stringValue, _logger, |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
370 | 375 |
371 String get shortName => path.basename(assetId.path); | 376 String get shortName => path.basename(assetId.path); |
372 | 377 |
373 UriKind get uriKind { | 378 UriKind get uriKind { |
374 if (assetId.path.startsWith('lib/')) return UriKind.PACKAGE_URI; | 379 if (assetId.path.startsWith('lib/')) return UriKind.PACKAGE_URI; |
375 return UriKind.FILE_URI; | 380 return UriKind.FILE_URI; |
376 } | 381 } |
377 | 382 |
378 bool get isInSystemLibrary => false; | 383 bool get isInSystemLibrary => false; |
379 | 384 |
380 Source resolveRelative(Uri relativeUri) { | 385 Uri resolveRelative(Uri relativeUri) { |
381 var id = _resolve(assetId, relativeUri.toString(), _logger, null); | 386 var id = _resolve(assetId, relativeUri.toString(), _logger, null); |
382 if (id == null) return null; | 387 if (id == null) return null; |
383 | 388 |
384 // The entire AST should have been parsed and loaded at this point. | 389 // The entire AST should have been parsed and loaded at this point. |
385 var source = _resolver.sources[id]; | 390 var source = _resolver.sources[id]; |
386 if (source == null) { | 391 if (source == null) { |
387 _logger.error('Could not load asset $id'); | 392 _logger.error('Could not load asset $id'); |
388 } | 393 } |
389 return source; | 394 return source.uri; |
390 } | 395 } |
391 | 396 |
392 /// For logging errors. | 397 /// For logging errors. |
393 SourceSpan _getSpan(AstNode node, [String contents]) => | 398 SourceSpan _getSpan(AstNode node, [String contents]) => |
394 _getSourceFile(contents).span(node.offset, node.end); | 399 _getSourceFile(contents).span(node.offset, node.end); |
395 /// For logging errors. | 400 /// For logging errors. |
396 SourceFile _getSourceFile([String contents]) { | 401 SourceFile _getSourceFile([String contents]) { |
397 var uri = getSourceUri(); | 402 var uri = getSourceUri(); |
398 var path = uri != null ? uri.toString() : assetId.path; | 403 var path = uri != null ? uri.toString() : assetId.path; |
399 return new SourceFile(contents != null ? contents : rawContents, url: path); | 404 return new SourceFile(contents != null ? contents : rawContents, url: path); |
(...skipping 24 matching lines...) Expand all Loading... | |
424 Source resolveAbsolute(Uri uri) { | 429 Source resolveAbsolute(Uri uri) { |
425 var assetId = _resolve(null, uri.toString(), logger, null); | 430 var assetId = _resolve(null, uri.toString(), logger, null); |
426 if (assetId == null) { | 431 if (assetId == null) { |
427 logger.error('Unable to resolve asset ID for "$uri"'); | 432 logger.error('Unable to resolve asset ID for "$uri"'); |
428 return null; | 433 return null; |
429 } | 434 } |
430 var source = _resolver.sources[assetId]; | 435 var source = _resolver.sources[assetId]; |
431 // Analyzer expects that sources which are referenced but do not exist yet | 436 // Analyzer expects that sources which are referenced but do not exist yet |
432 // still exist, so just make an empty source. | 437 // still exist, so just make an empty source. |
433 if (source == null) { | 438 if (source == null) { |
434 source = new _AssetBasedSource(assetId, _resolver); | 439 source = new _AssetBasedSource(assetId, _resolver, uri); |
Siggi Cherem (dart-lang)
2014/08/04 22:43:55
if this is intended to be the resolved uri, then I
| |
435 _resolver.sources[assetId] = source; | 440 _resolver.sources[assetId] = source; |
436 } | 441 } |
437 return source; | 442 return source; |
438 } | 443 } |
439 | 444 |
440 Source fromEncoding(UriKind kind, Uri uri) => | 445 Source fromEncoding(UriKind kind, Uri uri) => |
441 throw new UnsupportedError('fromEncoding is not supported'); | 446 throw new UnsupportedError('fromEncoding is not supported'); |
442 | 447 |
443 Uri restoreAbsolute(Source source) => | 448 Uri restoreAbsolute(Source source) => |
444 throw new UnsupportedError('restoreAbsolute is not supported'); | 449 throw new UnsupportedError('restoreAbsolute is not supported'); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
539 | 544 |
540 void apply(ChangeSet changeSet) { | 545 void apply(ChangeSet changeSet) { |
541 if (!source.updateContents(content)) return; | 546 if (!source.updateContents(content)) return; |
542 if (source._revision == 1 && source._contents != null) { | 547 if (source._revision == 1 && source._contents != null) { |
543 changeSet.addedSource(source); | 548 changeSet.addedSource(source); |
544 } else { | 549 } else { |
545 changeSet.changedSource(source); | 550 changeSet.changedSource(source); |
546 } | 551 } |
547 } | 552 } |
548 } | 553 } |
OLD | NEW |