Skip to content

Commit

Permalink
feat(fmt): variable override spacing option (foundry-rs#3524)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrasiuk authored Oct 21, 2022
1 parent d896050 commit afcfdfa
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
3 changes: 3 additions & 0 deletions config/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub struct FormatterConfig {
pub number_underscore: NumberUnderscore,
/// Style of single line blocks in statements
pub single_line_statement_blocks: SingleLineBlockStyle,
/// Print space in variable `override` definition
pub variable_override_spacing: bool,
/// Globs to ignore
pub ignore: Vec<String>,
}
Expand Down Expand Up @@ -109,6 +111,7 @@ impl Default for FormatterConfig {
quote_style: QuoteStyle::Double,
number_underscore: NumberUnderscore::Preserve,
single_line_statement_blocks: SingleLineBlockStyle::Preserve,
variable_override_spacing: true,
ignore: vec![],
}
}
Expand Down
6 changes: 5 additions & 1 deletion fmt/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2588,7 +2588,11 @@ impl<'a, W: Write> Visitor for Formatter<'a, W> {
VariableAttribute::Constant(_) => Some("constant".to_string()),
VariableAttribute::Immutable(_) => Some("immutable".to_string()),
VariableAttribute::Override(loc, idents) => {
self.visit_list("override", idents, Some(loc.start()), Some(loc.end()), false)?;
write_chunk!(self, loc.start(), "override")?;
if !idents.is_empty() && self.config.variable_override_spacing {
self.write_whitespace_separator(false)?;
}
self.visit_list("", idents, Some(loc.start()), Some(loc.end()), false)?;
None
}
};
Expand Down
8 changes: 8 additions & 0 deletions fmt/testdata/VariableDefinition/fmt.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
// config: line_length = 40
contract Contract {
bytes32 private constant BYTES;
bytes32
private
constant
override (Base1) BYTES;
bytes32
private
constant
override (Base1, Base2) BYTES;
bytes32
private
constant
Expand Down
2 changes: 2 additions & 0 deletions fmt/testdata/VariableDefinition/original.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
contract Contract {
bytes32 constant private BYTES;
bytes32 private constant override (Base1) BYTES;
bytes32 private constant override (Base1, Base2) BYTES;
bytes32 private constant override immutable BYTES;
bytes32 private constant override immutable BYTES_VERY_VERY_VERY_LONG;
bytes32 private constant override(Base1, Base2, SomeLongBaseContract, AndAnotherVeryLongBaseContract, Imported.Contract) BYTES_OVERRIDDEN;
Expand Down
66 changes: 66 additions & 0 deletions fmt/testdata/VariableDefinition/override-space.fmt.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// config: line_length = 40
// config: variable_override_spacing = false
contract Contract {
bytes32 private constant BYTES;
bytes32
private
constant
override(Base1) BYTES;
bytes32
private
constant
override(Base1, Base2) BYTES;
bytes32
private
constant
immutable
override BYTES;
bytes32
private
constant
immutable
override
BYTES_VERY_VERY_VERY_LONG;
bytes32
private
constant
override(
Base1,
Base2,
SomeLongBaseContract,
AndAnotherVeryLongBaseContract,
Imported.Contract
) BYTES_OVERRIDDEN;

bytes32 private constant BYTES =
0x035aff83d86937d35b32e04f0ddc6ff469290eef2f1b692d8a815c89404d4749;
bytes32
private
constant
immutable
override BYTES =
0x035aff83d86937d35b32e04f0ddc6ff469290eef2f1b692d8a815c89404d4749;
bytes32
private
constant
immutable
override
BYTES_VERY_VERY_VERY_LONG =
0x035aff83d86937d35b32e04f0ddc6ff469290eef2f1b692d8a815c89404d4749;
bytes32 private constant
BYTES_VERY_VERY_LONG =
0x035aff83d86937d35b32e04f0ddc6ff469290eef2f1b692d8a815c89404d4749;

uint256 constant POWER_EXPRESSION =
10 ** 27;
uint256 constant ADDED_EXPRESSION =
1 + 2;

// comment 1
uint256 constant example1 = 1;
// comment 2
// comment 3
uint256 constant example2 = 2; // comment 4
uint256 constant example3 = /* comment 5 */
3; // comment 6
}

0 comments on commit afcfdfa

Please sign in to comment.