Replies: 2 comments
-
This seems good to me. I think seeing my hand-formatted code vs. the code formatter would be useful. |
Beta Was this translation helpful? Give feedback.
0 replies
-
For what it's worth, these rules also look good to me. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Line width
The Racket style guide sets the line width limit to 102 characters (read justification here).
I disagree with this limit. It causes side-by-side diff really difficult to read, at least on my 13-inch laptop (which as I understand is a very common size). Personally, I think that 80 characters would make more sense.
Indentation
The most common indentation size that I have seen so far is 2.
Meta discussion on formatting
For most programming languages, there are multiple ways to format code. These multiple ways mostly offer trade-off between increasing the width and increasing the height. Shrubbery/Rhombus is no different.
It may be useful to think that there is a fallback layout for each code unit. Other layouts are "nicer", but they are not always applicable due to constraints. As a concrete example, in Racket, a function application has the following fallback layout:
(abc def ghi)
andwould be more desirable, as long as these layouts don't cause the code to go beyond the line width. Furthermore
(abc def ghi)
would have an additional constraint that they should be flat (has height 1). E.g., we probably should not writeShrubbery formatting
Following is a proposed style guide for a general Shrubbery (which is uninterpreted). For Rhombus (where Shrubbery is interpreted), formatting could be different based on contents. The guide is written with Shrubbery pretty printing in mind. I.e., it's intended to be the specification for the pretty printer.
Block formatting
In general, a block's body could be formatted in two ways:
The layout (a) is the fallback one.
In @mflatt's talk, he presented the code:
and then commented that he actually doesn't like this layout; a prettier layout would be:
I propose the following rule which captures the desired output:
If the first children of a block is directly another block, the layout (a) is preferred
Note that this rule, as it is currently stated, does not apply to:
because the first children is a
parens
, not ablock
.Paren-like formatting
There are three possible layouts:
Layout (a) is the fallback one.
Layout (b) is only applicable when "arguments" are all flat (height = 1). This is to reflect that:
is probably not a great layout to use.
Group formatting
Newlines and indents could be uniformly inserted before infix operators
(a) is the fallback one. (b) is only applicable when it is flat.
Alts formatting
There are two ways to format
alts
(a) is the fallback layout. (b) is only applicable when it is flat.
Operator formatting
There are three layouts:
Exception: when an infix operator appears as the first token in a line, there should be no space on the left of the operator.
In the pretty printer, when the
raw
information is not available, an extra operator table could be provided to supply this information.Others
;
and«»
are generally not recommended.Question
Another possible layout is "fit as many as possible in each line". For specific forms in Rhombus, I guess it could be a good idea. But is it a good idea for general Shrubbery?
This layout is only applicable when it is flat.
Beta Was this translation helpful? Give feedback.
All reactions