8000 Directive for dense list of records (or to skip formatting) · Issue #463 · kamadorueda/alejandra · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Directive for dense list of records (or to skip formatting) #463
Open
@akiross

Description

@akiross

Hello,
I've been using alejandra for a while with no major issues, but recently I stumbled across a case where it's formatting is, IMO, making readability worse by a long shot. The case in question is when formatting a "dense list of records", such as:

[
  { foo = "foo"; bar = "bar"; baz = "baz"; }
  { foo = "foo"; bar = "bar"; baz = "baz"; }
  { foo = "foo"; bar = "bar"; baz = "baz"; }
  { foo = "foo"; bar = "bar"; ba
6326
z = "baz"; }
  { foo = "foo"; bar = "bar"; baz = "baz"; }
  { foo = "foo"; bar = "bar"; baz = "baz"; }
  { foo = "foo"; bar = "bar"; baz = "baz"; }
]

alejandra formats it as

[
    {
      foo = "foo";
      bar = "bar";
      baz = "baz";
    }
    {
      foo = "foo";
      bar = "bar";
      baz = "baz";
    }
    {
      foo = "foo";
      bar = "bar";
      baz = "baz";
    }
    {
      foo = "foo";
      bar = "bar";
      baz = "baz";
    }
    {
      foo = "foo";
      bar = "bar";
      baz = "baz";
    }
    {
      foo = "foo";
      bar = "bar";
      baz = "baz";
    }
    {
      foo = "foo";
      bar = "bar";
      baz = "baz";
    }
  ];

but I think this is so much harder to read, especially if the list is longer.

When attributes are missing, like in this case:

[
  { foo = "foo"; bar = "barrr"; baz = "baz"; }
  { bar = "bar"; baz = "baz"; }
  { foo = "foooo"; bar = "bar"; baz = "baz"; }
  { foo = "foo"; baz = "baz"; }
  { foo = "foo"; bar = "baar"; baz = "baz"; }
  { foo = "fooo"; bar = "bar"; }
  { foo = "foo"; baz = "bazzz"; }
]

I think the ideal (most readable) formatting would be columnar, like this:

[
  { foo = "foo";   bar = "barrr"; baz = "baz";   }
  {                bar = "bar";   baz = "baz";   }
  { foo = "foooo"; bar = "bar";   baz = "baz";   }
  { foo = "foo";                  baz = "baz";   }
  { foo = "foo";   bar = "baar";  baz = "baz";   }
  { foo = "fooo";  bar = "bar";                  }
  { foo = "foo";                  baz = "bazzz"; }
]

In this case, the formatter might even sort the attributes (#276) to keep things tidy.

Of course, this is a very specific behavior, so to be honest I think a directive like # alejandra:dense,columnar could work.

If having such a directive is not desirable, then I'd like to have a directive to avoid formatting an entire block, such as:

# alejandra:skip
[
      { foo = "unformatted"; }
                  { bar="code";     }
]

I couldn't find something similar, so maybe it's already there.
I hope it makes sense!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0