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

Side by Side Diff: pkg/analyzer/lib/src/generated/source.dart

Issue 1223833008: Package map source factory support [FIXED]. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 5 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 unified diff | Download patch
OLDNEW
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 // This code was auto-generated, is not intended to be edited, and is subject to 5 // This code was auto-generated, is not intended to be edited, and is subject to
6 // significant change. Please see the README file for more information. 6 // significant change. Please see the README file for more information.
7 7
8 library engine.source; 8 library engine.source;
9 9
10 import 'dart:collection'; 10 import 'dart:collection';
11 import "dart:math" as math; 11 import "dart:math" as math;
12 12
13 import 'package:analyzer/file_system/file_system.dart'; 13 import 'package:analyzer/file_system/file_system.dart';
14 import 'package:analyzer/file_system/physical_file_system.dart';
14 import 'package:analyzer/source/package_map_resolver.dart'; 15 import 'package:analyzer/source/package_map_resolver.dart';
16 import 'package:analyzer/src/generated/utilities_dart.dart' as utils;
15 import 'package:analyzer/task/model.dart'; 17 import 'package:analyzer/task/model.dart';
18 import 'package:package_config/packages.dart';
16 import 'package:path/path.dart' as pathos; 19 import 'package:path/path.dart' as pathos;
17 20
18 import 'engine.dart'; 21 import 'engine.dart';
19 import 'java_core.dart'; 22 import 'java_core.dart';
20 import 'java_engine.dart'; 23 import 'java_engine.dart';
21 import 'java_io.dart' show JavaFile; 24 import 'java_io.dart' show JavaFile;
22 import 'sdk.dart' show DartSdk; 25 import 'sdk.dart' show DartSdk;
23 import 'source_io.dart' show FileBasedSource; 26 import 'source_io.dart' show FileBasedSource;
24 27
25 /** 28 /**
(...skipping 542 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 * Instances of the class `SourceFactory` resolve possibly relative URI's agains t an existing 571 * Instances of the class `SourceFactory` resolve possibly relative URI's agains t an existing
569 * [Source]. 572 * [Source].
570 */ 573 */
571 class SourceFactory { 574 class SourceFactory {
572 /** 575 /**
573 * The analysis context that this source factory is associated with. 576 * The analysis context that this source factory is associated with.
574 */ 577 */
575 AnalysisContext context; 578 AnalysisContext context;
576 579
577 /** 580 /**
581 * URI processor used to find mappings for `package:` URIs found in a `.packag es` config
582 * file.
583 */
584 final Packages _packages;
585
586 /**
587 * Resource provider used in working with package maps.
588 */
589 final ResourceProvider _resourceProvider;
590
591 /**
578 * The resolvers used to resolve absolute URI's. 592 * The resolvers used to resolve absolute URI's.
579 */ 593 */
580 final List<UriResolver> _resolvers; 594 final List<UriResolver> _resolvers;
581 595
582 /** 596 /**
583 * The predicate to determine is [Source] is local. 597 * The predicate to determine is [Source] is local.
584 */ 598 */
585 LocalSourcePredicate _localSourcePredicate = LocalSourcePredicate.NOT_SDK; 599 LocalSourcePredicate _localSourcePredicate = LocalSourcePredicate.NOT_SDK;
586 600
587 /** 601 /**
588 * Initialize a newly created source factory. 602 * Initialize a newly created source factory with the given absolute URI [reso lvers] and
589 * 603 * optional [packages] resolution helper.
590 * @param resolvers the resolvers used to resolve absolute URI's
591 */ 604 */
592 SourceFactory(this._resolvers); 605 SourceFactory(this._resolvers,
606 [this._packages, ResourceProvider resourceProvider])
607 : _resourceProvider = resourceProvider != null
608 ? resourceProvider
609 : PhysicalResourceProvider.INSTANCE;
593 610
594 /** 611 /**
595 * Return the [DartSdk] associated with this [SourceFactory], or `null` if the re 612 * Return the [DartSdk] associated with this [SourceFactory], or `null` if the re
596 * is no such SDK. 613 * is no such SDK.
597 * 614 *
598 * @return the [DartSdk] associated with this [SourceFactory], or `null` if 615 * @return the [DartSdk] associated with this [SourceFactory], or `null` if
599 * there is no such SDK 616 * there is no such SDK
600 */ 617 */
601 DartSdk get dartSdk { 618 DartSdk get dartSdk {
602 for (UriResolver resolver in _resolvers) { 619 for (UriResolver resolver in _resolvers) {
(...skipping 10 matching lines...) Expand all
613 * 630 *
614 * @param localSourcePredicate the predicate to determine is [Source] is local 631 * @param localSourcePredicate the predicate to determine is [Source] is local
615 */ 632 */
616 void set localSourcePredicate(LocalSourcePredicate localSourcePredicate) { 633 void set localSourcePredicate(LocalSourcePredicate localSourcePredicate) {
617 this._localSourcePredicate = localSourcePredicate; 634 this._localSourcePredicate = localSourcePredicate;
618 } 635 }
619 636
620 /// A table mapping package names to paths of directories containing 637 /// A table mapping package names to paths of directories containing
621 /// the package (or [null] if there is no registered package URI resolver). 638 /// the package (or [null] if there is no registered package URI resolver).
622 Map<String, List<Folder>> get packageMap { 639 Map<String, List<Folder>> get packageMap {
640 // Start by looking in .packages.
641 if (_packages != null) {
642 Map<String, List<Folder>> packageMap = <String, List<Folder>>{};
643 _packages.asMap().forEach((String name, Uri uri) {
644 if (uri.scheme == 'file' || uri.scheme == '' /* unspecified */) {
645 packageMap[name] =
646 <Folder>[_resourceProvider.getFolder(uri.toFilePath())];
647 }
648 });
649 return packageMap;
650 }
651
652 // Default to the PackageMapUriResolver.
623 PackageMapUriResolver resolver = _resolvers.firstWhere( 653 PackageMapUriResolver resolver = _resolvers.firstWhere(
624 (r) => r is PackageMapUriResolver, orElse: () => null); 654 (r) => r is PackageMapUriResolver, orElse: () => null);
625 return resolver != null ? resolver.packageMap : null; 655 return resolver != null ? resolver.packageMap : null;
626 } 656 }
627 657
628 /** 658 /**
629 * Return a source object representing the given absolute URI, or `null` if th e URI is not a 659 * Return a source object representing the given absolute URI, or `null` if th e URI is not a
630 * valid URI or if it is not an absolute URI. 660 * valid URI or if it is not an absolute URI.
631 * 661 *
632 * @param absoluteUri the absolute URI to be resolved 662 * @param absoluteUri the absolute URI to be resolved
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 } 750 }
721 751
722 /** 752 /**
723 * Return an absolute URI that represents the given source, or `null` if a val id URI cannot 753 * Return an absolute URI that represents the given source, or `null` if a val id URI cannot
724 * be computed. 754 * be computed.
725 * 755 *
726 * @param source the source to get URI for 756 * @param source the source to get URI for
727 * @return the absolute URI representing the given source 757 * @return the absolute URI representing the given source
728 */ 758 */
729 Uri restoreUri(Source source) { 759 Uri restoreUri(Source source) {
760 // First see if a resolver can restore the URI.
730 for (UriResolver resolver in _resolvers) { 761 for (UriResolver resolver in _resolvers) {
731 Uri uri = resolver.restoreAbsolute(source); 762 Uri uri = resolver.restoreAbsolute(source);
732 if (uri != null) { 763 if (uri != null) {
764 // Now see if there's a package mapping.
765 Uri packageMappedUri = _getPackageMapping(uri);
766 if (packageMappedUri != null) {
767 return packageMappedUri;
768 }
769 // Fall back to the resolver's computed URI.
733 return uri; 770 return uri;
734 } 771 }
735 } 772 }
773
736 return null; 774 return null;
737 } 775 }
738 776
777 Uri _getPackageMapping(Uri sourceUri) {
778 if (_packages == null) {
779 return null;
780 }
781 if (sourceUri.scheme != 'file') {
782 //TODO(pquitslund): verify this works for non-file URIs.
783 return null;
784 }
785
786 Uri packageUri;
787 _packages.asMap().forEach((String name, Uri uri) {
788 if (packageUri == null) {
789 if (utils.startsWith(sourceUri, uri)) {
790 packageUri = Uri.parse(
Brian Wilkerson 2015/07/06 22:17:48 If there are multiple matches, do we want the firs
pquitslund 2015/07/07 16:10:23 I *think* the test (packageUri == null) above guar
Brian Wilkerson 2015/07/07 16:18:03 Yes, I missed that.
791 'package:$name/${sourceUri.path.substring(uri.path.length)}');
792 }
793 }
794 });
795 return packageUri;
796 }
797
739 /** 798 /**
740 * Return a source object representing the URI that results from resolving the given (possibly 799 * Return a source object representing the URI that results from resolving the given (possibly
741 * relative) contained URI against the URI associated with an existing source object, or 800 * relative) contained URI against the URI associated with an existing source object, or
742 * `null` if the URI could not be resolved. 801 * `null` if the URI could not be resolved.
743 * 802 *
744 * @param containingSource the source containing the given URI 803 * @param containingSource the source containing the given URI
745 * @param containedUri the (possibly relative) URI to be resolved against the containing source 804 * @param containedUri the (possibly relative) URI to be resolved against the containing source
746 * @return the source representing the contained URI 805 * @return the source representing the contained URI
747 * @throws AnalysisException if either the contained URI is invalid or if it c annot be resolved 806 * @throws AnalysisException if either the contained URI is invalid or if it c annot be resolved
748 * against the source object's URI 807 * against the source object's URI
749 */ 808 */
750 Source _internalResolveUri(Source containingSource, Uri containedUri) { 809 Source _internalResolveUri(Source containingSource, Uri containedUri) {
751 if (!containedUri.isAbsolute) { 810 if (!containedUri.isAbsolute) {
752 if (containingSource == null) { 811 if (containingSource == null) {
753 throw new AnalysisException( 812 throw new AnalysisException(
754 "Cannot resolve a relative URI without a containing source: $contain edUri"); 813 "Cannot resolve a relative URI without a containing source: $contain edUri");
755 } 814 }
756 containedUri = containingSource.resolveRelativeUri(containedUri); 815 containedUri = containingSource.resolveRelativeUri(containedUri);
757 } 816 }
817 // Now check .packages.
818 if (_packages != null && containedUri.scheme == 'package') {
819 Uri packageUri =
820 _packages.resolve(containedUri, notFound: (Uri packageUri) => null);
821 //TODO(pquitslund): package_config needs to be updated to set schemes for file URIs.
822 if (packageUri != null && packageUri.scheme == '') {
823 containedUri = packageUri.replace(scheme: 'file');
824 }
825 }
758 for (UriResolver resolver in _resolvers) { 826 for (UriResolver resolver in _resolvers) {
759 Source result = resolver.resolveAbsolute(containedUri); 827 Source result = resolver.resolveAbsolute(containedUri);
760 if (result != null) { 828 if (result != null) {
761 return result; 829 return result;
762 } 830 }
763 } 831 }
764 return null; 832 return null;
765 } 833 }
766 } 834 }
767 835
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
999 1067
1000 /** 1068 /**
1001 * Return an absolute URI that represents the given source, or `null` if a val id URI cannot 1069 * Return an absolute URI that represents the given source, or `null` if a val id URI cannot
1002 * be computed. 1070 * be computed.
1003 * 1071 *
1004 * @param source the source to get URI for 1072 * @param source the source to get URI for
1005 * @return the absolute URI representing the given source 1073 * @return the absolute URI representing the given source
1006 */ 1074 */
1007 Uri restoreAbsolute(Source source) => null; 1075 Uri restoreAbsolute(Source source) => null;
1008 } 1076 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/file_system/physical_file_system.dart ('k') | pkg/analyzer/lib/src/generated/source_io.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698