Elm-format: Consider switching to trailing commas

Created on 2 May 2016  路  7Comments  路  Source: avh4/elm-format

Currently elm format the first item or a record / list like this:

record =
  { b = 1
  , c = 2
  }


list =
  [ "b"
  , "c"
  ]

Issue 1:

This doesn't allow me to easily sort lines alphabetically, as some lines start with { and others with ,.

Issue 2:

If I want to keep my attributes in alphabetic order (which I usually do), then adding an entry produces a lot of commit noise, as I need to touch two lines.

record =
{ b = 1
, c = 2
}

To

record =
  { a = 2
  , b = 1
  , c = 2
  }

I know there is a proposal to allow trailing commas (https://github.com/avh4/elm-format/issues/100). But even if this is not supported let's consider switching to trailing commas like:

record = { 
      a = 2,
      b = 1,
      c = 2
    }

This will fix the issue of sorting lines alphabetically.

invalid

Most helpful comment

I'd prefer

record =
  { 
    a = 2,
    b = 1,
    c = 2
  }

But we all know this could go on forever...

All 7 comments

I'd prefer

record =
  { 
    a = 2,
    b = 1,
    c = 2
  }

But we all know this could go on forever...

Sure, that works as well. The issue is just with having { or [ in front of the first item.

Seems reasonable to me - a bit of muscle memory in vim to transpose lines and it keeps working. Except the last line, but that requires language support.... (once it is, elm-format should add the trailing comma.)

This is a case where I agree with you, but elm-format needs to be consistent with the Elm Style Guide http://elm-lang.org/docs/style-guide

So this is something that's not going to happen until the style guide changes. Discussion for that should go to elm-discuss, or Elm slack.

Ok, I'll close this.
I posted a request in elm-dev
https://groups.google.com/forum/#!topic/elm-dev/c711R30X1lE

Please post something in that thread if you care about this issue, thanks.

Also, this related issue is remaining open: https://github.com/avh4/elm-format/issues/100

I know there is a proposal to allow trailing commas (#100). But even if this is not supported let's consider switching to trailing commas like:

I agree wholeheartedly. The format you suggest:

  • Requires fewer keystrokes in every case but adding element to the end (see below).
  • Is more familiar for people coming from JS.
  • Will be easier to migrate to using trailing commas if #100 is ever finished.

The currently-recommended format performs the worst possible in terms of reducing keystrokes, especially if you're like me and use shortcuts for duplicating/swapping lines.

-- Leading commas (Elm Format)
[ 2
, 1
, 3
]

-- Add one to beginning: 5 (7 with line dup method)
-- Add one to end: 4 (3 with line dup method)
-- Swap to beginning: 5 (8 with line swap method)
-- Swap to end: 1
-- Total: 15 (19 with line dup method)

-- Leading commas
[
  1
, 2
, 3
]

-- Add one to beginning: 8 (7 with line dup method)
-- Add one to end: 4 (3 with line dup method)
-- Swap to beginning: 8
-- Swap to end: 1
-- Total: 21 (19 with line dup method)

-- Regular commas
[
  1,
  2,
  3
]

-- Add one to beginning: 4 (5 with line dup method)
-- Add one to end: 4 (5 with line dup method)
-- Swap to beginning: 1
-- Swap to end: 3
-- Total: 12 (14 with line dup method)

-- Trailing commas (not possible in Elm yet)
[
  1,
  2,
  3,
]

-- Add one to beginning: 4 (5 with line dup method)
-- Add one to end: 3 (4 with line dup method)
-- Swap to beginning: 1
-- Swap to end: 1
-- Total: 9 (11 with line dup method)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

welblaud picture welblaud  路  3Comments

avh4 picture avh4  路  6Comments

rtfeldman picture rtfeldman  路  4Comments

rtfeldman picture rtfeldman  路  7Comments

Janiczek picture Janiczek  路  5Comments