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

Format of andalso and orelse #53

Closed
dram opened this issue Nov 8, 2022 · 5 comments · Fixed by #55
Closed

Format of andalso and orelse #53

dram opened this issue Nov 8, 2022 · 5 comments · Fixed by #55

Comments

@dram
Copy link

dram commented Nov 8, 2022

It seems that smlfmt does not support formating long chain of andalso and orelse, which will exceed the -max-width limit, e.g.:

fun main () =
  if
    1 = 1 andalso 2 = 2 andalso 3 = 3 andalso 4 = 4 andalso 5 = 5 andalso 6 = 6 andalso 7 = 7 andalso 8 = 8
  then
    print "foo\n"
  else
    print "bar\n"
@dram dram changed the title Indentation for andalso and orelse Format of andalso and orelse Nov 9, 2022
@shwestrick
Copy link
Owner

I took a stab at fixing this, and pushed a fix (3b39389) which better respects the max width.

New result at 80 chars max width (seems decent):

fun main () =
  if
    1 = 1 andalso 2 = 2 andalso 3 = 3 andalso 4 = 4 andalso 5 = 5 andalso 6 = 6
    andalso 7 = 7 andalso 8 = 8
  then
    print "foo\n"
  else
    print "bar\n"

I'm leaving this open for now though, because it sometimes puts the newline in a "bad place", e.g. here's the output for max 40 chars.

fun main () =
  if
    1 = 1 andalso 2 = 2 andalso 3 = 3
    andalso 4 = 4 andalso 5 = 5 andalso 6
    = 6
    andalso 7 = 7 andalso 8 = 8
  then
    print "foo\n"
  else
    print "bar\n"

(This is a tricky issue to solve!)

@dram
Copy link
Author

dram commented Nov 15, 2022

Thank you for your quick fix, @shwestrick.

It seems that after those changes, follow code will be formatted with max width exceeded:

fun main () =
  let
    val foo =
      "01234567890123456789012345678901234567890123456789" ^ "01234567890123456789012345678901234567890123456789"
      ^ "01234567890123456789012345678901234567890123456789"
  in
    print foo
  end

@shwestrick
Copy link
Owner

Thanks, I'll look into it.

@shwestrick
Copy link
Owner

I pushed some updates which fixes this issue by vertically aligning infix expressions.

The new output on your example looks like this:

fun main () =
  let
    val foo =
      "01234567890123456789012345678901234567890123456789"
      ^ "01234567890123456789012345678901234567890123456789"
      ^ "01234567890123456789012345678901234567890123456789"
  in
    print foo
  end

I think this is a decent solution for now, and it generally looks pretty good.

@dram
Copy link
Author

dram commented Dec 21, 2022

Fix confirmed, nice work!

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

Successfully merging a pull request may close this issue.

2 participants