parse the region of the preceding comment for an expect

This commit is contained in:
Folkert 2022-07-19 00:12:15 +02:00 committed by Richard Feldman
parent 35733d51dc
commit 7597d11b59
No known key found for this signature in database
GPG Key ID: 7E4127D1E4241798
8 changed files with 35 additions and 11 deletions

View File

@ -277,7 +277,7 @@ fn to_pending_def<'a>(
Type(TypeDef::Opaque { .. }) => todo_opaques!(),
Type(TypeDef::Ability { .. }) => todo_abilities!(),
Value(AstValueDef::Expect(_)) => todo!(),
Value(AstValueDef::Expect { .. }) => todo!(),
SpaceBefore(sub_def, _) | SpaceAfter(sub_def, _) => {
to_pending_def(env, sub_def, scope, pattern_type)

View File

@ -2457,9 +2457,12 @@ fn to_pending_value_def<'a>(
}
}
Expect(condition) => PendingValue::Expect(PendingExpect {
Expect {
condition,
preceding_comment: Region::zero(),
preceding_comment,
} => PendingValue::Expect(PendingExpect {
condition,
preceding_comment: *preceding_comment,
}),
}
}

View File

@ -82,9 +82,15 @@ fn desugar_value_def<'a>(arena: &'a Bump, def: &'a ValueDef<'a>) -> ValueDef<'a>
body_pattern: *body_pattern,
body_expr: desugar_expr(arena, body_expr),
},
Expect(condition) => {
Expect {
condition,
preceding_comment,
} => {
let desugared_condition = &*arena.alloc(desugar_expr(arena, condition));
Expect(desugared_condition)
Expect {
condition: desugared_condition,
preceding_comment: *preceding_comment,
}
}
}
}

View File

@ -158,7 +158,7 @@ impl<'a> Formattable for ValueDef<'a> {
}
Body(loc_pattern, loc_expr) => loc_pattern.is_multiline() || loc_expr.is_multiline(),
AnnotatedBody { .. } => true,
Expect(loc_expr) => loc_expr.is_multiline(),
Expect { condition, .. } => condition.is_multiline(),
}
}
@ -232,7 +232,7 @@ impl<'a> Formattable for ValueDef<'a> {
Body(loc_pattern, loc_expr) => {
fmt_body(buf, &loc_pattern.value, &loc_expr.value, indent);
}
Expect(condition) => fmt_expect(buf, condition, self.is_multiline(), indent),
Expect { condition, .. } => fmt_expect(buf, condition, self.is_multiline(), indent),
AnnotatedBody {
ann_pattern,
ann_type,

View File

@ -540,7 +540,13 @@ impl<'a> RemoveSpaces<'a> for ValueDef<'a> {
body_pattern: arena.alloc(body_pattern.remove_spaces(arena)),
body_expr: arena.alloc(body_expr.remove_spaces(arena)),
},
Expect(a) => Expect(arena.alloc(a.remove_spaces(arena))),
Expect {
condition,
preceding_comment,
} => Expect {
condition: arena.alloc(condition.remove_spaces(arena)),
preceding_comment,
},
}
}
}

View File

@ -200,7 +200,7 @@ fn generate_entry_docs<'a>(
ValueDef::Body(_, _) => (),
ValueDef::Expect(_) => {
ValueDef::Expect { .. } => {
// Don't generate docs for `expect`s
}
},

View File

@ -331,7 +331,10 @@ pub enum ValueDef<'a> {
body_expr: &'a Loc<Expr<'a>>,
},
Expect(&'a Loc<Expr<'a>>),
Expect {
condition: &'a Loc<Expr<'a>>,
preceding_comment: Region,
},
}
#[derive(Debug, Clone, PartialEq, Default)]

View File

@ -584,6 +584,7 @@ fn parse_defs_end<'a>(
let initial = state.clone();
let mut spaces_before_current = &[] as &[_];
let spaces_before_current_start = state.pos();
let state = match space0_e(min_indent, EExpr::IndentStart).parse(arena, state) {
Err((MadeProgress, _, s)) => {
@ -625,7 +626,12 @@ fn parse_defs_end<'a>(
let end = loc_def_expr.region.end();
let region = Region::new(start, end);
let value_def = ValueDef::Expect(arena.alloc(loc_def_expr));
let preceding_comment = Region::new(spaces_before_current_start, start);
let value_def = ValueDef::Expect {
condition: arena.alloc(loc_def_expr),
preceding_comment,
};
defs.push_value_def(value_def, region, spaces_before_current, &[]);
global_state = state;