Skip to content

Commit 701c611

Browse files
srawlinsCommit Queue
authored and
Commit Queue
committed
analyzer: Simplify interface of WorkspacePackage, use File for root
Work towards #50986 Change-Id: I56c14bc941e1204b9d9cc13086f5ed92a1228585 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/428623 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent fadf8a0 commit 701c611

20 files changed

+144
-175
lines changed

pkg/analysis_server/lib/src/lsp/lsp_analysis_server.dart

+8-7
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ import 'package:analyzer_plugin/protocol/protocol_common.dart' as plugin;
4747
import 'package:analyzer_plugin/protocol/protocol_generated.dart' as plugin;
4848
import 'package:analyzer_plugin/src/protocol/protocol_internal.dart' as plugin;
4949
import 'package:analyzer_plugin/src/utilities/client_uri_converter.dart';
50-
import 'package:collection/collection.dart';
5150
import 'package:http/http.dart' as http;
5251
import 'package:meta/meta.dart';
5352

@@ -1100,12 +1099,14 @@ class LspAnalysisServer extends AnalysisServer {
11001099
var packages = <String>{};
11011100
var additionalFiles = <String>[];
11021101
for (var file in openFiles) {
1103-
var package = roots
1104-
.where((root) => root.isAnalyzed(file))
1105-
.map((root) => root.workspace.findPackageFor(file)?.root)
1106-
.firstWhereOrNull((p) => p != null);
1107-
if (package != null && !resourceProvider.getFolder(package).isRoot) {
1108-
packages.add(package);
1102+
var package =
1103+
roots
1104+
.where((root) => root.isAnalyzed(file))
1105+
.map((root) => root.workspace.findPackageFor(file)?.root)
1106+
.nonNulls
1107+
.firstOrNull;
1108+
if (package != null && !package.isRoot) {
1109+
packages.add(package.path);
11091110
} else {
11101111
additionalFiles.add(file);
11111112
}

pkg/analysis_server/lib/src/services/correction/bulk_fix_processor.dart

+3-13
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,6 @@ class BulkFixProcessor {
391391
continue;
392392
}
393393
var pathContext = context.contextRoot.resourceProvider.pathContext;
394-
var resourceProvider = workspace.provider;
395394
var packageToDeps = <PubPackage, _PubspecDeps>{};
396395

397396
for (var path in context.contextRoot.analyzedFiles()) {
@@ -404,17 +403,8 @@ class BulkFixProcessor {
404403
continue;
405404
}
406405

407-
var libPath =
408-
resourceProvider.getFolder(package.root).getChild('lib').path;
409-
var binPath =
410-
resourceProvider.getFolder(package.root).getChild('bin').path;
411-
412-
bool isPublic(String path, PubPackage package) {
413-
if (path.startsWith(libPath) || path.startsWith(binPath)) {
414-
return true;
415-
}
416-
return false;
417-
}
406+
var libPath = package.root.getChildAssumingFolder('lib');
407+
var binPath = package.root.getChildAssumingFolder('bin');
418408

419409
var pubspecDeps = packageToDeps.putIfAbsent(
420410
package,
@@ -434,7 +424,7 @@ class BulkFixProcessor {
434424
(directive is ImportDirective) ? directive.uri.stringValue : '';
435425
if (uri!.startsWith('package:')) {
436426
var name = Uri.parse(uri).pathSegments.first;
437-
if (isPublic(path, package)) {
427+
if (libPath.contains(path) || binPath.contains(path)) {
438428
pubspecDeps.packages.add(name);
439429
} else {
440430
pubspecDeps.devPackages.add(name);

pkg/analysis_server/lib/src/status/diagnostics.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ class ContextsPage extends DiagnosticPageWithNav {
10961096
var workspaceFolder = folder.provider.getFolder(workspace!.root);
10971097

10981098
void writePackage(WorkspacePackage package) {
1099-
buf.writeln(writeOption('Package root', escape(package.root)));
1099+
buf.writeln(writeOption('Package root', escape(package.root.path)));
11001100
if (package is PubPackage) {
11011101
buf.writeln(
11021102
writeOption(

pkg/analysis_server/test/src/plugin/plugin_manager_test.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -1018,12 +1018,12 @@ class TestServerCommunicationChannel implements ServerCommunicationChannel {
10181018
}
10191019

10201020
mixin _ContextRoot on ResourceProviderMixin {
1021-
ContextRootImpl _newContextRoot(String root) {
1022-
root = convertPath(root);
1021+
ContextRootImpl _newContextRoot(String rootPath) {
1022+
rootPath = convertPath(rootPath);
10231023
return ContextRootImpl(
10241024
resourceProvider,
1025-
resourceProvider.getFolder(root),
1026-
BasicWorkspace.find(resourceProvider, Packages.empty, root),
1025+
resourceProvider.getFolder(rootPath),
1026+
BasicWorkspace.find(resourceProvider, Packages.empty, rootPath),
10271027
);
10281028
}
10291029
}

pkg/analyzer/lib/src/dart/analysis/file_state_filter.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class _PubFilter implements FileStateFilter {
6767
final Set<String> dependencies;
6868

6969
factory _PubFilter(PubPackage package, String path) {
70-
var packageRootFolder = package.workspace.provider.getFolder(package.root);
70+
var packageRootFolder = package.root;
7171
var inLibOrEntryPoint =
7272
packageRootFolder.getChildAssumingFolder('lib').contains(path) ||
7373
packageRootFolder.getChildAssumingFolder('bin').contains(path) ||

pkg/analyzer/lib/src/lint/linter.dart

+4-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import 'package:analyzer/src/lint/pub.dart';
1717
import 'package:analyzer/src/lint/state.dart';
1818
import 'package:analyzer/src/workspace/workspace.dart';
1919
import 'package:meta/meta.dart';
20-
import 'package:path/path.dart' as p;
2120

2221
export 'package:analyzer/src/lint/linter_visitor.dart' show NodeLintRegistry;
2322
export 'package:analyzer/src/lint/state.dart'
@@ -263,11 +262,11 @@ abstract class LinterContext {
263262

264263
TypeSystem get typeSystem;
265264

266-
static bool _isInLibDir(String? path, WorkspacePackage? package) {
265+
static bool _isInLibDir(String? filePath, WorkspacePackage? package) {
267266
if (package == null) return false;
268-
if (path == null) return false;
269-
var libDir = p.join(package.root, 'lib');
270-
return p.isWithin(libDir, path);
267+
if (filePath == null) return false;
268+
var libDir = package.root.getChildAssumingFolder('lib');
269+
return libDir.contains(filePath);
271270
}
272271
}
273272

pkg/analyzer/lib/src/workspace/basic.dart

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class BasicWorkspace extends SimpleWorkspace {
1717
/// Each basic workspace is itself one package.
1818
late final BasicWorkspacePackage theOnlyPackage;
1919

20-
BasicWorkspace._(ResourceProvider provider, Packages packages, String root)
21-
: super(provider, packages, root) {
20+
BasicWorkspace._(ResourceProvider provider, Packages packages, Folder root)
21+
: super(provider, packages, root.path) {
2222
theOnlyPackage = BasicWorkspacePackage(root, this);
2323
}
2424

@@ -44,9 +44,9 @@ class BasicWorkspace extends SimpleWorkspace {
4444
) {
4545
Resource resource = provider.getResource(path);
4646
if (resource is File) {
47-
path = resource.parent.path;
47+
resource = resource.parent;
4848
}
49-
return BasicWorkspace._(provider, packages, path);
49+
return BasicWorkspace._(provider, packages, resource as Folder);
5050
}
5151
}
5252

@@ -57,7 +57,7 @@ class BasicWorkspace extends SimpleWorkspace {
5757
/// a given package in a [BasicWorkspace].
5858
class BasicWorkspacePackage extends WorkspacePackage {
5959
@override
60-
final String root;
60+
final Folder root;
6161

6262
@override
6363
final SimpleWorkspace workspace;
@@ -75,7 +75,7 @@ class BasicWorkspacePackage extends WorkspacePackage {
7575
// There is a 1-1 relationship between [BasicWorkspace]s and
7676
// [BasicWorkspacePackage]s. If a file is in a package's workspace, then it
7777
// is in the package as well.
78-
return workspace.provider.pathContext.isWithin(root, filePath);
78+
return workspace.provider.pathContext.isWithin(root.path, filePath);
7979
}
8080

8181
@override

pkg/analyzer/lib/src/workspace/blaze.dart

+22-29
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ class BlazeWorkspace extends Workspace
353353
if (packageName == null) {
354354
return null;
355355
}
356-
var package = BlazeWorkspacePackage(packageName, folder.path, this);
356+
var package = BlazeWorkspacePackage(packageName, folder, this);
357357
_directoryToPackage[directoryPath] = package;
358358
return package;
359359
}
@@ -591,7 +591,7 @@ class BlazeWorkspacePackage extends WorkspacePackage {
591591
final String _uriPrefix;
592592

593593
@override
594-
final String root;
594+
final Folder root;
595595

596596
@override
597597
final BlazeWorkspace workspace;
@@ -628,9 +628,7 @@ class BlazeWorkspacePackage extends WorkspacePackage {
628628

629629
@override
630630
bool isInTestDirectory(File file) {
631-
var resourceProvider = workspace.provider;
632-
var packageRoot = resourceProvider.getFolder(root);
633-
return packageRoot.getChildAssumingFolder('test').contains(file.path);
631+
return root.getChildAssumingFolder('test').contains(file.path);
634632
}
635633

636634
@override
@@ -643,43 +641,38 @@ class BlazeWorkspacePackage extends WorkspacePackage {
643641
var filePath = filePathFromSource(source);
644642
if (filePath == null) return false;
645643

646-
var libFolder = workspace.provider.pathContext.join(root, 'lib');
647-
if (workspace.provider.pathContext.isWithin(libFolder, filePath)) {
644+
var libFolder = root.getChildAssumingFolder('lib');
645+
if (libFolder.contains(filePath)) {
648646
// A file in "$root/lib" is public iff it is not in "$root/lib/src".
649-
var libSrcFolder = workspace.provider.pathContext.join(libFolder, 'src');
650-
return !workspace.provider.pathContext.isWithin(libSrcFolder, filePath);
647+
var libSrcFolder = libFolder.getChildAssumingFolder('src');
648+
return !libSrcFolder.contains(filePath);
651649
}
652650

653651
var relativeRoot = workspace.provider.pathContext.relative(
654-
root,
652+
root.path,
655653
from: workspace.root,
656654
);
657655
for (var binPath in workspace.binPaths) {
658-
libFolder = workspace.provider.pathContext.join(
659-
binPath,
660-
relativeRoot,
661-
'lib',
662-
);
663-
if (workspace.provider.pathContext.isWithin(libFolder, filePath)) {
656+
Folder bin = workspace.provider.getFolder(binPath);
657+
libFolder = bin
658+
.getChildAssumingFolder(relativeRoot)
659+
.getChildAssumingFolder('lib');
660+
if (libFolder.contains(filePath)) {
664661
// A file in "$bin/lib" is public iff it is not in "$bin/lib/src".
665-
var libSrcFolder = workspace.provider.pathContext.join(
666-
libFolder,
667-
'src',
668-
);
669-
return !workspace.provider.pathContext.isWithin(libSrcFolder, filePath);
662+
var libSrcFolder = libFolder.getChildAssumingFolder('src');
663+
return !libSrcFolder.contains(filePath);
670664
}
671665
}
672666

673-
libFolder = workspace.provider.pathContext.join(
674-
workspace.genfiles,
675-
relativeRoot,
676-
'lib',
677-
);
678-
if (workspace.provider.pathContext.isWithin(libFolder, filePath)) {
667+
libFolder = workspace.provider
668+
.getFolder(workspace.genfiles)
669+
.getChildAssumingFolder(relativeRoot)
670+
.getChildAssumingFolder('lib');
671+
if (libFolder.contains(filePath)) {
679672
// A file in "$genfiles/lib" is public iff it is not in
680673
// "$genfiles/lib/src".
681-
var libSrcFolder = workspace.provider.pathContext.join(libFolder, 'src');
682-
return !workspace.provider.pathContext.isWithin(libSrcFolder, filePath);
674+
var libSrcFolder = libFolder.getChildAssumingFolder('src');
675+
return !libSrcFolder.contains(filePath);
683676
}
684677

685678
return false;

pkg/analyzer/lib/src/workspace/gn.dart

+10-12
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class GnWorkspace extends Workspace {
9191
}
9292

9393
if (folder.getChildAssumingFile(file_paths.buildGn).exists) {
94-
return GnWorkspacePackage(folder.path, this);
94+
return GnWorkspacePackage(folder, this);
9595
}
9696
}
9797
return null;
@@ -212,7 +212,7 @@ class GnWorkspace extends Workspace {
212212
/// a given package in a GnWorkspace.
213213
class GnWorkspacePackage extends WorkspacePackage {
214214
@override
215-
final String root;
215+
final Folder root;
216216

217217
@override
218218
final GnWorkspace workspace;
@@ -226,14 +226,14 @@ class GnWorkspacePackage extends WorkspacePackage {
226226
if (workspace.findFile(filePath) == null) {
227227
return false;
228228
}
229-
if (!workspace.provider.pathContext.isWithin(root, filePath)) {
229+
if (!root.contains(filePath)) {
230230
return false;
231231
}
232232

233233
// Just because [filePath] is within [root] does not mean it is in this
234234
// package; it could be in a "subpackage." Must go through the work of
235235
// learning exactly which package [filePath] is contained in.
236-
return workspace.findPackageFor(filePath)!.root == root;
236+
return workspace.findPackageFor(filePath)!.root.path == root.path;
237237
}
238238

239239
@override
@@ -243,14 +243,12 @@ class GnWorkspacePackage extends WorkspacePackage {
243243
bool sourceIsInPublicApi(Source source) {
244244
var filePath = filePathFromSource(source);
245245
if (filePath == null) return false;
246-
var libFolder = workspace.provider.pathContext.join(root, 'lib');
247-
if (workspace.provider.pathContext.isWithin(libFolder, filePath)) {
248-
var libSrcFolder = workspace.provider.pathContext.join(
249-
root,
250-
'lib',
251-
'src',
252-
);
253-
return !workspace.provider.pathContext.isWithin(libSrcFolder, filePath);
246+
var libFolder = root.getChildAssumingFolder('lib');
247+
if (libFolder.contains(filePath)) {
248+
var libSrcFolder = root
249+
.getChildAssumingFolder('lib')
250+
.getChildAssumingFolder('src');
251+
return !libSrcFolder.contains(filePath);
254252
}
255253
return false;
256254
}

0 commit comments

Comments
 (0)