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 ide_db::assists::{AssistId, AssistKind};
use syntax::{
ast::{self, edit::IndentLevel},
AstNode, TextSize,
ast::{self, make},
AstNode,
};
use crate::assist_context::{AssistContext, Assists};
@ -65,26 +65,16 @@ fn add_variant_to_accumulator(
) -> Option<()> {
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 = 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(
AssistId("generate_enum_variant", AssistKind::Generate),
"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 { "" },
indent_1 = IndentLevel(1),
name = name_ref,
enum_indent = enum_indent
);
builder.insert(offset, text)
let node = builder.make_mut(enum_node);
node.variant_list().map(|it| it.add_variant(variant.clone_for_update()));
},
)
}