Migrate generate_impl assist to use SyntaxEditor

This commit is contained in:
Hayashi Mikihiro 2025-07-10 18:11:32 +09:00
parent e9968fc555
commit c3a5a8c22a

View file

@ -1,14 +1,14 @@
use syntax::{ use syntax::{
ast::{self, AstNode, HasName, edit_in_place::Indent, make}, ast::{self, AstNode, HasName, edit_in_place::Indent, make},
ted, syntax_editor::{Position, SyntaxEditor},
}; };
use crate::{AssistContext, AssistId, Assists, utils}; use crate::{AssistContext, AssistId, Assists, utils};
fn insert_impl(impl_: ast::Impl, nominal: &ast::Adt) { fn insert_impl(editor: &mut SyntaxEditor, impl_: &ast::Impl, nominal: &ast::Adt) {
let indent = nominal.indent_level(); let indent = nominal.indent_level();
ted::insert_all_raw( editor.insert_all(
ted::Position::after(nominal.syntax()), Position::after(nominal.syntax()),
vec![ vec![
// Add a blank line after the ADT, and indentation for the impl to match the ADT // Add a blank line after the ADT, and indentation for the impl to match the ADT
make::tokens::whitespace(&format!("\n\n{indent}")).into(), make::tokens::whitespace(&format!("\n\n{indent}")).into(),
@ -51,14 +51,17 @@ pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
// Generate the impl // Generate the impl
let impl_ = utils::generate_impl(&nominal); let impl_ = utils::generate_impl(&nominal);
let mut editor = edit.make_editor(nominal.syntax());
// Add a tabstop after the left curly brace // Add a tabstop after the left curly brace
if let Some(cap) = ctx.config.snippet_cap { if let Some(cap) = ctx.config.snippet_cap {
if let Some(l_curly) = impl_.assoc_item_list().and_then(|it| it.l_curly_token()) { if let Some(l_curly) = impl_.assoc_item_list().and_then(|it| it.l_curly_token()) {
edit.add_tabstop_after_token(cap, l_curly); let tabstop = edit.make_tabstop_after(cap);
editor.add_annotation(l_curly, tabstop);
} }
} }
insert_impl(impl_, &edit.make_mut(nominal)); insert_impl(&mut editor, &impl_, &nominal);
edit.add_file_edits(ctx.vfs_file_id(), editor);
}, },
) )
} }
@ -97,18 +100,22 @@ pub(crate) fn generate_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) ->
// Generate the impl // Generate the impl
let impl_ = utils::generate_trait_impl_intransitive(&nominal, make::ty_placeholder()); let impl_ = utils::generate_trait_impl_intransitive(&nominal, make::ty_placeholder());
let mut editor = edit.make_editor(nominal.syntax());
// Make the trait type a placeholder snippet // Make the trait type a placeholder snippet
if let Some(cap) = ctx.config.snippet_cap { if let Some(cap) = ctx.config.snippet_cap {
if let Some(trait_) = impl_.trait_() { if let Some(trait_) = impl_.trait_() {
edit.add_placeholder_snippet(cap, trait_); let placeholder = edit.make_placeholder_snippet(cap);
editor.add_annotation(trait_.syntax(), placeholder);
} }
if let Some(l_curly) = impl_.assoc_item_list().and_then(|it| it.l_curly_token()) { if let Some(l_curly) = impl_.assoc_item_list().and_then(|it| it.l_curly_token()) {
edit.add_tabstop_after_token(cap, l_curly); let tabstop = edit.make_tabstop_after(cap);
editor.add_annotation(l_curly, tabstop);
} }
} }
insert_impl(impl_, &edit.make_mut(nominal)); insert_impl(&mut editor, &impl_, &nominal);
edit.add_file_edits(ctx.vfs_file_id(), editor);
}, },
) )
} }