Skip to content

Commit e64d7e5

Browse files
committed
Add syntax fixup for while loops
1 parent 1c75284 commit e64d7e5

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

crates/hir-expand/src/fixup.rs

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::mem;
55
use mbe::{SyntheticToken, SyntheticTokenId, TokenMap};
66
use rustc_hash::FxHashMap;
77
use syntax::{
8-
ast::{self, AstNode},
8+
ast::{self, AstNode, HasLoopBody},
99
match_ast, SyntaxElement, SyntaxKind, SyntaxNode, TextRange,
1010
};
1111
use tt::Subtree;
@@ -142,6 +142,46 @@ pub(crate) fn fixup_syntax(node: &SyntaxNode) -> SyntaxFixups {
142142
]);
143143
}
144144
},
145+
ast::WhileExpr(it) => {
146+
println!("Found while");
147+
if it.condition().is_none() {
148+
println!("Found no condition");
149+
// insert placeholder token after the while token
150+
let while_token = match it.while_token() {
151+
Some(t) => t,
152+
None => continue,
153+
};
154+
append.insert(while_token.into(), vec![
155+
SyntheticToken {
156+
kind: SyntaxKind::IDENT,
157+
text: "__ra_fixup".into(),
158+
range: end_range,
159+
id: EMPTY_ID,
160+
},
161+
]);
162+
} else {
163+
println!("Found condition: {:?}", it.condition())
164+
}
165+
if it.loop_body().is_none() {
166+
println!("Found no body");
167+
append.insert(node.clone().into(), vec![
168+
SyntheticToken {
169+
kind: SyntaxKind::L_CURLY,
170+
text: "{".into(),
171+
range: end_range,
172+
id: EMPTY_ID,
173+
},
174+
SyntheticToken {
175+
kind: SyntaxKind::R_CURLY,
176+
text: "}".into(),
177+
range: end_range,
178+
id: EMPTY_ID,
179+
},
180+
]);
181+
}else {
182+
println!("Found loop body: {:?}", it.loop_body())
183+
}
184+
},
145185
// FIXME: foo::
146186
// FIXME: for, loop, match etc.
147187
_ => (),
@@ -376,6 +416,47 @@ fn foo() {
376416
// the {} gets parsed as the condition, I think?
377417
expect![[r#"
378418
fn foo () {if {} {}}
419+
"#]],
420+
)
421+
}
422+
423+
#[test]
424+
fn fixup_while_1() {
425+
check(
426+
r#"
427+
fn foo() {
428+
while
429+
}
430+
"#,
431+
expect![[r#"
432+
fn foo () {while __ra_fixup {}}
433+
"#]],
434+
)
435+
}
436+
437+
#[test]
438+
fn fixup_while_2() {
439+
check(
440+
r#"
441+
fn foo() {
442+
while foo
443+
}
444+
"#,
445+
expect![[r#"
446+
fn foo () {while foo {}}
447+
"#]],
448+
)
449+
}
450+
#[test]
451+
fn fixup_while_3() {
452+
check(
453+
r#"
454+
fn foo() {
455+
while {}
456+
}
457+
"#,
458+
expect![[r#"
459+
fn foo () {while {}}
379460
"#]],
380461
)
381462
}

0 commit comments

Comments
 (0)