From b1ffd1e303360a564acc2112b7de858dad1af4bc Mon Sep 17 00:00:00 2001 From: Hashir Shoaib Date: Wed, 22 May 2024 02:08:20 +0500 Subject: [PATCH] [go_router] New feature improve debug full path (#6714) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR fixes https://github.com/flutter/flutter/issues/148121 - Replaced `=>` with `| `, `├─` and `└─ ` to improve readability - It prints the Widget name for easy referencing - Shell routes does not have their own paths for it is presented as ` (Shell route)` in the tree - Prints the widget name of the routes it is building. --- AUTHORS | 1 + packages/go_router/AUTHORS | 1 + packages/go_router/CHANGELOG.md | 4 ++ packages/go_router/lib/src/configuration.dart | 65 +++++++++++++++++-- packages/go_router/pubspec.yaml | 2 +- .../go_router/test/configuration_test.dart | 20 +++--- packages/go_router/test/logging_test.dart | 4 +- 7 files changed, 78 insertions(+), 19 deletions(-) diff --git a/AUTHORS b/AUTHORS index 2d6cec4ff9189..6ea7eeec4f513 100644 --- a/AUTHORS +++ b/AUTHORS @@ -76,3 +76,4 @@ Daniele Cambi Michele Benedetti Taskulu LDA LinXunFeng +Hashir Shoaib diff --git a/packages/go_router/AUTHORS b/packages/go_router/AUTHORS index fa2a9bee3bada..7174a736ef5de 100644 --- a/packages/go_router/AUTHORS +++ b/packages/go_router/AUTHORS @@ -5,3 +5,4 @@ Google Inc. csells@sellsbrothers.com +Hashir Shoaib diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md index d33dd28b23788..7b3e451ba90b9 100644 --- a/packages/go_router/CHANGELOG.md +++ b/packages/go_router/CHANGELOG.md @@ -1,3 +1,7 @@ +## 14.1.3 + +- Improves the logging of routes when `debugLogDiagnostics` is enabled or `debugKnownRoutes() is called. Explains the position of shell routes in the route tree. Prints the widget name of the routes it is building. + ## 14.1.2 - Fixes issue that path parameters are not set when using the `goBranch`. diff --git a/packages/go_router/lib/src/configuration.dart b/packages/go_router/lib/src/configuration.dart index bef882e14605e..d1cca5ce61f47 100644 --- a/packages/go_router/lib/src/configuration.dart +++ b/packages/go_router/lib/src/configuration.dart @@ -525,7 +525,8 @@ class RouteConfiguration { String debugKnownRoutes() { final StringBuffer sb = StringBuffer(); sb.writeln('Full paths for routes:'); - _debugFullPathsFor(_routingConfig.value.routes, '', 0, sb); + _debugFullPathsFor( + _routingConfig.value.routes, '', const <_DecorationType>[], sb); if (_nameToPath.isNotEmpty) { sb.writeln('known full paths for route names:'); @@ -538,15 +539,50 @@ class RouteConfiguration { } void _debugFullPathsFor(List routes, String parentFullpath, - int depth, StringBuffer sb) { - for (final RouteBase route in routes) { + List<_DecorationType> parentDecoration, StringBuffer sb) { + for (final (int index, RouteBase route) in routes.indexed) { + final List<_DecorationType> decoration = + _getDecoration(parentDecoration, index, routes.length); + final String decorationString = + decoration.map((_DecorationType e) => e.toString()).join(); + String path = parentFullpath; if (route is GoRoute) { - final String fullPath = concatenatePaths(parentFullpath, route.path); - sb.writeln(' => ${''.padLeft(depth * 2)}$fullPath'); - _debugFullPathsFor(route.routes, fullPath, depth + 1, sb); + path = concatenatePaths(parentFullpath, route.path); + final String? screenName = + route.builder?.runtimeType.toString().split('=> ').last; + sb.writeln('$decorationString$path ' + '${screenName == null ? '' : '($screenName)'}'); } else if (route is ShellRouteBase) { - _debugFullPathsFor(route.routes, parentFullpath, depth, sb); + sb.writeln('$decorationString (ShellRoute)'); + } + _debugFullPathsFor(route.routes, path, decoration, sb); + } + } + + List<_DecorationType> _getDecoration( + List<_DecorationType> parentDecoration, + int index, + int length, + ) { + final Iterable<_DecorationType> newDecoration = + parentDecoration.map((_DecorationType e) { + switch (e) { + // swap + case _DecorationType.branch: + return _DecorationType.parentBranch; + case _DecorationType.leaf: + return _DecorationType.none; + // no swap + case _DecorationType.parentBranch: + return _DecorationType.parentBranch; + case _DecorationType.none: + return _DecorationType.none; } + }); + if (index == length - 1) { + return <_DecorationType>[...newDecoration, _DecorationType.leaf]; + } else { + return <_DecorationType>[...newDecoration, _DecorationType.branch]; } } @@ -575,3 +611,18 @@ class RouteConfiguration { } } } + +enum _DecorationType { + parentBranch('│ '), + branch('├─'), + leaf('└─'), + none(' '), + ; + + const _DecorationType(this.value); + + final String value; + + @override + String toString() => value; +} diff --git a/packages/go_router/pubspec.yaml b/packages/go_router/pubspec.yaml index 0694585b54453..578b394be7789 100644 --- a/packages/go_router/pubspec.yaml +++ b/packages/go_router/pubspec.yaml @@ -1,7 +1,7 @@ name: go_router description: A declarative router for Flutter based on Navigation 2 supporting deep linking, data-driven routes and more -version: 14.1.2 +version: 14.1.3 repository: https://github.com/flutter/packages/tree/main/packages/go_router issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22 diff --git a/packages/go_router/test/configuration_test.dart b/packages/go_router/test/configuration_test.dart index a35f2b87e8143..0929756599fe2 100644 --- a/packages/go_router/test/configuration_test.dart +++ b/packages/go_router/test/configuration_test.dart @@ -1070,15 +1070,17 @@ void main() { }, ).debugKnownRoutes(), 'Full paths for routes:\n' - ' => /a\n' - ' => /a/b\n' - ' => /a/c\n' - ' => /d\n' - ' => /d/e\n' - ' => /d/e/f\n' - ' => /g\n' - ' => /g/h\n' - ' => /g/i\n', + '├─/a (Widget)\n' + '│ └─ (ShellRoute)\n' + '│ ├─/a/b (Widget)\n' + '│ └─/a/c (Widget)\n' + '├─/d (Widget)\n' + '│ └─/d/e (Widget)\n' + '│ └─/d/e/f (Widget)\n' + '└─/g (Widget)\n' + ' └─ (ShellRoute)\n' + ' ├─/g/h (Widget)\n' + ' └─/g/i (Widget)\n', ); }, ); diff --git a/packages/go_router/test/logging_test.dart b/packages/go_router/test/logging_test.dart index 1303f2909db15..d3251ee1b14e5 100644 --- a/packages/go_router/test/logging_test.dart +++ b/packages/go_router/test/logging_test.dart @@ -75,7 +75,7 @@ void main() { expect( logs, const [ - 'Full paths for routes:\n => /\n', + 'Full paths for routes:\n└─/ (Text)\n', 'setting initial location null' ], reason: 'Go router should have sent the 2 events to the logger', @@ -110,7 +110,7 @@ void main() { expect( logs, const [ - 'Full paths for routes:\n => /\n', + 'Full paths for routes:\n└─/ (Text)\n', 'setting initial location null' ], reason: 'Go router should have sent the 2 events to the logger',