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

Make juvix format line width 100 with ribbon width 100 #2883

Merged
merged 7 commits into from
Jul 10, 2024

Conversation

paulcadman
Copy link
Collaborator

@paulcadman paulcadman commented Jul 10, 2024

This PR increases the ribbon width of juvix format from 60 to 100 characters.

Reasons for the compromise to a fixed 100 chars ribbon width:

  • It is clear that the ribbon width of 60 characters was too small.
  • A ribbon width of 100 is an acceptable compromise between formatting code for display and editing code in multiple buffers on the same screen.
  • We would like to avoid making the formatter configurable so that Juvix code has a consistent look and to save future Juvix users from discussions about formatting. Maxim: "juvix format's style is no one's favourite, yet juvix format is everyone's favourite" (thanks go fmt).

Definition of ribbon width from the docs

The page has a certain maximum width, which the layouter tries to not exceed, by inserting line breaks where possible. The functions given in this module make it fairly straightforward to specify where, and under what circumstances, such a line break may be inserted by the layouter, for example via the sep function.

There is also the concept of ribbon width. The ribbon is the part of a line that is printed, i.e. the line length without the leading indentation. The layouters take a ribbon fraction argument, which specifies how much of a line should be filled before trying to break it up. A ribbon width of 0.5 in a document of width 80 will result in the layouter to try to not exceed 0.5*80 = 40 (ignoring current indentation depth).

Examples from anoma-app-patterns:/Token/Transaction.juvix. NB: The file in the repo is unformatted so will not match the layout in the left column below.

Current (line width 150, ribbon width 60) This PR (line width 100, ribbon width 100)
Screenshot 2024-07-10 at 12 22 46 Screenshot 2024-07-10 at 14 41 33
Screenshot 2024-07-10 at 12 23 10 Screenshot 2024-07-10 at 14 42 01
Screenshot 2024-07-10 at 12 23 21 Screenshot 2024-07-10 at 14 42 14
Screenshot 2024-07-10 at 12 23 34 Screenshot 2024-07-10 at 14 42 29
Screenshot 2024-07-10 at 12 23 50 Screenshot 2024-07-10 at 14 42 40
Screenshot 2024-07-10 at 12 24 13 Screenshot 2024-07-10 at 14 42 57

@paulcadman paulcadman self-assigned this Jul 10, 2024
@paulcadman paulcadman added the enhancement New feature or request label Jul 10, 2024
@paulcadman paulcadman marked this pull request as ready for review July 10, 2024 11:34
@paulcadman paulcadman added this to the 0.6.4 milestone Jul 10, 2024
@heueristik
Copy link

Setting the ribbon width to 100 is too large because it's common for text editors to have buffer width around 80 characters.

Two questions:
Which editors wouldn't be able to display code with >80 chars properly?
Will this encourage people to write very short, non-descriptive variable names in production code? This could lead to more errors.

@lukaszcz
Copy link
Collaborator

Two questions: Which editors wouldn't be able to display code with 80 chars properly?

VSCode with two panes on my 24 inch screen with a 14pt font and 1920x1080 resolution. Then one pane displays 80-something characters.

Will this encourage people to write very short, non-descriptive variable names in production code? This could lead to more errors.

I don't think so. It's not Java. The rest of the language is quite succinct, so there's space for variable names. Plus, you put newlines a lot more, with every case etc. 80 is not such a small number.

We're basically using around 80 characters per line in our Haskell code. I definitely don't write much longer lines. It works well.

@heueristik
Copy link

Maxim: "juvix format's style is no one's favourite, yet juvix format is everyone's favourite" (thanks go fmt)

It is also worth noting that go fmt has no line length limit:

Go has no line length limit. Don’t worry about overflowing a punched card. If a line feels too long, wrap it and indent with an extra tab.

https://go.dev/doc/effective_go#formatting

@lukaszcz
Copy link
Collaborator

lukaszcz commented Jul 10, 2024

Maxim: "juvix format's style is no one's favourite, yet juvix format is everyone's favourite" (thanks go fmt)

It is also worth noting that go fmt has no line length limit:

Go has no line length limit. Don’t worry about overflowing a punched card. If a line feels too long, wrap it and indent with an extra tab.

https://go.dev/doc/effective_go#formatting

There’s a point here. I think ormolu doesn’t break the lines by itself either, only in situations when e.g. there is already one newline after a case. The question is how uniform we want to be.

