From 1a20fdba0f4b3bbcf6514dab21807c75542d492e Mon Sep 17 00:00:00 2001 From: vnepveu Date: Tue, 8 Feb 2022 17:06:32 +0100 Subject: [PATCH 01/11] fix: fix formatter adding `--` to multiple `use` statements --- sway-fmt/src/fmt.rs | 4 ++-- sway-fmt/src/traversal.rs | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/sway-fmt/src/fmt.rs b/sway-fmt/src/fmt.rs index 3d8791d7158..641a23f9bff 100644 --- a/sway-fmt/src/fmt.rs +++ b/sway-fmt/src/fmt.rs @@ -359,7 +359,7 @@ struct Structure { name: string, // super comment } -struct Structure { +struct BIMBAMBOUM { age: u32, name: string, // super comment } @@ -467,7 +467,7 @@ struct Structure { name: string// super comment } -struct Structure { +struct BIMBAMBOUM { age: u32, name: string, // super comment } diff --git a/sway-fmt/src/traversal.rs b/sway-fmt/src/traversal.rs index 6ebfe00eb42..ed01d2a1f06 100644 --- a/sway-fmt/src/traversal.rs +++ b/sway-fmt/src/traversal.rs @@ -47,8 +47,15 @@ enum ChangeType { pub fn traverse_for_changes(parse_tree: &SwayParseTree) -> Vec { let mut changes = vec![]; + let mut previous_text_content = ""; for node in &parse_tree.tree.root_nodes { - traverse_ast_node(node, &mut changes) + let current_text_content = node.span.as_str(); + if current_text_content == previous_text_content { + continue; + } else { + previous_text_content = current_text_content; + } + traverse_ast_node(node, &mut changes); } changes.sort_by(|a, b| a.start.cmp(&b.start)); From 969fa4f9715dea6810f01b0a3ae4e8def56069b4 Mon Sep 17 00:00:00 2001 From: vnepveu Date: Fri, 11 Feb 2022 16:04:19 +0100 Subject: [PATCH 02/11] test: implement test for multiple use statement formatting * the renaming of the example struct `Lighthouse` and `Dart` is to have two different blocks of code. Otherwise the two code blocks are exactly identical (incl. comment) and the formatter adds an extra newline. --- sway-fmt/src/fmt.rs | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/sway-fmt/src/fmt.rs b/sway-fmt/src/fmt.rs index 641a23f9bff..bc57cd6dbb1 100644 --- a/sway-fmt/src/fmt.rs +++ b/sway-fmt/src/fmt.rs @@ -341,25 +341,25 @@ struct Rgb { blue: u64, } -struct Structure { +struct Bimbam { age: u32, name: string, } -struct Structure { +struct Building { age: u32, /* completely meaningless multiline comment not sure why would anyone write this but let's deal with it as well! */ name: string, } -struct Structure { +struct Darts { age: u32, name: string, // super comment } -struct BIMBAMBOUM { +struct Lighthouse { age: u32, name: string, // super comment } @@ -447,7 +447,7 @@ struct Rgb { blue: u64, } -struct Structure { +struct Bimbam { age: u32, @@ -455,24 +455,24 @@ struct Structure { } -struct Structure { +struct Building { age: u32, /* completely meaningless multiline comment not sure why would anyone write this but let's deal with it as well! */ name: string } -struct Structure { +struct Darts { age: u32, name: string// super comment } -struct BIMBAMBOUM { +struct Lighthouse { age: u32, name: string, // super comment } -struct Vehicle +struct Vehicle { age: u32, name: string , // some comment middle of nowhere } @@ -641,4 +641,23 @@ fn one_liner() -> bool { let (_, formatted_code) = result.unwrap(); assert_eq!(correct_sway_code, formatted_code); } + + #[test] + // This test is here to check that the `use` statements are properly formatted + // Currently, there is a hard-to-diagnose bug which adds a leading `--` to the first + // `use` statement + fn test_use_statement() { + let expected_sway = r#"script; +use std::chain::{panic,log_u8}; +use std::chain::assert; +use std::address::{Address,Address}; + +fn main() { +} +"#; + let result = get_formatted_data(expected_sway.into(), OPTIONS); + assert!(result.is_ok()); + let (_, formatted_code) = result.unwrap(); + assert_eq!(formatted_code, expected_sway); + } } From 46eea0d580cb0569fb0a192c3dedcf4b154c48b5 Mon Sep 17 00:00:00 2001 From: vnepveu Date: Fri, 11 Feb 2022 18:29:19 +0100 Subject: [PATCH 03/11] fix: fix the `use` statement formatter using a better approach --- sway-fmt/src/traversal.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/sway-fmt/src/traversal.rs b/sway-fmt/src/traversal.rs index ed01d2a1f06..8ec0a772cfc 100644 --- a/sway-fmt/src/traversal.rs +++ b/sway-fmt/src/traversal.rs @@ -47,14 +47,7 @@ enum ChangeType { pub fn traverse_for_changes(parse_tree: &SwayParseTree) -> Vec { let mut changes = vec![]; - let mut previous_text_content = ""; for node in &parse_tree.tree.root_nodes { - let current_text_content = node.span.as_str(); - if current_text_content == previous_text_content { - continue; - } else { - previous_text_content = current_text_content; - } traverse_ast_node(node, &mut changes); } @@ -76,7 +69,15 @@ fn traverse_ast_node(ast_node: &AstNode, changes: &mut Vec) { } AstNodeContent::UseStatement(_) => { - changes.push(Change::new(&ast_node.span, ChangeType::UseStatement)); + // The AST generates one root node per use statement, we must avoid duplicating them + // while formatting + if changes.is_empty() { + changes.push(Change::new(&ast_node.span, ChangeType::UseStatement)); + } + let previous_start = changes.last().unwrap().start; + if previous_start != ast_node.span.start() { + changes.push(Change::new(&ast_node.span, ChangeType::UseStatement)); + } } AstNodeContent::IncludeStatement(_) => { From 9702ab9f2630cc235c0a0a5ff5facf067e58806b Mon Sep 17 00:00:00 2001 From: vnepveu Date: Fri, 11 Feb 2022 23:54:13 +0100 Subject: [PATCH 04/11] test: use a compiling example for formatting use statements --- sway-fmt/src/fmt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway-fmt/src/fmt.rs b/sway-fmt/src/fmt.rs index bc57cd6dbb1..1b6fcdd5222 100644 --- a/sway-fmt/src/fmt.rs +++ b/sway-fmt/src/fmt.rs @@ -650,7 +650,7 @@ fn one_liner() -> bool { let expected_sway = r#"script; use std::chain::{panic,log_u8}; use std::chain::assert; -use std::address::{Address,Address}; +use std::hash::{HashMethod,hash_value,hash_pair}; fn main() { } From 9182a1ed4d7d9573895fa793ddf7f43ebd61aa7c Mon Sep 17 00:00:00 2001 From: vnepveu Date: Sat, 12 Feb 2022 15:09:42 +0100 Subject: [PATCH 05/11] docs: change comments for `use` statement formatting --- sway-fmt/src/fmt.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/sway-fmt/src/fmt.rs b/sway-fmt/src/fmt.rs index 1b6fcdd5222..1d5d1516ba4 100644 --- a/sway-fmt/src/fmt.rs +++ b/sway-fmt/src/fmt.rs @@ -341,25 +341,25 @@ struct Rgb { blue: u64, } -struct Bimbam { +struct Structure { age: u32, name: string, } -struct Building { +struct Structure { age: u32, /* completely meaningless multiline comment not sure why would anyone write this but let's deal with it as well! */ name: string, } -struct Darts { +struct Structure { age: u32, name: string, // super comment } -struct Lighthouse { +struct Structure { age: u32, name: string, // super comment } @@ -447,7 +447,7 @@ struct Rgb { blue: u64, } -struct Bimbam { +struct Structure { age: u32, @@ -455,19 +455,19 @@ struct Bimbam { } -struct Building { +struct Structure { age: u32, /* completely meaningless multiline comment not sure why would anyone write this but let's deal with it as well! */ name: string } -struct Darts { +struct Structure { age: u32, name: string// super comment } -struct Lighthouse { +struct Structure { age: u32, name: string, // super comment } @@ -643,9 +643,7 @@ fn one_liner() -> bool { } #[test] - // This test is here to check that the `use` statements are properly formatted - // Currently, there is a hard-to-diagnose bug which adds a leading `--` to the first - // `use` statement + // Test that the use statements with multiple imports are properly formatted fn test_use_statement() { let expected_sway = r#"script; use std::chain::{panic,log_u8}; From 38440497bf0803dce9f43acac1b47eb3bda4c379 Mon Sep 17 00:00:00 2001 From: vnepveu Date: Sat, 12 Feb 2022 15:17:48 +0100 Subject: [PATCH 06/11] fix: improve bugfix approach --- sway-fmt/src/traversal.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/sway-fmt/src/traversal.rs b/sway-fmt/src/traversal.rs index 8ec0a772cfc..95c21db15b8 100644 --- a/sway-fmt/src/traversal.rs +++ b/sway-fmt/src/traversal.rs @@ -71,12 +71,14 @@ fn traverse_ast_node(ast_node: &AstNode, changes: &mut Vec) { AstNodeContent::UseStatement(_) => { // The AST generates one root node per use statement, we must avoid duplicating them // while formatting - if changes.is_empty() { - changes.push(Change::new(&ast_node.span, ChangeType::UseStatement)); - } - let previous_start = changes.last().unwrap().start; - if previous_start != ast_node.span.start() { - changes.push(Change::new(&ast_node.span, ChangeType::UseStatement)); + let next_span = &ast_node.span; + match changes.last() { + Some(last_change) => { + if last_change.start != next_span.start() { + changes.push(Change::new(next_span, ChangeType::UseStatement)); + } + } + _ => changes.push(Change::new(next_span, ChangeType::UseStatement)), } } From b63a504cfaa14816fb28bc4d8ebc346080b45a21 Mon Sep 17 00:00:00 2001 From: vnepveu Date: Sat, 12 Feb 2022 15:18:34 +0100 Subject: [PATCH 07/11] fix: cancel renaming --- sway-fmt/src/fmt.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sway-fmt/src/fmt.rs b/sway-fmt/src/fmt.rs index 1d5d1516ba4..b5cd82410b5 100644 --- a/sway-fmt/src/fmt.rs +++ b/sway-fmt/src/fmt.rs @@ -364,7 +364,7 @@ struct Structure { name: string, // super comment } -struct Vehicle { +struct Structure { age: u32, name: string, // some comment middle of nowhere } @@ -472,7 +472,7 @@ struct Structure { name: string, // super comment } -struct Vehicle +struct Structure { age: u32, name: string , // some comment middle of nowhere } From 382ca066f0fa2d8537462b79ec59225bcbb4b2f4 Mon Sep 17 00:00:00 2001 From: vnepveu Date: Sat, 12 Feb 2022 15:21:17 +0100 Subject: [PATCH 08/11] Revert "fix: cancel renaming" This reverts commit b63a504cfaa14816fb28bc4d8ebc346080b45a21. --- sway-fmt/src/fmt.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sway-fmt/src/fmt.rs b/sway-fmt/src/fmt.rs index b5cd82410b5..1d5d1516ba4 100644 --- a/sway-fmt/src/fmt.rs +++ b/sway-fmt/src/fmt.rs @@ -364,7 +364,7 @@ struct Structure { name: string, // super comment } -struct Structure { +struct Vehicle { age: u32, name: string, // some comment middle of nowhere } @@ -472,7 +472,7 @@ struct Structure { name: string, // super comment } -struct Structure +struct Vehicle { age: u32, name: string , // some comment middle of nowhere } From b3e7b6e3451dc81333de31155653cdad0bc5284f Mon Sep 17 00:00:00 2001 From: vnepveu Date: Sat, 12 Feb 2022 15:23:48 +0100 Subject: [PATCH 09/11] chore: add missing whitespace --- sway-fmt/src/fmt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway-fmt/src/fmt.rs b/sway-fmt/src/fmt.rs index 1d5d1516ba4..6ebeca830c6 100644 --- a/sway-fmt/src/fmt.rs +++ b/sway-fmt/src/fmt.rs @@ -472,7 +472,7 @@ struct Structure { name: string, // super comment } -struct Vehicle +struct Vehicle { age: u32, name: string , // some comment middle of nowhere } From fe5016ce3d938372b8f58feff61d1fc132dd511d Mon Sep 17 00:00:00 2001 From: vnepveu Date: Sun, 13 Feb 2022 13:17:59 +0100 Subject: [PATCH 10/11] test: add more complex patterns for `use` statement formatting --- sway-fmt/src/fmt.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sway-fmt/src/fmt.rs b/sway-fmt/src/fmt.rs index 6ebeca830c6..5d2092ad197 100644 --- a/sway-fmt/src/fmt.rs +++ b/sway-fmt/src/fmt.rs @@ -649,6 +649,8 @@ fn one_liner() -> bool { use std::chain::{panic,log_u8}; use std::chain::assert; use std::hash::{HashMethod,hash_value,hash_pair}; +use a::b::{c,d::{e,f}}; +use a::b::{c,d::{self,f}}; fn main() { } From d856fb906c929801d898481f9cc31122b1b88559 Mon Sep 17 00:00:00 2001 From: vnepveu Date: Sun, 13 Feb 2022 13:28:47 +0100 Subject: [PATCH 11/11] docs: update the workaround for the formatter --- docs/src/getting-started/temporary_workarounds.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/getting-started/temporary_workarounds.md b/docs/src/getting-started/temporary_workarounds.md index e38b81d2ab0..a1ee335eb4b 100644 --- a/docs/src/getting-started/temporary_workarounds.md +++ b/docs/src/getting-started/temporary_workarounds.md @@ -65,4 +65,4 @@ The optimizing pass of the compiler is not yet implemented, therefore bytecode w ## Formatter -Currently, we need to parse the Sway code before formatting it, and the best way to do this is to compile the code. Hence, **the formatter cannot work on Sway code that does not compile**. This requirement may be changed in the future. +Currently, we need to parse the Sway code before formatting it. Hence, **the formatter cannot work on Sway code that does not parse correctly**. This requirement may be changed in the future.