diff --git a/sway-fmt-v2/src/fmt.rs b/sway-fmt-v2/src/fmt.rs index 2ce32f7267d..9b6904f4f6d 100644 --- a/sway-fmt-v2/src/fmt.rs +++ b/sway-fmt-v2/src/fmt.rs @@ -1,10 +1,9 @@ use crate::utils::{ - attributes::format_attributes, indent_style::Shape, newline_style::apply_newline_style, - program_type::insert_program_type, + indent_style::Shape, newline_style::apply_newline_style, program_type::insert_program_type, }; use std::{path::Path, sync::Arc}; use sway_core::BuildConfig; -use sway_parse::ItemKind; +use sway_parse::attribute::Annotated; pub use crate::{ config::manifest::Config, @@ -55,21 +54,8 @@ impl Formatter { raw_formatted_code += &items .into_iter() .map(|item| -> Result { - use ItemKind::*; - // format attributes first, then add corresponding item - let mut buf = format_attributes(item.attribute_list, self); - buf.push_str(&match item.value { - Use(item_use) => item_use.format(self), - Struct(item_struct) => item_struct.format(self), - Enum(item_enum) => item_enum.format(self), - Fn(item_fn) => item_fn.format(self), - Trait(item_trait) => item_trait.format(self), - Impl(item_impl) => item_impl.format(self), - Abi(item_abi) => item_abi.format(self), - Const(item_const) => item_const.format(self), - Storage(item_storage) => item_storage.format(self), - }); - Ok(buf) + // format Annotated + Ok(Annotated::format(&item, self)) }) .collect::, _>>()? .join("\n"); @@ -86,14 +72,9 @@ impl Formatter { #[cfg(test)] mod tests { - use crate::utils::indent_style::Shape; use std::sync::Arc; - use super::{Config, Formatter}; - - fn get_formatter(config: Config, shape: Shape) -> Formatter { - Formatter { config, shape } - } + use super::Formatter; #[test] fn test_const() { @@ -103,7 +84,7 @@ pub const TEST:u16=10;"#; pub const TEST: u16 = 10;"#; - let mut formatter = get_formatter(Config::default(), Shape::default()); + let mut formatter = Formatter::default(); let formatted_sway_code = Formatter::format(&mut formatter, Arc::from(sway_code_to_format), None).unwrap(); assert!(correct_sway_code == formatted_sway_code) @@ -129,7 +110,7 @@ enum Color { Silver : (), Grey : (), }"#; - let mut formatter = get_formatter(Config::default(), Shape::default()); + let mut formatter = Formatter::default(); let formatted_sway_code = Formatter::format(&mut formatter, Arc::from(sway_code_to_format), None).unwrap(); assert!(correct_sway_code == formatted_sway_code) @@ -156,10 +137,9 @@ enum Color { }"#; // Creating a config with enum_variant_align_threshold that exceeds longest variant length - let mut config = Config::default(); - config.structures.enum_variant_align_threshold = 20; + let mut formatter = Formatter::default(); + formatter.config.structures.enum_variant_align_threshold = 20; - let mut formatter = get_formatter(config, Shape::default()); let formatted_sway_code = Formatter::format(&mut formatter, Arc::from(sway_code_to_format), None).unwrap(); assert!(correct_sway_code == formatted_sway_code) diff --git a/sway-fmt-v2/src/utils.rs b/sway-fmt-v2/src/utils.rs index b78f8aac422..47a62fa3b45 100644 --- a/sway-fmt-v2/src/utils.rs +++ b/sway-fmt-v2/src/utils.rs @@ -1,5 +1,6 @@ -pub mod attributes; +pub mod attribute; pub mod bracket; pub mod indent_style; +pub mod item; pub mod newline_style; pub mod program_type; diff --git a/sway-fmt-v2/src/utils/attribute.rs b/sway-fmt-v2/src/utils/attribute.rs new file mode 100644 index 00000000000..91b2f12e732 --- /dev/null +++ b/sway-fmt-v2/src/utils/attribute.rs @@ -0,0 +1,79 @@ +use crate::fmt::{Format, FormattedCode, Formatter}; +use sway_parse::{ + attribute::{Annotated, AttributeDecl}, + token::Delimiter, + Parse, +}; +use sway_types::Spanned; + +use super::bracket::{Parenthesis, SquareBracket}; + +impl Format for Annotated { + fn format(&self, formatter: &mut Formatter) -> FormattedCode { + let attributes = &self.attribute_list; + let mut formatted_code = String::new(); + + for attr in attributes { + AttributeDecl::format(attr, &mut formatted_code, formatter); + } + + formatted_code + &self.value.format(formatter) + } +} + +trait FormatDecl { + fn format(&self, line: &mut String, formatter: &mut Formatter); +} + +impl FormatDecl for AttributeDecl { + fn format(&self, line: &mut String, formatter: &mut Formatter) { + // At some point there will be enough attributes to warrant the need + // of formatting the list according to `config::lists::ListTactic`. + // For now the default implementation will be `Horizontal`. + // + // `#` + line.push_str(self.hash_token.span().as_str()); + // `[` + Self::open_square_bracket(line, formatter); + let attr = self.attribute.clone().into_inner(); + // name e.g. `storage` + line.push_str(attr.name.span().as_str()); + // `(` + Self::open_parenthesis(line, formatter); + // format and add args `read, write` + if let Some(args) = attr.args { + let mut args = args + .into_inner() + .value_separator_pairs + .iter() + .map(|arg| format!("{}{}", arg.0.as_str(), arg.1.span().as_str())) + .collect::>() + .join(" "); + args.pop(); // pop the ending space + args.pop(); // pop the ending comma + line.push_str(&args); + } + // ')' + Self::close_parenthesis(line, formatter); + // `]\n` + Self::close_square_bracket(line, formatter); + } +} + +impl SquareBracket for AttributeDecl { + fn open_square_bracket(line: &mut String, _formatter: &mut Formatter) { + line.push(Delimiter::Bracket.as_open_char()); + } + fn close_square_bracket(line: &mut String, _formatter: &mut Formatter) { + line.push_str(&format!("{}\n", Delimiter::Bracket.as_close_char())); + } +} + +impl Parenthesis for AttributeDecl { + fn open_parenthesis(line: &mut String, _formatter: &mut Formatter) { + line.push(Delimiter::Parenthesis.as_open_char()) + } + fn close_parenthesis(line: &mut String, _formatter: &mut Formatter) { + line.push(Delimiter::Parenthesis.as_close_char()) + } +} diff --git a/sway-fmt-v2/src/utils/attributes.rs b/sway-fmt-v2/src/utils/attributes.rs deleted file mode 100644 index 4700c41b7f6..00000000000 --- a/sway-fmt-v2/src/utils/attributes.rs +++ /dev/null @@ -1,11 +0,0 @@ -use crate::Formatter; -use sway_parse::AttributeDecl; -use sway_types::Spanned; -pub fn format_attributes(attributes: Vec, _formatter: &mut Formatter) -> String { - // TODO format attributes - attributes - .into_iter() - .map(|x| x.span().as_str().to_string()) - .collect::>() - .join("\n") -} diff --git a/sway-fmt-v2/src/utils/item.rs b/sway-fmt-v2/src/utils/item.rs new file mode 100644 index 00000000000..8dbb2f2ef5b --- /dev/null +++ b/sway-fmt-v2/src/utils/item.rs @@ -0,0 +1,19 @@ +use sway_parse::{Item, ItemKind::*}; + +use crate::fmt::{Format, FormattedCode, Formatter}; + +impl Format for Item { + fn format(&self, formatter: &mut Formatter) -> FormattedCode { + match &self.value { + Use(item_use) => item_use.format(formatter), + Struct(item_struct) => item_struct.format(formatter), + Enum(item_enum) => item_enum.format(formatter), + Fn(item_fn) => item_fn.format(formatter), + Trait(item_trait) => item_trait.format(formatter), + Impl(item_impl) => item_impl.format(formatter), + Abi(item_abi) => item_abi.format(formatter), + Const(item_const) => item_const.format(formatter), + Storage(item_storage) => item_storage.format(formatter), + } + } +} diff --git a/sway-parse/src/lib.rs b/sway-parse/src/lib.rs index c453b005d01..92f3cc561a0 100644 --- a/sway-parse/src/lib.rs +++ b/sway-parse/src/lib.rs @@ -17,7 +17,7 @@ pub mod pattern; mod priv_prelude; pub mod punctuated; pub mod statement; -mod token; +pub mod token; pub mod ty; pub mod where_clause; diff --git a/sway-parse/src/token.rs b/sway-parse/src/token.rs index 972ce4dd4f8..3c036f4aa29 100644 --- a/sway-parse/src/token.rs +++ b/sway-parse/src/token.rs @@ -96,6 +96,13 @@ impl Delimiter { Delimiter::Bracket => '[', } } + pub fn as_close_char(self) -> char { + match self { + Delimiter::Parenthesis => ')', + Delimiter::Brace => '}', + Delimiter::Bracket => ']', + } + } } #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]