mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-22 01:21:46 +00:00
Make nodes more strongly normalizing to fix confusion with outdentability
This commit is contained in:
parent
a945fd9c5c
commit
ff230c4261
6 changed files with 107 additions and 21 deletions
|
@ -1206,7 +1206,6 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
|
|||
before: first_node.before,
|
||||
node: Node::CommaSequence {
|
||||
allow_blank_lines: false,
|
||||
allow_newlines: true,
|
||||
indent_rest: false,
|
||||
first: arena.alloc(first_node.node),
|
||||
rest: rest_nodes.into_bump_slice(),
|
||||
|
@ -1313,11 +1312,12 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
|
|||
.to_node(arena, flags)
|
||||
.add_parens(arena, Parens::NotNeeded);
|
||||
|
||||
let mut needs_indent = annot.needs_indent || !annot.after.is_empty();
|
||||
let before = filter_newlines(arena, annot.after);
|
||||
let mut needs_indent = annot.needs_indent || !before.is_empty();
|
||||
|
||||
items.push(Item {
|
||||
comma_before: false,
|
||||
before: annot.after,
|
||||
before,
|
||||
newline: false,
|
||||
space: true,
|
||||
node: Node::Literal(roc_parse::keyword::WHERE),
|
||||
|
@ -1327,7 +1327,8 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
|
|||
|
||||
for (i, clause) in implements_clauses.iter().enumerate() {
|
||||
let node = clause.value.to_node(arena, flags);
|
||||
let before = merge_spaces_conservative(arena, last_after, node.before);
|
||||
let before =
|
||||
filter_newlines(arena, merge_spaces(arena, last_after, node.before));
|
||||
last_after = node.after;
|
||||
items.push(Item {
|
||||
before,
|
||||
|
@ -1346,7 +1347,6 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
|
|||
first: arena.alloc(annot.node),
|
||||
rest: arena.alloc_slice_copy(&items),
|
||||
allow_blank_lines: false,
|
||||
allow_newlines: false,
|
||||
indent_rest: true,
|
||||
},
|
||||
after: last_after,
|
||||
|
@ -1358,6 +1358,24 @@ impl<'a> Nodify<'a> for TypeAnnotation<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn filter_newlines<'a, 'b: 'a>(
|
||||
arena: &'a Bump,
|
||||
items: &'b [CommentOrNewline<'b>],
|
||||
) -> &'a [CommentOrNewline<'a>] {
|
||||
let count = items.iter().filter(|i| i.is_newline()).count();
|
||||
if count > 0 {
|
||||
let mut new_items = Vec::with_capacity_in(items.len() - count, arena);
|
||||
for item in items {
|
||||
if !item.is_newline() {
|
||||
new_items.push(*item);
|
||||
}
|
||||
}
|
||||
arena.alloc_slice_copy(&new_items)
|
||||
} else {
|
||||
items
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Nodify<'a> for &'a str {
|
||||
fn to_node<'b>(&'a self, arena: &'b Bump, flags: MigrationFlags) -> NodeInfo<'b>
|
||||
where
|
||||
|
@ -1451,7 +1469,6 @@ impl<'a> Nodify<'a> for ImplementsClause<'a> {
|
|||
first: arena.alloc(var.node),
|
||||
rest: arena.alloc_slice_copy(&items),
|
||||
allow_blank_lines: false,
|
||||
allow_newlines: true,
|
||||
indent_rest: false,
|
||||
},
|
||||
after: last_after,
|
||||
|
|
|
@ -5,7 +5,7 @@ use crate::{
|
|||
annotation::{Formattable, Newlines, Parens},
|
||||
collection::Braces,
|
||||
expr::merge_spaces_conservative,
|
||||
spaces::{fmt_comments_only, fmt_spaces, fmt_spaces_no_blank_lines, NewlineAt, INDENT},
|
||||
spaces::{fmt_spaces, fmt_spaces_no_blank_lines, INDENT},
|
||||
Buf, MigrationFlags,
|
||||
};
|
||||
|
||||
|
@ -87,7 +87,6 @@ pub enum Node<'a> {
|
|||
},
|
||||
CommaSequence {
|
||||
allow_blank_lines: bool,
|
||||
allow_newlines: bool,
|
||||
indent_rest: bool,
|
||||
first: &'a Node<'a>,
|
||||
rest: &'a [Item<'a>],
|
||||
|
@ -124,12 +123,8 @@ pub struct Item<'a> {
|
|||
}
|
||||
|
||||
impl<'a> Item<'a> {
|
||||
fn is_multiline(&self, allow_newlines: bool) -> bool {
|
||||
let has_newlines = if allow_newlines {
|
||||
!self.before.is_empty()
|
||||
} else {
|
||||
self.before.iter().any(|c| c.is_comment())
|
||||
};
|
||||
fn is_multiline(&self) -> bool {
|
||||
let has_newlines = !self.before.is_empty();
|
||||
self.newline || has_newlines || self.node.is_multiline()
|
||||
}
|
||||
}
|
||||
|
@ -273,7 +268,6 @@ impl<'b> NodeInfo<'b> {
|
|||
},
|
||||
node: Node::CommaSequence {
|
||||
allow_blank_lines: false,
|
||||
allow_newlines: true,
|
||||
indent_rest,
|
||||
first: arena.alloc(first.node),
|
||||
rest: rest.into_bump_slice(),
|
||||
|
@ -361,11 +355,10 @@ impl<'a> Formattable for Node<'a> {
|
|||
} => after.is_multiline() || items.iter().any(|item| item.is_multiline()),
|
||||
Node::CommaSequence {
|
||||
allow_blank_lines: _,
|
||||
allow_newlines,
|
||||
indent_rest: _,
|
||||
first,
|
||||
rest,
|
||||
} => first.is_multiline() || rest.iter().any(|item| item.is_multiline(*allow_newlines)),
|
||||
} => first.is_multiline() || rest.iter().any(|item| item.is_multiline()),
|
||||
Node::Literal(_) => false,
|
||||
Node::TypeAnnotation(type_annotation) => type_annotation.is_multiline(),
|
||||
Node::Pattern(pat) => pat.is_multiline(),
|
||||
|
@ -429,7 +422,6 @@ impl<'a> Formattable for Node<'a> {
|
|||
}
|
||||
Node::CommaSequence {
|
||||
allow_blank_lines,
|
||||
allow_newlines,
|
||||
indent_rest,
|
||||
first,
|
||||
rest,
|
||||
|
@ -448,10 +440,8 @@ impl<'a> Formattable for Node<'a> {
|
|||
}
|
||||
if *allow_blank_lines {
|
||||
fmt_spaces(buf, item.before.iter(), indent);
|
||||
} else if *allow_newlines {
|
||||
fmt_spaces_no_blank_lines(buf, item.before.iter(), inner_indent);
|
||||
} else {
|
||||
fmt_comments_only(buf, item.before.iter(), NewlineAt::Bottom, inner_indent);
|
||||
fmt_spaces_no_blank_lines(buf, item.before.iter(), inner_indent);
|
||||
}
|
||||
if item.newline {
|
||||
buf.ensure_ends_with_newline();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue