sway icon indicating copy to clipboard operation
sway copied to clipboard

Problem with newline not being inserted after comments when a token follows it

Open eureka-cpu opened this issue 3 years ago • 1 comments

something like:

if self == PrimaryColor::Blue {
            Rgb {
                red: 0,
                blue: 255,
                green: 0,
            }
        }
        // TODO remove this else when exhaustive ifs are checked for
      else {
            Rgb {
                red: 0,
                green: 0,
                blue: 0,
            }
        }

turns into:

if self == PrimaryColor::Blue {
            Rgb {
                red: 0,
                blue: 255,
                green: 0,
            }
        }
        // TODO remove this else when exhaustive ifs are checked for else { <-- part of the comment now, should be on newline
            Rgb {
                red: 0,
                green: 0,
                blue: 0,
            }
        }
  • [x] #2654

eureka-cpu avatar Aug 29 '22 22:08 eureka-cpu

This is also happening when multiple comments are before the module_kind eg

//! A reentrancy check for use in Sway contracts.
//! Note that this only works in internal contexts.
//! to prevent reentrancy: `assert(!is_reentrant);library reentrancy; <-- Here

eureka-cpu avatar Sep 08 '22 19:09 eureka-cpu

I tried playing with this for a bit on forc-fmt v0.33.0, writing down what I found here:

It seems like indentation matters when it comes to this issue. For example, in the code sample below i intentionally mis-indented the else statement here:

impl MyContract for Contract {
    fn test_function() -> bool {
        if true {
            Rgb {
                red: 0,
                blue: 255,
                green: 0,
            }
        }
        // TODO remove this else when exhaustive ifs are checked for
    else {
            Rgb {
                red: 0,
                blue: 0,
                green: 0,
            }
        }
    }
}

The formatter ran fine and left it as is - else seems to respect the same column that fn is in, instead of if. The same thing happens if you moved else to the right, the formatter respects the spacing left there and leaves it as is.

Then I tried this:

impl MyContract for Contract {
    fn test_function() -> bool {
        if true {
            Rgb {
                red: 0,
                blue: 255,
                green: 0,
            }
        }
        // TODO remove this else when exhaustive ifs are checked for
else {
            Rgb {
                red: 0,
                blue: 0,
                green: 0,
            }
        }
    }
}

This errors:

$ forc fmt --path qwe
This file: "/.../fuellabs/sway/qwe/src/main.sw" is not part of the build
Error parsing file: Unable to parse: unexpected close delimiter

It seems like it might have to do with forc-fmt overly respecting indentation, will have to dig deeper to find out what's causing this.

spiral-ladder avatar Jan 13 '23 06:01 spiral-ladder

Thanks for looking further into this @bingcicle, conditionals were the most difficult to deal with IIRC

eureka-cpu avatar Jan 13 '23 06:01 eureka-cpu

It seems like it might have to do with forc-fmt overly respecting indentation, will have to dig deeper to find out what's causing this.

I think I might be wrong about this - just occurred to me that I had to fix issue with comment formatting when it appears between if/else blocks here: https://github.com/FuelLabs/sway/pull/3556. So this is probably the result of that behavior as well

spiral-ladder avatar Jan 13 '23 06:01 spiral-ladder