Migrate generate_delegate_methods to mutable ast

This commit is contained in:
DropDemBits 2023-06-25 00:06:30 -04:00
parent f8b6b4cc0b
commit 58e2053327
No known key found for this signature in database
GPG Key ID: 7FE02A6C1EDFA075

View File

@ -1,13 +1,18 @@
use std::collections::HashSet; use std::collections::HashSet;
use hir::{self, HasCrate, HasSource, HasVisibility}; use hir::{self, HasCrate, HasSource, HasVisibility};
use syntax::ast::{self, make, AstNode, HasGenericParams, HasName, HasVisibility as _}; use syntax::{
ast::{
self, edit::IndentLevel, edit_in_place::Indent, make, AstNode, HasGenericParams, HasName,
HasVisibility as _,
},
ted,
};
use crate::{ use crate::{
utils::{convert_param_list_to_arg_list, find_struct_impl, render_snippet, Cursor}, utils::{convert_param_list_to_arg_list, find_struct_impl},
AssistContext, AssistId, AssistKind, Assists, GroupLabel, AssistContext, AssistId, AssistKind, Assists, GroupLabel,
}; };
use syntax::ast::edit::AstNodeEdit;
// Assist: generate_delegate_methods // Assist: generate_delegate_methods
// //
@ -96,7 +101,7 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
AssistId("generate_delegate_methods", AssistKind::Generate), AssistId("generate_delegate_methods", AssistKind::Generate),
format!("Generate delegate for `{field_name}.{name}()`",), format!("Generate delegate for `{field_name}.{name}()`",),
target, target,
|builder| { |edit| {
// Create the function // Create the function
let method_source = match method.source(ctx.db()) { let method_source = match method.source(ctx.db()) {
Some(source) => source.value, Some(source) => source.value,
@ -135,32 +140,25 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
is_const, is_const,
is_unsafe, is_unsafe,
) )
.indent(ast::edit::IndentLevel(1))
.clone_for_update(); .clone_for_update();
f.reindent_to(IndentLevel(1));
let cursor = Cursor::Before(f.syntax());
// Create or update an impl block, attach the function to it, // Create or update an impl block, attach the function to it,
// then insert into our code. // then insert into our code.
match impl_def { match impl_def {
Some(impl_def) => { Some(impl_def) => {
// Remember where in our source our `impl` block lives. // Remember where in our source our `impl` block lives.
let impl_def = impl_def.clone_for_update(); let impl_def = edit.make_mut(impl_def);
let old_range = impl_def.syntax().text_range();
// Attach the function to the impl block // Attach the function to the impl block.
let assoc_items = impl_def.get_or_create_assoc_item_list(); let assoc_items = impl_def.get_or_create_assoc_item_list();
assoc_items.add_item(f.clone().into()); assoc_items.add_item(f.clone().into());
// Update the impl block. // Update the impl block.
match ctx.config.snippet_cap { ted::replace(impl_def.syntax(), impl_def.syntax());
Some(cap) => {
let snippet = render_snippet(cap, impl_def.syntax(), cursor); if let Some(cap) = ctx.config.snippet_cap {
builder.replace_snippet(cap, old_range, snippet); edit.add_tabstop_before(cap, f);
}
None => {
builder.replace(old_range, impl_def.syntax().to_string());
}
} }
} }
None => { None => {
@ -178,22 +176,22 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
None, None,
) )
.clone_for_update(); .clone_for_update();
let assoc_items = impl_def.get_or_create_assoc_item_list(); let assoc_items = impl_def.get_or_create_assoc_item_list();
assoc_items.add_item(f.clone().into()); assoc_items.add_item(f.clone().into());
// Insert the impl block. // Insert the impl block.
match ctx.config.snippet_cap { let strukt = edit.make_mut(strukt.clone());
Some(cap) => { ted::insert_all(
let offset = strukt.syntax().text_range().end(); ted::Position::after(strukt.syntax()),
let snippet = render_snippet(cap, impl_def.syntax(), cursor); vec![
let snippet = format!("\n\n{snippet}"); make::tokens::blank_line().into(),
builder.insert_snippet(cap, offset, snippet); impl_def.syntax().clone().into(),
} ],
None => { );
let offset = strukt.syntax().text_range().end();
let snippet = format!("\n\n{}", impl_def.syntax()); if let Some(cap) = ctx.config.snippet_cap {
builder.insert(offset, snippet); edit.add_tabstop_before(cap, f)
}
} }
} }
} }