From 89e27ed0b9bbf2a70f959cd12294f8e32400a337 Mon Sep 17 00:00:00 2001 From: Amos Wenger Date: Wed, 25 May 2022 16:43:14 +0200 Subject: [PATCH] Generate variant: insert code in file with enum definition Closes #12372 --- .../ide-assists/src/handlers/generate_enum_variant.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/ide-assists/src/handlers/generate_enum_variant.rs b/crates/ide-assists/src/handlers/generate_enum_variant.rs index f3a79accf9..fa6242460b 100644 --- a/crates/ide-assists/src/handlers/generate_enum_variant.rs +++ b/crates/ide-assists/src/handlers/generate_enum_variant.rs @@ -1,4 +1,4 @@ -use hir::HasSource; +use hir::{HasSource, InFile}; use ide_db::assists::{AssistId, AssistKind}; use syntax::{ ast::{self, edit::IndentLevel}, @@ -63,10 +63,11 @@ fn add_variant_to_accumulator( adt: hir::Enum, name_ref: &ast::NameRef, ) -> Option<()> { - let adt_ast = adt.source(ctx.db())?.original_ast_node(ctx.db())?.value; - let enum_indent = IndentLevel::from_node(&adt_ast.syntax()); + let db = ctx.db(); + let InFile { file_id, value: enum_node } = adt.source(db)?.original_ast_node(db)?; + let enum_indent = IndentLevel::from_node(&enum_node.syntax()); - let variant_list = adt_ast.variant_list()?; + let variant_list = enum_node.variant_list()?; let offset = variant_list.syntax().text_range().end() - TextSize::of('}'); let empty_enum = variant_list.variants().next().is_none(); @@ -75,6 +76,7 @@ fn add_variant_to_accumulator( "Generate variant", target, |builder| { + builder.edit_file(file_id.original_file(db)); let text = format!( "{maybe_newline}{indent_1}{name},\n{enum_indent}", maybe_newline = if empty_enum { "\n" } else { "" },