Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: (formatter) correctly format quote delimiters #6377

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions tooling/nargo_fmt/src/formatter/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,24 @@
}

pub(super) fn format_quote(&mut self) -> ChunkGroup {
// A quote's prefix isn't captured in the token, so let's figure it out which one
// is it by looking at the source code.
let mut quote_source_code =
&self.source[self.token_span.start() as usize..self.token_span.end() as usize];

// Remove "quote" and any whitespace following it
quote_source_code = quote_source_code.strip_prefix("quote").unwrap();
quote_source_code = quote_source_code.trim_start();

// The first char is the delimiter
let delimiter_start = quote_source_code.chars().next().unwrap();
let delimiter_end = match delimiter_start {
'(' => ')',
'{' => '}',
'[' => ']',
_ => panic!("Unexpected delimiter: {}", delimiter_start),
};

// We use the current token rather than the Tokens we got from `Token::Quote` because
// the current token has whitespace and comments in it, while the one we got from
// the parser doesn't.
Expand All @@ -319,11 +337,13 @@

let mut group = ChunkGroup::new();
group.verbatim(self.chunk(|formatter| {
formatter.write("quote {");
formatter.write("quote");
formatter.write_space();
formatter.write(&delimiter_start.to_string());
for token in tokens.0 {
formatter.write_source_span(token.to_span());
}
formatter.write("}");
formatter.write(&delimiter_end.to_string());
}));
group
}
Expand Down Expand Up @@ -1751,9 +1771,9 @@

#[test]
fn format_method_call_chain_3() {
let src = "fn foo() { assert(p4_affine.eq(Gaffine::new(6890855772600357754907169075114257697580319025794532037257385534741338397365, 4338620300185947561074059802482547481416142213883829469920100239455078257889))); }";

Check warning on line 1774 in tooling/nargo_fmt/src/formatter/expression.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Gaffine)
let expected = "fn foo() {
assert(p4_affine.eq(Gaffine::new(

Check warning on line 1776 in tooling/nargo_fmt/src/formatter/expression.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Gaffine)
6890855772600357754907169075114257697580319025794532037257385534741338397365,
4338620300185947561074059802482547481416142213883829469920100239455078257889,
)));
Expand Down Expand Up @@ -1789,7 +1809,7 @@
fn format_nested_method_call_with_maximum_width_2() {
let src = "fn foo() {
assert(
p4_affine.eq(Gaffine::new(

Check warning on line 1812 in tooling/nargo_fmt/src/formatter/expression.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Gaffine)
6890855772600357754907169075114257697580319025794532037257385534741338397365,
4338620300185947561074059802482547481416142213883829469920100239455078257889,
)),
Expand All @@ -1797,7 +1817,7 @@
}
";
let expected = "fn foo() {
assert(p4_affine.eq(Gaffine::new(

Check warning on line 1820 in tooling/nargo_fmt/src/formatter/expression.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Gaffine)
6890855772600357754907169075114257697580319025794532037257385534741338397365,
4338620300185947561074059802482547481416142213883829469920100239455078257889,
)));
Expand Down Expand Up @@ -2045,6 +2065,13 @@
assert_format(src, expected);
}

#[test]
fn format_quote_with_bracket_delimiter() {
let src = "global x = quote [ 1 2 3 $four $(five) ];";
let expected = "global x = quote [ 1 2 3 $four $(five) ];\n";
assert_format(src, expected);
}

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