aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_assists/src/assist_ctx.rs4
-rw-r--r--crates/ra_assists/src/auto_import.rs40
2 files changed, 36 insertions, 8 deletions
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs
index e80e35738..17a9041c5 100644
--- a/crates/ra_assists/src/assist_ctx.rs
+++ b/crates/ra_assists/src/assist_ctx.rs
@@ -144,6 +144,10 @@ impl AssistBuilder {
144 self.replace(node.range(), replace_with) 144 self.replace(node.range(), replace_with)
145 } 145 }
146 146
147 pub(crate) fn set_edit_builder(&mut self, edit: TextEditBuilder) {
148 self.edit = edit;
149 }
150
147 #[allow(unused)] 151 #[allow(unused)]
148 pub(crate) fn delete(&mut self, range: TextRange) { 152 pub(crate) fn delete(&mut self, range: TextRange) {
149 self.edit.delete(range) 153 self.edit.delete(range)
diff --git a/crates/ra_assists/src/auto_import.rs b/crates/ra_assists/src/auto_import.rs
index 3fdf6b0d9..6bc5caf76 100644
--- a/crates/ra_assists/src/auto_import.rs
+++ b/crates/ra_assists/src/auto_import.rs
@@ -1,3 +1,4 @@
1use ra_text_edit::TextEditBuilder;
1use hir::db::HirDatabase; 2use hir::db::HirDatabase;
2 3
3use ra_syntax::{ 4use ra_syntax::{
@@ -374,7 +375,7 @@ fn best_action_for_target<'b, 'a: 'b>(
374 } 375 }
375} 376}
376 377
377fn make_assist(action: &ImportAction, target: &[&ast::PathSegment], edit: &mut AssistBuilder) { 378fn make_assist(action: &ImportAction, target: &[&ast::PathSegment], edit: &mut TextEditBuilder) {
378 match action { 379 match action {
379 ImportAction::AddNewUse { anchor, add_after_anchor } => { 380 ImportAction::AddNewUse { anchor, add_after_anchor } => {
380 make_assist_add_new_use(anchor, *add_after_anchor, target, edit) 381 make_assist_add_new_use(anchor, *add_after_anchor, target, edit)
@@ -408,7 +409,7 @@ fn make_assist_add_new_use(
408 anchor: &Option<&SyntaxNode>, 409 anchor: &Option<&SyntaxNode>,
409 after: bool, 410 after: bool,
410 target: &[&ast::PathSegment], 411 target: &[&ast::PathSegment],
411 edit: &mut AssistBuilder, 412 edit: &mut TextEditBuilder,
412) { 413) {
413 if let Some(anchor) = anchor { 414 if let Some(anchor) = anchor {
414 let indent = ra_fmt::leading_indent(anchor); 415 let indent = ra_fmt::leading_indent(anchor);
@@ -437,7 +438,7 @@ fn make_assist_add_in_tree_list(
437 tree_list: &ast::UseTreeList, 438 tree_list: &ast::UseTreeList,
438 target: &[&ast::PathSegment], 439 target: &[&ast::PathSegment],
439 add_self: bool, 440 add_self: bool,
440 edit: &mut AssistBuilder, 441 edit: &mut TextEditBuilder,
441) { 442) {
442 let last = tree_list.use_trees().last(); 443 let last = tree_list.use_trees().last();
443 if let Some(last) = last { 444 if let Some(last) = last {
@@ -466,7 +467,7 @@ fn make_assist_add_nested_import(
466 first_segment_to_split: &Option<&ast::PathSegment>, 467 first_segment_to_split: &Option<&ast::PathSegment>,
467 target: &[&ast::PathSegment], 468 target: &[&ast::PathSegment],
468 add_self: bool, 469 add_self: bool,
469 edit: &mut AssistBuilder, 470 edit: &mut TextEditBuilder,
470) { 471) {
471 let use_tree = path.syntax().ancestors().find_map(ast::UseTree::cast); 472 let use_tree = path.syntax().ancestors().find_map(ast::UseTree::cast);
472 if let Some(use_tree) = use_tree { 473 if let Some(use_tree) = use_tree {
@@ -491,7 +492,7 @@ fn make_assist_add_nested_import(
491 buf.push_str(", "); 492 buf.push_str(", ");
492 } 493 }
493 edit.insert(start, buf); 494 edit.insert(start, buf);
494 edit.insert(end, "}"); 495 edit.insert(end, "}".to_string());
495 } 496 }
496} 497}
497 498
@@ -499,7 +500,7 @@ fn apply_auto_import<'a>(
499 container: &SyntaxNode, 500 container: &SyntaxNode,
500 path: &ast::Path, 501 path: &ast::Path,
501 target: &[&'a ast::PathSegment], 502 target: &[&'a ast::PathSegment],
502 edit: &mut AssistBuilder, 503 edit: &mut TextEditBuilder,
503) { 504) {
504 let action = best_action_for_target(container, path, target); 505 let action = best_action_for_target(container, path, target);
505 make_assist(&action, target, edit); 506 make_assist(&action, target, edit);
@@ -513,6 +514,25 @@ fn apply_auto_import<'a>(
513 } 514 }
514} 515}
515 516
517pub fn auto_import_text_edit<'a>(
518 position: &SyntaxNode,
519 path: &ast::Path,
520 target: &[&'a ast::PathSegment],
521 edit: &mut TextEditBuilder,
522) {
523 let container = position.ancestors().find_map(|n| {
524 if let Some(module) = ast::Module::cast(n) {
525 return module.item_list().map(ast::AstNode::syntax);
526 }
527 ast::SourceFile::cast(n).map(ast::AstNode::syntax)
528 });
529
530 if let Some(container) = container {
531 let action = best_action_for_target(container, path, target);
532 make_assist(&action, target, edit);
533 }
534}
535
516pub(crate) fn auto_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 536pub(crate) fn auto_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
517 let path: &ast::Path = ctx.node_at_offset()?; 537 let path: &ast::Path = ctx.node_at_offset()?;
518 // We don't want to mess with use statements 538 // We don't want to mess with use statements
@@ -531,7 +551,9 @@ pub(crate) fn auto_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist
531 AssistId("auto_import"), 551 AssistId("auto_import"),
532 format!("import {} in mod {}", fmt_segments(&segments), name.text()), 552 format!("import {} in mod {}", fmt_segments(&segments), name.text()),
533 |edit| { 553 |edit| {
534 apply_auto_import(item_list.syntax(), path, &segments, edit); 554 let mut text_edit = TextEditBuilder::default();
555 apply_auto_import(item_list.syntax(), path, &segments, &mut text_edit);
556 edit.set_edit_builder(text_edit);
535 }, 557 },
536 ); 558 );
537 } 559 }
@@ -541,7 +563,9 @@ pub(crate) fn auto_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist
541 AssistId("auto_import"), 563 AssistId("auto_import"),
542 format!("import {} in the current file", fmt_segments(&segments)), 564 format!("import {} in the current file", fmt_segments(&segments)),
543 |edit| { 565 |edit| {
544 apply_auto_import(current_file.syntax(), path, &segments, edit); 566 let mut text_edit = TextEditBuilder::default();
567 apply_auto_import(current_file.syntax(), path, &segments, &mut text_edit);
568 edit.set_edit_builder(text_edit);
545 }, 569 },
546 ); 570 );
547 } 571 }