Skip to content

Allow single-line if expressions#648

Open
rlefevre wants to merge 2 commits into
avh4:mainfrom
rlefevre:single-line-if-expressions
Open

Allow single-line if expressions#648
rlefevre wants to merge 2 commits into
avh4:mainfrom
rlefevre:single-line-if-expressions

Conversation

@rlefevre

@rlefevre rlefevre commented Nov 16, 2019

Copy link
Copy Markdown
Contributor

Resolves #209

@rlefevre

rlefevre commented Nov 16, 2019

Copy link
Copy Markdown
Contributor Author

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

Comment thread tests/test-files/transform/Elm-0.18/Examples.formatted.elm Outdated
@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

@avh4 avh4 left a comment

Copy link
Copy Markdown
Owner

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

Comment thread parser/src/Parse/Expression.hs Outdated
Comment thread tests/test-files/transform/Elm-0.18/Examples.formatted.elm
@rlefevre

Copy link
Copy Markdown
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

avh4 commented Nov 17, 2019

Copy link
Copy Markdown
Owner

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 force-pushed the single-line-if-expressions branch from bb3f1c2 to 67161e6 Compare November 17, 2019 20:08
Comment thread tests/test-files/good/Elm-0.19/AllSyntax/Expressions.elm Outdated
@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
Comment thread tests/test-files/good/Elm-0.19/AllSyntax/Expressions.elm
@rlefevre rlefevre force-pushed the single-line-if-expressions branch from e83afa4 to c541461 Compare November 18, 2019 10:50
Comment thread tests/test-files/transform/Elm-0.18/Examples.formatted.elm Outdated
@rlefevre rlefevre requested a review from avh4 November 18, 2019 11:19
Comment thread src/ElmFormat/Render/Box.hs Outdated
@avh4 avh4 dismissed their stale review November 18, 2019 16:22

tests added

@rlefevre rlefevre force-pushed the single-line-if-expressions branch from c541461 to 12f4095 Compare November 18, 2019 17:37
@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
Copy Markdown
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
Copy Markdown
Owner

Choose a reason for hiding this comment

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

👍

Comment thread src/ElmFormat/Render/Box.hs Outdated
]

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

Copy link
Copy Markdown
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
Copy Markdown
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

avh4 commented Nov 24, 2019

Copy link
Copy Markdown
Owner

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

@rlefevre rlefevre Nov 25, 2019

Copy link
Copy Markdown
Contributor Author

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.

@rlefevre rlefevre force-pushed the single-line-if-expressions branch from d0ec5dc to d9404ad Compare November 25, 2019 16:22
@andys8

andys8 commented Nov 29, 2019

Copy link
Copy Markdown
Contributor

@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
Copy Markdown

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