From 6407e2e789e45c64727a996da825d23f1516cb27 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Oct 2021 17:36:41 +0300 Subject: [PATCH] indent macro output when pprinting --- crates/hir_def/src/macro_expansion_tests.rs | 10 ++++++++++ crates/hir_def/src/macro_expansion_tests/mbe.rs | 12 ++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/crates/hir_def/src/macro_expansion_tests.rs b/crates/hir_def/src/macro_expansion_tests.rs index d4520939b6..ac0a2d27b3 100644 --- a/crates/hir_def/src/macro_expansion_tests.rs +++ b/crates/hir_def/src/macro_expansion_tests.rs @@ -98,6 +98,7 @@ fn reindent(indent: IndentLevel, pp: String) -> String { fn pretty_print_macro_expansion(expn: SyntaxNode) -> String { let mut res = String::new(); let mut prev_kind = SyntaxKind::EOF; + let mut indent_level = 0; for token in iter::successors(expn.first_token(), |t| t.next_token()) { let curr_kind = token.kind(); let space = match (prev_kind, curr_kind) { @@ -113,7 +114,16 @@ fn pretty_print_macro_expansion(expn: SyntaxNode) -> String { _ => "", }; + match prev_kind { + T!['{'] => indent_level += 1, + T!['}'] => indent_level -= 1, + _ => (), + } + res.push_str(space); + if space == "\n" && curr_kind != T!['}'] { + res.push_str(&" ".repeat(indent_level)); + } prev_kind = curr_kind; format_to!(res, "{}", token) } diff --git a/crates/hir_def/src/macro_expansion_tests/mbe.rs b/crates/hir_def/src/macro_expansion_tests/mbe.rs index 36c1858a04..f5d00b6666 100644 --- a/crates/hir_def/src/macro_expansion_tests/mbe.rs +++ b/crates/hir_def/src/macro_expansion_tests/mbe.rs @@ -174,8 +174,8 @@ macro_rules! m { ($($i:ident),*) => ( impl Bar { $(fn $i {})* } ); } impl Bar { -fn foo {} -fn bar {} + fn foo {} + fn bar {} } "#]], ); @@ -195,8 +195,8 @@ macro_rules! m { ($($i:ident),*) => ( fn baz { $($i ();)* } ); } fn baz { -foo(); -bar(); + foo(); + bar(); } "#]], ) @@ -216,8 +216,8 @@ macro_rules! m { ($($i:ident),*) => ( fn baz { $($i() );* } ); } fn baz { -foo(); -bar() + foo(); + bar() } "#]], )