diff options
Diffstat (limited to 'crates/assists/src/handlers')
3 files changed, 21 insertions, 18 deletions
diff --git a/crates/assists/src/handlers/auto_import.rs b/crates/assists/src/handlers/auto_import.rs index f3b4c5708..66e819154 100644 --- a/crates/assists/src/handlers/auto_import.rs +++ b/crates/assists/src/handlers/auto_import.rs | |||
@@ -1,11 +1,13 @@ | |||
1 | use std::collections::BTreeSet; | 1 | use std::collections::BTreeSet; |
2 | 2 | ||
3 | use ast::make; | ||
3 | use either::Either; | 4 | use either::Either; |
4 | use hir::{ | 5 | use hir::{ |
5 | AsAssocItem, AssocItemContainer, ModPath, Module, ModuleDef, PathResolution, Semantics, Trait, | 6 | AsAssocItem, AssocItemContainer, ModPath, Module, ModuleDef, PathResolution, Semantics, Trait, |
6 | Type, | 7 | Type, |
7 | }; | 8 | }; |
8 | use ide_db::{imports_locator, RootDatabase}; | 9 | use ide_db::{imports_locator, RootDatabase}; |
10 | use insert_use::ImportScope; | ||
9 | use rustc_hash::FxHashSet; | 11 | use rustc_hash::FxHashSet; |
10 | use syntax::{ | 12 | use syntax::{ |
11 | ast::{self, AstNode}, | 13 | ast::{self, AstNode}, |
@@ -16,8 +18,6 @@ use crate::{ | |||
16 | utils::{insert_use, MergeBehaviour}, | 18 | utils::{insert_use, MergeBehaviour}, |
17 | AssistContext, AssistId, AssistKind, Assists, GroupLabel, | 19 | AssistContext, AssistId, AssistKind, Assists, GroupLabel, |
18 | }; | 20 | }; |
19 | use ast::make; | ||
20 | use insert_use::find_insert_use_container; | ||
21 | 21 | ||
22 | // Assist: auto_import | 22 | // Assist: auto_import |
23 | // | 23 | // |
@@ -47,8 +47,9 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> | |||
47 | 47 | ||
48 | let range = ctx.sema.original_range(&auto_import_assets.syntax_under_caret).range; | 48 | let range = ctx.sema.original_range(&auto_import_assets.syntax_under_caret).range; |
49 | let group = auto_import_assets.get_import_group_message(); | 49 | let group = auto_import_assets.get_import_group_message(); |
50 | let container = find_insert_use_container(&auto_import_assets.syntax_under_caret, ctx)?; | 50 | let scope = |
51 | let syntax = container.either(|l| l.syntax().clone(), |r| r.syntax().clone()); | 51 | ImportScope::find_insert_use_container(&auto_import_assets.syntax_under_caret, ctx)?; |
52 | let syntax = scope.as_syntax_node(); | ||
52 | for import in proposed_imports { | 53 | for import in proposed_imports { |
53 | acc.add_group( | 54 | acc.add_group( |
54 | &group, | 55 | &group, |
@@ -57,7 +58,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> | |||
57 | range, | 58 | range, |
58 | |builder| { | 59 | |builder| { |
59 | let new_syntax = insert_use( | 60 | let new_syntax = insert_use( |
60 | &syntax, | 61 | &scope, |
61 | make::path_from_text(&import.to_string()), | 62 | make::path_from_text(&import.to_string()), |
62 | Some(MergeBehaviour::Full), | 63 | Some(MergeBehaviour::Full), |
63 | ); | 64 | ); |
diff --git a/crates/assists/src/handlers/extract_struct_from_enum_variant.rs b/crates/assists/src/handlers/extract_struct_from_enum_variant.rs index d9c50f25f..a50a57f1a 100644 --- a/crates/assists/src/handlers/extract_struct_from_enum_variant.rs +++ b/crates/assists/src/handlers/extract_struct_from_enum_variant.rs | |||
@@ -15,7 +15,7 @@ use crate::{ | |||
15 | AssistContext, AssistId, AssistKind, Assists, | 15 | AssistContext, AssistId, AssistKind, Assists, |
16 | }; | 16 | }; |
17 | use ast::make; | 17 | use ast::make; |
18 | use insert_use::find_insert_use_container; | 18 | use insert_use::ImportScope; |
19 | 19 | ||
20 | // Assist: extract_struct_from_enum_variant | 20 | // Assist: extract_struct_from_enum_variant |
21 | // | 21 | // |
@@ -110,11 +110,11 @@ fn insert_import( | |||
110 | if let Some(mut mod_path) = mod_path { | 110 | if let Some(mut mod_path) = mod_path { |
111 | mod_path.segments.pop(); | 111 | mod_path.segments.pop(); |
112 | mod_path.segments.push(variant_hir_name.clone()); | 112 | mod_path.segments.push(variant_hir_name.clone()); |
113 | let container = find_insert_use_container(path.syntax(), ctx)?; | 113 | let scope = ImportScope::find_insert_use_container(path.syntax(), ctx)?; |
114 | let syntax = container.either(|l| l.syntax().clone(), |r| r.syntax().clone()); | 114 | let syntax = scope.as_syntax_node(); |
115 | 115 | ||
116 | let new_syntax = insert_use( | 116 | let new_syntax = insert_use( |
117 | &syntax, | 117 | &scope, |
118 | make::path_from_text(&mod_path.to_string()), | 118 | make::path_from_text(&mod_path.to_string()), |
119 | Some(MergeBehaviour::Full), | 119 | Some(MergeBehaviour::Full), |
120 | ); | 120 | ); |
diff --git a/crates/assists/src/handlers/replace_qualified_name_with_use.rs b/crates/assists/src/handlers/replace_qualified_name_with_use.rs index 597bc268c..85c70d16b 100644 --- a/crates/assists/src/handlers/replace_qualified_name_with_use.rs +++ b/crates/assists/src/handlers/replace_qualified_name_with_use.rs | |||
@@ -2,7 +2,7 @@ use syntax::{algo::SyntaxRewriter, ast, match_ast, AstNode, SyntaxNode, TextRang | |||
2 | use test_utils::mark; | 2 | use test_utils::mark; |
3 | 3 | ||
4 | use crate::{ | 4 | use crate::{ |
5 | utils::{find_insert_use_container, insert_use, MergeBehaviour}, | 5 | utils::{insert_use, ImportScope, MergeBehaviour}, |
6 | AssistContext, AssistId, AssistKind, Assists, | 6 | AssistContext, AssistId, AssistKind, Assists, |
7 | }; | 7 | }; |
8 | use ast::make; | 8 | use ast::make; |
@@ -44,8 +44,8 @@ pub(crate) fn replace_qualified_name_with_use( | |||
44 | }; | 44 | }; |
45 | 45 | ||
46 | let target = path.syntax().text_range(); | 46 | let target = path.syntax().text_range(); |
47 | let container = find_insert_use_container(path.syntax(), ctx)?; | 47 | let scope = ImportScope::find_insert_use_container(path.syntax(), ctx)?; |
48 | let syntax = container.either(|l| l.syntax().clone(), |r| r.syntax().clone()); | 48 | let syntax = scope.as_syntax_node(); |
49 | acc.add( | 49 | acc.add( |
50 | AssistId("replace_qualified_name_with_use", AssistKind::RefactorRewrite), | 50 | AssistId("replace_qualified_name_with_use", AssistKind::RefactorRewrite), |
51 | "Replace qualified path with use", | 51 | "Replace qualified path with use", |
@@ -56,12 +56,14 @@ pub(crate) fn replace_qualified_name_with_use( | |||
56 | let mut rewriter = SyntaxRewriter::default(); | 56 | let mut rewriter = SyntaxRewriter::default(); |
57 | shorten_paths(&mut rewriter, syntax.clone(), path); | 57 | shorten_paths(&mut rewriter, syntax.clone(), path); |
58 | let rewritten_syntax = rewriter.rewrite(&syntax); | 58 | let rewritten_syntax = rewriter.rewrite(&syntax); |
59 | let new_syntax = insert_use( | 59 | if let Some(ref import_scope) = ImportScope::from(rewritten_syntax) { |
60 | &rewritten_syntax, | 60 | let new_syntax = insert_use( |
61 | make::path_from_text(path_to_import), | 61 | import_scope, |
62 | Some(MergeBehaviour::Full), | 62 | make::path_from_text(path_to_import), |
63 | ); | 63 | Some(MergeBehaviour::Full), |
64 | builder.replace(syntax.text_range(), new_syntax.to_string()) | 64 | ); |
65 | builder.replace(syntax.text_range(), new_syntax.to_string()) | ||
66 | } | ||
65 | }, | 67 | }, |
66 | ) | 68 | ) |
67 | } | 69 | } |