Though “wrap it and indent with an extra tab” doesn’t work too well with complex code structure - quickly becomes unreadable when there are many such lines.

@lukaszcz
Copy link
Collaborator

lukaszcz commented Jul 10, 2024

I'd say not enforcing a line length limit is less strict than having an option to enforce different line length limits. It allows people to choose the line length arbitrarily and differently within even a single project.

@lukaszcz
Copy link
Collaborator

lukaszcz commented Jul 10, 2024

For me personally, ribbon width of 100 chars would still be palatable, but with anything longer than this used consistently I'd need to give up on two panes side by side.

@paulcadman
Copy link
Collaborator Author

I'd say not enforcing a line length limit is less strict than having an option to enforce different line length limits. It allows people to choose the line length arbitrarily and differently within even a single project.

I think we can agree on this. Q: can we implement this using prettyprinter, if so what's the effort required? Perhaps @janmasrovira knows?

@paulcadman
Copy link
Collaborator Author

For me personally, ribbon width of 100 chars would still be palatable, but with anything longer than this used consistently I'd need to give up on two panes side by side.

Thanks - I'll update this PR to line width 100 and ribbon width 100 (i.e 100 1.0).

@paulcadman
Copy link
Collaborator Author

Maxim: "juvix format's style is no one's favourite, yet juvix format is everyone's favourite" (thanks go fmt)

It is also worth noting that go fmt has no line length limit:

Go has no line length limit. Don’t worry about overflowing a punched card. If a line feels too long, wrap it and indent with an extra tab.

https://go.dev/doc/effective_go#formatting

For clarity I didn't make this comment to endorse all of gofmts design choices, just the maxim.

@lukaszcz
Copy link
Collaborator

For me personally, ribbon width of 100 chars would still be palatable, but with anything longer than this used consistently I'd need to give up on two panes side by side.

Thanks - I'll update this PR to line width 100 and ribbon width 100 (i.e 100 1.0).

Palatable, but a bit inconvenient. I won't protest against ribbon width 100, but would prefer something a bit smaller.

@paulcadman paulcadman changed the title Make juvix format line width 100 with ribbon width 80 Make juvix format line width 100 with ribbon width 100 Jul 10, 2024
@janmasrovira
Copy link
Collaborator

janmasrovira commented Jul 10, 2024

I'm fine with either choice. Let's try it for a while, if we feel like it can be improved, we can always change it.

I'd like to add that we don't respect the user newlines by design. The goal of our formatter is indeed to make code look readable, but also (and not less important) to remove as much formatting responsability from the user as possible.

lukaszcz
lukaszcz previously approved these changes Jul 10, 2024
janmasrovira
janmasrovira previously approved these changes Jul 10, 2024
@paulcadman paulcadman dismissed stale reviews from janmasrovira and lukaszcz via a99154e July 10, 2024 14:08
Copy link
Collaborator

@janmasrovira janmasrovira left a comment

Choose a reason for hiding this comment

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

I now realize that some changes are needed in the Format.juvix test.
Some lines were written with the intention to force the formatter to introduce a newline.

tests/positive/Format.juvix Outdated Show resolved Hide resolved
tests/positive/Format.juvix Outdated Show resolved Hide resolved
tests/positive/Format.juvix Outdated Show resolved Hide resolved
tests/positive/Format.juvix Outdated Show resolved Hide resolved
@paulcadman paulcadman requested a review from janmasrovira July 10, 2024 15:52
@paulcadman
Copy link
Collaborator Author

I now realize that some changes are needed in the Format.juvix test. Some lines were written with the intention to force the formatter to introduce a newline.

Thanks - I'll fix these now

@paulcadman paulcadman force-pushed the pretty-printer-line-width branch from 2e365fe to 3160f92 Compare July 10, 2024 16:08
The existing highlight smoke test contains assertions included newlines.
The newlines are no longer present now we have changed the desired line
length of the output.

To fix this we use the YAML trim annotation `-`, so we're only asserting
the text rather than the text and the trailing newline.
@paulcadman paulcadman force-pushed the pretty-printer-line-width branch from 3160f92 to 8d2ab9d Compare July 10, 2024 16:26
@paulcadman paulcadman merged commit 4e22743 into main Jul 10, 2024
4 checks passed
@paulcadman paulcadman deleted the pretty-printer-line-width branch July 10, 2024 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request juvix-formatter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants