From 5c86543312802629f294e13fef799ce88ce35a4f Mon Sep 17 00:00:00 2001 From: Elliott Brooks <21270878+elliette@users.noreply.github.com> Date: Fri, 24 Jan 2025 15:26:01 -0800 Subject: [PATCH] Inspector works for widget previews --- .../preferences/_inspector_preferences.dart | 60 ++++++++++++++----- .../preferences_controller_test.dart | 4 ++ 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/packages/devtools_app/lib/src/shared/preferences/_inspector_preferences.dart b/packages/devtools_app/lib/src/shared/preferences/_inspector_preferences.dart index 7f94e0e952a..cc6f9ed51e7 100644 --- a/packages/devtools_app/lib/src/shared/preferences/_inspector_preferences.dart +++ b/packages/devtools_app/lib/src/shared/preferences/_inspector_preferences.dart @@ -47,6 +47,21 @@ class InspectorPreferencesController extends DisposableController 'inspector.defaultDetailsViewType'; static const _customPubRootDirectoriesStoragePrefix = 'inspector.customPubRootDirectories'; + + static const _dartToolDir = '.dart_tool'; + static const _libDir = 'lib'; + static const _webDir = 'web'; + static const _packagesDir = 'packages'; + + /// The following are directories that DevTools can use to determine the + /// correct pub root directory. + static const _knownDirectories = { + _dartToolDir, + _libDir, + _webDir, + _packagesDir, + }; + String? _mainScriptDir; bool _checkedFlutterPubRoot = false; @@ -300,21 +315,8 @@ class InspectorPreferencesController extends DisposableController // /third_party/dart): if (isGoogle3Path(parts)) { pubRootDirectory = _pubRootDirectoryForGoogle3(parts); - } else { - final parts = fileUriString.split('/'); - - for (int i = parts.length - 1; i >= 0; i--) { - final part = parts[i]; - if (part == 'lib' || part == 'web') { - pubRootDirectory = parts.sublist(0, i).join('/'); - break; - } - - if (part == 'packages') { - pubRootDirectory = parts.sublist(0, i + 1).join('/'); - break; - } - } + } else if (_pathContainsKnownDirectories(parts)) { + pubRootDirectory = _determinePubRootFromKnownDirectories(parts); } pubRootDirectory ??= (parts..removeLast()).join('/'); // Make sure the root directory ends with /, otherwise we will patch with @@ -326,6 +328,34 @@ class InspectorPreferencesController extends DisposableController return pubRootDirectory; } + bool _pathContainsKnownDirectories(List pathParts) => + _knownDirectories.any((dir) => pathParts.contains(dir)); + + String? _determinePubRootFromKnownDirectories(List pathParts) { + pathParts = _maybeRemoveDartToolDirectory(pathParts); + for (int i = pathParts.length - 1; i >= 0; i--) { + final part = pathParts[i]; + + if (part == _libDir || part == _webDir) { + pathParts = pathParts.sublist(0, i); + break; + } + + if (part == _packagesDir) { + pathParts = pathParts.sublist(0, i + 1); + break; + } + } + return pathParts.join('/'); + } + + List _maybeRemoveDartToolDirectory(List pathParts) { + if (pathParts.contains(_dartToolDir)) { + return pathParts.sublist(0, pathParts.indexOf(_dartToolDir)); + } + return pathParts; + } + String? _pubRootDirectoryForGoogle3(List pathParts) { final strippedParts = stripGoogle3(pathParts); if (strippedParts.isEmpty) return null; diff --git a/packages/devtools_app/test/shared/preferences/preferences_controller_test.dart b/packages/devtools_app/test/shared/preferences/preferences_controller_test.dart index e5d133a03e0..de1078317b8 100644 --- a/packages/devtools_app/test/shared/preferences/preferences_controller_test.dart +++ b/packages/devtools_app/test/shared/preferences/preferences_controller_test.dart @@ -162,6 +162,10 @@ void main() { 'my_user/google3/dart_apps/test_app/lib/main.dart': '/dart_apps/', 'my_user/google3/third_party/dart/dart_apps/test_app/lib/main.dart': '/third_party/dart/', + 'my_user/fake_app/.dart_tool/widget_preview_scaffold': + 'my_user/fake_app/', + 'my_user/fake_app/lib/.dart_tool/widget_preview_scaffold': + 'my_user/fake_app/', }; for (final MapEntry(key: rootLib, value: expectedPubRoot)