Skip to content

Commit

Permalink
fix(fmt): respect bracket spacing in named function call (foundry-rs#…
Browse files Browse the repository at this point in the history
…4363)

* fix(fmt): respect bracket spacing in named function call

* fix function call test
  • Loading branch information
rkrasiuk authored Feb 16, 2023
1 parent 9317d19 commit 43b1fc9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
12 changes: 10 additions & 2 deletions fmt/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2993,7 +2993,10 @@ impl<'a, W: Write> Visitor for Formatter<'a, W> {
}

if let Some(first) = chunks.first_mut() {
if first.prefixes.is_empty() && first.postfixes_before.is_empty() {
if first.prefixes.is_empty() &&
first.postfixes_before.is_empty() &&
!self.config.bracket_spacing
{
first.needs_space = Some(false);
}
}
Expand All @@ -3003,7 +3006,12 @@ impl<'a, W: Write> Visitor for Formatter<'a, W> {
let prefix = if multiline && !self.is_beginning_of_line() { "\n" } else { "" };
let closing_bracket = format!("{prefix}{}", "}");
let closing_bracket_loc = args.last().unwrap().loc.end();
write_chunk_spaced!(self, closing_bracket_loc, Some(false), "{closing_bracket}")?;
write_chunk_spaced!(
self,
closing_bracket_loc,
Some(self.config.bracket_spacing),
"{closing_bracket}"
)?;

Ok(())
}
Expand Down
37 changes: 37 additions & 0 deletions fmt/testdata/FunctionCall/bracket-spacing.fmt.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// config: line_length = 120
// config: bracket_spacing = true
contract FunctionCall {
function foo() public pure {
bar(1111111111111111111111111111111111111111111111111111, 111111111111111111111111111111111111111111111111111);
bar(1111111111111111111111111111111111111111111111111112, 1111111111111111111111111111111111111111111111111112);
bar(1111111111111111111111111111111111111111111111111113, 11111111111111111111111111111111111111111111111111113); // the semicolon is not considered when determining line break
bar(
1111111111111111111111111111111111111111111111111114, 111111111111111111111111111111111111111111111111111114
);
bar(
111111111111111111111111111111111115,
11111111111111111111111111111111115,
11111111111111111111111111111111115
);
bar(
111111111111111111111111111111111111111111111111111116,
111111111111111111111111111111111111111111111111111116
);
bar(
111111111111111111111111111111111111111111111111111117,
1111111111111111111111111111111111111111111111111111117
);
}

function bar(uint256, uint256) private pure {
return;
}
}

function a(uint256 foo) {
foo;
}

function b() {
a({ foo: 5 });
}
8 changes: 8 additions & 0 deletions fmt/testdata/FunctionCall/fmt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ contract FunctionCall {
return;
}
}

function a(uint256 foo) {
foo;
}

function b() {
a({foo: 5});
}
8 changes: 8 additions & 0 deletions fmt/testdata/FunctionCall/original.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ contract FunctionCall {
return;
}
}

function a(uint256 foo) {
foo;
}

function b() {
a( {foo: 5} );
}
1 change: 0 additions & 1 deletion fmt/testdata/FunctionCallArgsStatement/fmt.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// config: bracket_spacing = true
interface ITarget {
function run() external payable;
function veryAndVeryLongNameOfSomeRunFunction() external payable;
Expand Down

0 comments on commit 43b1fc9

Please sign in to comment.