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

forc fmt doesn't format arrays very well. #726

Closed
otrho opened this issue Jan 31, 2022 · 2 comments · Fixed by #2593
Closed

forc fmt doesn't format arrays very well. #726

otrho opened this issue Jan 31, 2022 · 2 comments · Fixed by #2593
Assignees
Labels
bug Something isn't working forc formatter
Milestone

Comments

@otrho
Copy link
Contributor

otrho commented Jan 31, 2022

Here's what it did to a recent PR of mine:

diff --git a/src/b512.sw b/src/b512.sw
index 2bcf573..ff5f664 100644
--- a/src/b512.sw
+++ b/src/b512.sw
@@ -4,7 +4,8 @@ library b512;
 /// Stores two b256s in contiguous memory.
 /// Guaranteed to be contiguous for use with ec-recover: std::ecr::ec_recover().
 pub struct B512 {
-    bytes: [b256; 2],
+    bytes: [b256;
+    2],
 }

 // @todo use generic form when possible
@@ -19,7 +20,8 @@ pub trait From {
 impl From for B512 {
     fn from(h: b256, l: b256) -> B512 {
         B512 {
-            bytes: [h, l],
+            bytes: [h,
+            l],
         }
     }
 }
@@ -30,7 +32,7 @@ impl B512 {
     fn new() -> B512 {
         B512 {
             bytes: [0x0000000000000000000000000000000000000000000000000000000000000000,
-                    0x0000000000000000000000000000000000000000000000000000000000000000],
+            0x0000000000000000000000000000000000000000000000000000000000000000],
         }
     }
 }
@eureka-cpu
Copy link
Contributor

@leviathanbeak I just made a draft #1054 for this, but I'm running into the +/+ symbol and I'm wondering if you have an insight on how to get around it? Here's a snippet for example: `r"library b512;

pub struct B512 {
    bytes: [b256; 2],+/+
}
impl B512 {
    fn new() -> B512 {
        B512 {
            bytes: [0x0000000000000000000000000000000000000000000000000000000000000000,+/+ 0x0000000000000000000000000000000000000000000000000000000000000000],
        }
    }
}";`

@leviathanbeak
Copy link
Contributor

@eureka-cpu check this file: https://github.com/FuelLabs/sway/blob/master/sway-fmt/src/constants.rs
that "+/+" is added in the first pass,

from what I see here:
https://github.com/FuelLabs/sway/blob/master/sway-fmt/src/code_builder.rs#L134

"," - comma, is considered a line breaker so it creates a newline,
one thing you could do is to add a flag is_array (maybe a bit more generic name if some other things behave similarly)
and then not break into newline here
https://github.com/FuelLabs/sway/blob/master/sway-fmt/src/code_builder.rs#L137

you could also add the logic to check the length of the current CodeLine, and if it's above 60 or whatever then actually go into newline.

is_array() could also be part of the CodeLine
just like is_string() is
https://github.com/FuelLabs/sway/blob/master/sway-fmt/src/code_line.rs#L45

just in this case maybe something more generic is_collection() would be more appropriate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working forc formatter
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants