Comments (9)
I'm confused. The record example seems to use 4-space indent consistently, but the list example doesn't...
from elm-format.
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.
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.
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.
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.
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.
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.
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.
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)
- remove `\ ->` HOT 2
- in `import`: remove `exposing ()`
- Automatic fix request: module Path.To.File instead of module Main
- Performance drops exponentially with deeply nested lists HOT 2
- Critical severity vulnerability - json-schema (via binwrap) HOT 1
- Upgrade to optparse-applicative-0.17.*
- Planned for 0.10.0: Drop official support for Elm 0.17,0.18
- Use stm-conduit for logging
- Dependency updates for 0.8.6
- Upgrade to ghc 9.4
- Upgrade to hspec-golden >= 0.2.0.0
- Use hpack
- 0.8.6 failing to run on Windows HOT 18
- Expose elm-format as a Haskell library
- Formatting unspaced operators HOT 2
- Required parens around comments are removed HOT 1
- Upgrade to ghc 9.6
- Failing to install in Github Actions HOT 3
- Parse and format incomplete code
- Sort case branches to match Msg constructor ordering (only in update functions?) HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from elm-format.