Allow single-line case-expressions branches#649
Conversation
|
Note:
|
74bfb5a to
1d1ef0b
Compare
1d1ef0b to
163199f
Compare
|
Updated with |
|
@avh4 Given what's accepted with single-line For now this is not supported in this PR (comments lead to multi-line except those between |
yes, I think so |
This comment has been minimized.
This comment has been minimized.
7b7560b to
95f70f9
Compare
|
I think that the PR is now complete, including tests. |
I suspect there was not a reason for that difference. comments in position A are very rare from code I've observed, so I don't think it really matters how we choose to deal with these. |
| result <- cases firstPatternComments | ||
| return $ E.Case (e, multilineToBool multilineSubject) result | ||
| (branches, multilineFirstBranch) <- cases firstPatternComments | ||
| return $ E.Case (e, multilineSubject) (branches, multilineFirstBranch) |
There was a problem hiding this comment.
Was it in a separate issue you were suggesting using the linebreak of the first item to control multiline for other kinds of expressions besides cases? I'm still hesitant about that. I'd be more comfortable having this only be JoinAll if all of the branches are single line, and then maybe doing a separate experiment after adding this to try the idea of having the first item control everything.
There was a problem hiding this comment.
I thought we could experiment it with only case expressions, but you're right, it's better to introduce the single if and case expressions in a consistent way with existing behavior, then discuss eventually the other method. I will make the change.
There was a problem hiding this comment.
New version uses JoinAll if all branches are single line.
|
|
||
| (multilineBranches, branches') = | ||
| List.mapAccumR | ||
| (\mline (AST.Commented prePat pat postPat, (preRes, res)) -> |
There was a problem hiding this comment.
not necessary to change before merging, but FYI my preferred style here is to take this anonymous function and define it in the outermost let block right above the (multilineBranches, branches') = definition.
| preRes' = | ||
| Maybe.maybeToList $ formatComments preRes | ||
| res' = | ||
| formatExpression elmVersion importInfo SyntaxSeparated res |
There was a problem hiding this comment.
res/res'/preRes should I think be called "body" instead of "res" to be consistent with other code, unless there was a specific reason you chose that name.
| opening | ||
| |> andThen | ||
| (branches' | ||
| |> map (branch multilineBranches) |
There was a problem hiding this comment.
Not necessary to change before merging, but FYI I'm trying to prefer fmap over map (just so that people new to the haskell don't have to wonder what the difference is)
| (branches' | ||
| |> map (branch multilineBranches) | ||
| |> (if AST.isMultiline multilineBranches then List.intersperse blankLine else id) | ||
| |> map indent |
There was a problem hiding this comment.
Will this indent the interspersed blank lines? I can't remember if that matters... if this ends up leaving empty lines containing spaces, then that needs to be avoided
There was a problem hiding this comment.
Good point. I will check.
There was a problem hiding this comment.
I tested and blank lines are trimmed to only "\n".
I have not checked where though, most likely during Box -> Text rendering.
|
|
||
| branch multiline' (singles, prePat, pat, postPat, preRes, res) = | ||
| case (multiline', singles, prePat, pat, postPat) of | ||
| (AST.JoinAll, Right singleLines, _, _, _) -> |
There was a problem hiding this comment.
I see my previous code was similarly confusing, but it's not obvious here that singleLines includes the data from pat, postPat, preRes, res. Could you at least add a comment right above this case noting that? (without it, I started reading these branches and though "oh no, is some of the AST getting lost here?")
There was a problem hiding this comment.
I have reworked this part and it is hopefully a lot less confusing.
I used an Either to differentiate between single lines and boxes to avoid the variables overlap.
52e8480 to
93c89e4
Compare
93c89e4 to
efbecb9
Compare
|
Nice! I was just about to stop using elm-format after having used it for some time. I find that all the extra lines makes the code much harder to read than if I wasn't using anything at all. This fixes the main problem. 👍 |
Résolves #507