Skip to content

Commit ea4aee5

Browse files
authored
Inspector works for widget previews (#8808)
1 parent c4d8c63 commit ea4aee5

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

packages/devtools_app/lib/src/shared/preferences/_inspector_preferences.dart

+45-15
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ class InspectorPreferencesController extends DisposableController
4747
'inspector.defaultDetailsViewType';
4848
static const _customPubRootDirectoriesStoragePrefix =
4949
'inspector.customPubRootDirectories';
50+
51+
static const _dartToolDir = '.dart_tool';
52+
static const _libDir = 'lib';
53+
static const _webDir = 'web';
54+
static const _packagesDir = 'packages';
55+
56+
/// The following are directories that DevTools can use to determine the
57+
/// correct pub root directory.
58+
static const _knownDirectories = {
59+
_dartToolDir,
60+
_libDir,
61+
_webDir,
62+
_packagesDir,
63+
};
64+
5065
String? _mainScriptDir;
5166
bool _checkedFlutterPubRoot = false;
5267

@@ -300,21 +315,8 @@ class InspectorPreferencesController extends DisposableController
300315
// /third_party/dart):
301316
if (isGoogle3Path(parts)) {
302317
pubRootDirectory = _pubRootDirectoryForGoogle3(parts);
303-
} else {
304-
final parts = fileUriString.split('/');
305-
306-
for (int i = parts.length - 1; i >= 0; i--) {
307-
final part = parts[i];
308-
if (part == 'lib' || part == 'web') {
309-
pubRootDirectory = parts.sublist(0, i).join('/');
310-
break;
311-
}
312-
313-
if (part == 'packages') {
314-
pubRootDirectory = parts.sublist(0, i + 1).join('/');
315-
break;
316-
}
317-
}
318+
} else if (_pathContainsKnownDirectories(parts)) {
319+
pubRootDirectory = _determinePubRootFromKnownDirectories(parts);
318320
}
319321
pubRootDirectory ??= (parts..removeLast()).join('/');
320322
// Make sure the root directory ends with /, otherwise we will patch with
@@ -326,6 +328,34 @@ class InspectorPreferencesController extends DisposableController
326328
return pubRootDirectory;
327329
}
328330

331+
bool _pathContainsKnownDirectories(List<String> pathParts) =>
332+
_knownDirectories.any((dir) => pathParts.contains(dir));
333+
334+
String? _determinePubRootFromKnownDirectories(List<String> pathParts) {
335+
pathParts = _maybeRemoveDartToolDirectory(pathParts);
336+
for (int i = pathParts.length - 1; i >= 0; i--) {
337+
final part = pathParts[i];
338+
339+
if (part == _libDir || part == _webDir) {
340+
pathParts = pathParts.sublist(0, i);
341+
break;
342+
}
343+
344+
if (part == _packagesDir) {
345+
pathParts = pathParts.sublist(0, i + 1);
346+
break;
347+
}
348+
}
349+
return pathParts.join('/');
350+
}
351+
352+
List<String> _maybeRemoveDartToolDirectory(List<String> pathParts) {
353+
if (pathParts.contains(_dartToolDir)) {
354+
return pathParts.sublist(0, pathParts.indexOf(_dartToolDir));
355+
}
356+
return pathParts;
357+
}
358+
329359
String? _pubRootDirectoryForGoogle3(List<String> pathParts) {
330360
final strippedParts = stripGoogle3(pathParts);
331361
if (strippedParts.isEmpty) return null;

packages/devtools_app/test/shared/preferences/preferences_controller_test.dart

+4
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ void main() {
162162
'my_user/google3/dart_apps/test_app/lib/main.dart': '/dart_apps/',
163163
'my_user/google3/third_party/dart/dart_apps/test_app/lib/main.dart':
164164
'/third_party/dart/',
165+
'my_user/fake_app/.dart_tool/widget_preview_scaffold':
166+
'my_user/fake_app/',
167+
'my_user/fake_app/lib/.dart_tool/widget_preview_scaffold':
168+
'my_user/fake_app/',
165169
};
166170

167171
for (final MapEntry(key: rootLib, value: expectedPubRoot)

0 commit comments

Comments
 (0)