Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rlefevre
Copy link
Contributor

@rlefevre rlefevre commented Nov 16, 2019

Résolves #507

@rlefevre
Copy link
Contributor Author

rlefevre commented Nov 16, 2019

Note:

@rlefevre
Copy link
Contributor Author

Updated with Multiline instead of Bool, and tests added.

@rlefevre
Copy link
Contributor Author

@avh4 Given what's accepted with single-line if expressions, do we want to accept single-line comments in branches like the following example?

case Just 1 of
    Just x -> {- A -} x
    _ -> {- B -} 2

For now this is not supported in this PR (comments lead to multi-line except those between case...of).

@avh4
Copy link
Owner

avh4 commented Nov 18, 2019

do we want to accept single-line comments

yes, I think so

@rlefevre

This comment has been minimized.

@rlefevre rlefevre force-pushed the single-line-case-expressions branch 2 times, most recently from 7b7560b to 95f70f9 Compare November 22, 2019 23:12
@rlefevre
Copy link
Contributor Author

I think that the PR is now complete, including tests.
I will just rebase it once the upgrade-tool branch is merged.

@avh4
Copy link
Owner

avh4 commented Nov 24, 2019

so several comments are on the same line, whereas for case branches, we get

I suspect there was not a reason for that difference.

case Just 1 of
    {- A -} Just x {- B -} -> {- C -} x

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)
Copy link
Owner

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.


(multilineBranches, branches') =
List.mapAccumR
(\mline (AST.Commented prePat pat postPat, (preRes, res)) ->
Copy link
Owner

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

preRes' =
Maybe.maybeToList $ formatComments preRes
res' =
formatExpression elmVersion importInfo SyntaxSeparated res
Copy link
Owner

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

opening
|> andThen
(branches'
|> map (branch multilineBranches)
Copy link
Owner

@avh4 avh4 Nov 24, 2019

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

(branches'
|> map (branch multilineBranches)
|> (if AST.isMultiline multilineBranches then List.intersperse blankLine else id)
|> map indent
Copy link
Owner

@avh4 avh4 Nov 24, 2019

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.


branch multiline' (singles, prePat, pat, postPat, preRes, res) =
case (multiline', singles, prePat, pat, postPat) of
(AST.JoinAll, Right singleLines, _, _, _) ->
Copy link
Owner

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?")

Copy link
Contributor Author

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.

@avh4 avh4 added this to the 0.9.0-exp milestone Nov 24, 2019
@rlefevre rlefevre force-pushed the single-line-case-expressions branch 2 times, most recently from 52e8480 to 93c89e4 Compare November 26, 2019 00:19
@tommyengstrom
Copy link

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. 👍

@avh4 avh4 changed the base branch from master to main September 18, 2020 02:54
@avh4 avh4 added the 0.9 (temporary label for search filtering) label Sep 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0.9 (temporary label for search filtering)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants