Skip to content

Commit

Permalink
Removed angle bracket trait from sway-fmt-v2 (FuelLabs#2417)
Browse files Browse the repository at this point in the history
* removed angle bracket trait

* added functions for angle bracket

Co-authored-by: Alex Hansen <[email protected]>
Co-authored-by: Kaya Gökalp <[email protected]>
  • Loading branch information
3 people authored Aug 2, 2022
1 parent cd88f47 commit c2aaa0f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 96 deletions.
26 changes: 14 additions & 12 deletions sway-fmt-v2/src/utils/bracket.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! The purpose of this file is to house the traits and associated functions for formatting opening and closing delimiters.
//! This allows us to avoid matching a second time for the `ItemKind` and keeps the code pertaining to individual formatting
//! contained to each item's file.
use crate::{fmt::FormattedCode, Formatter, FormatterError};
use crate::fmt::*;
use std::fmt::Write;
use sway_parse::token::PunctKind;

pub(crate) trait CurlyBrace {
/// Handles brace open scenerio. Checks the config for the placement of the brace.
Expand Down Expand Up @@ -46,16 +48,16 @@ pub(crate) trait Parenthesis {
) -> Result<(), FormatterError>;
}

pub trait AngleBracket {
fn open_angle_bracket(
self,
line: &mut FormattedCode,
formatter: &mut Formatter,
) -> Result<(), FormatterError>;
pub(crate) fn open_angle_bracket(formatted_code: &mut FormattedCode) -> Result<(), FormatterError> {
write!(formatted_code, "{}", PunctKind::LessThan.as_char())?;

fn close_angle_bracket(
self,
line: &mut FormattedCode,
formatter: &mut Formatter,
) -> Result<(), FormatterError>;
Ok(())
}

pub(crate) fn close_angle_bracket(
formatted_code: &mut FormattedCode,
) -> Result<(), FormatterError> {
write!(formatted_code, "{}", PunctKind::GreaterThan.as_char())?;

Ok(())
}
70 changes: 6 additions & 64 deletions sway-fmt-v2/src/utils/generics.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use crate::{
fmt::{Format, FormattedCode, Formatter, FormatterError},
utils::bracket::AngleBracket,
};
use std::fmt::Write;
use super::bracket::{close_angle_bracket, open_angle_bracket};
use crate::fmt::{Format, FormattedCode, Formatter, FormatterError};
use sway_parse::{GenericArgs, GenericParams};
use sway_types::Spanned;

// In the future we will need to determine whether the generic arguments
// are better suited with a `where` clause. At present they will be
Expand All @@ -19,43 +15,16 @@ impl Format for GenericParams {
let params = self.parameters.clone().into_inner();

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

Ok(())
}
}

impl AngleBracket for GenericParams {
fn open_angle_bracket(
self,
line: &mut String,
_formatter: &mut Formatter,
) -> Result<(), FormatterError> {
write!(
line,
"{}",
self.parameters.open_angle_bracket_token.span().as_str()
)?;
Ok(())
}
fn close_angle_bracket(
self,
line: &mut String,
_formatter: &mut Formatter,
) -> Result<(), FormatterError> {
write!(
line,
"{}",
self.parameters.close_angle_bracket_token.span().as_str()
)?;
Ok(())
}
}

impl Format for GenericArgs {
fn format(
&self,
Expand All @@ -66,39 +35,12 @@ impl Format for GenericArgs {
let params = self.parameters.clone().into_inner();

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

Ok(())
}
}

impl AngleBracket for GenericArgs {
fn open_angle_bracket(
self,
line: &mut String,
_formatter: &mut Formatter,
) -> Result<(), FormatterError> {
write!(
line,
"{}",
self.parameters.open_angle_bracket_token.span().as_str()
)?;
Ok(())
}
fn close_angle_bracket(
self,
line: &mut String,
_formatter: &mut Formatter,
) -> Result<(), FormatterError> {
write!(
line,
"{}",
self.parameters.close_angle_bracket_token.span().as_str()
)?;
Ok(())
}
}
26 changes: 6 additions & 20 deletions sway-fmt-v2/src/utils/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::{fmt::Write, vec};
use sway_parse::{PathExpr, PathExprSegment, PathType, PathTypeSegment, QualifiedPathRoot};
use sway_types::Spanned;

use super::bracket::{close_angle_bracket, open_angle_bracket};

impl Format for PathExpr {
fn format(
&self,
Expand All @@ -14,19 +16,11 @@ impl Format for PathExpr {
) -> Result<(), FormatterError> {
if let Some((root, double_colon_token)) = &self.root_opt {
if let Some(root) = &root {
write!(
formatted_code,
"{}",
root.open_angle_bracket_token.span().as_str()
)?;
open_angle_bracket(formatted_code)?;
root.clone()
.into_inner()
.format(formatted_code, formatter)?;
write!(
formatted_code,
"{}",
root.close_angle_bracket_token.span().as_str()
)?;
close_angle_bracket(formatted_code)?;
}
write!(formatted_code, "{}", double_colon_token.ident().as_str())?;
}
Expand Down Expand Up @@ -86,19 +80,11 @@ impl Format for PathType {
) -> Result<(), FormatterError> {
if let Some(root_opt) = &self.root_opt {
if let Some(root) = &root_opt.0 {
write!(
formatted_code,
"{}",
root.open_angle_bracket_token.span().as_str()
)?;
open_angle_bracket(formatted_code)?;
root.clone()
.into_inner()
.format(formatted_code, formatter)?;
write!(
formatted_code,
"{}",
root.close_angle_bracket_token.span().as_str()
)?;
close_angle_bracket(formatted_code)?;
}
write!(formatted_code, "{}", root_opt.1.span().as_str())?;
}
Expand Down

0 comments on commit c2aaa0f

Please sign in to comment.