Skip to content

Commit

Permalink
[go_router] New feature improve debug full path (flutter#6714)
Browse files Browse the repository at this point in the history
This PR fixes flutter#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.
  • Loading branch information
hashirshoaeb authored May 21, 2024
1 parent ef756da commit b1ffd1e
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 19 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,4 @@ Daniele Cambi <[email protected]>
Michele Benedetti <[email protected]>
Taskulu LDA <[email protected]>
LinXunFeng <[email protected]>
Hashir Shoaib <[email protected]>
1 change: 1 addition & 0 deletions packages/go_router/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

Google Inc.
[email protected]
Hashir Shoaib <[email protected]>
4 changes: 4 additions & 0 deletions packages/go_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
65 changes: 58 additions & 7 deletions packages/go_router/lib/src/configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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:');
Expand All @@ -538,15 +539,50 @@ class RouteConfiguration {
}

void _debugFullPathsFor(List<RouteBase> 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];
}
}

Expand Down Expand Up @@ -575,3 +611,18 @@ class RouteConfiguration {
}
}
}

enum _DecorationType {
parentBranch('│ '),
branch('├─'),
leaf('└─'),
none(' '),
;

const _DecorationType(this.value);

final String value;

@override
String toString() => value;
}
2 changes: 1 addition & 1 deletion packages/go_router/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down
20 changes: 11 additions & 9 deletions packages/go_router/test/configuration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
},
);
Expand Down
4 changes: 2 additions & 2 deletions packages/go_router/test/logging_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void main() {
expect(
logs,
const <String>[
'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',
Expand Down Expand Up @@ -110,7 +110,7 @@ void main() {
expect(
logs,
const <String>[
'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',
Expand Down

0 comments on commit b1ffd1e

Please sign in to comment.