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 source code with current gren format #11

Open
wants to merge 1 commit into
base: gren-0.2.0
Choose a base branch
from

Conversation

avh4
Copy link

@avh4 avh4 commented Nov 28, 2022

This formats the examples with gren format from gren-lang/compiler@256eeaa which is still WIP, but is at a point where it will safely format this repo (all code is unchanged, and all comments are retained).

Feel free to merge if you like, or use this as a place to comment on anything you'd like to see changed in the formatter before applying it to the official code repos.

Copy link
Member

@robinheghan robinheghan left a comment

Choose a reason for hiding this comment

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

This is super promising! I've got a few comments, but only the comments regarding the code block with await function calls is what I consider important enough for 0.2.

Fantastic work on this, @avh4 !

Comment on lines +37 to +57
Program.await Node.initialize
<| (\nodeConfig ->
Program.await FileSystem.initialize
<| (\fsPermission ->
Program.startProgram
{ model =
{ stdout = nodeConfig.stdout
}
, command =
case nodeConfig.args of
[ _, _, file ] ->
FileSystem.openForRead fsPermission file
|> Task.attempt OpenResult

_ ->
Stream.send nodeConfig.stderr
<| BE.encode
<| BE.string "Exactly one argument is required: the file name to read\n"
}
)
)
Copy link
Member

Choose a reason for hiding this comment

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

I hope we can format this block of code in a nicer way.

  1. <| seems to always be split out to a seperate line, I'd usually prefer it to be on the same line. I believe that's what happens in elm-format too, or am I wrong?
  2. As with <|, I prefer lambdas to stay on the previous line, instead of being broken out to a separate line. Not sure what elm-format does in this case.
  3. Wrapping a lambda in parenthesis should be uneccessary when on the right side of <|.

While I'm giving this feedback to this particular block of code, my comments apply universally.

Personally, I'd love to see the await statements formatted to be on the same line, but I appriciate there being different views on this and it's not important to get in before the 0.2 release.

Copy link
Author

Choose a reason for hiding this comment

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

👍 Sounds good. I already was expecting this from following some of the discussions on Zulip :D I think this will be doable, but as it's quite different from elm-format, it'll take a bit of thinking through the details of the rules, so I've postponed worrying about it yet.

(1): Yes, elm-format will keep it on a single line if it's not otherwise forced to expand. For gren format, retaining information about where linebreaks originally were isn't implemented at all yet, so allowing single-line <| would fit as part of that future goal. I guess the main question here is, is <| going to be considered special, or do we want a rule that applies to all binary operators? (For elm-format, <| is in fact a special case, see below.)

(2): elm-format doesn't allow that, though it does have one special case for <| \_ -> ... where the <| will stick at the end of the previous line (but the lambda will still start on the next line). This sounds fine to try to implement.

(3): I think I made it add parens in the first pass because I wasn't confident that I understood the edge cases well enough to be confident that it wouldn't remove parens in some cases that are actually necessary. (Since the parser originally dropped all parens when parsing, the formatter actually is only adding parens that are required -- which in total has the result of removing unnecessary parens.) This should be fine to implement. Though if anyone happens to have some time to help enumerate all the possible edge cases where parens may or may not be required around lambdas to avoid changing the meaning of code, that'd be helpful!

Comment on lines +39 to +40
Html.div
[]
Copy link
Member

Choose a reason for hiding this comment

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

When the first argument is a simple expression, like [] or an Int or a String, I think it looks nicer when being placed on the same line as the function call. I believe that's also how elm-format works.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, elm-format allows function calls to be one of (with the added rule that if any argument spans multiple lines, then it is not allowed to be on the same line as anything else):

  • function and all arguments on the same line
  • function and first argument on the same line, all other arguments on individual lines
  • everything on separate lines

It's a rule that I feel like isn't very elegant, and I guess it's been fine in Elm, but I'm not 100% sure it always makes sense.

We could use that same rule for gren, or consider some different kind of rule for this.

Comment on lines +44 to +46
[ Html.text
<| String.fromInt model
]
Copy link
Member

Choose a reason for hiding this comment

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

Here, I think it makes sense to format as Html.text <| String.fromInt model.

Copy link
Author

Choose a reason for hiding this comment

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

👍 this is part of retaining linebreak information, noted in #11 (comment) re "(1)"

Comment on lines +172 to +175
bookButton :
{ isDisabled : Bool
}
-> Html Msg
Copy link
Member

Choose a reason for hiding this comment

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

IMHO, type signatures containing records with at most one property, looks nicer when formatted on a single line.

Copy link
Author

Choose a reason for hiding this comment

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

👍 Yeah, I was thinking it would be a good rule for arrays and records (for types, and expressions, and maybe also patterns?) that if there's only a single element, prefer single-line formatting.

++ String.fromInt date.month
++ "."
++ String.fromInt date.year
String.fromInt date.day ++ "." ++ String.fromInt date.month ++ "." ++ String.fromInt date.year
Copy link
Member

Choose a reason for hiding this comment

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

I guess this one is tricky.

In this concrete case, I'd rather this being broken up in separate lines. But simple cases such as (a ++ b) looks better in a single line.

Copy link
Author

Choose a reason for hiding this comment

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

Similar to single-element arrays and records, I could try preferring single-line if there's only one operator in the sequence.

{ celsius = ""
, fahrenheit = ""
}


--- UPDATE

-- - UPDATE
Copy link
Member

Choose a reason for hiding this comment

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

This is interesting. How does the space get in there?

Copy link
Author

Choose a reason for hiding this comment

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

Ah, it tries to insert a leading space in the comment if there isn't one, but I guess that doesn't make sense if it starts with -! I'll have to check how I did it in elm-format.

Comment on lines +4 to +5
import Html exposing ( Html, text, div, input, button, select, option )
import Html.Attributes exposing ( id, style, value, disabled )

Choose a reason for hiding this comment

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

Should the imports be automatically sorted?

Suggested change
import Html exposing ( Html, text, div, input, button, select, option )
import Html.Attributes exposing ( id, style, value, disabled )
import Html exposing ( Html, button, div, input, option, select, text)
import Html.Attributes exposing ( disabled, id, style, value )

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 this pull request may close these issues.

3 participants