Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
528c1ee
wip
eureka-cpu Jun 18, 2022
18973e6
add test, update function
eureka-cpu Jun 18, 2022
c8ac435
Merge branch 'master' into eureka-cpu/add-abi
eureka-cpu Jun 20, 2022
b0d2fd6
Merge branch 'master' into eureka-cpu/add-abi
eureka-cpu Jun 20, 2022
d7a2ca4
adding items --
eureka-cpu Jun 21, 2022
62d49c2
update abi with new annotation formatting
eureka-cpu Jun 22, 2022
ba9690d
Merge branch 'master' into eureka-cpu/add-abi
eureka-cpu Jun 22, 2022
6faad01
Merge branch 'master' into eureka-cpu/add-abi
eureka-cpu Jun 22, 2022
fa0dcf8
Merge branch 'master' into eureka-cpu/add-abi
eureka-cpu Jun 22, 2022
8a6616d
small refactor, test now fails as intended
eureka-cpu Jun 22, 2022
cf5f3fa
small fix for indentations
eureka-cpu Jun 22, 2022
ebf521b
update extra_width with config values
eureka-cpu Jun 22, 2022
148f5b0
abi test passes
Jun 22, 2022
307f7c1
Merge branch 'master' into eureka-cpu/add-abi
eureka-cpu Jun 22, 2022
7350179
Merge branch 'master' of https://github.com/FuelLabs/sway into eureka…
Jun 22, 2022
52587b5
add shape for ItemFn
Jun 22, 2022
11004a5
Merge branch 'eureka-cpu/add-abi' of https://github.com/FuelLabs/sway…
Jun 22, 2022
1e92a7b
ensure unannotated functions have correct indentation
eureka-cpu Jun 22, 2022
7af2469
Merge branch 'master' into eureka-cpu/add-abi
eureka-cpu Jun 22, 2022
f250ef4
clippy
eureka-cpu Jun 22, 2022
1352cb2
Merge branch 'master' into eureka-cpu/add-abi
eureka-cpu Jun 22, 2022
99ad95b
Merge branch 'master' into eureka-cpu/add-abi
eureka-cpu Jun 22, 2022
dcf27a3
Merge branch 'master' into eureka-cpu/add-abi
eureka-cpu Jun 23, 2022
e9cb875
Merge branch 'master' into eureka-cpu/add-abi
eureka-cpu Jun 23, 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
42 changes: 32 additions & 10 deletions sway-fmt-v2/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ enum Color {
let correct_sway_code = r#"contract;

enum Color {
Blue : (),
Green : (),
Red : (),
Silver : (),
Grey : (),
Blue : (),
Green : (),
Red : (),
Silver : (),
Grey : (),
}"#;
let mut formatter = Formatter::default();
let formatted_sway_code =
Expand All @@ -129,11 +129,11 @@ enum Color {
let correct_sway_code = r#"contract;

enum Color {
Blue : (),
Green : (),
Red : (),
Silver : (),
Grey : (),
Blue : (),
Green : (),
Red : (),
Silver : (),
Grey : (),
}"#;

// Creating a config with enum_variant_align_threshold that exceeds longest variant length
Expand All @@ -144,4 +144,26 @@ enum Color {
Formatter::format(&mut formatter, Arc::from(sway_code_to_format), None).unwrap();
assert!(correct_sway_code == formatted_sway_code)
}
#[test]
fn test_item_abi() {
let sway_code_to_format = r#"contract;

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

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

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

fn hello(key: u64, value: u64);
}"#;
let mut formatter = Formatter::default();
let formatted_sway_code =
Formatter::format(&mut formatter, Arc::from(sway_code_to_format), None).unwrap();
assert!(dbg!(correct_sway_code) == dbg!(formatted_sway_code))
}
}
111 changes: 107 additions & 4 deletions sway-fmt-v2/src/items/item_abi.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,111 @@
use crate::fmt::{Format, FormattedCode, Formatter};
use sway_parse::ItemAbi;
use crate::{
config::items::ItemBraceStyle,
fmt::{Format, FormattedCode, Formatter},
utils::{attribute::FormatDecl, bracket::CurlyBrace},
};
use sway_parse::{token::Delimiter, AttributeDecl, ItemAbi};
use sway_types::Spanned;

