Skip to content

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

Merged
merged 3 commits into from
Aug 1, 2025

Conversation

zth
Copy link
Collaborator

@zth zth commented Jul 30, 2025

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 that type.resi defines the nested record as:

type options = {
  permissions: {
    all: bool
  }
}

But it's printed as:

type options = {
  permissions: {all: bool},
}

Similarily, if I change it one more level and define the type in type.resi as:

type options = {
  permissions: {all: bool}
}

Then it again compacts it one level:

type options = {permissions: {all: bool}}

I wonder if this is some easy fix or some setting I've just missed in printing. Anyone have ideas?

@zth zth requested review from shulhi and nojaf July 30, 2025 14:30
@zth
Copy link
Collaborator Author

zth commented Jul 30, 2025

The formatting issue lies in how force_break is calculated. It's looking at lines of labels from parsing, but these lines won't be aligned properly with inline records. Need to figure out how to deal with that.

@shulhi
Copy link
Member

shulhi commented Jul 30, 2025

The unstable formatting is related to #6976

@nojaf
Copy link
Member

nojaf commented Jul 31, 2025

Could you update the snapshots so we know what has changed by this fix?

@zth
Copy link
Collaborator Author

zth commented Jul 31, 2025

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.

@zth zth force-pushed the refactor-inline-record-printing branch from e4559c8 to 28cc10c Compare July 31, 2025 18:54
Copy link

pkg-pr-new bot commented Jul 31, 2025

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@7741

@rescript/darwin-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-arm64@7741

@rescript/darwin-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-x64@7741

@rescript/linux-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-arm64@7741

@rescript/linux-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-x64@7741

@rescript/win32-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/win32-x64@7741

commit: 28cc10c

@zth zth requested a review from shulhi July 31, 2025 19:23
@zth
Copy link
Collaborator Author

zth commented Jul 31, 2025

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.

Comment on lines +1175 to +1195
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
Copy link
Collaborator Author

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.

Comment on lines +1470 to +1471
match (check_break_from_loc, lds, List.rev lds) with
| Some loc, _, _ -> loc.Location.loc_start.pos_lnum < loc.loc_end.pos_lnum
Copy link
Collaborator Author

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.

Copy link
Member

@nojaf nojaf left a comment

Choose a reason for hiding this comment

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

Thanks!

@zth zth merged commit 640bb93 into master Aug 1, 2025
27 checks passed
@zth zth deleted the refactor-inline-record-printing branch August 1, 2025 07:20
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.

Nested record type formats weird in interface file
3 participants