Skip to content

Commit

Permalink
fix: function returns formatting (#6086)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Oct 25, 2023
1 parent 251ef74 commit 3fe2392
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
17 changes: 16 additions & 1 deletion crates/fmt/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1497,12 +1497,27 @@ impl<'a, W: Write> Formatter<'a, W> {
if fmt.inline_config.is_disabled(returns_loc) {
fmt.indented(1, |fmt| fmt.visit_source(returns_loc))?;
} else {
let returns = fmt.items_to_chunks(
let mut returns = fmt.items_to_chunks(
returns_end,
func.returns
.iter_mut()
.filter_map(|(loc, param)| param.as_mut().map(|param| (*loc, param))),
)?;

// there's an issue with function return value that would lead to indent issues because those can be formatted with line breaks <https://github.com/foundry-rs/foundry/issues/4080>
for function_chunk in
returns.iter_mut().filter(|chunk| chunk.content.starts_with("function("))
{
// this will bypass the recursive indent that was applied when the function
// content was formatted in the chunk
function_chunk.content = function_chunk
.content
.split('\n')
.map(|s| s.trim_start())
.collect::<Vec<_>>()
.join("\n");
}

fmt.write_postfix_comments_before(returns_loc.start())?;
fmt.write_whitespace_separator(multiline)?;
fmt.indented(1, |fmt| {
Expand Down
13 changes: 13 additions & 0 deletions crates/fmt/testdata/FunctionDefinitionWithFunctionReturns/fmt.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract ReturnFnFormat {
function returnsFunction()
internal
pure
returns (
function()
internal pure returns (uint256)
)
{}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract ReturnFnFormat {
function returnsFunction()
internal
pure
returns (
function()
internal pure returns (uint256)
)
{}
}
1 change: 1 addition & 0 deletions crates/fmt/tests/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ test_directories! {
ErrorDefinition,
EventDefinition,
FunctionDefinition,
FunctionDefinitionWithFunctionReturns,
FunctionType,
ImportDirective,
ModifierDefinition,
Expand Down

0 comments on commit 3fe2392

Please sign in to comment.