Skip to content

Commit 9e6c85c

Browse files
committed
fix: use grouped annotation for add_label_to_loop
Example --- ```rust fn main() { $0loop { break; continue; } } ``` **Before this PR** ```rust fn main() { ${1:'l}: loop { break ${2:'l}; continue ${0:'l}; } } ``` **After this PR** ```rust fn main() { ${0:'l}: loop { break ${0:'l}; continue ${0:'l}; } } ```
1 parent 124e11d commit 9e6c85c

2 files changed

Lines changed: 27 additions & 27 deletions

File tree

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

Lines changed: 25 additions & 25 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,8 +65,10 @@ 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

@@ -86,17 +89,14 @@ fn loop_token(loop_expr: &ast::AnyHasLoopBody) -> Option<syntax::SyntaxToken> {
8689
fn insert_label_after_token(
8790
editor: &SyntaxEditor,
8891
token: &SyntaxToken,
89-
ctx: &AssistContext<'_, '_>,
90-
builder: &mut SourceChangeBuilder,
92+
annotation: SyntaxAnnotation,
9193
) {
9294
let make = editor.make();
9395
let label = make.lifetime("'l");
9496
let elements = vec![make.whitespace(" ").into(), label.syntax().clone().into()];
9597
editor.insert_all(Position::after(token), elements);
9698

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

102102
#[cfg(test)]
@@ -118,8 +118,8 @@ fn main() {
118118
}"#,
119119
r#"
120120
fn main() {
121-
${1:'l}: loop {
122-
break ${2:'l};
121+
${0:'l}: loop {
122+
break ${0:'l};
123123
continue ${0:'l};
124124
}
125125
}"#,
@@ -139,8 +139,8 @@ fn main() {
139139
}"#,
140140
r#"
141141
fn main() {
142-
${1:'l}: while true {
143-
break ${2:'l};
142+
${0:'l}: while true {
143+
break ${0:'l};
144144
continue ${0:'l};
145145
}
146146
}"#,
@@ -160,8 +160,8 @@ fn main() {
160160
}"#,
161161
r#"
162162
fn main() {
163-
${1:'l}: for _ in 0..5 {
164-
break ${2:'l};
163+
${0:'l}: for _ in 0..5 {
164+
break ${0:'l};
165165
continue ${0:'l};
166166
}
167167
}"#,
@@ -185,8 +185,8 @@ fn main() {
185185
}"#,
186186
r#"
187187
fn main() {
188-
${1:'l}: loop {
189-
break ${2:'l};
188+
${0:'l}: loop {
189+
break ${0:'l};
190190
continue ${0:'l};
191191
loop {
192192
break;
@@ -217,8 +217,8 @@ fn main() {
217217
loop {
218218
break;
219219
continue;
220-
${1:'l}: loop {
221-
break ${2:'l};
220+
${0:'l}: loop {
221+
break ${0:'l};
222222
continue ${0:'l};
223223
}
224224
}

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)