From c2aaa0f7b3313dc067bc0312f12c020d8bf2ac68 Mon Sep 17 00:00:00 2001 From: Mitchell Mackert <98185767+gr00vytvniks@users.noreply.github.com> Date: Tue, 2 Aug 2022 14:37:21 -0500 Subject: [PATCH] Removed angle bracket trait from sway-fmt-v2 (#2417) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * removed angle bracket trait * added functions for angle bracket Co-authored-by: Alex Hansen Co-authored-by: Kaya Gökalp --- sway-fmt-v2/src/utils/bracket.rs | 26 ++++++------ sway-fmt-v2/src/utils/generics.rs | 70 +++---------------------------- sway-fmt-v2/src/utils/path.rs | 26 +++--------- 3 files changed, 26 insertions(+), 96 deletions(-) diff --git a/sway-fmt-v2/src/utils/bracket.rs b/sway-fmt-v2/src/utils/bracket.rs index c5ca4e5884b..07b316412e8 100644 --- a/sway-fmt-v2/src/utils/bracket.rs +++ b/sway-fmt-v2/src/utils/bracket.rs @@ -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. @@ -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(()) } diff --git a/sway-fmt-v2/src/utils/generics.rs b/sway-fmt-v2/src/utils/generics.rs index b5bfdd0f989..d202ae695d3 100644 --- a/sway-fmt-v2/src/utils/generics.rs +++ b/sway-fmt-v2/src/utils/generics.rs @@ -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 @@ -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, @@ -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(()) - } -} diff --git a/sway-fmt-v2/src/utils/path.rs b/sway-fmt-v2/src/utils/path.rs index c4aae285d0f..9fc55fd46f1 100644 --- a/sway-fmt-v2/src/utils/path.rs +++ b/sway-fmt-v2/src/utils/path.rs @@ -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, @@ -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())?; } @@ -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())?; }