avh4 / avh4/elm-format

Nesting |> andThen consumes too much indent

Open
#352 24 comments 1 reaction 0 assignees View on GitHub
discussion
Dominant language
Haskell
Stars
1.3k
Forks
147
PR merge metrics
No merged PRs in 30d

Description

Today I first tried elm-format and shocked with [this result](https://github.com/WorksApplications/office-maker/commit/a2a584c097cbe15de0e84ea03fbc990853cd35e4#diff-9e9c9d7a6a3aa1ddef3de9fd517c5c86L308). It consumes 12 spaces per one chain.

```diff
- model.floor
- |> Maybe.map EditingFloor.present
- |> Maybe.andThen (\floor -> List.head (selectedObjects model)
- |> Maybe.andThen (\primarySelected ->
- ObjectsOperation.nearest direction primarySelected (Floor.objects floor)
- |> Maybe.map (\object ->
- { model |
- selectedObjects =
- List.map Object.idOf [object]
- }
- )))
- |> Maybe.withDefault model
+ model.floor
+ |> Maybe.map EditingFloor.present
+ |> Maybe.andThen
+ (\floor ->
+ List.head (selectedObjects model)
+ |> Maybe.andThen
+ (\primarySelected ->
+ ObjectsOperation.nearest direction primarySelected (Floor.objects floor)
+ |> Maybe.map
+ (\object ->
+ { model
+ | selectedObjects =
+ List.map Object.idOf [ object ]
+ }
+ )
+ )
+ )
+ |> Maybe.withDefault model
```
In Haskell, this may be flattened by `do` notation. So I don't think the former style is too strange.

Another alternative would be the following style.

```diff
+ model.floor
+ |> Maybe.map EditingFloor.present
+ |> Maybe.andThen (\floor ->
+ List.head (selectedObjects model)
+ |> Maybe.andThen (\primarySelected ->
+ ObjectsOperation.nearest direction primarySelected (Floor.objects floor)
+ |> Maybe.map (\object ->
+ { model
+ | selectedObjects =
+ List.map Object.idOf [ object ]
+ }
+ )
+ )
+ )
+ |> Maybe.withDefault model
```
This is 8 spaces per one chain.

And this is 4 spaces.
```diff
+ model.floor
+ |> Maybe.map EditingFloor.present
+ |> Maybe.andThen (\floor -> List.head (selectedObjects model)
+ |> Maybe.andThen (\primarySelected -> ObjectsOperation.nearest direction primarySelected (Floor.objects floor)
+ |> Maybe.map (\object ->
+ { model
+ | selectedObjects =
+ List.map Object.idOf [ object ]
+ }
+ )
+ )
+ )
+ |> Maybe.withDefault model
```

I found this was [once discussed before](https://github.com/avh4/elm-format/issues/187). No progress on this issue since then? I don't have clear idea but wanted to bring it up again before public release. Is it possibly true that elm-lang/core will be formatted in the future? It has [similar code](https://github.com/elm-lang/core/blob/master/src/Task.elm#L123) too.

Contributor guide

No contributing guide indexed for this repository

Research direction

Review the prior discussion in issue #187 and compare the linked commit's formatting examples with the current handling of nested |> andThen chains. Decide which indentation style should be supported, then add coverage for the shown cases and verify the formatter produces the selected result.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.