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

autoformatting: if-then-else chaining increases indentation #29

Closed
shwestrick opened this issue Oct 21, 2021 · 3 comments
Closed

autoformatting: if-then-else chaining increases indentation #29

shwestrick opened this issue Oct 21, 2021 · 3 comments

Comments

@shwestrick
Copy link
Owner

The autoformatter should handle chains of if-then-else expressions to make sure that indentation doesn't get
funked up, like in the example below.

INPUT

val x =
  if b1 then e1
  else if b2 then e2
  else if b3 then e3
  else if b4 then e4
  else if b5 then e5
  else if b6 then e6
  else if b7 then e7
  else e8

CURRENT OUTPUT

val x =
  if b1 then
    e1
  else
    if b2 then
      e2
    else
      if b3 then
        e3
      else
        if b4 then
          e4
        else
          if b5 then
            e5
          else
            if b6 then e6 else if b7 then e7 else e8
@shwestrick
Copy link
Owner Author

Some progress on this in 6c468d5

Added a combinator called besideAndAbove which is like beside, except it causes the second doc to use the indentation of the first.

    d1       d2           besideAndAbove (d1, d2)
  .......   ****             .......
  .......   ***    ====>     .......
  .....                      .....****
                             ***

Using besideAndAbove to attach the else-branch of a conditional, we get formatting like this:

val x =
  if b1 then
    e1
  else if b2 then
    e2
  else if b3 then
    e3
  else if b4 then
    e4
  else if b5 then
    e5
  else if b6 then e6 else if b7 then e7 else e8

This feels like a step in the right direction. To fix the weird final line of the above example, it seems like we would need to traverse the children of an if/then/else to find the whole chain, before formatting.

@shwestrick
Copy link
Owner Author

Although I suppose the irony here is that, if we traverse the children in the CST to identify the whole chain, then it's possible to implement the desired formatting in terms of the old combinators 🥲

must-compute added a commit to must-compute/parse-sml that referenced this issue Dec 24, 2021
shwestrick added a commit that referenced this issue Dec 24, 2021
Finishes #29: formatting for if-then-else chaining
@shwestrick
Copy link
Owner Author

Fixed by #34 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant