Skip to content

Commit

Permalink
feat(pretty): Preserve the frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Mar 10, 2025
1 parent c002b34 commit 078a6f6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
9 changes: 9 additions & 0 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec<Comment>
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 {
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/unpretty/frontmatter.stdout
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
---
---

//@ compile-flags: -Zunpretty=normal
//@ check-pass
Expand Down

0 comments on commit 078a6f6

Please sign in to comment.