}
}
- pub(crate) fn make_ast_mut<N: AstNode>(&mut self, node: N) -> N {
- N::cast(self.make_mut(node.syntax().clone())).unwrap()
+ pub(crate) fn make_mut<N: AstNode>(&mut self, node: N) -> N {
+ self.mutated_tree.get_or_insert_with(|| TreeMutator::new(node.syntax())).make_mut(&node)
}
/// Returns a copy of the `node`, suitable for mutation.
///
/// The typical pattern for an assist is to find specific nodes in the read
/// phase, and then get their mutable couterparts using `make_mut` in the
/// mutable state.
- pub(crate) fn make_mut(&mut self, node: SyntaxNode) -> SyntaxNode {
+ pub(crate) fn make_syntax_mut(&mut self, node: SyntaxNode) -> SyntaxNode {
self.mutated_tree.get_or_insert_with(|| TreeMutator::new(&node)).make_syntax_mut(&node)
}
range,
|builder| {
let scope = match scope.clone() {
- ImportScope::File(it) => ImportScope::File(builder.make_ast_mut(it)),
- ImportScope::Module(it) => ImportScope::Module(builder.make_ast_mut(it)),
+ ImportScope::File(it) => ImportScope::File(builder.make_mut(it)),
+ ImportScope::Module(it) => ImportScope::Module(builder.make_mut(it)),
};
insert_use(&scope, mod_path_to_ast(&import.import_path), ctx.config.insert_use);
},
"Expand glob import",
target.text_range(),
|builder| {
- let use_tree = builder.make_ast_mut(use_tree);
+ let use_tree = builder.make_mut(use_tree);
let names_to_import = find_names_to_import(ctx, refs_in_target, imported_defs);
let expanded = make::use_tree_list(names_to_import.iter().map(|n| {
continue;
}
builder.edit_file(file_id);
- let source_file = builder.make_ast_mut(ctx.sema.parse(file_id));
+ let source_file = builder.make_mut(ctx.sema.parse(file_id));
let processed = process_references(
ctx,
&mut visited_modules_set,
});
}
builder.edit_file(ctx.frange.file_id);
- let source_file = builder.make_ast_mut(ctx.sema.parse(ctx.frange.file_id));
- let variant = builder.make_ast_mut(variant.clone());
+ let source_file = builder.make_mut(ctx.sema.parse(ctx.frange.file_id));
+ let variant = builder.make_mut(variant.clone());
if let Some(references) = def_file_references {
let processed = process_references(
ctx,
}
};
acc.add(AssistId(ASSIST_NAME, AssistKind::Refactor), ASSIST_LABEL, lifetime_loc, |builder| {
- let fn_def = builder.make_ast_mut(fn_def);
- let lifetime = builder.make_ast_mut(lifetime);
+ let fn_def = builder.make_mut(fn_def);
+ let lifetime = builder.make_mut(lifetime);
let loc_needing_lifetime =
loc_needing_lifetime.and_then(|it| it.make_mut(builder).to_position());
) -> Option<()> {
let new_lifetime_param = generate_unique_lifetime_param_name(impl_def.generic_param_list())?;
acc.add(AssistId(ASSIST_NAME, AssistKind::Refactor), ASSIST_LABEL, lifetime_loc, |builder| {
- let impl_def = builder.make_ast_mut(impl_def);
- let lifetime = builder.make_ast_mut(lifetime);
+ let impl_def = builder.make_mut(impl_def);
+ let lifetime = builder.make_mut(lifetime);
impl_def.get_or_create_generic_param_list().add_generic_param(
make::lifetime_param(new_lifetime_param.clone()).clone_for_update().into(),
impl NeedsLifetime {
fn make_mut(self, builder: &mut AssistBuilder) -> Self {
match self {
- Self::SelfParam(it) => Self::SelfParam(builder.make_ast_mut(it)),
- Self::RefType(it) => Self::RefType(builder.make_ast_mut(it)),
+ Self::SelfParam(it) => Self::SelfParam(builder.make_mut(it)),
+ Self::RefType(it) => Self::RefType(builder.make_mut(it)),
}
}
target,
|builder| {
if let Some((to_replace, replacement, to_remove)) = imports {
- let to_replace = builder.make_ast_mut(to_replace);
- let to_remove = builder.make_ast_mut(to_remove);
+ let to_replace = builder.make_mut(to_replace);
+ let to_remove = builder.make_mut(to_remove);
ted::replace(to_replace.syntax(), replacement.syntax());
to_remove.remove();
}
if let Some((to_replace, replacement, to_remove)) = uses {
- let to_replace = builder.make_ast_mut(to_replace);
- let to_remove = builder.make_ast_mut(to_remove);
+ let to_replace = builder.make_mut(to_replace);
+ let to_remove = builder.make_mut(to_remove);
ted::replace(to_replace.syntax(), replacement.syntax());
to_remove.remove()
"Move to where clause",
target,
|edit| {
- let type_param_list = edit.make_ast_mut(type_param_list);
- let parent = edit.make_mut(parent);
+ let type_param_list = edit.make_mut(type_param_list);
+ let parent = edit.make_syntax_mut(parent);
let where_clause: ast::WhereClause = match_ast! {
match parent {
let assignments: Vec<_> = collector
.assignments
.into_iter()
- .map(|(stmt, rhs)| (edit.make_ast_mut(stmt), rhs.clone_for_update()))
+ .map(|(stmt, rhs)| (edit.make_mut(stmt), rhs.clone_for_update()))
.collect();
- let tgt = edit.make_ast_mut(tgt);
+ let tgt = edit.make_mut(tgt);
for (stmt, rhs) in assignments {
let mut stmt = stmt.syntax().clone();
target,
|builder| match fields {
Either::Left((sorted, field_list)) => {
- replace(builder.make_ast_mut(field_list).fields(), sorted)
+ replace(builder.make_mut(field_list).fields(), sorted)
}
Either::Right((sorted, field_list)) => {
- replace(builder.make_ast_mut(field_list).fields(), sorted)
+ replace(builder.make_mut(field_list).fields(), sorted)
}
},
)
"Sort methods",
target,
|builder| {
- let methods =
- methods.into_iter().map(|fn_| builder.make_ast_mut(fn_)).collect::<Vec<_>>();
+ let methods = methods.into_iter().map(|fn_| builder.make_mut(fn_)).collect::<Vec<_>>();
methods
.into_iter()
.zip(sorted)
"Replace impl trait with generic",
target,
|edit| {
- let impl_trait_type = edit.make_ast_mut(impl_trait_type);
- let fn_ = edit.make_ast_mut(fn_);
+ let impl_trait_type = edit.make_mut(impl_trait_type);
+ let fn_ = edit.make_mut(fn_);
let type_param_name = suggest_name::for_generic_parameter(&impl_trait_type);
|builder| {
// Now that we've brought the name into scope, re-qualify all paths that could be
// affected (that is, all paths inside the node we added the `use` to).
- let syntax = builder.make_mut(syntax.clone());
+ let syntax = builder.make_syntax_mut(syntax.clone());
if let Some(ref import_scope) = ImportScope::from(syntax.clone()) {
shorten_paths(&syntax, &path.clone_for_update());
insert_use(import_scope, path, ctx.config.insert_use);