convert generate_enum_variant to use add_variant

This commit is contained in:
Dorian Scheidt 2022-07-20 12:11:00 -05:00
parent 405dd77d30
commit bea1fec7a2

View file

@ -1,8 +1,8 @@
use hir::{HasSource, InFile}; use hir::{HasSource, InFile};
use ide_db::assists::{AssistId, AssistKind}; use ide_db::assists::{AssistId, AssistKind};
use syntax::{ use syntax::{
ast::{self, edit::IndentLevel}, ast::{self, make},
AstNode, TextSize, AstNode,
}; };
use crate::assist_context::{AssistContext, Assists}; use crate::assist_context::{AssistContext, Assists};
@ -65,26 +65,16 @@ fn add_variant_to_accumulator(
) -> Option<()> { ) -> Option<()> {
let db = ctx.db(); let db = ctx.db();
let InFile { file_id, value: enum_node } = adt.source(db)?.original_ast_node(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 = enum_node.variant_list()?;
let offset = variant_list.syntax().text_range().end() - TextSize::of('}');
let empty_enum = variant_list.variants().next().is_none();
let variant = make::variant(make::name(&name_ref.text()), None);
acc.add( acc.add(
AssistId("generate_enum_variant", AssistKind::Generate), AssistId("generate_enum_variant", AssistKind::Generate),
"Generate variant", "Generate variant",
target, target,
|builder| { |builder| {
builder.edit_file(file_id.original_file(db)); builder.edit_file(file_id.original_file(db));
let text = format!( let node = builder.make_mut(enum_node);
"{maybe_newline}{indent_1}{name},\n{enum_indent}", node.variant_list().map(|it| it.add_variant(variant.clone_for_update()));
maybe_newline = if empty_enum { "\n" } else { "" },
indent_1 = IndentLevel(1),
name = name_ref,
enum_indent = enum_indent
);
builder.insert(offset, text)
}, },
) )
} }