Skip to content

Conversation

grandizzy
Copy link
Collaborator

Motivation

Solution

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

@grandizzy grandizzy force-pushed the fmt-solar-gg branch 10 times, most recently from aa71d29 to 695e98a Compare September 23, 2025 10:43
@grandizzy grandizzy requested a review from DaniPopes September 23, 2025 13:25
@grandizzy grandizzy marked this pull request as ready for review September 23, 2025 13:25
Copy link
Contributor

@onbjerg onbjerg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testing this it looks good from my end

{
self.hardbreak_if_not_bol()
&& next_lo
.filter(|&lo| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_some_and, but i feel like the previous is more readable. let chain > bunch of option methods

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reverted to prev in 5cc4a15

if let Some(first) = bases.first().map(|base| base.span())
&& let Some(last) = bases.last().map(|base| base.span())
&& self.inline_config.is_disabled(Span::new(first.lo(), last.hi()))
&& self.inline_config.is_disabled(first.until(last))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to not until, see doc comments on both

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, 5cc4a15

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

brain fart, sorry

then: &'ast ast::Stmt<'ast>,
inline: bool,
) {
// NOTE(rusowsky): unless we add bracket spans to solar,
Copy link
Member

@DaniPopes DaniPopes Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment is outdated, can this be fixed / what's missing? @0xrusowsky
the until is correct here, but should be to as indicated by the comment

Copy link
Contributor

@0xrusowsky 0xrusowsky Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it outdated? @DaniPopes

    /// An `if` statement with an optional `else` block: `if (expr) { ... } else { ... }`.
    If(Box<'ast, Expr<'ast>>, Box<'ast, Stmt<'ast>>, Option<Box<'ast, Stmt<'ast>>>),

with this info i cannot print the comments as in the original code for these 2 scenarios:

    if /* comment10 */ (anotherLongCondition) // comment11
       /* comment12 */ {
            execute() ;
    } // comment13
    if (
        step.opcode == 0x52
            && /*MSTORE*/ step.stack[0] == testContract.memPtr() // MSTORE offset
            && step.stack[1] == testContract.expectedValueInMemory() // MSTORE val
    ) {
        mstoreCalled = true;
    }

cause the span of the if expr in the 1st scenario is only the anotherLongCondition ident, and in the 2nd one the binary operator expr

so i either need to:

  • self.print_if_cond("if", cond, then.span.lo())
  • self.print_if_cond("if", cond, cond.span.hi())

the former is the one we do now, cause imo is more consistent with the old behavior (puts everything inside the condition's parenthesis)

the later leaves all comments out of the parenthesis, so the first scenario would work, but the other one would output something like:

    if (
        step.opcode == 0x52
            && /*MSTORE*/ step.stack[0] == testContract.memPtr() // MSTORE offset
            && step.stack[1] == testContract.expectedValueInMemory()
    ) // MSTORE val
    {
        mstoreCalled = true;
    }

anyways, i think we can simply remove the comment. i only added it initially because i didn't know we were willing to not have 100% exact compatibility with the old formatter.
imo the if statement comment handling looks good, so the comment is not really necessary (unlike the DeclMulti comment, which could be a little bit more annoying for users, although they can simply use inline disable)

Copy link
Contributor

@0xrusowsky 0xrusowsky Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi, i deleted the cmnt.

the only implication is that this code:

    if /* comment10 */ (anotherLongCondition) // comment11
       /* comment12 */ {
            execute() ;
    } // comment13

was previously formatted as:

    if /* comment10 */ (
       anotherLongCondition // comment11
    ) {
       /* comment12 */
       execute();
    } // comment13

and now is:

```solidity
    if /* comment10 */ (
       anotherLongCondition // comment11
       /* comment12 */
    ) {
       execute();
    } // comment13

which is totally acceptable.
and if users want cmnt12 to go below, they simply need to put it inside the block, so that its parsed as part of the stmt

fn is_inline_stmt(&self, stmt: &'ast ast::Stmt<'ast>, cond_len: usize) -> bool {
if let ast::StmtKind::If(cond, then, els_opt) = &stmt.kind {
let if_span = Span::new(cond.span.lo(), then.span.hi());
let if_span = cond.span.until(then.span);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, to

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed in 5cc4a15

Copy link
Contributor

@0xrusowsky 0xrusowsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, pending Dani's validation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

4 participants