Skip to content

Commit b0d2e1d

Browse files
johnniwintherCommit Queue
authored andcommitted
[_fe_analyzer_shared] Add debugPrint
This adds a `debugPrint` method along with methods to support automatic indentation. The debug helpers are reexported inside package:analyzer and package:front_end for easy access. Change-Id: I80a4103e6fb30a6b459551184916c3fa26591a20 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/425280 Commit-Queue: Johnni Winther <[email protected]> Reviewed-by: Jens Johansen <[email protected]>
1 parent 3c2f59d commit b0d2e1d

File tree

6 files changed

+68
-0
lines changed

6 files changed

+68
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
export 'print.dart';
6+
export 'stack_trace.dart';
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
int _indentLevel = 0;
6+
7+
/// Prints the `toString()` of [value] to stdout.
8+
///
9+
/// This supports automatic indentation when used with [debugPrintEnd],
10+
/// [debugPrintStart] and [inDebugPrint].
11+
void debugPrint(Object value) {
12+
_debugPrint(value);
13+
}
14+
15+
/// Prints the `toString()` of [value] using [debugPrint] and increases the
16+
/// indentation level used by [debugPrint].
17+
void debugPrintEnd(Object value) {
18+
_indentLevel--;
19+
_debugPrint(value);
20+
}
21+
22+
/// Decreases the indentation level used by [debugPrint] and prints the
23+
/// `toString()` of [value] using [debugPrint].
24+
void debugPrintStart(Object value) {
25+
_debugPrint(value);
26+
_indentLevel++;
27+
}
28+
29+
/// Wraps the call to [f] with a start and end message containing the
30+
/// `toString()` of [value], increasing the indentation level during the call
31+
/// to [f].
32+
///
33+
/// This can be used for easily printing call stacks for debugging.
34+
void inDebugPrint(Object value, void Function() f) {
35+
debugPrintStart('start:$value');
36+
try {
37+
f();
38+
} finally {
39+
debugPrintEnd('end :$value');
40+
}
41+
}
42+
43+
void _debugPrint(Object value) {
44+
print('${' ' * _indentLevel}${value}');
45+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Reexport debug helpers to make them easily available inside package:analyzer.
6+
export 'package:_fe_analyzer_shared/src/debug_helpers/debug_helpers.dart';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Reexport debug helpers to make them easily available inside
6+
// package:front_end.
7+
export 'package:_fe_analyzer_shared/src/debug_helpers/debug_helpers.dart';

pkg/front_end/test/lint_test.status

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# for details. All rights reserved. Use of this source code is governed by a
33
# BSD-style license that can be found in the LICENSE file.
44

5+
_fe_analyzer_shared/lib/src/debug_helpers/debug_helpers/Exports: Fail
56
_fe_analyzer_shared/lib/src/metadata/ast/Exports: Fail
67
_fe_analyzer_shared/lib/src/parser/parser/Exports: Fail
78
_fe_analyzer_shared/lib/src/scanner/scanner/Exports: Fail
@@ -20,6 +21,7 @@ front_end/lib/src/api_prototype/testing/Exports: Fail
2021
front_end/lib/src/api_prototype/try_constant_evaluator/Exports: Fail
2122
front_end/lib/src/base/messages/Exports: Fail
2223
front_end/lib/src/codes/cfe_codes/Exports: Fail
24+
front_end/lib/src/debug_helpers/Exports: Fail
2325
front_end/lib/src/testing/id_testing_helper/Exports: Fail
2426
kernel/lib/ast/Exports: Fail
2527
kernel/lib/kernel/Exports: Fail

pkg/front_end/test/spell_checking_list_code.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ augments
118118
author
119119
auto
120120
automagically
121+
automatic
121122
auxiliary
122123
averages
123124
awaited
@@ -1485,6 +1486,7 @@ redirections
14851486
redirector
14861487
redo
14871488
reevaluation
1489+
reexport
14881490
reexports
14891491
ref
14901492
refactoring

0 commit comments

Comments
 (0)