Skip to content

Commit b1ffd1e

Browse files
authored
[go_router] New feature improve debug full path (flutter#6714)
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.
1 parent ef756da commit b1ffd1e

File tree

7 files changed

+78
-19
lines changed

7 files changed

+78
-19
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,4 @@ Daniele Cambi <[email protected]>
7676
Michele Benedetti <[email protected]>
7777
Taskulu LDA <[email protected]>
7878
LinXunFeng <[email protected]>
79+
Hashir Shoaib <[email protected]>

packages/go_router/AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55

66
Google Inc.
77
8+
Hashir Shoaib <[email protected]>

packages/go_router/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 14.1.3
2+
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.
4+
15
## 14.1.2
26

37
- Fixes issue that path parameters are not set when using the `goBranch`.

packages/go_router/lib/src/configuration.dart

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,8 @@ class RouteConfiguration {
525525
String debugKnownRoutes() {
526526
final StringBuffer sb = StringBuffer();
527527
sb.writeln('Full paths for routes:');
528-
_debugFullPathsFor(_routingConfig.value.routes, '', 0, sb);
528+
_debugFullPathsFor(
529+
_routingConfig.value.routes, '', const <_DecorationType>[], sb);
529530

530531
if (_nameToPath.isNotEmpty) {
531532
sb.writeln('known full paths for route names:');
@@ -538,15 +539,50 @@ class RouteConfiguration {
538539
}
539540

540541
void _debugFullPathsFor(List<RouteBase> routes, String parentFullpath,
541-
int depth, StringBuffer sb) {
542-
for (final RouteBase route in routes) {
542+
List<_DecorationType> parentDecoration, StringBuffer sb) {
543+
for (final (int index, RouteBase route) in routes.indexed) {
544+
final List<_DecorationType> decoration =
545+
_getDecoration(parentDecoration, index, routes.length);
546+
final String decorationString =
547+
decoration.map((_DecorationType e) => e.toString()).join();
548+
String path = parentFullpath;
543549
if (route is GoRoute) {
544-
final String fullPath = concatenatePaths(parentFullpath, route.path);
545-
sb.writeln(' => ${''.padLeft(depth * 2)}$fullPath');
546-
_debugFullPathsFor(route.routes, fullPath, depth + 1, sb);
550+
path = concatenatePaths(parentFullpath, route.path);
551+
final String? screenName =
552+
route.builder?.runtimeType.toString().split('=> ').last;
553+
sb.writeln('$decorationString$path '
554+
'${screenName == null ? '' : '($screenName)'}');
547555
} else if (route is ShellRouteBase) {
548-
_debugFullPathsFor(route.routes, parentFullpath, depth, sb);
556+
sb.writeln('$decorationString (ShellRoute)');
557+
}
558+
_debugFullPathsFor(route.routes, path, decoration, sb);
559+
}
560+
}
561+
562+
List<_DecorationType> _getDecoration(
563+
List<_DecorationType> parentDecoration,
564+
int index,
565+
int length,
566+
) {
567+
final Iterable<_DecorationType> newDecoration =
568+
parentDecoration.map((_DecorationType e) {
569+
switch (e) {
570+
// swap
571+
case _DecorationType.branch:
572+
return _DecorationType.parentBranch;
573+
case _DecorationType.leaf:
574+
return _DecorationType.none;
575+
// no swap
576+
case _DecorationType.parentBranch:
577+
return _DecorationType.parentBranch;
578+
case _DecorationType.none:
579+
return _DecorationType.none;
549580
}
581+
});
582+
if (index == length - 1) {
583+
return <_DecorationType>[...newDecoration, _DecorationType.leaf];
584+
} else {
585+
return <_DecorationType>[...newDecoration, _DecorationType.branch];
550586
}
551587
}
552588

@@ -575,3 +611,18 @@ class RouteConfiguration {
575611
}
576612
}
577613
}
614+
615+
enum _DecorationType {
616+
parentBranch('│ '),
617+
branch('├─'),
618+
leaf('└─'),
619+
none(' '),
620+
;
621+
622+
const _DecorationType(this.value);
623+
624+
final String value;
625+
626+
@override
627+
String toString() => value;
628+
}

packages/go_router/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: go_router
22
description: A declarative router for Flutter based on Navigation 2 supporting
33
deep linking, data-driven routes and more
4-
version: 14.1.2
4+
version: 14.1.3
55
repository: https://github.com/flutter/packages/tree/main/packages/go_router
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22
77

packages/go_router/test/configuration_test.dart

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,15 +1070,17 @@ void main() {
10701070
},
10711071
).debugKnownRoutes(),
10721072
'Full paths for routes:\n'
1073-
' => /a\n'
1074-
' => /a/b\n'
1075-
' => /a/c\n'
1076-
' => /d\n'
1077-
' => /d/e\n'
1078-
' => /d/e/f\n'
1079-
' => /g\n'
1080-
' => /g/h\n'
1081-
' => /g/i\n',
1073+
'├─/a (Widget)\n'
1074+
'│ └─ (ShellRoute)\n'
1075+
'│ ├─/a/b (Widget)\n'
1076+
'│ └─/a/c (Widget)\n'
1077+
'├─/d (Widget)\n'
1078+
'│ └─/d/e (Widget)\n'
1079+
'│ └─/d/e/f (Widget)\n'
1080+
'└─/g (Widget)\n'
1081+
' └─ (ShellRoute)\n'
1082+
' ├─/g/h (Widget)\n'
1083+
' └─/g/i (Widget)\n',
10821084
);
10831085
},
10841086
);

packages/go_router/test/logging_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void main() {
7575
expect(
7676
logs,
7777
const <String>[
78-
'Full paths for routes:\n => /\n',
78+
'Full paths for routes:\n└─/ (Text)\n',
7979
'setting initial location null'
8080
],
8181
reason: 'Go router should have sent the 2 events to the logger',
@@ -110,7 +110,7 @@ void main() {
110110
expect(
111111
logs,
112112
const <String>[
113-
'Full paths for routes:\n => /\n',
113+
'Full paths for routes:\n└─/ (Text)\n',
114114
'setting initial location null'
115115
],
116116
reason: 'Go router should have sent the 2 events to the logger',

0 commit comments

Comments
 (0)