Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8725368

Browse files
committed
Make add_label_to_loop basically work
1 parent 9ef01d0 commit 8725368

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use ide_db::syntax_helpers::node_ext::for_each_break_and_continue_expr;
2+
use syntax::ast::{self, AstNode, HasLoopBody};
3+
use syntax::T;
4+
5+
use crate::{AssistContext, AssistId, AssistKind, Assists};
6+
7+
// Assist: add_lifetime_to_type
8+
//
9+
// Adds a new lifetime to a struct, enum or union.
10+
//
11+
// ```
12+
// struct Point {
13+
// x: &u32,
14+
// y: u32,
15+
// }
16+
// ```
17+
// ->
18+
// ```
19+
// struct Point<'a> {
20+
// x: &'a u32,
21+
// y: u32,
22+
// }
23+
// ```
24+
pub(crate) fn add_label_to_loop(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
25+
let loop_expr = ctx.find_node_at_offset::<ast::LoopExpr>()?;
26+
let loop_body = loop_expr.loop_body().and_then(|it| it.stmt_list());
27+
let mut related_exprs = vec![];
28+
related_exprs.push(ast::Expr::LoopExpr(loop_expr.clone()));
29+
for_each_break_and_continue_expr(loop_expr.label(), loop_body, &mut |expr| {
30+
if let ast::Expr::BreakExpr(_) | ast::Expr::ContinueExpr(_) = expr {
31+
related_exprs.push(expr)
32+
}
33+
});
34+
dbg!(loop_expr.syntax().text_range());
35+
36+
acc.add(
37+
AssistId("add_label_to_loop", AssistKind::Generate),
38+
"Add Label",
39+
loop_expr.syntax().text_range(),
40+
|builder| {
41+
for expr in related_exprs {
42+
match expr {
43+
ast::Expr::BreakExpr(break_expr) => {
44+
if let Some(break_token) = break_expr.break_token() {
45+
builder.insert(break_token.text_range().end(), " 'loop")
46+
}
47+
},
48+
ast::Expr::ContinueExpr(continue_expr) => {
49+
if let Some(continue_token) = continue_expr.continue_token() {
50+
builder.insert(continue_token.text_range().end(), " 'loop")
51+
}
52+
},
53+
ast::Expr::LoopExpr(loop_expr) => {
54+
if let Some(loop_token) = loop_expr.loop_token() {
55+
builder.insert(loop_token.text_range().start(), "'loop: ")
56+
}
57+
},
58+
_ => todo!()
59+
}
60+
}
61+
},
62+
)
63+
}
64+
65+
66+
#[cfg(test)]
67+
mod tests {
68+
use crate::tests::check_assist;
69+
70+
use super::*;
71+
72+
#[test]
73+
fn add_label() {
74+
check_assist(
75+
add_label_to_loop,
76+
r#"
77+
fn main() {
78+
loop$0 {
79+
break;
80+
continue;
81+
}
82+
}"#,
83+
r#"
84+
fn main() {
85+
'loop: loop {
86+
break 'loop;
87+
continue 'loop;
88+
}
89+
}"#,
90+
);
91+
}
92+
93+
}

crates/ide-assists/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ mod handlers {
104104
pub(crate) type Handler = fn(&mut Assists, &AssistContext) -> Option<()>;
105105

106106
mod add_explicit_type;
107+
mod add_label_to_loop;
107108
mod add_lifetime_to_type;
108109
mod add_missing_impl_members;
109110
mod add_turbo_fish;
@@ -193,6 +194,7 @@ mod handlers {
193194
&[
194195
// These are alphabetic for the foolish consistency
195196
add_explicit_type::add_explicit_type,
197+
add_label_to_loop::add_label_to_loop,
196198
add_missing_match_arms::add_missing_match_arms,
197199
add_lifetime_to_type::add_lifetime_to_type,
198200
add_return_type::add_return_type,

0 commit comments

Comments
 (0)