-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,13 @@ let add_braces doc = | |
|
||
let add_async doc = Doc.concat [Doc.text "async "; doc] | ||
|
||
let has_inline_type_definitions type_declarations = | ||
type_declarations | ||
|> List.find_opt (fun (td : Parsetree.type_declaration) -> | ||
Res_parsetree_viewer.has_inline_record_definition_attribute | ||
td.ptype_attributes) | ||
|> Option.is_some | ||
|
||
let get_first_leading_comment tbl loc = | ||
match Hashtbl.find tbl.CommentTable.leading loc with | ||
| comment :: _ -> Some comment | ||
|
@@ -587,29 +594,7 @@ and print_structure_item ~state (si : Parsetree.structure_item) cmt_tbl = | |
| Asttypes.Recursive -> Doc.text "rec " | ||
in | ||
print_value_bindings ~state ~rec_flag value_bindings cmt_tbl | ||
| Pstr_type (Recursive, type_declarations) | ||
when type_declarations | ||
|> List.find_opt (fun (td : Parsetree.type_declaration) -> | ||
Res_parsetree_viewer.has_inline_record_definition_attribute | ||
td.ptype_attributes) | ||
|> Option.is_some -> | ||
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 | ||
print_type_declarations ~inline_record_definitions ~state | ||
~rec_flag: | ||
(if List.length regular_declarations > 1 then Doc.text "rec " | ||
else Doc.nil) | ||
regular_declarations cmt_tbl | ||
| Pstr_type (rec_flag, type_declarations) -> | ||
let rec_flag = | ||
match rec_flag with | ||
| Asttypes.Nonrecursive -> Doc.nil | ||
| Asttypes.Recursive -> Doc.text "rec " | ||
in | ||
print_type_declarations ~state ~rec_flag type_declarations cmt_tbl | ||
| Pstr_primitive value_description -> | ||
print_value_description ~state value_description cmt_tbl | ||
|
@@ -985,11 +970,6 @@ and print_signature_item ~state (si : Parsetree.signature_item) cmt_tbl = | |
| Parsetree.Psig_value value_description -> | ||
print_value_description ~state value_description cmt_tbl | ||
| Psig_type (rec_flag, type_declarations) -> | ||
let rec_flag = | ||
match rec_flag with | ||
| Asttypes.Nonrecursive -> Doc.nil | ||
| Asttypes.Recursive -> Doc.text "rec " | ||
in | ||
print_type_declarations ~state ~rec_flag type_declarations cmt_tbl | ||
| Psig_typext type_extension -> | ||
print_type_extension ~state type_extension cmt_tbl | ||
|
@@ -1191,13 +1171,39 @@ and print_value_description ~state value_description cmt_tbl = | |
else Doc.nil); | ||
]) | ||
|
||
and print_type_declarations ?inline_record_definitions ~state ~rec_flag | ||
type_declarations cmt_tbl = | ||
print_listi | ||
~get_loc:(fun n -> n.Parsetree.ptype_loc) | ||
~nodes:type_declarations | ||
~print:(print_type_declaration2 ?inline_record_definitions ~state ~rec_flag) | ||
cmt_tbl | ||
and print_type_declarations ~state ~rec_flag type_declarations cmt_tbl = | ||
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 | ||
else | ||
print_listi | ||
~get_loc:(fun n -> n.Parsetree.ptype_loc) | ||
~nodes:type_declarations | ||
~print: | ||
(print_type_declaration2 ~state | ||
~rec_flag: | ||
(match rec_flag with | ||
| Nonrecursive -> Doc.nil | ||
| Recursive -> Doc.text "rec ")) | ||
cmt_tbl | ||
|
||
(* | ||
* type_declaration = { | ||
|
@@ -1458,13 +1464,14 @@ and print_type_param ~state (param : Parsetree.core_type * Asttypes.variance) | |
in | ||
Doc.concat [printed_variance; print_typ_expr ~state typ cmt_tbl] | ||
|
||
and print_record_declaration ?inline_record_definitions ~state | ||
(lds : Parsetree.label_declaration list) cmt_tbl = | ||
and print_record_declaration ?check_break_from_loc ?inline_record_definitions | ||
~state (lds : Parsetree.label_declaration list) cmt_tbl = | ||
let force_break = | ||
match (lds, List.rev lds) with | ||
| first :: _, last :: _ -> | ||
match (check_break_from_loc, lds, List.rev lds) with | ||
| Some loc, _, _ -> loc.Location.loc_start.pos_lnum < loc.loc_end.pos_lnum | ||
Comment on lines
+1470
to
+1471
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. 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. |
||
| _, first :: _, last :: _ -> | ||
first.pld_loc.loc_start.pos_lnum < last.pld_loc.loc_end.pos_lnum | ||
| _ -> false | ||
| _, _, _ -> false | ||
in | ||
Doc.breakable_group ~force_break | ||
(Doc.concat | ||
|
@@ -1799,8 +1806,8 @@ and print_typ_expr ?inline_record_definitions ~(state : State.t) | |
inline_record_definitions | ||
|> find_inline_record_definition inline_record_name | ||
with | ||
| Some {ptype_kind = Ptype_record lds} -> | ||
print_record_declaration | ||
| Some {ptype_kind = Ptype_record lds; ptype_loc} -> | ||
print_record_declaration ~check_break_from_loc:ptype_loc | ||
~inline_record_definitions:(inline_record_definitions |> Option.get) | ||
~state lds cmt_tbl | ||
| _ -> assert false) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,11 @@ type color = | |
| Red | ||
| Blue | ||
| Black | ||
|
||
type options = { | ||
permissions: { | ||
all: { | ||
stuff: bool, | ||
}, | ||
}, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,11 @@ type color = | |
| Red | ||
| Blue | ||
| Black | ||
|
||
type options = { | ||
permissions: { | ||
all: { | ||
stuff: bool | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.