]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide_assists/src/handlers/extract_struct_from_enum_variant.rs
Merge #11481
[rust.git] / crates / ide_assists / src / handlers / extract_struct_from_enum_variant.rs
index 961158811034dfaba2bb8413f48aab4fe8a4b9d2..82e0970cc4bf84cb6e8fb508c9fafe61035b5ef6 100644 (file)
@@ -15,8 +15,8 @@
 use rustc_hash::FxHashSet;
 use syntax::{
     ast::{
-        self, edit::IndentLevel, edit_in_place::Indent, make, AstNode, AttrsOwner,
-        GenericParamsOwner, NameOwner, TypeBoundsOwner, VisibilityOwner,
+        self, edit::IndentLevel, edit_in_place::Indent, make, AstNode, HasAttrs, HasGenericParams,
+        HasName, HasTypeBounds, HasVisibility,
     },
     match_ast,
     ted::{self, Position},
@@ -63,8 +63,7 @@ pub(crate) fn extract_struct_from_enum_variant(
         |builder| {
             let variant_hir_name = variant_hir.name(ctx.db());
             let enum_module_def = ModuleDef::from(enum_hir);
-            let usages =
-                Definition::ModuleDef(ModuleDef::Variant(variant_hir)).usages(&ctx.sema).all();
+            let usages = Definition::Variant(variant_hir).usages(&ctx.sema).all();
 
             let mut visited_modules_set = FxHashSet::default();
             let current_module = enum_hir.module(ctx.db());
@@ -72,7 +71,7 @@ pub(crate) fn extract_struct_from_enum_variant(
             // record file references of the file the def resides in, we only want to swap to the edited file in the builder once
             let mut def_file_references = None;
             for (file_id, references) in usages {
-                if file_id == ctx.frange.file_id {
+                if file_id == ctx.file_id() {
                     def_file_references = Some(references);
                     continue;
                 }
@@ -89,7 +88,7 @@ pub(crate) fn extract_struct_from_enum_variant(
                     apply_references(ctx.config.insert_use, path, node, import)
                 });
             }
-            builder.edit_file(ctx.frange.file_id);
+            builder.edit_file(ctx.file_id());
 
             let variant = builder.make_mut(variant.clone());
             if let Some(references) = def_file_references {
@@ -231,7 +230,7 @@ fn create_struct_def(
     let variant_attrs = attrs_and_docs(variant.syntax())
         .map(|tok| match tok.kind() {
             WHITESPACE => make::tokens::single_newline().into(),
-            _ => tok.into(),
+            _ => tok,
         })
         .collect();
     ted::insert_all(Position::first_child_of(strukt.syntax()), variant_attrs);
@@ -251,12 +250,14 @@ fn update_variant(variant: &ast::Variant, generic: Option<ast::GenericParamList>
         Some(gpl) => {
             let gpl = gpl.clone_for_update();
             gpl.generic_params().for_each(|gp| {
-                match gp {
+                let tbl = match gp {
                     ast::GenericParam::LifetimeParam(it) => it.type_bound_list(),
                     ast::GenericParam::TypeParam(it) => it.type_bound_list(),
                     ast::GenericParam::ConstParam(_) => return,
+                };
+                if let Some(tbl) = tbl {
+                    tbl.remove();
                 }
-                .map(|it| it.remove());
             });
             make::ty(&format!("{}<{}>", name.text(), gpl.generic_params().join(", ")))
         }
@@ -312,7 +313,7 @@ fn process_references(
                 if let Some(mut mod_path) = mod_path {
                     mod_path.pop_segment();
                     mod_path.push_segment(variant_hir_name.clone());
-                    let scope = ImportScope::find_insert_use_container(&scope_node)?;
+                    let scope = ImportScope::find_insert_use_container(&scope_node, &ctx.sema)?;
                     visited_modules.insert(module);
                     return Some((segment, scope_node, Some((scope, mod_path))));
                 }