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 if expressions #648

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

Resolves #209

@rlefevre
Copy link
Contributor Author

rlefevre commented Nov 16, 2019

Note: I have not found a more optimized solution than the 3 trackNewline but I may have overlooked it.

@rlefevre rlefevre force-pushed the single-line-if-expressions branch 3 times, most recently from 14298af to bb3f1c2 Compare November 16, 2019 23:50
@rlefevre rlefevre changed the title Add support for single-line if expressions Allow single-line if expressions Nov 17, 2019
avh4
avh4 previously requested changes Nov 17, 2019
Copy link
Owner

@avh4 avh4 left a comment

Choose a reason for hiding this comment

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

Thanks for taking this on!

This will need more tests. Specifically an example of all the different single/multiline combinations should be added to tests/test-files/good/Elm-0.19/AllSyntax/Expressions.elm (probably in or immediately after ifStatement (line 273)).

@rlefevre
Copy link
Contributor Author

I totally agree this needs more tests, but I need some guidance because I don't understand if tests in ExpressionTest.hs should use example that seem to only test parsing, example' that compares to the formatted string, or tests like tests/test-files/transform/Elm-0.18/Examples.formatted.elm that compare a full file. I'm a little confused.

@avh4
Copy link
Owner

avh4 commented Nov 17, 2019

Ah, ExpressionTest.hs is meant to be phased out (as you've probably noticed, it's a pain to update). The tests/test-files/good is the main test suite, and every new feature should have tests there (for this change, I think Elm-0.19/AllSyntax/Expressions.elm makes sense. Then tests/test-files/transform is for things that require the input file to be different from the output file.

So for this PR:

  • add examples of the new allowed formats in tests/test-files/good/Elm-0.19/AllSyntax/Expressions.elm
  • update ExpressionTest.hs as little as possible so that it compiles and passes
  • If there's a tests/test-files/transform you want to add, you can, but it's not required

@rlefevre

This comment has been minimized.

@rlefevre rlefevre requested a review from avh4 November 17, 2019 20:20
@rlefevre rlefevre force-pushed the single-line-if-expressions branch 2 times, most recently from 31f33b7 to e83afa4 Compare November 17, 2019 22:44
@rlefevre rlefevre requested a review from avh4 November 18, 2019 11:19
src/ElmFormat/Render/Box.hs Outdated Show resolved Hide resolved
@rlefevre rlefevre force-pushed the single-line-if-expressions branch 2 times, most recently from e4f3518 to 7ee8a14 Compare November 22, 2019 23:03
@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.

@@ -0,0 +1 @@
x = if True then 1 else if False then 2 else 3
Copy link
Owner

Choose a reason for hiding this comment

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

👍

]

(_, ifCond, thenBody, _, elseBody) ->
formatIfExpressionMultiline elmVersion importInfo ifCond thenBody elseifs elseBody
Copy link
Owner

Choose a reason for hiding this comment

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

The way these two functions work together seems a bit complicated. The ideal is that these functions basically have two phases: 1) turning all the stuff into boxes and 2) assembling the boxes based on the multiline features. Here we turn just enough into boxes to decide if we should call the other function, which makes it a bit harder to follow the full logic.

Though looking at the code for a few minutes, I don't have any quick suggestions of how to refactor this, so if you don't have time to give a try to simplifying this more, that's fine. (A lot of the existing code in here is way more confusing than this)

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 think it should be easier to follow the logic now.

@avh4
Copy link
Owner

avh4 commented Nov 24, 2019

Looks great, thanks! I'll let you know when I get upgrade-tool merged (goal is the next two weeks).

And also I think this will start the 0.9 releases as a notable behavior change.

@rlefevre rlefevre force-pushed the single-line-if-expressions branch 2 times, most recently from dafb82b to d0ec5dc Compare November 25, 2019 12:05
Left box -> box


boxesOrLinesToBox :: Bool -> Either [Box] [Line] -> Box
Copy link
Contributor Author

@rlefevre rlefevre Nov 25, 2019

Choose a reason for hiding this comment

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

Maybe we could put this function and boxOrLineToBox into Box.hs?
I'm not sure because of the use of ElmStructure.forceableSpaceSepOrStack1.

@andys8
Copy link
Contributor

andys8 commented Nov 29, 2019

@rlefevre (and @avh4) Thanks so much for tackling this issue. It is really "improving the language" for me :)

@avh4 avh4 self-assigned this Apr 27, 2020
@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
@ChristophP
Copy link

Really looking forward to this feature. I feel like this would clean up a bunch of code the previously required lots of space

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.

Should single-line if expressions be allowed?
5 participants