Skip to content

Commit

Permalink
Merge pull request #7460 from gamebox/issue-7339
Browse files Browse the repository at this point in the history
Migrate type vars to snake case (Redo)
  • Loading branch information
lukewilliamboswell authored Jan 4, 2025
2 parents 1b15896 + 89b7900 commit 07cd1c3
Show file tree
Hide file tree
Showing 6 changed files with 481 additions and 78 deletions.
117 changes: 76 additions & 41 deletions crates/compiler/fmt/src/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use crate::{
parens_around_node, DelimitedItem, Item, Node, NodeInfo, NodeSequenceBuilder, Nodify, Prec,
Sp,
},
pattern::pattern_lift_spaces_after,
pattern::snakify_camel_ident,
pattern::{pattern_lift_spaces_after, snakify_camel_ident},
spaces::{fmt_comments_only, fmt_spaces, NewlineAt, INDENT},
Buf,
Buf, MigrationFlags,
};
use bumpalo::{
collections::{String, Vec},
Expand Down Expand Up @@ -232,7 +231,7 @@ fn fmt_ty_ann(
}

me.item
.to_node(buf.text.bump())
.to_node(buf.text.bump(), buf.flags())
.add_parens(buf.text.bump(), parens)
.node
.format(buf, indent);
Expand All @@ -243,7 +242,7 @@ fn fmt_ty_ann(
}

impl<'a> Nodify<'a> for Tag<'a> {
fn to_node<'b>(&'a self, arena: &'b Bump) -> NodeInfo<'b>
fn to_node<'b>(&'a self, arena: &'b Bump, flags: MigrationFlags) -> NodeInfo<'b>
where
'a: 'b,
{
Expand All @@ -258,7 +257,10 @@ impl<'a> Nodify<'a> for Tag<'a> {
let mut last_after: &[CommentOrNewline<'_>] = &[];

for arg in args.iter() {
let lifted = arg.value.to_node(arena).add_parens(arena, Parens::InApply);
let lifted = arg
.value
.to_node(arena, flags)
.add_parens(arena, Parens::InApply);
let before = merge_spaces_conservative(arena, last_after, lifted.before);
last_after = lifted.after;
new_args.push((Sp::with_space(before), lifted.node));
Expand All @@ -278,12 +280,12 @@ impl<'a> Nodify<'a> for Tag<'a> {
}
}
Tag::SpaceBefore(inner, sp) => {
let mut inner = inner.to_node(arena);
let mut inner = inner.to_node(arena, flags);
inner.before = merge_spaces_conservative(arena, sp, inner.before);
inner
}
Tag::SpaceAfter(inner, sp) => {
let mut inner = inner.to_node(arena);
let mut inner = inner.to_node(arena, flags);
inner.after = merge_spaces_conservative(arena, inner.after, sp);
inner
}
Expand Down Expand Up @@ -369,22 +371,22 @@ impl<'a> Formattable for AssignedField<'a, Expr<'a>> {
}

impl<'a> Nodify<'a> for AssignedField<'a, TypeAnnotation<'a>> {
fn to_node<'b>(&'a self, arena: &'b Bump) -> NodeInfo<'b>
fn to_node<'b>(&'a self, arena: &'b Bump, flags: MigrationFlags) -> NodeInfo<'b>
where
'a: 'b,
{
match self {
AssignedField::RequiredValue(name, sp, value) => {
assigned_field_value_to_node(name.value, arena, sp, &value.value, ":")
assigned_field_value_to_node(name.value, arena, sp, &value.value, ":", flags)
}
AssignedField::IgnoredValue(name, sp, value) => {
let mut n = String::with_capacity_in(name.value.len() + 1, arena);
n.push('_');
n.push_str(name.value);
assigned_field_value_to_node(n.into_bump_str(), arena, sp, &value.value, ":")
assigned_field_value_to_node(n.into_bump_str(), arena, sp, &value.value, ":", flags)
}
AssignedField::OptionalValue(name, sp, value) => {
assigned_field_value_to_node(name.value, arena, sp, &value.value, "?")
assigned_field_value_to_node(name.value, arena, sp, &value.value, "?", flags)
}
AssignedField::LabelOnly(name) => NodeInfo {
before: &[],
Expand All @@ -394,12 +396,12 @@ impl<'a> Nodify<'a> for AssignedField<'a, TypeAnnotation<'a>> {
prec: Prec::Term,
},
AssignedField::SpaceBefore(inner, sp) => {
let mut inner = inner.to_node(arena);
let mut inner = inner.to_node(arena, flags);
inner.before = merge_spaces_conservative(arena, sp, inner.before);
inner
}
AssignedField::SpaceAfter(inner, sp) => {
let mut inner = inner.to_node(arena);
let mut inner = inner.to_node(arena, flags);
inner.after = merge_spaces_conservative(arena, inner.after, sp);
inner
}
Expand All @@ -413,17 +415,27 @@ fn assigned_field_value_to_node<'a, 'b>(
sp: &'a [CommentOrNewline<'a>],
value: &'a TypeAnnotation<'a>,
sep: &'static str,
flags: MigrationFlags,
) -> NodeInfo<'b>
where
'a: 'b,
{
let first = Node::Literal(name);
let field_name = if flags.snakify {
let mut buf = Buf::new_in(arena, flags);
buf.indent(0); // Take out of beginning of line
snakify_camel_ident(&mut buf, name);
let s: &str = arena.alloc_str(buf.as_str());
s
} else {
name
};
let first = Node::Literal(field_name);

let mut b = NodeSequenceBuilder::new(arena, first, 2, false);

b.push(Sp::with_space(sp), Node::Literal(sep));

let value_lifted = value.to_node(arena);
let value_lifted = value.to_node(arena, flags);

b.push(Sp::with_space(value_lifted.before), value_lifted.node);

Expand Down Expand Up @@ -968,7 +980,7 @@ pub fn type_head_lift_spaces_after<'a, 'b: 'a>(
}

impl<'a> Nodify<'a> for TypeAnnotation<'a> {
fn to_node<'b>(&'a self, arena: &'b Bump) -> NodeInfo<'b>
fn to_node<'b>(&'a self, arena: &'b Bump, flags: MigrationFlags) -> NodeInfo<'b>
where
'a: 'b,
{
Expand All @@ -983,24 +995,24 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
NodeInfo::apply(
arena,
NodeInfo::item(first),
args.iter().map(|arg| arg.value.to_node(arena)),
args.iter().map(|arg| arg.value.to_node(arena, flags)),
)
}
TypeAnnotation::SpaceBefore(expr, spaces) => {
let mut inner = expr.to_node(arena);
let mut inner = expr.to_node(arena, flags);
inner.before = merge_spaces_conservative(arena, spaces, inner.before);
inner
}
TypeAnnotation::SpaceAfter(expr, spaces) => {
let mut inner = expr.to_node(arena);
let mut inner = expr.to_node(arena, flags);
inner.after = merge_spaces_conservative(arena, inner.after, spaces);
inner
}
TypeAnnotation::Function(args, purity, res) => {
let (first, rest) = args.split_first().expect("args must not be empty");
let first_node = first
.value
.to_node(arena)
.to_node(arena, flags)
.add_parens(arena, Parens::InFunctionType);
let mut last_after: &'_ [CommentOrNewline<'_>] = &[];
let mut rest_nodes = Vec::with_capacity_in(rest.len() + 2, arena);
Expand All @@ -1009,7 +1021,7 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
for item in rest {
let node = item
.value
.to_node(arena)
.to_node(arena, flags)
.add_parens(arena, Parens::InFunctionType);
let before = merge_spaces_conservative(arena, last_after, node.before);
multiline |= node.node.is_multiline() || !before.is_empty();
Expand All @@ -1025,7 +1037,7 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {

let res_node = res
.value
.to_node(arena)
.to_node(arena, flags)
.add_parens(arena, Parens::InFunctionType);
multiline |= res_node.node.is_multiline()
|| !last_after.is_empty()
Expand Down Expand Up @@ -1073,9 +1085,11 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
TypeAnnotation::As(left, sp, right) => {
let left = left
.value
.to_node(arena)
.to_node(arena, flags)
.add_parens(arena, Parens::InAsPattern);
let right = right
.to_node(arena, flags)
.add_parens(arena, Parens::InAsPattern);
let right = right.to_node(arena).add_parens(arena, Parens::InAsPattern);
let before_as = merge_spaces(arena, left.after, sp);
let mut b = NodeSequenceBuilder::new(arena, left.node, 2, true);
b.push(Sp::with_space(before_as), Node::Literal("as"));
Expand All @@ -1090,7 +1104,16 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
}
}
TypeAnnotation::BoundVariable(text) => {
let item = NodeInfo::item(Node::Literal(text));
let var_name = if flags.snakify {
let mut buf = Buf::new_in(arena, flags);
buf.indent(0); // Take out of beginning of line
snakify_camel_ident(&mut buf, text);
let s: &str = arena.alloc_str(buf.as_str());
s
} else {
text
};
let item = NodeInfo::item(Node::Literal(var_name));

if *text == "implements" {
parens_around_node(arena, item, false)
Expand All @@ -1105,33 +1128,35 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
let coll =
collection_to_node(arena, Braces::Curly, true, fields, |_is_first, f| {
f.value
.to_node(arena)
.to_node(arena, flags)
.add_parens(arena, Parens::InCollection)
});
maybe_add_ext(
arena,
coll,
ext,
fields.is_empty() && fields.final_comments().is_empty(),
flags,
)
}
TypeAnnotation::TagUnion { ext, tags } => {
let coll =
collection_to_node(arena, Braces::Square, false, tags, |_is_first, t| {
t.value
.to_node(arena)
.to_node(arena, flags)
.add_parens(arena, Parens::InCollection)
});
maybe_add_ext(
arena,
coll,
ext,
tags.is_empty() && tags.final_comments().is_empty(),
flags,
)
}
TypeAnnotation::Tuple { elems, ext } => {
let coll = collection_to_node(arena, Braces::Round, false, elems, |is_first, e| {
let v = e.value.to_node(arena);
let v = e.value.to_node(arena, flags);
if is_first {
v
} else {
Expand All @@ -1143,14 +1168,15 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
coll,
ext,
elems.is_empty() && elems.final_comments().is_empty(),
flags,
)
}
TypeAnnotation::Where(annot, implements_clauses) => {
let mut items = Vec::with_capacity_in(implements_clauses.len() + 2, arena);

let annot = annot
.value
.to_node(arena)
.to_node(arena, flags)
.add_parens(arena, Parens::NotNeeded);

let mut needs_indent = annot.needs_indent || !annot.after.is_empty();
Expand All @@ -1166,7 +1192,7 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
let mut last_after: &[CommentOrNewline<'_>] = &[];

for (i, clause) in implements_clauses.iter().enumerate() {
let node = clause.value.to_node(arena);
let node = clause.value.to_node(arena, flags);
let before = merge_spaces_conservative(arena, last_after, node.before);
last_after = node.after;
items.push(Item {
Expand Down Expand Up @@ -1199,28 +1225,36 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
}

impl<'a> Nodify<'a> for &'a str {
fn to_node<'b>(&'a self, _arena: &'b Bump) -> NodeInfo<'b>
fn to_node<'b>(&'a self, arena: &'b Bump, flags: MigrationFlags) -> NodeInfo<'b>
where
'a: 'b,
{
NodeInfo::item(Node::Literal(self))
if flags.snakify {
let mut buf = Buf::new_in(arena, flags);
buf.indent(0); // Take out of beginning of line
snakify_camel_ident(&mut buf, self);
let s: &str = arena.alloc_str(buf.as_str());
NodeInfo::item(Node::Literal(s))
} else {
NodeInfo::item(Node::Literal(self))
}
}
}

impl<'a, T: Nodify<'a>> Nodify<'a> for Spaced<'a, T> {
fn to_node<'b>(&'a self, arena: &'b Bump) -> NodeInfo<'b>
fn to_node<'b>(&'a self, arena: &'b Bump, flags: MigrationFlags) -> NodeInfo<'b>
where
'a: 'b,
{
match self {
Spaced::Item(item) => item.to_node(arena),
Spaced::Item(item) => item.to_node(arena, flags),
Spaced::SpaceBefore(inner, sp) => {
let mut inner = inner.to_node(arena);
let mut inner = inner.to_node(arena, flags);
inner.before = merge_spaces_conservative(arena, sp, inner.before);
inner
}
Spaced::SpaceAfter(inner, sp) => {
let mut inner = inner.to_node(arena);
let mut inner = inner.to_node(arena, flags);
inner.after = merge_spaces_conservative(arena, inner.after, sp);
inner
}
Expand All @@ -1229,7 +1263,7 @@ impl<'a, T: Nodify<'a>> Nodify<'a> for Spaced<'a, T> {
}

impl<'a> Nodify<'a> for ImplementsClause<'a> {
fn to_node<'b>(&'a self, arena: &'b Bump) -> NodeInfo<'b>
fn to_node<'b>(&'a self, arena: &'b Bump, flags: MigrationFlags) -> NodeInfo<'b>
where
'a: 'b,
{
Expand All @@ -1238,7 +1272,7 @@ impl<'a> Nodify<'a> for ImplementsClause<'a> {
let var = self
.var
.value
.to_node(arena)
.to_node(arena, flags)
.add_parens(arena, Parens::InAsPattern);

items.push(Item {
Expand All @@ -1252,7 +1286,7 @@ impl<'a> Nodify<'a> for ImplementsClause<'a> {
let mut last_after: &[CommentOrNewline<'_>] = &[];

for (i, clause) in self.abilities.iter().enumerate() {
let node = clause.value.to_node(arena);
let node = clause.value.to_node(arena, flags);
let before = merge_spaces_conservative(arena, last_after, node.before);
last_after = node.after;

Expand Down Expand Up @@ -1373,9 +1407,10 @@ fn maybe_add_ext<'a>(
delim: Node<'a>,
ext: &Option<&'a Loc<TypeAnnotation<'a>>>,
needs_indent: bool,
flags: MigrationFlags,
) -> NodeInfo<'a> {
if let Some(ext) = ext {
let ext = ext.value.to_node(arena).add_ty_ext_parens(arena);
let ext = ext.value.to_node(arena, flags).add_ty_ext_parens(arena);
debug_assert_eq!(ext.before, &[]);
let item = Node::Sequence {
first: arena.alloc(delim),
Expand Down
6 changes: 4 additions & 2 deletions crates/compiler/fmt/src/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ impl<'a> Formattable for TypeDef<'a> {
fn format_with_options(&self, buf: &mut Buf, _parens: Parens, newlines: Newlines, indent: u16) {
use roc_parse::ast::TypeDef::*;

println!("WTF???");

match self {
Alias { header, ann } => {
header.format(buf, indent);
Expand Down Expand Up @@ -596,7 +598,7 @@ fn type_head_lift_spaces<'a, 'b: 'a>(
}

impl<'a> Nodify<'a> for TypeHeader<'a> {
fn to_node<'b>(&'a self, arena: &'b Bump) -> NodeInfo<'b>
fn to_node<'b>(&'a self, arena: &'b Bump, _flags: MigrationFlags) -> NodeInfo<'b>
where
'a: 'b,
{
Expand Down Expand Up @@ -936,7 +938,7 @@ fn fmt_general_def<L: Formattable>(
buf.push_str(sep);
buf.spaces(1);

let rhs = rhs.to_node(buf.text.bump());
let rhs = rhs.to_node(buf.text.bump(), buf.flags());

if rhs.node.is_multiline() || !rhs.before.is_empty() || !rhs.after.is_empty() {
if rhs.node.is_multiline() && !rhs.needs_indent && rhs.before.iter().all(|s| s.is_newline())
Expand Down
Loading

0 comments on commit 07cd1c3

Please sign in to comment.