aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_assists/src/assist_context.rs20
-rw-r--r--crates/ra_assists/src/handlers/unwrap_block.rs24
2 files changed, 13 insertions, 31 deletions
diff --git a/crates/ra_assists/src/assist_context.rs b/crates/ra_assists/src/assist_context.rs
index 005c17776..9f6ca449b 100644
--- a/crates/ra_assists/src/assist_context.rs
+++ b/crates/ra_assists/src/assist_context.rs
@@ -171,19 +171,13 @@ impl Assists {
171 171
172pub(crate) struct AssistBuilder { 172pub(crate) struct AssistBuilder {
173 edit: TextEditBuilder, 173 edit: TextEditBuilder,
174 cursor_position: Option<TextSize>,
175 file: FileId, 174 file: FileId,
176 is_snippet: bool, 175 is_snippet: bool,
177} 176}
178 177
179impl AssistBuilder { 178impl AssistBuilder {
180 pub(crate) fn new(file: FileId) -> AssistBuilder { 179 pub(crate) fn new(file: FileId) -> AssistBuilder {
181 AssistBuilder { 180 AssistBuilder { edit: TextEditBuilder::default(), file, is_snippet: false }
182 edit: TextEditBuilder::default(),
183 cursor_position: None,
184 file,
185 is_snippet: false,
186 }
187 } 181 }
188 182
189 /// Remove specified `range` of text. 183 /// Remove specified `range` of text.
@@ -241,10 +235,6 @@ impl AssistBuilder {
241 algo::diff(&node, &new).into_text_edit(&mut self.edit) 235 algo::diff(&node, &new).into_text_edit(&mut self.edit)
242 } 236 }
243 237
244 /// Specify desired position of the cursor after the assist is applied.
245 pub(crate) fn set_cursor(&mut self, offset: TextSize) {
246 self.cursor_position = Some(offset)
247 }
248 // FIXME: better API 238 // FIXME: better API
249 pub(crate) fn set_file(&mut self, assist_file: FileId) { 239 pub(crate) fn set_file(&mut self, assist_file: FileId) {
250 self.file = assist_file; 240 self.file = assist_file;
@@ -258,12 +248,8 @@ impl AssistBuilder {
258 248
259 fn finish(self, change_label: String) -> SourceChange { 249 fn finish(self, change_label: String) -> SourceChange {
260 let edit = self.edit.finish(); 250 let edit = self.edit.finish();
261 if edit.is_empty() && self.cursor_position.is_none() { 251 let mut res = SingleFileChange { label: change_label, edit, cursor_position: None }
262 panic!("Only call `add_assist` if the assist can be applied") 252 .into_source_change(self.file);
263 }
264 let mut res =
265 SingleFileChange { label: change_label, edit, cursor_position: self.cursor_position }
266 .into_source_change(self.file);
267 if self.is_snippet { 253 if self.is_snippet {
268 res.is_snippet = true; 254 res.is_snippet = true;
269 } 255 }
diff --git a/crates/ra_assists/src/handlers/unwrap_block.rs b/crates/ra_assists/src/handlers/unwrap_block.rs
index b76182d79..8440c7d0f 100644
--- a/crates/ra_assists/src/handlers/unwrap_block.rs
+++ b/crates/ra_assists/src/handlers/unwrap_block.rs
@@ -62,7 +62,6 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
62 let range_to_del_else_if = TextRange::new(ancestor_then_branch.syntax().text_range().end(), l_curly_token.text_range().start()); 62 let range_to_del_else_if = TextRange::new(ancestor_then_branch.syntax().text_range().end(), l_curly_token.text_range().start());
63 let range_to_del_rest = TextRange::new(then_branch.syntax().text_range().end(), if_expr.syntax().text_range().end()); 63 let range_to_del_rest = TextRange::new(then_branch.syntax().text_range().end(), if_expr.syntax().text_range().end());
64 64
65 edit.set_cursor(ancestor_then_branch.syntax().text_range().end());
66 edit.delete(range_to_del_rest); 65 edit.delete(range_to_del_rest);
67 edit.delete(range_to_del_else_if); 66 edit.delete(range_to_del_else_if);
68 edit.replace(target, update_expr_string(then_branch.to_string(), &[' ', '{'])); 67 edit.replace(target, update_expr_string(then_branch.to_string(), &[' ', '{']));
@@ -79,7 +78,6 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
79 return acc.add(assist_id, assist_label, target, |edit| { 78 return acc.add(assist_id, assist_label, target, |edit| {
80 let range_to_del = TextRange::new(then_branch.syntax().text_range().end(), l_curly_token.text_range().start()); 79 let range_to_del = TextRange::new(then_branch.syntax().text_range().end(), l_curly_token.text_range().start());
81 80
82 edit.set_cursor(then_branch.syntax().text_range().end());
83 edit.delete(range_to_del); 81 edit.delete(range_to_del);
84 edit.replace(target, update_expr_string(else_block.to_string(), &[' ', '{'])); 82 edit.replace(target, update_expr_string(else_block.to_string(), &[' ', '{']));
85 }); 83 });
@@ -97,8 +95,6 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
97 95
98 let target = expr_to_unwrap.syntax().text_range(); 96 let target = expr_to_unwrap.syntax().text_range();
99 acc.add(assist_id, assist_label, target, |edit| { 97 acc.add(assist_id, assist_label, target, |edit| {
100 edit.set_cursor(expr.syntax().text_range().start());
101
102 edit.replace( 98 edit.replace(
103 expr.syntax().text_range(), 99 expr.syntax().text_range(),
104 update_expr_string(expr_to_unwrap.to_string(), &[' ', '{', '\n']), 100 update_expr_string(expr_to_unwrap.to_string(), &[' ', '{', '\n']),
@@ -154,7 +150,7 @@ mod tests {
154 r#" 150 r#"
155 fn main() { 151 fn main() {
156 bar(); 152 bar();
157 <|>foo(); 153 foo();
158 154
159 //comment 155 //comment
160 bar(); 156 bar();
@@ -188,7 +184,7 @@ mod tests {
188 184
189 //comment 185 //comment
190 bar(); 186 bar();
191 }<|> 187 }
192 println!("bar"); 188 println!("bar");
193 } 189 }
194 "#, 190 "#,
@@ -222,7 +218,7 @@ mod tests {
222 218
223 //comment 219 //comment
224 //bar(); 220 //bar();
225 }<|> 221 }
226 println!("bar"); 222 println!("bar");
227 } 223 }
228 "#, 224 "#,
@@ -258,7 +254,7 @@ mod tests {
258 //bar(); 254 //bar();
259 } else if false { 255 } else if false {
260 println!("bar"); 256 println!("bar");
261 }<|> 257 }
262 println!("foo"); 258 println!("foo");
263 } 259 }
264 "#, 260 "#,
@@ -298,7 +294,7 @@ mod tests {
298 println!("bar"); 294 println!("bar");
299 } else if true { 295 } else if true {
300 println!("foo"); 296 println!("foo");
301 }<|> 297 }
302 println!("else"); 298 println!("else");
303 } 299 }
304 "#, 300 "#,
@@ -336,7 +332,7 @@ mod tests {
336 //bar(); 332 //bar();
337 } else if false { 333 } else if false {
338 println!("bar"); 334 println!("bar");
339 }<|> 335 }
340 println!("foo"); 336 println!("foo");
341 } 337 }
342 "#, 338 "#,
@@ -383,7 +379,7 @@ mod tests {
383 "#, 379 "#,
384 r#" 380 r#"
385 fn main() { 381 fn main() {
386 <|>if true { 382 if true {
387 foo(); 383 foo();
388 384
389 //comment 385 //comment
@@ -417,7 +413,7 @@ mod tests {
417 r#" 413 r#"
418 fn main() { 414 fn main() {
419 for i in 0..5 { 415 for i in 0..5 {
420 <|>foo(); 416 foo();
421 417
422 //comment 418 //comment
423 bar(); 419 bar();
@@ -447,7 +443,7 @@ mod tests {
447 "#, 443 "#,
448 r#" 444 r#"
449 fn main() { 445 fn main() {
450 <|>if true { 446 if true {
451 foo(); 447 foo();
452 448
453 //comment 449 //comment
@@ -480,7 +476,7 @@ mod tests {
480 "#, 476 "#,
481 r#" 477 r#"
482 fn main() { 478 fn main() {
483 <|>if true { 479 if true {
484 foo(); 480 foo();
485 481
486 //comment 482 //comment