Skip to content

Commit

Permalink
Remove Spanned and add Format trait to Punctuated (FuelLabs#2269)
Browse files Browse the repository at this point in the history
* update punctuated with Format trait

* add Format to punctuation
  • Loading branch information
eureka-cpu authored Jul 8, 2022
1 parent aadca68 commit 2a42019
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions sway-fmt-v2/src/utils/punctuated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,49 @@ use crate::{
FormatterError,
};
use std::fmt::Write;
use sway_parse::{punctuated::Punctuated, StorageField, TypeField};
use sway_types::Spanned;
use sway_parse::{keywords::CommaToken, punctuated::Punctuated, StorageField, TypeField};
use sway_types::{Ident, Spanned};

impl<T, P> Format for Punctuated<T, P>
where
T: Spanned,
P: Spanned,
T: Format,
P: Format,
{
fn format(
&self,
formatted_code: &mut FormattedCode,
_formatter: &mut Formatter,
formatter: &mut Formatter,
) -> Result<(), FormatterError> {
// format and add Type & Punct
let value_pairs = &self.value_separator_pairs;

// Later on we may want to handle instances
// where the user wants to keep the trailing commas.
for pair in value_pairs.iter() {
write!(
formatted_code,
"{}{} ",
pair.0.span().as_str(),
pair.1.span().as_str(),
)?;
pair.0.format(formatted_code, formatter)?;
pair.1.format(formatted_code, formatter)?;
}

// add final value, if any
if let Some(final_value) = &self.final_value_opt {
write!(formatted_code, "{}", final_value.span().as_str())?;
final_value.format(formatted_code, formatter)?;
}

Ok(())
}
}

impl Format for Ident {
fn format(
&self,
formatted_code: &mut FormattedCode,
_formatter: &mut Formatter,
) -> Result<(), FormatterError> {
write!(formatted_code, "{}", self.span().as_str())?;
Ok(())
}
}

impl Format for TypeField {
fn format(
&self,
Expand Down Expand Up @@ -72,3 +79,14 @@ impl Format for StorageField {
Ok(())
}
}

impl Format for CommaToken {
fn format(
&self,
formatted_code: &mut FormattedCode,
_formatter: &mut Formatter,
) -> Result<(), FormatterError> {
write!(formatted_code, "{} ", self.span().as_str())?;
Ok(())
}
}

0 comments on commit 2a42019

Please sign in to comment.