Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
4 changes: 2 additions & 2 deletions sway-fmt-v2/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,14 @@ enum Color {
let sway_code_to_format = r#"contract;

abi StorageMapExample {
#[storage(write,)]fn insert_into_map1(key: u64, value: u64);
#[storage(write, read,)]fn insert_into_map1(key: u64, value: u64);

fn hello(key: u64, value: u64);
}"#;
let correct_sway_code = r#"contract;

abi StorageMapExample {
#[storage(write)]
#[storage(write, read)]
fn insert_into_map1(key: u64, value: u64);

fn hello(key: u64, value: u64);
Expand Down
30 changes: 3 additions & 27 deletions sway-fmt-v2/src/items/item_struct.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::{
config::items::ItemBraceStyle,
fmt::{Format, FormattedCode, Formatter},
utils::{
bracket::{AngleBracket, CurlyBrace},
item_len::ItemLen,
},
utils::{bracket::CurlyBrace, item_len::ItemLen},
};
use sway_parse::ItemStruct;
use sway_types::Spanned;
Expand Down Expand Up @@ -74,20 +71,9 @@ fn format_struct(
// Add struct name
formatted_code.push_str(item_struct.name.as_str());

// Check if there is generic provided
// Format `GenericParams`, if any
if let Some(generics) = &item_struct.generics {
// Push angle brace
ItemStruct::open_angle_bracket(formatted_code, formatter);
// Get generics fields
let generics = generics.parameters.inner.value_separator_pairs.clone();
for (index, generic) in generics.iter().enumerate() {
// Push ident
formatted_code.push_str(generic.0.as_str());
if index != generics.len() - 1 {
// Push `, ` if this is not the last generic
formatted_code.push_str(", ");
}
}
formatted_code.push_str(&generics.format(formatter))
}

// Handle openning brace
Expand Down Expand Up @@ -198,13 +184,3 @@ impl CurlyBrace for ItemStruct {
.unwrap_or_default();
}
}

impl AngleBracket for ItemStruct {
fn open_angle_bracket(line: &mut String, _formatter: &mut Formatter) {
line.push('<');
}

fn close_angle_bracket(line: &mut String, _formatter: &mut Formatter) {
line.push('>');
}
}
16 changes: 9 additions & 7 deletions sway-fmt-v2/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod attribute;
pub mod bracket;
pub mod indent_style;
pub mod item;
pub mod item_len;
pub mod newline_style;
pub mod program_type;
pub(crate) mod attribute;
pub(crate) mod bracket;
pub(crate) mod generics;
pub(crate) mod indent_style;
pub(crate) mod item;
pub(crate) mod item_len;
pub(crate) mod newline_style;
pub(crate) mod program_type;
pub(crate) mod punctuated;
16 changes: 2 additions & 14 deletions sway-fmt-v2/src/utils/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,8 @@ impl FormatDecl for AttributeDecl {
Self::open_parenthesis(line, formatter);
// format and add args `read, write`
if let Some(args) = attr.args {
let args = args.into_inner().value_separator_pairs;
let mut buf = args
.iter()
.map(|arg| format!("{}{}", arg.0.as_str(), arg.1.span().as_str()))
.collect::<Vec<String>>()
.join(" ");
if args.len() == 1 {
buf.pop(); // pop the ending comma
line.push_str(&buf);
} else {
buf.pop(); // pop the ending space
buf.pop(); // pop the ending comma
line.push_str(&buf);
}
let args = args.into_inner();
line.push_str(&args.format(formatter));
}
// ')'
Self::close_parenthesis(line, formatter);
Expand Down
12 changes: 6 additions & 6 deletions sway-fmt-v2/src/utils/bracket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! contained to each item's file.
use crate::Formatter;

pub trait CurlyBrace {
pub(crate) trait CurlyBrace {
/// Handles brace open scenerio. Checks the config for the placement of the brace.
/// Modifies the current shape of the formatter.
fn open_curly_brace(line: &mut String, formatter: &mut Formatter);
Expand All @@ -13,13 +13,13 @@ pub trait CurlyBrace {
fn close_curly_brace(line: &mut String, formatter: &mut Formatter);
}

pub trait SquareBracket {
pub(crate) trait SquareBracket {
fn open_square_bracket(line: &mut String, formatter: &mut Formatter);

fn close_square_bracket(line: &mut String, formatter: &mut Formatter);
}

pub trait Parenthesis {
pub(crate) trait Parenthesis {
/// Handles open parenthesis scenarios, checking the config for placement
/// and modifying the shape of the formatter where necessary.
fn open_parenthesis(line: &mut String, formatter: &mut Formatter);
Expand All @@ -28,8 +28,8 @@ pub trait Parenthesis {
fn close_parenthesis(line: &mut String, formatter: &mut Formatter);
}

pub trait AngleBracket {
fn open_angle_bracket(line: &mut String, formatter: &mut Formatter);
pub(crate) trait AngleBracket {
fn open_angle_bracket(self, line: &mut String, formatter: &mut Formatter);

fn close_angle_bracket(line: &mut String, formatter: &mut Formatter);
fn close_angle_bracket(self, line: &mut String, formatter: &mut Formatter);
}
35 changes: 35 additions & 0 deletions sway-fmt-v2/src/utils/generics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::{
fmt::{Format, FormattedCode, Formatter},
utils::bracket::AngleBracket,
};
use sway_parse::GenericParams;
use sway_types::Spanned;

// In the future we will need to determine whether the generic arguments
// are better suited as a `where` clause. At present they will be
// formatted in line.
//
impl Format for GenericParams {
fn format(&self, formatter: &mut Formatter) -> FormattedCode {
let mut formatted_code = String::new();
let params = self.parameters.clone().into_inner();

// `<`
Self::open_angle_bracket(self.clone(), &mut formatted_code, formatter);
// format and add parameters
formatted_code.push_str(&params.format(formatter));
// `>`
Self::close_angle_bracket(self.clone(), &mut formatted_code, formatter);

formatted_code
}
}

impl AngleBracket for GenericParams {
fn open_angle_bracket(self, line: &mut String, _formatter: &mut Formatter) {
line.push_str(self.parameters.open_angle_bracket_token.span().as_str())
}
fn close_angle_bracket(self, line: &mut String, _formatter: &mut Formatter) {
line.push_str(self.parameters.close_angle_bracket_token.span().as_str())
}
}
30 changes: 30 additions & 0 deletions sway-fmt-v2/src/utils/punctuated.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::fmt::{Format, FormattedCode, Formatter};
use sway_parse::punctuated::Punctuated;
use sway_types::Spanned;

impl<T, P> Format for Punctuated<T, P>
where
T: Spanned,
P: Spanned,
{
fn format(&self, _formatter: &mut Formatter) -> FormattedCode {
let mut formatted_code = FormattedCode::new();

// format and add Type & Punct
let mut buf = self
.value_separator_pairs
.iter()
.map(|pair| format!("{}{}", pair.0.span().as_str(), pair.1.span().as_str()))
.collect::<Vec<String>>()
.join(" ");
buf.pop(); // pop the ending comma
formatted_code.push_str(&buf);

// add boxed type
if let Some(final_value) = &self.final_value_opt {
formatted_code.push_str(final_value.span().as_str());
}

formatted_code
}
}