Skip to content

Commit 113f1db

Browse files
committed
Auto merge of rust-lang#12880 - palango:while-fixup, r=Veykril
Add syntax fixup for while loops Part of rust-lang/rust-analyzer#12777 This is a first iteration to gather some feedback. In particular I'm not sure if the curly braces should be added here, but I couldn't get the test to work without them. Any hints welcome!
2 parents c6c0ac2 + c16e4f2 commit 113f1db

File tree

1 file changed

+108
-2
lines changed

1 file changed

+108
-2
lines changed

crates/hir-expand/src/fixup.rs

Lines changed: 108 additions & 2 deletions
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,8 +142,59 @@ pub(crate) fn fixup_syntax(node: &SyntaxNode) -> SyntaxFixups {
142142
]);
143143
}
144144
},
145+
ast::WhileExpr(it) => {
146+
if it.condition().is_none() {
147+
// insert placeholder token after the while token
148+
let while_token = match it.while_token() {
149+
Some(t) => t,
150+
None => continue,
151+
};
152+
append.insert(while_token.into(), vec![
153+
SyntheticToken {
154+
kind: SyntaxKind::IDENT,
155+
text: "__ra_fixup".into(),
156+
range: end_range,
157+
id: EMPTY_ID,
158+
},
159+
]);
160+
}
161+
if it.loop_body().is_none() {
162+
append.insert(node.clone().into(), vec![
163+
SyntheticToken {
164+
kind: SyntaxKind::L_CURLY,
165+
text: "{".into(),
166+
range: end_range,
167+
id: EMPTY_ID,
168+
},
169+
SyntheticToken {
170+
kind: SyntaxKind::R_CURLY,
171+
text: "}".into(),
172+
range: end_range,
173+
id: EMPTY_ID,
174+
},
175+
]);
176+
}
177+
},
178+
ast::LoopExpr(it) => {
179+
if it.loop_body().is_none() {
180+
append.insert(node.clone().into(), vec![
181+
SyntheticToken {
182+
kind: SyntaxKind::L_CURLY,
183+
text: "{".into(),
184+
range: end_range,
185+
id: EMPTY_ID,
186+
},
187+
SyntheticToken {
188+
kind: SyntaxKind::R_CURLY,
189+
text: "}".into(),
190+
range: end_range,
191+
id: EMPTY_ID,
192+
},
193+
]);
194+
}
195+
},
145196
// FIXME: foo::
146-
// FIXME: for, loop, match etc.
197+
// FIXME: for, match etc.
147198
_ => (),
148199
}
149200
}
@@ -376,6 +427,61 @@ fn foo() {
376427
// the {} gets parsed as the condition, I think?
377428
expect![[r#"
378429
fn foo () {if {} {}}
430+
"#]],
431+
)
432+
}
433+
434+
#[test]
435+
fn fixup_while_1() {
436+
check(
437+
r#"
438+
fn foo() {
439+
while
440+
}
441+
"#,
442+
expect![[r#"
443+
fn foo () {while __ra_fixup {}}
444+
"#]],
445+
)
446+
}
447+
448+
#[test]
449+
fn fixup_while_2() {
450+
check(
451+
r#"
452+
fn foo() {
453+
while foo
454+
}
455+
"#,
456+
expect![[r#"
457+
fn foo () {while foo {}}
458+
"#]],
459+
)
460+
}
461+
#[test]
462+
fn fixup_while_3() {
463+
check(
464+
r#"
465+
fn foo() {
466+
while {}
467+
}
468+
"#,
469+
expect![[r#"
470+
fn foo () {while __ra_fixup {}}
471+
"#]],
472+
)
473+
}
474+
475+
#[test]
476+
fn fixup_loop() {
477+
check(
478+
r#"
479+
fn foo() {
480+
loop
481+
}
482+
"#,
483+
expect![[r#"
484+
fn foo () {loop {}}
379485
"#]],
380486
)
381487
}

0 commit comments

Comments
 (0)