-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow single-line case-expressions branches #649
base: main
Are you sure you want to change the base?
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. |
parser/src/Parse/Expression.hs
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New version uses JoinAll if all branches are single line.
src/ElmFormat/Render/Box.hs
Outdated
|
||
(multilineBranches, branches') = | ||
List.mapAccumR | ||
(\mline (AST.Commented prePat pat postPat, (preRes, res)) -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
src/ElmFormat/Render/Box.hs
Outdated
preRes' = | ||
Maybe.maybeToList $ formatComments preRes | ||
res' = | ||
formatExpression elmVersion importInfo SyntaxSeparated res |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
src/ElmFormat/Render/Box.hs
Outdated
opening | ||
|> andThen | ||
(branches' | ||
|> map (branch multilineBranches) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
src/ElmFormat/Render/Box.hs
Outdated
(branches' | ||
|> map (branch multilineBranches) | ||
|> (if AST.isMultiline multilineBranches then List.intersperse blankLine else id) | ||
|> map indent |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I will check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested and blank lines are trimmed to only "\n".
I have not checked where though, most likely during Box -> Text
rendering.
src/ElmFormat/Render/Box.hs
Outdated
|
||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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