Skip to content

Commit 4a32bbe

Browse files
authored
Merge pull request #22419 from A4-Tacks/add-loop-label-group
fix: use grouped annotation for add_label_to_loop
2 parents 608f881 + 079aad9 commit 4a32bbe

2 files changed

Lines changed: 27 additions & 28 deletions

File tree

crates/ide-assists/src/handlers/add_label_to_loop.rs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
use ide_db::{
2-
source_change::SourceChangeBuilder, syntax_helpers::node_ext::for_each_break_and_continue_expr,
3-
};
1+
use ide_db::syntax_helpers::node_ext::for_each_break_and_continue_expr;
42
use syntax::{
53
SyntaxToken, T,
64
ast::{self, AstNode, HasLoopBody},
7-
syntax_editor::{Position, SyntaxEditor},
5+
syntax_editor::{Position, SyntaxAnnotation, SyntaxEditor},
86
};
97

108
use crate::{AssistContext, AssistId, Assists};
@@ -24,8 +22,8 @@ use crate::{AssistContext, AssistId, Assists};
2422
// ->
2523
// ```
2624
// fn main() {
27-
// ${1:'l}: loop {
28-
// break ${2:'l};
25+
// ${0:'l}: loop {
26+
// break ${0:'l};
2927
// continue ${0:'l};
3028
// }
3129
// }
@@ -53,8 +51,11 @@ pub(crate) fn add_label_to_loop(acc: &mut Assists, ctx: &AssistContext<'_, '_>)
5351
];
5452
editor.insert_all(Position::before(&loop_kw), elements);
5553

56-
if let Some(cap) = ctx.config.snippet_cap {
57-
editor.add_annotation(label.syntax(), builder.make_placeholder_snippet(cap));
54+
let annotation =
55+
ctx.config.snippet_cap.map(|cap| builder.make_placeholder_snippet(cap));
56+
57+
if let Some(annotation) = annotation {
58+
editor.add_annotation(label.syntax(), annotation);
5859
}
5960

6061
let loop_body = loop_expr.loop_body().and_then(|it| it.stmt_list());
@@ -64,13 +65,14 @@ pub(crate) fn add_label_to_loop(acc: &mut Assists, ctx: &AssistContext<'_, '_>)
6465
ast::Expr::ContinueExpr(continue_expr) => continue_expr.continue_token(),
6566
_ => return,
6667
};
67-
if let Some(token) = token {
68-
insert_label_after_token(&editor, &token, ctx, builder);
68+
if let Some(token) = token
69+
&& let Some(annotation) = annotation
70+
{
71+
insert_label_after_token(&editor, &token, annotation);
6972
}
7073
});
7174

7275
builder.add_file_edits(ctx.vfs_file_id(), editor);
73-
builder.rename();
7476
},
7577
)
7678
}
@@ -86,17 +88,14 @@ fn loop_token(loop_expr: &ast::AnyHasLoopBody) -> Option<syntax::SyntaxToken> {
8688
fn insert_label_after_token(
8789
editor: &SyntaxEditor,
8890
token: &SyntaxToken,
89-
ctx: &AssistContext<'_, '_>,
90-
builder: &mut SourceChangeBuilder,
91+
annotation: SyntaxAnnotation,
9192
) {
9293
let make = editor.make();
9394
let label = make.lifetime("'l");
9495
let elements = vec![make.whitespace(" ").into(), label.syntax().clone().into()];
9596
editor.insert_all(Position::after(token), elements);
9697

97-
if let Some(cap) = ctx.config.snippet_cap {
98-
editor.add_annotation(label.syntax(), builder.make_placeholder_snippet(cap));
99-
}
98+
editor.add_annotation(label.syntax(), annotation);
10099
}
101100

102101
#[cfg(test)]
@@ -118,8 +117,8 @@ fn main() {
118117
}"#,
119118
r#"
120119
fn main() {
121-
${1:'l}: loop {
122-
break ${2:'l};
120+
${0:'l}: loop {
121+
break ${0:'l};
123122
continue ${0:'l};
124123
}
125124
}"#,
@@ -139,8 +138,8 @@ fn main() {
139138
}"#,
140139
r#"
141140
fn main() {
142-
${1:'l}: while true {
143-
break ${2:'l};
141+
${0:'l}: while true {
142+
break ${0:'l};
144143
continue ${0:'l};
145144
}
146145
}"#,
@@ -160,8 +159,8 @@ fn main() {
160159
}"#,
161160
r#"
162161
fn main() {
163-
${1:'l}: for _ in 0..5 {
164-
break ${2:'l};
162+
${0:'l}: for _ in 0..5 {
163+
break ${0:'l};
165164
continue ${0:'l};
166165
}
167166
}"#,
@@ -185,8 +184,8 @@ fn main() {
185184
}"#,
186185
r#"
187186
fn main() {
188-
${1:'l}: loop {
189-
break ${2:'l};
187+
${0:'l}: loop {
188+
break ${0:'l};
190189
continue ${0:'l};
191190
loop {
192191
break;
@@ -217,8 +216,8 @@ fn main() {
217216
loop {
218217
break;
219218
continue;
220-
${1:'l}: loop {
221-
break ${2:'l};
219+
${0:'l}: loop {
220+
break ${0:'l};
222221
continue ${0:'l};
223222
}
224223
}

crates/ide-assists/src/tests/generated.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ fn main() {
204204
"#####,
205205
r#####"
206206
fn main() {
207-
${1:'l}: loop {
208-
break ${2:'l};
207+
${0:'l}: loop {
208+
break ${0:'l};
209209
continue ${0:'l};
210210
}
211211
}

0 commit comments

Comments
 (0)