Skip to content

Commit

Permalink
Sort modes and modalities
Browse files Browse the repository at this point in the history
x @ mode1 mode2 and x @ mode2 mode1 are equivalent, as are things like `value
mod mode1 mode2` and `value mod mode2 mode1`. It'll be nice to have a canonical
order on these to avoid thrashing and inconsistency in code with modalities and
mode annotations. I decided to just go with sorting by String.compare for now,
rather than trying to impose any opinions on the order.

Signed-off-by: Aspen Smith <[email protected]>
  • Loading branch information
glittershark committed Nov 16, 2024
1 parent 9ff450e commit f950f30
Show file tree
Hide file tree
Showing 12 changed files with 1,289 additions and 1,230 deletions.
12 changes: 12 additions & 0 deletions lib/Fmt_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,12 @@ and type_constr_and_body c xbody =
| _ -> (None, xbody)

and fmt_modalities ?(break = true) c modalities =
let modalities =
List.sort
~compare:(fun { Location.txt = Modality m1; _ } { Location.txt = Modality m2; _ } ->
String.compare m1 m2)
modalities
in
let fmt_modality {txt= Modality modality; loc} =
Cmts.fmt c loc (str modality)
in
Expand All @@ -782,6 +788,12 @@ and fmt_modalities ?(break = true) c modalities =
$ hvbox 0 (list modalities "@ " fmt_modality)

and fmt_modes ~ats c modes =
let modes =
List.sort
~compare:(fun { txt = Mode m1; _ } { txt = Mode m2; _ } ->
String.compare m1 m2)
modes
in
let fmt_mode {txt= Mode mode; loc} = Cmts.fmt c loc (str mode) in
if List.is_empty modes then noop
else
Expand Down
17 changes: 15 additions & 2 deletions lib/Normalize_std_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -469,11 +469,24 @@ let make_mapper conf ~ignore_doc_comments ~erase_jane_syntax =
Ast_mapper.default_mapper.type_declaration m {decl with ptype_attributes}
in
let modes (m : Ast_mapper.mapper) ms =
Ast_mapper.default_mapper.modes m (if erase_jane_syntax then [] else ms)
Ast_mapper.default_mapper.modes m
(if erase_jane_syntax
then []
else
List.sort
~compare:(fun { Location.txt = Mode m1; _ } { Location.txt = Mode m2; _ } ->
String.compare m1 m2)
ms)
in
let modalities (m : Ast_mapper.mapper) ms =
Ast_mapper.default_mapper.modalities m
(if erase_jane_syntax then [] else ms)
(if erase_jane_syntax
then []
else
List.sort
~compare:(fun { Location.txt = Modality m1; _ } { Location.txt = Modality m2; _ } ->
String.compare m1 m2)
ms)
in
let value_binding (m : Ast_mapper.mapper) vb =
let vb =
Expand Down
Loading

0 comments on commit f950f30

Please sign in to comment.