Skip to content

Commit 76f056a

Browse files
authored
Improve pub root directory interface (#106567)
The pubRoot directory interface is being changed to promote adding and removing directories
1 parent 0b6e0e2 commit 76f056a

File tree

3 files changed

+965
-56
lines changed

3 files changed

+965
-56
lines changed

packages/flutter/lib/src/widgets/widget_inspector.dart

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,12 @@ mixin WidgetInspectorService {
748748
final Map<Object, String> _objectToId = Map<Object, String>.identity();
749749
int _nextId = 0;
750750

751+
/// the pubRootDirectories that are currently configured for the widget inspector.
752+
///
753+
/// This is for testing use only.
754+
@visibleForTesting
755+
@protected
756+
List<String>? get pubRootDirectories => _pubRootDirectories == null ? const <String>[] : List<String>.from(_pubRootDirectories!);
751757
List<String>? _pubRootDirectories;
752758
/// Memoization for [_isLocalCreationLocation].
753759
final HashMap<String, bool> _isLocalCreationCache = HashMap<String, bool>();
@@ -1106,6 +1112,20 @@ mixin WidgetInspectorService {
11061112
return null;
11071113
},
11081114
);
1115+
_registerServiceExtensionVarArgs(
1116+
name: 'addPubRootDirectories',
1117+
callback: (List<String> args) async {
1118+
addPubRootDirectories(args);
1119+
return null;
1120+
},
1121+
);
1122+
_registerServiceExtensionVarArgs(
1123+
name: 'removePubRootDirectories',
1124+
callback: (List<String> args) async {
1125+
removePubRootDirectories(args);
1126+
return null;
1127+
},
1128+
);
11091129
_registerServiceExtensionWithArg(
11101130
name: 'setSelectionById',
11111131
callback: setSelectionById,
@@ -1233,7 +1253,7 @@ mixin WidgetInspectorService {
12331253
void resetAllState() {
12341254
disposeAllGroups();
12351255
selection.clear();
1236-
setPubRootDirectories(<String>[]);
1256+
resetPubRootDirectories();
12371257
}
12381258

12391259
/// Free all references to objects in a group.
@@ -1355,12 +1375,66 @@ mixin WidgetInspectorService {
13551375
/// project.
13561376
///
13571377
/// The local project directories are used to distinguish widgets created by
1358-
/// the local project over widgets created from inside the framework.
1378+
/// the local project from widgets created from inside the framework
1379+
/// or other packages.
13591380
@protected
1381+
@Deprecated(
1382+
'Use addPubRootDirectories instead. '
1383+
'This feature was deprecated after v3.1.0-9.0.pre.',
1384+
)
13601385
void setPubRootDirectories(List<String> pubRootDirectories) {
1361-
_pubRootDirectories = pubRootDirectories
1362-
.map<String>((String directory) => Uri.parse(directory).path)
1363-
.toList();
1386+
addPubRootDirectories(pubRootDirectories);
1387+
}
1388+
1389+
/// Resets the list of directories, that should be considered part of the
1390+
/// local project, to the value passed in [pubRootDirectories].
1391+
///
1392+
/// The local project directories are used to distinguish widgets created by
1393+
/// the local project from widgets created from inside the framework
1394+
/// or other packages.
1395+
@visibleForTesting
1396+
@protected
1397+
void resetPubRootDirectories() {
1398+
_pubRootDirectories = <String>[];
1399+
_isLocalCreationCache.clear();
1400+
}
1401+
1402+
/// Add a list of directories that should be considered part of the local
1403+
/// project.
1404+
///
1405+
/// The local project directories are used to distinguish widgets created by
1406+
/// the local project from widgets created from inside the framework
1407+
/// or other packages.
1408+
@protected
1409+
void addPubRootDirectories(List<String> pubRootDirectories) {
1410+
pubRootDirectories = pubRootDirectories.map<String>((String directory) => Uri.parse(directory).path).toList();
1411+
1412+
final Set<String> directorySet = Set<String>.from(pubRootDirectories);
1413+
if(_pubRootDirectories != null) {
1414+
directorySet.addAll(_pubRootDirectories!);
1415+
}
1416+
1417+
_pubRootDirectories = directorySet.toList();
1418+
_isLocalCreationCache.clear();
1419+
}
1420+
1421+
/// Remove a list of directories that should no longer be considered part
1422+
/// of the local project.
1423+
///
1424+
/// The local project directories are used to distinguish widgets created by
1425+
/// the local project from widgets created from inside the framework
1426+
/// or other packages.
1427+
@protected
1428+
void removePubRootDirectories(List<String> pubRootDirectories) {
1429+
if (_pubRootDirectories == null) {
1430+
return;
1431+
}
1432+
pubRootDirectories = pubRootDirectories.map<String>((String directory) => Uri.parse(directory).path).toList();
1433+
1434+
final Set<String> directorySet = Set<String>.from(_pubRootDirectories!);
1435+
directorySet.removeAll(pubRootDirectories);
1436+
1437+
_pubRootDirectories = directorySet.toList();
13641438
_isLocalCreationCache.clear();
13651439
}
13661440

packages/flutter/test/foundation/service_extensions_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ void main() {
176176
// framework, excluding any that are for the widget inspector
177177
// (see widget_inspector_test.dart for tests of the ext.flutter.inspector
178178
// service extensions).
179-
const int serviceExtensionCount = 36;
179+
const int serviceExtensionCount = 38;
180180

181181
expect(binding.extensions.length, serviceExtensionCount + widgetInspectorExtensionCount - disabledExtensions);
182182

0 commit comments

Comments
 (0)