From 078a6f68e170033250888604caca3b5ea5a3d4b7 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 20 Feb 2025 09:57:54 -0600 Subject: [PATCH] feat(pretty): Preserve the frontmatter --- compiler/rustc_ast_pretty/src/pprust/state.rs | 9 +++++++++ tests/ui/unpretty/frontmatter.stdout | 2 ++ 2 files changed, 11 insertions(+) diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index 01fc272a45897..03572a86735c1 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -121,6 +121,15 @@ fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec pos += shebang_len; } + if let Some(frontmatter_len) = rustc_lexer::strip_frontmatter(&text[pos..]) { + comments.push(Comment { + style: CommentStyle::Isolated, + lines: vec![text[pos..pos + frontmatter_len].to_string()], + pos: start_bpos + BytePos(pos as u32), + }); + pos += frontmatter_len; + } + for token in rustc_lexer::tokenize(&text[pos..]) { let token_text = &text[pos..pos + token.len as usize]; match token.kind { diff --git a/tests/ui/unpretty/frontmatter.stdout b/tests/ui/unpretty/frontmatter.stdout index 9ad6bbbe5774b..2ccbb1b258255 100644 --- a/tests/ui/unpretty/frontmatter.stdout +++ b/tests/ui/unpretty/frontmatter.stdout @@ -1,3 +1,5 @@ +--- +--- //@ compile-flags: -Zunpretty=normal //@ check-pass