Skip to content

Commit d513b4c

Browse files
committed
Added fixup for for loops w/ missing parts
1 parent 5cb3e7a commit d513b4c

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed

crates/hir-expand/src/fixup.rs

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ pub(crate) fn fixup_syntax(node: &SyntaxNode) -> SyntaxFixups {
193193
}
194194
},
195195
// FIXME: foo::
196-
// FIXME: for, match etc.
197196
ast::MatchExpr(it) => {
198197
if it.expr().is_none() {
199198
let match_token = match it.match_token() {
@@ -257,6 +256,42 @@ pub(crate) fn fixup_syntax(node: &SyntaxNode) -> SyntaxFixups {
257256
]);
258257
}
259258
},
259+
ast::ForExpr(it) => {
260+
let for_token = match it.for_token() {
261+
Some(token) => token,
262+
None => continue
263+
};
264+
265+
let [pat, in_token, iter] = [
266+
(SyntaxKind::UNDERSCORE, "_"),
267+
(SyntaxKind::IN_KW, "in"),
268+
(SyntaxKind::IDENT, "__ra_fixup")
269+
].map(|(kind, text)| SyntheticToken { kind, text: text.into(), range: end_range, id: EMPTY_ID});
270+
271+
if it.pat().is_none() && it.in_token().is_none() && it.iterable().is_none() {
272+
append.insert(for_token.into(), vec![pat, in_token, iter]);
273+
}
274+
275+
// Tricky: add logic to add in just a pattern or iterable if not all
276+
// the pieces are missing
277+
278+
if it.loop_body().is_none() {
279+
append.insert(node.clone().into(), vec![
280+
SyntheticToken {
281+
kind: SyntaxKind::L_CURLY,
282+
text: "{".into(),
283+
range: end_range,
284+
id: EMPTY_ID,
285+
},
286+
SyntheticToken {
287+
kind: SyntaxKind::R_CURLY,
288+
text: "}".into(),
289+
range: end_range,
290+
id: EMPTY_ID,
291+
},
292+
]);
293+
}
294+
},
260295
_ => (),
261296
}
262297
}
@@ -349,6 +384,47 @@ mod tests {
349384
assert_eq!(tt.to_string(), original_as_tt.to_string());
350385
}
351386

387+
#[test]
388+
fn for_no_iter_no_body() {
389+
check(
390+
r#"
391+
fn foo() {
392+
for
393+
}
394+
"#,
395+
expect![[r#"
396+
fn foo () {for _ in __ra_fixup {}}
397+
"#]],
398+
)
399+
}
400+
401+
#[test]
402+
fn for_no_iter() {
403+
check(
404+
r#"
405+
fn foo() {
406+
for {}
407+
}
408+
"#,
409+
expect![[r#"
410+
fn foo () {for _ in __ra_fixup {}}
411+
"#]],
412+
)
413+
}
414+
415+
#[test]
416+
fn for_no_body() {
417+
check(
418+
r#"
419+
fn foo() {
420+
for bar in qux
421+
}
422+
"#,
423+
expect![[r#"
424+
fn foo () {for bar in qux {}}
425+
"#]],
426+
)
427+
}
352428

353429
#[test]
354430
fn match_no_expr_no_arms() {

0 commit comments

Comments
 (0)