Giter Site home page Giter Site logo

Comments (9)

Apanatshka avatar Apanatshka commented on May 12, 2024

I'm confused. The record example seems to use 4-space indent consistently, but the list example doesn't...

from elm-format.

avh4 avatar avh4 commented on May 12, 2024

I guess that's true, let's focus on the list example for this thread. I'll remove the record one from the description.

from elm-format.

rtfeldman avatar rtfeldman commented on May 12, 2024

Here is a real-world example:

        div
            [ classList classes ]
            [ div
                [ class "modal-backdrop" ]
                []
            , div
                [ class "school-search-modal" ]
                [ div
                    [ class "school-search-modal-header" ]
                    [ viewHeader address model ]
                , div
                    [ class "school-search-modal-body" ]
                    [ viewBody address model ]
                ]
            ]

We basically did what you did above: deviate from the 4-space grid as little as possible.

In my experience, this is the most common case for multiline expressions inside list literals: a function being applied, followed by indented arguments. I agree that it's a bit weird that things like else will be de facto indented 2 spaces instead of 4, but I don't think it will come up often enough in practice to be a cause for concern.

from elm-format.

Apanatshka avatar Apanatshka commented on May 12, 2024

I think I understand the issue now, and have formed an opinion (which also relates to what I think about #18):

The 4-space1 grid is not holy. Easily recognisable code patterns are much more important. So if you normally have a definition in a let indented by four spaces:

f a b =
    let
        x = 0
    in
        x + a * b

Then you should have a four space difference between the starting column of the let and of the definition in other contexts too:

f2 a b =
    [ let
          x = 0
      in
          x + a * b
    , Just Nothing
    ]

So for the larger example this looks like:

multilineExpressionsInsideList =
    [ let
          x = 1
      in
          x
    , if True then
          2
      else if False then
          3
      else
          4
    , case True of
          _ ->
              5
    , [ 6
      , 7
      ]
      |> head
      |> Maybe.withDefault 8
    , \a ->
          9
    ]

1 I don't remember talking about what size the indentation should be and don't see it in the Google doc. Did we agree on 4 spaces? Because I much prefer 2 spaces actually.

from elm-format.

rtfeldman avatar rtfeldman commented on May 12, 2024

I tried both based on Laszlo's style guide suggestion of 4, and I have a minor aesthetic preference for 4.

I can get behind the idea of 2 making things more consistent and fixing edge cases though. Maybe we should try 2 and see how it goes? Quick enough change I'd assume.

from elm-format.

avh4 avatar avh4 commented on May 12, 2024

Discussion of 2 vs 4 space indent can be moved to another issue. (I thought Evan had a strong preference for 4 spaces?)

Assuming we keep 4 spaces, there seems to be agreement on this issue.

However, there is an edge case with nested lists. Does this look good:

foo =
    [ [ 1 ]
    , [ 20
      , 21
      ]
    , [ [ 30
        , 31
        ]
            |> head
            |> Maybe.withDefault 0
      ]
    ]

from elm-format.

avh4 avatar avh4 commented on May 12, 2024

Here is another bad edge-case:

(==/==) a b = a

multilineParenthesizedExpressions =
    graphHeight
        / ( if range == 0 then
            0.1
          else
            toFloat range
          )
        ==/== ( if range == 0 then
            0.1
          else
            toFloat range
          )

The if and else are not aligned.

from elm-format.

rtfeldman avatar rtfeldman commented on May 12, 2024

How about putting the if and the parens on their own lines? I did a lot of that stuff in my trailing-commas days (including with lists and records), and it's very easy to read:

(==/==) a b = a

multilineParenthesizedExpressions =
    graphHeight
        /
        (
            if range == 0 then
                0.1
            else
                toFloat range
        )
        ==/==
        (
            if range == 0 then
                0.1
            else
                toFloat range
        )

from elm-format.

avh4 avatar avh4 commented on May 12, 2024

So I wrote a test case and ran it through the formatter, and it currently formats like this, which I think makes enough sense for now, given that it took no effort to implement:

multilineParenthesizedExpressions =
    graphHeight
        / ( if range == 0 then
              0.1
            else
              toFloat range
          )
        ==/== ( if range == 0 then
                  0.2
                else
                  toFloat (range - 1)
              )
        <<>> ( if range == 0 then
                 -1.0
               else
                 0.0
             )

We can consider revisiting at a later time after seeing more real-world examples.

from elm-format.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.