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: fix formatter adding -- to multiple use statements #773

Merged
merged 13 commits into from
Feb 13, 2022
37 changes: 28 additions & 9 deletions sway-fmt/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,25 +341,25 @@ struct Rgb {
blue: u64,
}

struct Structure {
struct Bimbam {
iqdecay marked this conversation as resolved.
Show resolved Hide resolved
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 Structure {
struct Lighthouse {
age: u32,
name: string, // super comment
}
Expand Down Expand Up @@ -447,32 +447,32 @@ 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 Structure {
struct Lighthouse {
age: u32,
name: string, // super comment
}

struct Vehicle
struct Vehicle
{ age: u32, name: string , // some comment middle of nowhere
}

Expand Down Expand Up @@ -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
iqdecay marked this conversation as resolved.
Show resolved Hide resolved
// `use` statement
fn test_use_statement() {
let expected_sway = r#"script;
use std::chain::{panic,log_u8};
adlerjohn marked this conversation as resolved.
Show resolved Hide resolved
use std::chain::assert;
use std::address::{Address,Address};
adlerjohn marked this conversation as resolved.
Show resolved Hide resolved

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);
}
}
12 changes: 10 additions & 2 deletions sway-fmt/src/traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn traverse_for_changes(parse_tree: &SwayParseTree) -> Vec<Change> {
let mut changes = vec![];

for node in &parse_tree.tree.root_nodes {
traverse_ast_node(node, &mut changes)
traverse_ast_node(node, &mut changes);
}

changes.sort_by(|a, b| a.start.cmp(&b.start));
Expand All @@ -69,7 +69,15 @@ fn traverse_ast_node(ast_node: &AstNode, changes: &mut Vec<Change>) {
}

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() {
iqdecay marked this conversation as resolved.
Show resolved Hide resolved
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(_) => {
Expand Down