aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorMikhail Rakhmanov <[email protected]>2020-06-05 12:17:17 +0100
committerMikhail Rakhmanov <[email protected]>2020-06-05 12:17:17 +0100
commit5dda9955380c6214aa5720ad640b76b870aaa556 (patch)
tree8f01ff6f3a1dc9a6e61b0b9bc0bc95cef74edc7c /crates
parent74c3e7a1adf9d14bbac5cbbe9cd893d758f82561 (diff)
Fix review comments
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs41
-rw-r--r--crates/ra_assists/src/utils/insert_use.rs11
2 files changed, 19 insertions, 33 deletions
diff --git a/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs b/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs
index ef963ddba..2c455a1fd 100644
--- a/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs
+++ b/crates/ra_assists/src/handlers/extract_struct_from_enum_variant.rs
@@ -1,6 +1,4 @@
1use ra_ide_db::{ 1use ra_ide_db::{defs::Definition, search::Reference, RootDatabase};
2 defs::Definition, imports_locator::ImportsLocator, search::Reference, RootDatabase,
3};
4use ra_syntax::{ 2use ra_syntax::{
5 algo::find_node_at_offset, 3 algo::find_node_at_offset,
6 ast::{self, AstNode, NameOwner}, 4 ast::{self, AstNode, NameOwner},
@@ -9,11 +7,11 @@ use ra_syntax::{
9 7
10use crate::{ 8use crate::{
11 assist_context::{AssistBuilder, AssistDirector}, 9 assist_context::{AssistBuilder, AssistDirector},
12 utils::insert_use::insert_use_statement_with_string_path, 10 utils::insert_use_statement,
13 AssistContext, AssistId, Assists, 11 AssistContext, AssistId, Assists,
14}; 12};
15use ast::{ArgListOwner, VisibilityOwner}; 13use ast::{ArgListOwner, VisibilityOwner};
16use hir::{EnumVariant, Module, ModuleDef}; 14use hir::{EnumVariant, Module, ModuleDef, Name};
17use ra_db::FileId; 15use ra_db::FileId;
18use ra_fmt::leading_indent; 16use ra_fmt::leading_indent;
19use rustc_hash::FxHashSet; 17use rustc_hash::FxHashSet;
@@ -46,11 +44,11 @@ pub(crate) fn extract_struct_from_enum_variant(
46 return None; 44 return None;
47 } 45 }
48 let enum_ast = variant.parent_enum(); 46 let enum_ast = variant.parent_enum();
49 let enum_name = enum_ast.name()?.to_string();
50 let visibility = enum_ast.visibility(); 47 let visibility = enum_ast.visibility();
51 let current_module_def = 48 let enum_hir = ctx.sema.to_def(&enum_ast)?;
52 ImportsLocator::new(ctx.db).find_imports(&enum_name).first()?.left()?; 49 let variant_hir_name = variant_hir.name(ctx.db);
53 let current_module = current_module_def.module(ctx.db)?; 50 let enum_module_def = ModuleDef::from(enum_hir);
51 let current_module = enum_hir.module(ctx.db);
54 let target = variant.syntax().text_range(); 52 let target = variant.syntax().text_range();
55 acc.add_in_multiple_files( 53 acc.add_in_multiple_files(
56 AssistId("extract_struct_from_enum_variant"), 54 AssistId("extract_struct_from_enum_variant"),
@@ -69,7 +67,8 @@ pub(crate) fn extract_struct_from_enum_variant(
69 edit, 67 edit,
70 reference, 68 reference,
71 &source_file, 69 &source_file,
72 &current_module_def, 70 &enum_module_def,
71 &variant_hir_name,
73 &mut visited_modules_set, 72 &mut visited_modules_set,
74 ); 73 );
75 } 74 }
@@ -102,20 +101,15 @@ fn insert_import(
102 builder: &mut AssistBuilder, 101 builder: &mut AssistBuilder,
103 path: &ast::PathExpr, 102 path: &ast::PathExpr,
104 module: &Module, 103 module: &Module,
105 module_def: &ModuleDef, 104 enum_module_def: &ModuleDef,
106 path_segment: ast::NameRef, 105 variant_hir_name: &Name,
107) -> Option<()> { 106) -> Option<()> {
108 let db = ctx.db; 107 let db = ctx.db;
109 let mod_path = module.find_use_path(db, module_def.clone()); 108 let mod_path = module.find_use_path(db, enum_module_def.clone());
110 if let Some(mut mod_path) = mod_path { 109 if let Some(mut mod_path) = mod_path {
111 mod_path.segments.pop(); 110 mod_path.segments.pop();
112 let use_path = format!("{}::{}", mod_path.to_string(), path_segment.to_string()); 111 mod_path.segments.push(variant_hir_name.clone());
113 insert_use_statement_with_string_path( 112 insert_use_statement(path.syntax(), &mod_path, ctx, builder.text_edit_builder());
114 path.syntax(),
115 &use_path,
116 ctx,
117 builder.text_edit_builder(),
118 );
119 } 113 }
120 Some(()) 114 Some(())
121} 115}
@@ -175,7 +169,8 @@ fn update_reference(
175 edit: &mut AssistDirector, 169 edit: &mut AssistDirector,
176 reference: Reference, 170 reference: Reference,
177 source_file: &SourceFile, 171 source_file: &SourceFile,
178 module_def: &ModuleDef, 172 enum_module_def: &ModuleDef,
173 variant_hir_name: &Name,
179 visited_modules_set: &mut FxHashSet<Module>, 174 visited_modules_set: &mut FxHashSet<Module>,
180) -> Option<()> { 175) -> Option<()> {
181 let path_expr: ast::PathExpr = find_node_at_offset::<ast::PathExpr>( 176 let path_expr: ast::PathExpr = find_node_at_offset::<ast::PathExpr>(
@@ -185,7 +180,6 @@ fn update_reference(
185 let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?; 180 let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?;
186 let list = call.arg_list()?; 181 let list = call.arg_list()?;
187 let segment = path_expr.path()?.segment()?; 182 let segment = path_expr.path()?.segment()?;
188 let segment_name = segment.name_ref()?;
189 let module = ctx.sema.scope(&path_expr.syntax()).module()?; 183 let module = ctx.sema.scope(&path_expr.syntax()).module()?;
190 let list_range = list.syntax().text_range(); 184 let list_range = list.syntax().text_range();
191 let inside_list_range = TextRange::new( 185 let inside_list_range = TextRange::new(
@@ -194,7 +188,8 @@ fn update_reference(
194 ); 188 );
195 edit.perform(reference.file_range.file_id, |builder| { 189 edit.perform(reference.file_range.file_id, |builder| {
196 if !visited_modules_set.contains(&module) { 190 if !visited_modules_set.contains(&module) {
197 if insert_import(ctx, builder, &path_expr, &module, module_def, segment_name).is_some() 191 if insert_import(ctx, builder, &path_expr, &module, enum_module_def, variant_hir_name)
192 .is_some()
198 { 193 {
199 visited_modules_set.insert(module); 194 visited_modules_set.insert(module);
200 } 195 }
diff --git a/crates/ra_assists/src/utils/insert_use.rs b/crates/ra_assists/src/utils/insert_use.rs
index 114f5949a..0ee43482f 100644
--- a/crates/ra_assists/src/utils/insert_use.rs
+++ b/crates/ra_assists/src/utils/insert_use.rs
@@ -23,16 +23,7 @@ pub(crate) fn insert_use_statement(
23 ctx: &AssistContext, 23 ctx: &AssistContext,
24 builder: &mut TextEditBuilder, 24 builder: &mut TextEditBuilder,
25) { 25) {
26 insert_use_statement_with_string_path(position, &path_to_import.to_string(), ctx, builder); 26 let target = path_to_import.to_string().split("::").map(SmolStr::new).collect::<Vec<_>>();
27}
28
29pub(crate) fn insert_use_statement_with_string_path(
30 position: &SyntaxNode,
31 path_to_import: &str,
32 ctx: &AssistContext,
33 builder: &mut TextEditBuilder,
34) {
35 let target = path_to_import.split("::").map(SmolStr::new).collect::<Vec<_>>();
36 let container = ctx.sema.ancestors_with_macros(position.clone()).find_map(|n| { 27 let container = ctx.sema.ancestors_with_macros(position.clone()).find_map(|n| {
37 if let Some(module) = ast::Module::cast(n.clone()) { 28 if let Some(module) = ast::Module::cast(n.clone()) {
38 return module.item_list().map(|it| it.syntax().clone()); 29 return module.item_list().map(|it| it.syntax().clone());