Skip to content

Commit

Permalink
Attempt to break long expressions into multiple lines (#5207)
Browse files Browse the repository at this point in the history
Fixes #3474

## Description

Breaks long expressions (focusing into chained methods calls but will
cover all expressions) into multiple lines

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
  • Loading branch information
cr-fuel authored Oct 28, 2023
1 parent de13543 commit d9de68d
Show file tree
Hide file tree
Showing 22 changed files with 895 additions and 62 deletions.
7 changes: 6 additions & 1 deletion examples/identity/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ impl IdentityExample for Contract {
fn access_control_with_identity() {
// ANCHOR: access_control_with_identity
let sender = msg_sender().unwrap();
require(sender == storage.owner.read(), MyError::UnauthorizedUser(sender));
require(
sender == storage
.owner
.read(),
MyError::UnauthorizedUser(sender),
);
// ANCHOR_END: access_control_with_identity
}
}
4 changes: 3 additions & 1 deletion examples/msg_sender/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ abi MyOwnedContract {
fn receive(field_1: u64) -> bool;
}

const OWNER = Address::from(0x9ae5b658754e096e4d681c548daf46354495a437cc61492599e33fc64dcdc30c);
const OWNER = Address::from(
0x9ae5b658754e096e4d681c548daf46354495a437cc61492599e33fc64dcdc30c,
);

impl MyOwnedContract for Contract {
fn receive(field_1: u64) -> bool {
Expand Down
16 changes: 12 additions & 4 deletions examples/storage_map/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ impl StorageMapExample for Contract {
// ANCHOR: storage_map_insert
#[storage(write)]
fn insert_into_storage_map() {
let addr1 = Address::from(0x0101010101010101010101010101010101010101010101010101010101010101);
let addr2 = Address::from(0x0202020202020202020202020202020202020202020202020202020202020202);
let addr1 = Address::from(
0x0101010101010101010101010101010101010101010101010101010101010101,
);
let addr2 = Address::from(
0x0202020202020202020202020202020202020202020202020202020202020202,
);

storage.map.insert(addr1, 42);
storage.map.insert(addr2, 77);
Expand All @@ -39,8 +43,12 @@ impl StorageMapExample for Contract {
// ANCHOR: storage_map_get
#[storage(read, write)]
fn get_from_storage_map() {
let addr1 = Address::from(0x0101010101010101010101010101010101010101010101010101010101010101);
let addr2 = Address::from(0x0202020202020202020202020202020202020202020202020202020202020202);
let addr1 = Address::from(
0x0101010101010101010101010101010101010101010101010101010101010101,
);
let addr2 = Address::from(
0x0202020202020202020202020202020202020202020202020202020202020202,
);

storage.map.insert(addr1, 42);
storage.map.insert(addr2, 77);
Expand Down
11 changes: 9 additions & 2 deletions examples/storage_variables/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ impl StorageExample for Contract {
fn store_something() {
storage.var1.x.write(42);
storage.var1.y.write(77);
storage.var2.w.write(0x1111111111111111111111111111111111111111111111111111111111111111);
storage
.var2
.w
.write(
0x1111111111111111111111111111111111111111111111111111111111111111,
);
storage.var2.z.write(true);
}
// ANCHOR_END: storage_write
Expand All @@ -44,7 +49,9 @@ impl StorageExample for Contract {
(
storage.var1.x.try_read().unwrap_or(0),
storage.var1.y.try_read().unwrap_or(0),
storage.var2.w.try_read().unwrap_or(0x0000000000000000000000000000000000000000000000000000000000000000),
storage.var2.w.try_read().unwrap_or(
0x0000000000000000000000000000000000000000000000000000000000000000,
),
storage.var2.z.try_read().unwrap_or(false),
)
}
Expand Down
8 changes: 7 additions & 1 deletion examples/storage_vec/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ impl StorageVecContract for Contract {
#[storage(read, write)]
fn push_to_multiple_types_storage_vec() {
storage.row.push(TableCell::Int(3));
storage.row.push(TableCell::B256(0x0101010101010101010101010101010101010101010101010101010101010101));
storage
.row
.push(
TableCell::B256(
0x0101010101010101010101010101010101010101010101010101010101010101,
),
);
storage.row.push(TableCell::Boolean(true));
}
// ANCHOR_END: storage_vec_multiple_types_fn
Expand Down
26 changes: 23 additions & 3 deletions examples/subcurrency/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ abi Token {
// Constants
////////////////////////////////////////
/// Address of contract creator.
const MINTER = Address::from(0x9299da6c73e6dc03eeabcce242bb347de3f5f56cd1c70926d76526d7ed199b8b);
const MINTER = Address::from(
0x9299da6c73e6dc03eeabcce242bb347de3f5f56cd1c70926d76526d7ed199b8b,
);

////////////////////////////////////////
// Contract storage
Expand All @@ -64,7 +66,16 @@ impl Token for Contract {
};

// Increase the balance of receiver
storage.balances.insert(receiver, storage.balances.get(receiver).try_read().unwrap_or(0) + amount);
storage
.balances
.insert(
receiver,
storage
.balances
.get(receiver)
.try_read()
.unwrap_or(0) + amount,
);
}

#[storage(read, write)]
Expand All @@ -81,7 +92,16 @@ impl Token for Contract {
storage.balances.insert(sender, sender_amount - amount);

// Increase the balance of receiver
storage.balances.insert(receiver, storage.balances.get(receiver).try_read().unwrap_or(0) + amount);
storage
.balances
.insert(
receiver,
storage
.balances
.get(receiver)
.try_read()
.unwrap_or(0) + amount,
);

log(Sent {
from: sender,
Expand Down
7 changes: 6 additions & 1 deletion examples/vec/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ fn main() {

let mut row = Vec::new();
row.push(TableCell::Int(3));
row.push(TableCell::B256(0x0101010101010101010101010101010101010101010101010101010101010101));
row
.push(
TableCell::B256(
0x0101010101010101010101010101010101010101010101010101010101010101,
),
);
row.push(TableCell::Boolean(true));
// ANCHOR_END: vec_multiple_data_types
}
15 changes: 9 additions & 6 deletions examples/wallet_contract_caller_script/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ fn main() {
let contract_address = 0x9299da6c73e6dc03eeabcce242bb347de3f5f56cd1c70926d76526d7ed199b8b;
let caller = abi(Wallet, contract_address);
let amount_to_send = 200;
let recipient_address = Address::from(0x9299da6c73e6dc03eeabcce242bb347de3f5f56cd1c70926d76526d7ed199b8b);
caller.send_funds {
gas: 10000,
coins: 0,
asset_id: ZERO_B256,
}(amount_to_send, recipient_address);
let recipient_address = Address::from(
0x9299da6c73e6dc03eeabcce242bb347de3f5f56cd1c70926d76526d7ed199b8b,
);
caller
.send_funds {
gas: 10000,
coins: 0,
asset_id: ZERO_B256,
}(amount_to_send, recipient_address);
}
4 changes: 3 additions & 1 deletion examples/wallet_smart_contract/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use std::{
// ANCHOR: abi_import
use wallet_abi::Wallet;
// ANCHOR_END: abi_import
const OWNER_ADDRESS = Address::from(0x8900c5bec4ca97d4febf9ceb4754a60d782abbf3cd815836c1872116f203f861);
const OWNER_ADDRESS = Address::from(
0x8900c5bec4ca97d4febf9ceb4754a60d782abbf3cd815836c1872116f203f861,
);

storage {
balance: u64 = 0,
Expand Down
11 changes: 9 additions & 2 deletions swayfmt/src/config/heuristics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::{
config::user_opts::HeuristicsOptions,
constants::{
DEFAULT_ATTR_FN_LIKE_WIDTH, DEFAULT_CHAIN_WIDTH, DEFAULT_COLLECTION_WIDTH,
DEFAULT_FN_CALL_WIDTH, DEFAULT_MAX_LINE_WIDTH, DEFAULT_SINGLE_LINE_IF_ELSE_WIDTH,
DEFAULT_STRUCTURE_LIT_WIDTH, DEFAULT_STRUCTURE_VAR_WIDTH,
DEFAULT_FN_CALL_WIDTH, DEFAULT_MAX_LINE_WIDTH, DEFAULT_SHORT_ARRAY_ELEM_WIDTH_THRESHOLD,
DEFAULT_SINGLE_LINE_IF_ELSE_WIDTH, DEFAULT_STRUCTURE_LIT_WIDTH,
DEFAULT_STRUCTURE_VAR_WIDTH,
},
};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -84,6 +85,7 @@ pub struct WidthHeuristics {
// Maximum line length for single line if-else expressions. A value
// of zero means always break if-else expressions.
pub(crate) single_line_if_else_max_width: usize,
pub(crate) short_array_element_width: usize,
}

impl WidthHeuristics {
Expand All @@ -97,6 +99,7 @@ impl WidthHeuristics {
collection_width: usize::max_value(),
chain_width: usize::max_value(),
single_line_if_else_max_width: 0,
short_array_element_width: 0,
}
}

Expand All @@ -109,6 +112,7 @@ impl WidthHeuristics {
collection_width: max_width,
chain_width: max_width,
single_line_if_else_max_width: max_width,
short_array_element_width: max_width,
}
}

Expand All @@ -135,6 +139,9 @@ impl WidthHeuristics {
single_line_if_else_max_width: (DEFAULT_SINGLE_LINE_IF_ELSE_WIDTH as f32
* max_width_ratio)
.round() as usize,
short_array_element_width: (DEFAULT_SHORT_ARRAY_ELEM_WIDTH_THRESHOLD as f32
* max_width_ratio)
.round() as usize,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion swayfmt/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub const DEFAULT_STRUCTURE_LIT_WIDTH: usize = 18;
pub const DEFAULT_STRUCTURE_VAR_WIDTH: usize = 35;
/// Default Maximum width of an array literal before falling back to vertical formatting.
pub const DEFAULT_COLLECTION_WIDTH: usize = 60;
/// Defalt width threshold for an array element to be considered short.
/// Default width threshold for an array element to be considered short.
pub const DEFAULT_SHORT_ARRAY_ELEM_WIDTH_THRESHOLD: usize = 10;
/// Default max length of a chain to fit on a single line.
pub const DEFAULT_CHAIN_WIDTH: usize = 60;
Expand Down
14 changes: 10 additions & 4 deletions swayfmt/src/formatter/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ pub(crate) struct CodeLine {
pub(crate) expr_kind: ExprKind,
/// Used in determining `SameLineWhere` formatting.
pub(crate) has_where_clause: bool,
/// Expression is too long to fit in a single line
pub(crate) expr_new_line: bool,
}

impl CodeLine {
Expand All @@ -121,6 +123,7 @@ impl CodeLine {
line_style,
expr_kind,
has_where_clause: Default::default(),
expr_new_line: false,
}
}
pub(crate) fn reset_width(&mut self) {
Expand All @@ -147,6 +150,10 @@ impl CodeLine {
pub(crate) fn update_where_clause(&mut self, has_where_clause: bool) {
self.has_where_clause = has_where_clause;
}

pub(crate) fn update_expr_new_line(&mut self, expr_new_line: bool) {
self.expr_new_line = expr_new_line;
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -300,10 +307,9 @@ impl Shape {
}
/// Create a new `Shape` with default `CodeLine`.
pub(crate) fn with_default_code_line(self) -> Self {
Self {
code_line: CodeLine::default(),
..self
}
let mut code_line = CodeLine::default();
code_line.update_expr_new_line(self.code_line.expr_new_line);
Self { code_line, ..self }
}
}

Expand Down
2 changes: 2 additions & 0 deletions swayfmt/src/items/item_configurable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ impl Format for ItemConfigurable {
// Handle opening brace
Self::open_curly_brace(formatted_code, formatter)?;

formatter.shape.code_line.update_expr_new_line(true);

// Determine alignment tactic
match formatter.config.structures.field_alignment {
FieldAlignment::AlignFields(configurable_field_align_threshold) => {
Expand Down
2 changes: 2 additions & 0 deletions swayfmt/src/items/item_enum/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ impl Format for ItemEnum {

let fields = self.fields.get();

formatter.shape.code_line.update_expr_new_line(true);

// Handle opening brace
Self::open_curly_brace(formatted_code, formatter)?;
// Determine alignment tactic
Expand Down
2 changes: 2 additions & 0 deletions swayfmt/src/items/item_fn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ fn format_fn_args(
match fn_args {
FnArgs::Static(args) => match formatter.shape.code_line.line_style {
LineStyle::Multiline => {
formatter.shape.code_line.update_expr_new_line(true);
if !args.value_separator_pairs.is_empty() || args.final_value_opt.is_some() {
formatter.indent();
args.format(formatted_code, formatter)?;
Expand All @@ -223,6 +224,7 @@ fn format_fn_args(
} => {
match formatter.shape.code_line.line_style {
LineStyle::Multiline => {
formatter.shape.code_line.update_expr_new_line(true);
formatter.indent();
write!(formatted_code, "\n{}", formatter.indent_to_str()?)?;
format_self(self_token, ref_self, mutable_self, formatted_code)?;
Expand Down
2 changes: 2 additions & 0 deletions swayfmt/src/items/item_storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ impl Format for ItemStorage {
// Handle opening brace
Self::open_curly_brace(formatted_code, formatter)?;

formatter.shape.code_line.update_expr_new_line(true);

// Determine alignment tactic
match formatter.config.structures.field_alignment {
FieldAlignment::AlignFields(storage_field_align_threshold) => {
Expand Down
2 changes: 2 additions & 0 deletions swayfmt/src/items/item_struct/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ impl Format for ItemStruct {
write_comments(formatted_code, self.span().into(), formatter)?;
}

formatter.shape.code_line.update_expr_new_line(true);

// Determine alignment tactic
match formatter.config.structures.field_alignment {
FieldAlignment::AlignFields(enum_variant_align_threshold) => {
Expand Down
Loading

0 comments on commit d9de68d

Please sign in to comment.