2022-06-06 05:49:29 -05:00
|
|
|
use ide_db::syntax_helpers::node_ext::for_each_break_and_continue_expr;
|
2022-06-10 18:02:42 -05:00
|
|
|
use syntax::{
|
|
|
|
ast::{self, AstNode, HasLoopBody},
|
|
|
|
T,
|
|
|
|
};
|
2022-06-06 05:49:29 -05:00
|
|
|
|
|
|
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
|
|
|
|
2022-06-06 05:57:37 -05:00
|
|
|
// Assist: add_label_to_loop
|
2022-06-06 05:49:29 -05:00
|
|
|
//
|
2022-06-06 05:57:37 -05:00
|
|
|
// Adds a label to a loop.
|
2022-06-06 05:49:29 -05:00
|
|
|
//
|
|
|
|
// ```
|
2022-06-06 06:18:44 -05:00
|
|
|
// fn main() {
|
|
|
|
// loop$0 {
|
|
|
|
// break;
|
|
|
|
// continue;
|
|
|
|
// }
|
2022-06-06 05:49:29 -05:00
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
2022-06-06 06:18:44 -05:00
|
|
|
// fn main() {
|
2022-06-06 06:20:01 -05:00
|
|
|
// 'l: loop {
|
|
|
|
// break 'l;
|
|
|
|
// continue 'l;
|
2022-06-06 06:18:44 -05:00
|
|
|
// }
|
2022-06-06 05:49:29 -05:00
|
|
|
// }
|
|
|
|
// ```
|
2022-07-20 08:02:08 -05:00
|
|
|
pub(crate) fn add_label_to_loop(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
2022-06-10 18:02:42 -05:00
|
|
|
let loop_kw = ctx.find_token_syntax_at_offset(T![loop])?;
|
|
|
|
let loop_expr = loop_kw.parent().and_then(ast::LoopExpr::cast)?;
|
2022-06-06 06:18:44 -05:00
|
|
|
if loop_expr.label().is_some() {
|
|
|
|
return None;
|
|
|
|
}
|
2022-06-06 05:49:29 -05:00
|
|
|
|
|
|
|
acc.add(
|
|
|
|
AssistId("add_label_to_loop", AssistKind::Generate),
|
|
|
|
"Add Label",
|
|
|
|
loop_expr.syntax().text_range(),
|
|
|
|
|builder| {
|
2022-06-10 18:02:42 -05:00
|
|
|
builder.insert(loop_kw.text_range().start(), "'l: ");
|
|
|
|
|
|
|
|
let loop_body = loop_expr.loop_body().and_then(|it| it.stmt_list());
|
|
|
|
for_each_break_and_continue_expr(
|
|
|
|
loop_expr.label(),
|
|
|
|
loop_body,
|
|
|
|
&mut |expr| match expr {
|
2022-06-06 05:49:29 -05:00
|
|
|
ast::Expr::BreakExpr(break_expr) => {
|
|
|
|
if let Some(break_token) = break_expr.break_token() {
|
2022-06-06 06:20:01 -05:00
|
|
|
builder.insert(break_token.text_range().end(), " 'l")
|
2022-06-06 05:49:29 -05:00
|
|
|
}
|
2022-06-06 05:57:37 -05:00
|
|
|
}
|
2022-06-06 05:49:29 -05:00
|
|
|
ast::Expr::ContinueExpr(continue_expr) => {
|
|
|
|
if let Some(continue_token) = continue_expr.continue_token() {
|
2022-06-06 06:20:01 -05:00
|
|
|
builder.insert(continue_token.text_range().end(), " 'l")
|
2022-06-06 05:49:29 -05:00
|
|
|
}
|
2022-06-06 05:57:37 -05:00
|
|
|
}
|
2022-06-06 06:18:44 -05:00
|
|
|
_ => {}
|
2022-06-10 18:02:42 -05:00
|
|
|
},
|
|
|
|
);
|
2022-06-06 05:49:29 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-06-06 06:18:44 -05:00
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
2022-06-06 05:49:29 -05:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_label() {
|
|
|
|
check_assist(
|
|
|
|
add_label_to_loop,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
2022-06-06 06:18:44 -05:00
|
|
|
loop$0 {
|
|
|
|
break;
|
|
|
|
continue;
|
2022-06-06 05:49:29 -05:00
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
2022-06-06 06:20:01 -05:00
|
|
|
'l: loop {
|
|
|
|
break 'l;
|
|
|
|
continue 'l;
|
2022-06-06 05:49:29 -05:00
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-06 05:57:37 -05:00
|
|
|
#[test]
|
|
|
|
fn add_label_to_outer_loop() {
|
|
|
|
check_assist(
|
|
|
|
add_label_to_loop,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
2022-06-06 06:18:44 -05:00
|
|
|
loop$0 {
|
|
|
|
break;
|
|
|
|
continue;
|
2022-06-06 05:57:37 -05:00
|
|
|
loop {
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
2022-06-06 06:20:01 -05:00
|
|
|
'l: loop {
|
|
|
|
break 'l;
|
|
|
|
continue 'l;
|
2022-06-06 05:57:37 -05:00
|
|
|
loop {
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_label_to_inner_loop() {
|
|
|
|
check_assist(
|
|
|
|
add_label_to_loop,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
2022-06-06 06:18:44 -05:00
|
|
|
loop {
|
|
|
|
break;
|
|
|
|
continue;
|
2022-06-06 05:57:37 -05:00
|
|
|
loop$0 {
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
2022-06-06 06:18:44 -05:00
|
|
|
loop {
|
|
|
|
break;
|
|
|
|
continue;
|
2022-06-06 06:20:01 -05:00
|
|
|
'l: loop {
|
|
|
|
break 'l;
|
|
|
|
continue 'l;
|
2022-06-06 05:57:37 -05:00
|
|
|
}
|
|
|
|
}
|
2022-06-06 06:18:44 -05:00
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn do_not_add_label_if_exists() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
add_label_to_loop,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
2022-06-06 06:20:01 -05:00
|
|
|
'l: loop$0 {
|
|
|
|
break 'l;
|
|
|
|
continue 'l;
|
2022-06-06 06:18:44 -05:00
|
|
|
}
|
2022-06-06 05:57:37 -05:00
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
2022-06-06 05:49:29 -05:00
|
|
|
}
|