Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: function returns formatting #6086

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
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