-
Notifications
You must be signed in to change notification settings - Fork 476
Improve multiline printing of record types and values #7993
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
Changes from all commits
2dee6f6
d463267
2b823aa
5221a07
e6bd9a0
ea4101d
9376d6e
48062ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1282,7 +1282,7 @@ and print_type_declaration ~state ~name ~equal_sign ~rec_flag i | |
| manifest; | ||
| Doc.concat [Doc.space; Doc.text equal_sign; Doc.space]; | ||
| print_private_flag td.ptype_private; | ||
| print_record_declaration ~state lds cmt_tbl; | ||
| print_record_declaration ~record_loc:td.ptype_loc ~state lds cmt_tbl; | ||
| ] | ||
| | Ptype_variant cds -> | ||
| let manifest = | ||
|
|
@@ -1370,8 +1370,8 @@ and print_type_declaration2 ?inline_record_definitions ~state ~rec_flag | |
| manifest; | ||
| Doc.concat [Doc.space; Doc.text equal_sign; Doc.space]; | ||
| print_private_flag td.ptype_private; | ||
| print_record_declaration ?inline_record_definitions ~state lds | ||
| cmt_tbl; | ||
| print_record_declaration ?inline_record_definitions | ||
| ~record_loc:td.ptype_loc ~state lds cmt_tbl; | ||
| ] | ||
| | Ptype_variant cds -> | ||
| let manifest = | ||
|
|
@@ -1465,12 +1465,22 @@ and print_type_param ~state (param : Parsetree.core_type * Asttypes.variance) | |
| Doc.concat [printed_variance; print_typ_expr ~state typ cmt_tbl] | ||
|
|
||
| and print_record_declaration ?check_break_from_loc ?inline_record_definitions | ||
| ~state (lds : Parsetree.label_declaration list) cmt_tbl = | ||
| ?record_loc ~state (lds : Parsetree.label_declaration list) cmt_tbl = | ||
| let get_field_start_line (ld : Parsetree.label_declaration) = | ||
| (* For spread fields (...), use the type location instead of pld_loc | ||
| because pld_loc may incorrectly include preceding whitespace *) | ||
| if ld.pld_name.txt = "..." then ld.pld_type.ptyp_loc.loc_start.pos_lnum | ||
| else ld.pld_loc.loc_start.pos_lnum | ||
| in | ||
| let force_break = | ||
| match (check_break_from_loc, lds, List.rev lds) with | ||
| match (check_break_from_loc, record_loc, lds) with | ||
| | Some loc, _, _ -> loc.Location.loc_start.pos_lnum < loc.loc_end.pos_lnum | ||
| | _, first :: _, last :: _ -> | ||
| first.pld_loc.loc_start.pos_lnum < last.pld_loc.loc_end.pos_lnum | ||
| | None, Some loc, first :: _ -> | ||
| (* Check if first field is on a different line than the opening brace *) | ||
| loc.loc_start.pos_lnum < get_field_start_line first | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep in mind: if we lean into this more over time, we'll probably want a small helper function so we can easily trace where we apply this heuristic and where we don't. It's fine to implement that in a later phase.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely, I'm going to tackle a similar multiline support for record and we will see if this something we can extract to a helper function. |
||
| | None, None, first :: _ -> | ||
| let last = List.hd (List.rev lds) in | ||
| get_field_start_line first < last.pld_loc.loc_end.pos_lnum | ||
| | _, _, _ -> false | ||
| in | ||
| Doc.breakable_group ~force_break | ||
|
|
@@ -3223,7 +3233,14 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl = | |
| * b: 2, | ||
| * }` -> record is written on multiple lines, break the group *) | ||
| let force_break = | ||
| e.pexp_loc.loc_start.pos_lnum < e.pexp_loc.loc_end.pos_lnum | ||
| match (spread_expr, rows) with | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want to pause here a bit — I don’t think we should merge this part right now. The change around force_break introduces behaviour that depends on how the user originally formatted their code. That’s a significant precedent: it ties output formatting decisions to source‑layout heuristics. If we want to allow this kind of stylistic inference, I believe it needs wider agreement within the core team. We should either decide as a group that this is a direction we’re comfortable with, or explicitly document why we don’t want to go that way. If we do agree in principle, I’d also like to see a short architectural or design record capturing the rationale and boundaries for this sort of logic, so that future contributions have something concrete to refer to. Finally, even with buy‑in, I’d suggest postponing this until after v12. We’re in the release‑candidate stage, and introducing new formatting heuristics at this point could have more ripple effects than we realise. So in short: let’s not merge this specific part yet; let’s first get consensus and document it properly. |
||
| | Some expr, _ -> | ||
| (* If there's a spread, compare with spread expression's location *) | ||
| e.pexp_loc.loc_start.pos_lnum < expr.pexp_loc.loc_start.pos_lnum | ||
| | None, first_row :: _ -> | ||
| (* Otherwise, compare with the first row's location *) | ||
| e.pexp_loc.loc_start.pos_lnum < first_row.lid.loc.loc_start.pos_lnum | ||
| | None, [] -> false | ||
| in | ||
| let punning_allowed = | ||
| match (spread_expr, rows) with | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # ReScript Formatter | ||
|
|
||
| ## Philosophy | ||
|
|
||
| The ReScript formatter is **opinionated**. Formatting decisions are made by the core team based on our collective judgment and vision for the language. We do not aim to accommodate every stylistic preference or engage in extended debates about formatting choices. | ||
|
|
||
| The formatter currently has **no configuration settings**, and we aspire to keep it that way. This ensures that ReScript code looks consistent across all projects and teams, eliminating style debates and configuration overhead. | ||
|
|
||
| ## Decision Making | ||
|
|
||
| - **Core team consensus is final**: When the core team reaches consensus on a formatting decision, that decision stands. There is no requirement for community-wide agreement or extensive discussion. | ||
|
|
||
| - **Community input is welcome but not binding**: We appreciate suggestions and feedback from the community, but these can be closed without extensive justification if the core team is not aligned with the proposal. | ||
|
|
||
| - **No endless style discussions**: We are not interested in protracted debates about formatting preferences. The formatter exists to provide consistent, automated formatting—not to serve as a platform for style negotiations. | ||
|
|
||
| ## Prior Decisions | ||
|
|
||
| The following are examples of formatting decisions the core team has made. This list is not exhaustive, and these decisions do not create binding precedents for future discussions. The core team retains full discretion to make different decisions in similar cases. | ||
|
|
||
| - **Smart linebreaks for pipe chains**: The formatter preserves user-introduced linebreaks in pipe chains (`->`), allowing users to control multiline formatting. See [forum announcement](https://forum.rescript-lang.org/t/ann-smart-linebreaks-for-pipe-chains/4734). | ||
|
|
||
| - **Preserve multilineness for records**: The formatter preserves multiline formatting for record types and values when users introduce linebreaks. See [issue #7961](https://github.com/rescript-lang/rescript/issues/7961). | ||
|
|
||
| **Important**: These examples are provided for reference only. They do not establish rules or precedents that constrain future formatting decisions. The core team may choose different approaches in similar situations based on current consensus. | ||
|
|
||
| ## Guidelines for Contributors | ||
|
|
||
| ### Submitting Formatting Issues | ||
|
|
||
| - You may open issues to report bugs or propose improvements | ||
| - Understand that proposals may be closed if they don't align with core team vision | ||
| - Avoid reopening closed issues unless there's new technical information | ||
| - Respect that "the core team isn't feeling it" is a valid reason for closure | ||
|
|
||
| ### What We Consider | ||
|
|
||
| - Technical correctness and consistency | ||
| - Alignment with ReScript's design philosophy | ||
| - Maintainability and simplicity of the formatter implementation | ||
| - Core team consensus | ||
|
|
||
| ### What We Generally Avoid | ||
|
|
||
| - Style preferences that don't align with our vision | ||
| - Using comparisons to other formatters as the sole justification for changes (while we may align with other formatters on many decisions, we make choices based on our own judgment, not because another formatter does it) | ||
| - Requests that would significantly complicate the formatter implementation | ||
| - Debates about subjective formatting choices |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: add extra newline after header