Skip to content

Commit

Permalink
Fix quote contents getting wrong format
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Oct 21, 2024
1 parent fb5be0f commit 6d9d375
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
33 changes: 26 additions & 7 deletions tooling/nargo_fmt/src/formatter/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ impl TextChunk {
pub(crate) enum Chunk {
/// A text chunk. It might contain leading comments.
Text(TextChunk),
/// A text chunk that should be printed unmodified (used for `quote { ... }` contents).
Verbatim(TextChunk),
/// A trailing comma that's only written if we decide to format chunks in multiple lines
/// (for example for a call we'll add a trailing comma to the last argument).
TrailingComma,
Expand Down Expand Up @@ -59,9 +61,10 @@ pub(crate) enum Chunk {
impl Chunk {
pub(crate) fn width(&self) -> usize {
match self {
Chunk::Text(chunk) | Chunk::TrailingComment(chunk) | Chunk::LeadingComment(chunk) => {
chunk.width
}
Chunk::Text(chunk)
| Chunk::Verbatim(chunk)
| Chunk::TrailingComment(chunk)
| Chunk::LeadingComment(chunk) => chunk.width,
Chunk::Group(group) => group.width(),
Chunk::SpaceOrLine => 1,
Chunk::Line { .. }
Expand Down Expand Up @@ -89,9 +92,10 @@ impl Chunk {

pub(crate) fn has_newlines(&self) -> bool {
match self {
Chunk::Text(chunk) | Chunk::TrailingComment(chunk) | Chunk::LeadingComment(chunk) => {
chunk.has_newlines
}
Chunk::Text(chunk)
| Chunk::Verbatim(chunk)
| Chunk::TrailingComment(chunk)
| Chunk::LeadingComment(chunk) => chunk.has_newlines,
Chunk::Group(group) => group.has_newlines(),
Chunk::TrailingComma
| Chunk::Line { .. }
Expand Down Expand Up @@ -195,6 +199,15 @@ impl ChunkGroup {
}
}

/// Appends a verbatim text chunk to this group.
pub(crate) fn verbatim(&mut self, chunk: TextChunk) {
if chunk.width == 0 {
return;
}

self.push(Chunk::Verbatim(chunk));
}

/// Appends a single space to this group by reading it from the given formatter.
pub(crate) fn space(&mut self, formatter: &mut Formatter<'_>) {
self.text(formatter.chunk(|formatter| {
Expand Down Expand Up @@ -370,6 +383,7 @@ impl ChunkGroup {
for chunk in self.chunks {
match chunk {
Chunk::Text(chunk) => group.text(chunk),
Chunk::Verbatim(chunk) => group.verbatim(chunk),
Chunk::TrailingComma => {
// If there's a trailing comma after a group, append the text to that group
// so that it glues with the last text present there (if any)
Expand Down Expand Up @@ -630,7 +644,9 @@ impl<'a> Formatter<'a> {
pub(super) fn format_chunk_group_in_one_line(&mut self, chunks: ChunkGroup) {
for chunk in chunks.chunks {
match chunk {
Chunk::Text(text_chunk) => self.write(&text_chunk.string),
Chunk::Text(text_chunk) | Chunk::Verbatim(text_chunk) => {
self.write(&text_chunk.string)
}
Chunk::TrailingComment(text_chunk) | Chunk::LeadingComment(text_chunk) => {
self.write(&text_chunk.string);
self.write(" ");
Expand Down Expand Up @@ -690,6 +706,9 @@ impl<'a> Formatter<'a> {
self.write(&text_chunk.string);
}
}
Chunk::Verbatim(text_chunk) => {
self.write(&text_chunk.string);
}
Chunk::TrailingComment(text_chunk) => {
self.write_chunk_lines(&text_chunk.string);
self.write_line_without_skipping_whitespace_and_comments();
Expand Down
18 changes: 17 additions & 1 deletion tooling/nargo_fmt/src/formatter/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ impl<'a> Formatter<'a> {
};

let mut group = ChunkGroup::new();
group.text(self.chunk(|formatter| {
group.verbatim(self.chunk(|formatter| {
formatter.write("quote {");
for token in tokens.0 {
formatter.write_source_span(token.to_span());
Expand Down Expand Up @@ -1815,6 +1815,22 @@ global y = 1;
assert_format(src, expected);
}

#[test]
fn format_quote_with_newlines() {
let src = "fn foo() {
quote {
foo
bar
}
}
";
let expected = src;
assert_format(src, expected);
}

#[test]
fn format_lambda_no_parameters() {
let src = "global x = | | 1 ;";
Expand Down

0 comments on commit 6d9d375

Please sign in to comment.