impl Format for ItemAbi {
fn format(&self, _formatter: &mut Formatter) -> FormattedCode {
todo!()
fn format(&self, formatter: &mut Formatter) -> FormattedCode {
let mut formatted_code = String::new();

// Add enum token
formatted_code.push_str(self.abi_token.span().as_str());
formatted_code.push(' ');

// Add name of the abi
formatted_code.push_str(self.name.as_str());
Self::open_curly_brace(&mut formatted_code, formatter);

// Add item fields
// abi_items
formatted_code += self
.abi_items
.clone()
.into_inner()
.iter()
.map(|item| -> FormattedCode {
let mut buf = String::new();
let attribute_list = item.0.attribute_list.clone();
// add indent + format attribute if it exists
if !attribute_list.is_empty() {
buf.push_str(&formatter.shape.indent.to_string(formatter));
for attr in attribute_list {
AttributeDecl::format(&attr, &mut buf, formatter)
}
}
// add indent + format item
buf.push_str(&formatter.shape.indent.to_string(formatter));
buf.push_str(&format!(
"{}{}\n",
item.0.value.span().as_str(), // FnSignature formatting (todo!)
item.1.span().as_str(), // SemicolonToken
));

buf
})
.collect::<Vec<String>>()
.join("\n")
.as_str();
// abi_defs_opt
if let Some(abi_defs) = self.abi_defs_opt.clone() {
formatted_code += abi_defs
.into_inner()
.iter()
.map(|item| -> FormattedCode {
let mut buf = String::new();
let attribute_list = item.attribute_list.clone();
// add indent + format attribute if it exists
if !attribute_list.is_empty() {
buf.push_str(&formatter.shape.indent.to_string(formatter));
for attr in attribute_list {
AttributeDecl::format(&attr, &mut buf, formatter)
}
}
// add indent + format item
buf.push_str(&formatter.shape.indent.to_string(formatter));
buf.push_str(&item.value.format(formatter)); // ItemFn formatting (todo!)

buf
})
.collect::<Vec<String>>()
.join("\n")
.as_str();
}
Self::close_curly_brace(&mut formatted_code, formatter);

formatted_code
}
}

impl CurlyBrace for ItemAbi {
fn open_curly_brace(line: &mut String, formatter: &mut Formatter) {
let brace_style = formatter.config.items.item_brace_style;
let extra_width = formatter.config.whitespace.tab_spaces;
let mut shape = formatter.shape;
let open_brace = Delimiter::Brace.as_open_char();
match brace_style {
ItemBraceStyle::AlwaysNextLine => {
// Add openning brace to the next line.
line.push_str(&format!("\n{}\n", open_brace));
shape = shape.block_indent(extra_width);
}
_ => {
// Add opening brace to the same line
line.push_str(&format!(" {}\n", open_brace));
shape = shape.block_indent(extra_width);
}
}

formatter.shape = shape;
}
fn close_curly_brace(line: &mut String, formatter: &mut Formatter) {
line.push(Delimiter::Brace.as_close_char());
// If shape is becoming left-most alligned or - indent just have the defualt shape
formatter.shape = formatter
.shape
.shrink_left(formatter.config.whitespace.tab_spaces)
.unwrap_or_default();
}
}
22 changes: 13 additions & 9 deletions sway-fmt-v2/src/items/item_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
fmt::{Format, FormattedCode, Formatter},
utils::bracket::CurlyBrace,
};
use sway_parse::ItemEnum;
use sway_parse::{token::Delimiter, ItemEnum};
use sway_types::Spanned;

impl Format for ItemEnum {
Expand Down Expand Up @@ -79,26 +79,30 @@ impl Format for ItemEnum {
impl CurlyBrace for ItemEnum {
fn open_curly_brace(line: &mut String, formatter: &mut Formatter) {
let brace_style = formatter.config.items.item_brace_style;
let extra_width = formatter.config.whitespace.tab_spaces;
let mut shape = formatter.shape;
let open_brace = Delimiter::Brace.as_open_char();
match brace_style {
ItemBraceStyle::AlwaysNextLine => {
// Add openning brace to the next line.
line.push_str("\n{\n");
shape = shape.block_indent(1);
line.push_str(&format!("\n{}\n", open_brace));
shape = shape.block_indent(extra_width);
}
_ => {
// Add opening brace to the same line
line.push_str(" {\n");
shape = shape.block_indent(1);
line.push_str(&format!(" {}\n", open_brace));
shape = shape.block_indent(extra_width);
}
}

formatter.shape = shape;
}

fn close_curly_brace(line: &mut String, formatter: &mut Formatter) {
line.push('}');
// If shape is becoming left-most alligned or - indent just have the defualt shape
formatter.shape = formatter.shape.shrink_left(1).unwrap_or_default();
line.push(Delimiter::Brace.as_close_char());
// If shape is becoming left-most aligned or - indent just have the defualt shape
formatter.shape = formatter
.shape
.shrink_left(formatter.config.whitespace.tab_spaces)
.unwrap_or_default();
}
}
18 changes: 11 additions & 7 deletions sway-fmt-v2/src/utils/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<T: Parse + Format> Format for Annotated<T> {
}
}

trait FormatDecl {
pub trait FormatDecl {
fn format(&self, line: &mut String, formatter: &mut Formatter);
}

Expand All @@ -42,16 +42,20 @@ impl FormatDecl for AttributeDecl {
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
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(" ");
args.pop(); // pop the ending space
args.pop(); // pop the ending comma
line.push_str(&args);
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);
}
}
// ')'
Self::close_parenthesis(line, formatter);
Expand Down