-
Notifications
You must be signed in to change notification settings - Fork 471
Refactor printing inline type declarations #7741
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
Conversation
The formatting issue lies in how |
The unstable formatting is related to #6976 |
Could you update the snapshots so we know what has changed by this fix? |
I've added to an existing test file, which you can see in the diff. Separately, there's an issue described in the PR about formatting not working as intended, which is what's making CI red. So that needs either a proper or intermediate fix. I have some ideas. |
…ot being printed correctly in interface files
e4559c8
to
28cc10c
Compare
rescript
@rescript/darwin-arm64
@rescript/darwin-x64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/win32-x64
commit: |
Ok @shulhi I think I cracked it for this specific case (printing inline records). The problem was that we were only caring about the label locs when deciding whether to break in formatting or not. That's fine for regular records, but when printing inline we want to care about the full inline record body to decide whether to break or not, since that is what has the information about how the inline records were laid out in the code at parse time. |
if has_inline_type_definitions type_declarations then | ||
let inline_record_definitions, regular_declarations = | ||
type_declarations | ||
|> List.partition (fun (td : Parsetree.type_declaration) -> | ||
Res_parsetree_viewer.has_inline_record_definition_attribute | ||
td.ptype_attributes) | ||
in | ||
let adjusted_rec_flag = | ||
match rec_flag with | ||
| Recursive -> | ||
if List.length regular_declarations > 1 then Doc.text "rec " | ||
else Doc.nil | ||
| Nonrecursive -> Doc.nil | ||
in | ||
print_listi | ||
~get_loc:(fun n -> n.Parsetree.ptype_loc) | ||
~nodes:regular_declarations | ||
~print: | ||
(print_type_declaration2 ~inline_record_definitions ~state | ||
~rec_flag:adjusted_rec_flag) | ||
cmt_tbl |
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.
Move the inline record printing logic into print_type_declarations
itself. That's where it belongs IMO.
match (check_break_from_loc, lds, List.rev lds) with | ||
| Some loc, _, _ -> loc.Location.loc_start.pos_lnum < loc.loc_end.pos_lnum |
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.
Special case printing inline records - look at the full inline record loc to decide whether the code had line breaks (and should get force breaks) or not.
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.
Thanks!
Fixes #7737
EDIT: The below has been fixed.
@shulhi separate issue, but I've noticed for inline type declarations, the formatted automatically "compacts" the records. I wonder if this is something we could fix somehow? It's above my syntax knowledge.Examples:1) In this PR you'll see thattype.resi
defines the nested record as:But it's printed as:Similarily, if I change it one more level and define the type intype.resi
as:Then it again compacts it one level:I wonder if this is some easy fix or some setting I've just missed in printing. Anyone have ideas?