Skip to content
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
64a33f2
wip
eureka-cpu Jun 18, 2022
0c116be
add unit test
eureka-cpu Jun 18, 2022
733f644
update tests
eureka-cpu Jun 18, 2022
fb9ffbe
updated unimplemented cases to return span
eureka-cpu Jun 18, 2022
cb8ce15
test now passes incorrectly
eureka-cpu Jun 18, 2022
feaf356
update AttributeDecl::format()
Jun 20, 2022
21c2aa9
update test comment
Jun 20, 2022
9985938
Merge branch 'master' into eureka-cpu/2012
eureka-cpu Jun 20, 2022
5e0d7ee
add close paren
Jun 20, 2022
a0cab9f
Merge branch 'master' into eureka-cpu/2012
Jun 20, 2022
53b06ea
Merge branch 'eureka-cpu/2012' of https://github.com/FuelLabs/sway in…
Jun 20, 2022
a68ce01
update Annotated for consistency
Jun 21, 2022
ca81d7a
chng return type for Annotated
Jun 21, 2022
ce3d73a
Merge branch 'master' of https://github.com/FuelLabs/sway into eureka…
Jun 21, 2022
0b448e7
remove test and add todos
Jun 21, 2022
0591fb9
chng return value back to FormattedCode
Jun 21, 2022
50b6987
.
Jun 21, 2022
4a65e91
Merge branch 'master' of https://github.com/FuelLabs/sway into eureka…
Jun 21, 2022
488d5a4
experimenting with using token instead of char
Jun 21, 2022
4926670
update Delimiter fns
Jun 21, 2022
7242449
Merge branch 'master' into eureka-cpu/2012
eureka-cpu Jun 21, 2022
2ba891e
Merge branch 'master' of https://github.com/FuelLabs/sway into eureka…
Jun 21, 2022
92596b1
change file name to reflect parser
Jun 21, 2022
f19492d
.
Jun 21, 2022
4848b78
Merge branch 'eureka-cpu/2012' of https://github.com/FuelLabs/sway in…
Jun 21, 2022
55a1c2e
fix files from merge
Jun 21, 2022
64a1d7c
add Format to Annotated, create item.format()
Jun 21, 2022
954e3a3
Merge branch 'master' into eureka-cpu/2012
eureka-cpu Jun 21, 2022
17ecb86
Merge branch 'master' into eureka-cpu/2012
eureka-cpu Jun 21, 2022
11c1be1
Merge branch 'master' into eureka-cpu/2012
eureka-cpu Jun 21, 2022
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
38 changes: 9 additions & 29 deletions sway-fmt-v2/src/fmt.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -55,21 +54,8 @@ impl Formatter {
raw_formatted_code += &items
.into_iter()
.map(|item| -> Result<String, FormatterError> {
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<ItemKind>
Ok(Annotated::format(&item, self))
})
.collect::<Result<Vec<String>, _>>()?
.join("\n");
Expand All @@ -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() {
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion sway-fmt-v2/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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;
79 changes: 79 additions & 0 deletions sway-fmt-v2/src/utils/attribute.rs
Original file line number Diff line number Diff line change
@@ -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<T: Parse + Format> Format for Annotated<T> {
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::<Vec<String>>()
.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())
}
}
11 changes: 0 additions & 11 deletions sway-fmt-v2/src/utils/attributes.rs

This file was deleted.

19 changes: 19 additions & 0 deletions sway-fmt-v2/src/utils/item.rs
Original file line number Diff line number Diff line change
@@ -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),
}
}
}
2 changes: 1 addition & 1 deletion sway-parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 7 additions & 0 deletions sway-parse/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down