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

Side by Side Diff: pkg/analyzer/lib/source/package_map_resolver.dart

Issue 953863004: When converting files to package URI's, favor longer directory matches. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/analyzer/test/source/package_map_resolver_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 library source.package_map_resolver; 5 library source.package_map_resolver;
6 6
7 import 'package:analyzer/file_system/file_system.dart'; 7 import 'package:analyzer/file_system/file_system.dart';
8 import 'package:analyzer/src/generated/source.dart'; 8 import 'package:analyzer/src/generated/source.dart';
9 import 'package:analyzer/src/util/asserts.dart' as asserts; 9 import 'package:analyzer/src/util/asserts.dart' as asserts;
10 10
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 } 70 }
71 // Return a NonExistingSource instance. 71 // Return a NonExistingSource instance.
72 // This helps provide more meaningful error messages to users 72 // This helps provide more meaningful error messages to users
73 // (a missing file error, as opposed to an invalid URI error). 73 // (a missing file error, as opposed to an invalid URI error).
74 return new NonExistingSource(uri.toString(), UriKind.PACKAGE_URI); 74 return new NonExistingSource(uri.toString(), UriKind.PACKAGE_URI);
75 } 75 }
76 76
77 @override 77 @override
78 Uri restoreAbsolute(Source source) { 78 Uri restoreAbsolute(Source source) {
79 String sourcePath = source.fullName; 79 String sourcePath = source.fullName;
80 Uri bestMatch;
81 int bestMatchLength = -1;
80 for (String pkgName in packageMap.keys) { 82 for (String pkgName in packageMap.keys) {
81 List<Folder> pkgFolders = packageMap[pkgName]; 83 List<Folder> pkgFolders = packageMap[pkgName];
82 for (int i = 0; i < pkgFolders.length; i++) { 84 for (int i = 0; i < pkgFolders.length; i++) {
83 Folder pkgFolder = pkgFolders[i]; 85 Folder pkgFolder = pkgFolders[i];
84 String pkgFolderPath = pkgFolder.path; 86 String pkgFolderPath = pkgFolder.path;
85 // TODO(paulberry): figure out the right thing to do for Windows. 87 // TODO(paulberry): figure out the right thing to do for Windows.
86 if (sourcePath.startsWith(pkgFolderPath + '/')) { 88 if (pkgFolderPath.length > bestMatchLength && sourcePath.startsWith(pkgF olderPath + '/')) {
87 String relPath = sourcePath.substring(pkgFolderPath.length + 1); 89 String relPath = sourcePath.substring(pkgFolderPath.length + 1);
88 if (_isReversibleTranslation(pkgFolders, i, relPath)) { 90 if (_isReversibleTranslation(pkgFolders, i, relPath)) {
89 return Uri.parse('$PACKAGE_SCHEME:$pkgName/$relPath'); 91 bestMatch = Uri.parse('$PACKAGE_SCHEME:$pkgName/$relPath');
92 bestMatchLength = pkgFolderPath.length;
90 } 93 }
91 } 94 }
92 } 95 }
93 } 96 }
94 return null; 97 return bestMatch;
95 } 98 }
96 99
97 /** 100 /**
98 * A translation from file path to package URI has just been found for 101 * A translation from file path to package URI has just been found for
99 * using the [packageDirIndex]th element of [packageDirs], and appending the 102 * using the [packageDirIndex]th element of [packageDirs], and appending the
100 * relative path [relPath]. Determine whether the translation is reversible; 103 * relative path [relPath]. Determine whether the translation is reversible;
101 * that is, whether translating the package URI pack to a file path will 104 * that is, whether translating the package URI pack to a file path will
102 * produce the file path we started with. 105 * produce the file path we started with.
103 */ 106 */
104 bool _isReversibleTranslation(List<Folder> packageDirs, int packageDirIndex, 107 bool _isReversibleTranslation(List<Folder> packageDirs, int packageDirIndex,
(...skipping 12 matching lines...) Expand all
117 return true; 120 return true;
118 } 121 }
119 122
120 /** 123 /**
121 * Returns `true` if [uri] is a `package` URI. 124 * Returns `true` if [uri] is a `package` URI.
122 */ 125 */
123 static bool isPackageUri(Uri uri) { 126 static bool isPackageUri(Uri uri) {
124 return uri.scheme == PACKAGE_SCHEME; 127 return uri.scheme == PACKAGE_SCHEME;
125 } 128 }
126 } 129 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/source/package_map_resolver_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698