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

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

Issue 2403733002: Implement BazelPackageUriResolver.restoreAbsolute(). (Closed)
Patch Set: Created 4 years, 2 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
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/bazel_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) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 analyzer.src.generated.bazel; 5 library analyzer.src.generated.bazel;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 import 'dart:core'; 8 import 'dart:core';
9 9
10 import 'package:analyzer/file_system/file_system.dart'; 10 import 'package:analyzer/file_system/file_system.dart';
11 import 'package:analyzer/src/generated/source.dart'; 11 import 'package:analyzer/src/generated/source.dart';
12 import 'package:analyzer/src/generated/source_io.dart'; 12 import 'package:analyzer/src/generated/source_io.dart';
13 import 'package:analyzer/src/util/fast_uri.dart';
13 import 'package:path/path.dart'; 14 import 'package:path/path.dart';
14 15
15 /** 16 /**
16 * Instances of the class `BazelFileUriResolver` resolve `file` URI's by first 17 * Instances of the class `BazelFileUriResolver` resolve `file` URI's by first
17 * resolving file uri's in the expected way, and then by looking in the 18 * resolving file uri's in the expected way, and then by looking in the
18 * corresponding generated directories. 19 * corresponding generated directories.
19 */ 20 */
20 class BazelFileUriResolver extends ResourceUriResolver { 21 class BazelFileUriResolver extends ResourceUriResolver {
21 final BazelWorkspace workspace; 22 final BazelWorkspace workspace;
22 23
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 return file?.createSource(uri); 80 return file?.createSource(uri);
80 } else { 81 } else {
81 String packagePath = packageName.replaceAll('.', _context.separator); 82 String packagePath = packageName.replaceAll('.', _context.separator);
82 String path = 83 String path =
83 _context.join(_workspace.root, packagePath, 'lib', filePath); 84 _context.join(_workspace.root, packagePath, 'lib', filePath);
84 File file = _workspace.findFile(path); 85 File file = _workspace.findFile(path);
85 return file?.createSource(uri); 86 return file?.createSource(uri);
86 } 87 }
87 }); 88 });
88 } 89 }
90
91 @override
92 Uri restoreAbsolute(Source source) {
93 Context context = _workspace.provider.pathContext;
94 String path = source.fullName;
95
96 Uri restore(String root, String path) {
97 if (root != null && context.isWithin(root, path)) {
98 String relative = context.relative(path, from: root);
99 List<String> components = context.split(relative);
100 if (components.length >= 1 && components[0] == 'third_party') {
101 if (components.length > 4 &&
102 components[1] == 'dart' &&
103 components[3] == 'lib') {
104 String packageName = components[2];
105 String pathInLib = components.skip(4).join('/');
106 return FastUri.parse('package:$packageName/$pathInLib');
107 }
108 } else {
109 for (int i = 2; i < components.length - 1; i++) {
110 String component = components[i];
111 if (component == 'lib') {
112 String packageName = components.getRange(0, i).join('.');
113 String pathInLib = components.skip(i + 1).join('/');
114 return FastUri.parse('package:$packageName/$pathInLib');
115 }
116 }
117 }
118 }
119 return null;
120 }
121
122 // Search in each root.
123 for (String root in [
124 _workspace.bin,
125 _workspace.genfiles,
126 _workspace.readonly,
127 _workspace.root
128 ]) {
129 Uri uri = restore(root, path);
130 if (uri != null) {
131 return uri;
132 }
133 }
134
135 return null;
136 }
89 } 137 }
90 138
91 /** 139 /**
92 * Information about a Bazel workspace. 140 * Information about a Bazel workspace.
93 */ 141 */
94 class BazelWorkspace { 142 class BazelWorkspace {
95 static const String _WORKSPACE = 'WORKSPACE'; 143 static const String _WORKSPACE = 'WORKSPACE';
96 static const String _READONLY = 'READONLY'; 144 static const String _READONLY = 'READONLY';
97 145
98 final ResourceProvider provider; 146 final ResourceProvider provider;
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 Context context = provider.pathContext; 288 Context context = provider.pathContext;
241 if (provider.getFolder(context.join(root, 'blaze-genfiles')).exists) { 289 if (provider.getFolder(context.join(root, 'blaze-genfiles')).exists) {
242 return 'blaze'; 290 return 'blaze';
243 } 291 }
244 if (provider.getFolder(context.join(root, 'bazel-genfiles')).exists) { 292 if (provider.getFolder(context.join(root, 'bazel-genfiles')).exists) {
245 return 'bazel'; 293 return 'bazel';
246 } 294 }
247 return null; 295 return null;
248 } 296 }
249 } 297 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/bazel_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698