Migrate generate_impl to mutable ast

This commit is contained in:
DropDemBits 2023-12-15 00:18:21 -05:00
parent ab2233e562
commit e0117154cf
No known key found for this signature in database
GPG key ID: 7FE02A6C1EDFA075
2 changed files with 53 additions and 81 deletions

View file

@ -1,10 +1,10 @@
use syntax::ast::{self, AstNode, HasName}; use syntax::{
ast::{self, make, AstNode, HasName},
use crate::{ ted,
utils::{generate_impl_text, generate_trait_impl_text_intransitive},
AssistContext, AssistId, AssistKind, Assists,
}; };
use crate::{utils, AssistContext, AssistId, AssistKind, Assists};
// Assist: generate_impl // Assist: generate_impl
// //
// Adds a new inherent impl for a type. // Adds a new inherent impl for a type.
@ -20,9 +20,7 @@ use crate::{
// data: T, // data: T,
// } // }
// //
// impl<T: Clone> Ctx<T> { // impl<T: Clone> Ctx<T> {$0}
// $0
// }
// ``` // ```
pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
let nominal = ctx.find_node_at_offset::<ast::Adt>()?; let nominal = ctx.find_node_at_offset::<ast::Adt>()?;
@ -38,17 +36,22 @@ pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
format!("Generate impl for `{name}`"), format!("Generate impl for `{name}`"),
target, target,
|edit| { |edit| {
let start_offset = nominal.syntax().text_range().end(); // Generate the impl
match ctx.config.snippet_cap { let impl_ = utils::generate_impl(&nominal);
Some(cap) => {
let snippet = generate_impl_text(&nominal, " $0"); // Add a tabstop after the left curly brace
edit.insert_snippet(cap, start_offset, snippet); if let Some(cap) = ctx.config.snippet_cap {
} if let Some(l_curly) = impl_.assoc_item_list().and_then(|it| it.l_curly_token()) {
None => { edit.add_tabstop_after_token(cap, l_curly);
let snippet = generate_impl_text(&nominal, "");
edit.insert(start_offset, snippet);
} }
} }
// Add the impl after the adt
let nominal = edit.make_mut(nominal);
ted::insert_all_raw(
ted::Position::after(nominal.syntax()),
vec![make::tokens::blank_line().into(), impl_.syntax().clone().into()],
);
}, },
) )
} }
@ -68,9 +71,7 @@ pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Optio
// data: T, // data: T,
// } // }
// //
// impl<T: Clone> $0 for Ctx<T> { // impl<T: Clone> ${0:_} for Ctx<T> {}
//
// }
// ``` // ```
pub(crate) fn generate_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { pub(crate) fn generate_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
let nominal = ctx.find_node_at_offset::<ast::Adt>()?; let nominal = ctx.find_node_at_offset::<ast::Adt>()?;
@ -86,17 +87,22 @@ pub(crate) fn generate_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) ->
format!("Generate trait impl for `{name}`"), format!("Generate trait impl for `{name}`"),
target, target,
|edit| { |edit| {
let start_offset = nominal.syntax().text_range().end(); // Generate the impl
match ctx.config.snippet_cap { let impl_ = utils::generate_trait_impl_intransitive(&nominal, make::ty_placeholder());
Some(cap) => {
let snippet = generate_trait_impl_text_intransitive(&nominal, "$0", ""); // Make the trait type a placeholder snippet
edit.insert_snippet(cap, start_offset, snippet); if let Some(cap) = ctx.config.snippet_cap {
} if let Some(trait_) = impl_.trait_() {
None => { edit.add_placeholder_snippet(cap, trait_);
let text = generate_trait_impl_text_intransitive(&nominal, "", "");
edit.insert(start_offset, text);
} }
} }
// Add the impl after the adt
let nominal = edit.make_mut(nominal);
ted::insert_all_raw(
ted::Position::after(nominal.syntax()),
vec![make::tokens::blank_line().into(), impl_.syntax().clone().into()],
);
}, },
) )
} }
@ -117,9 +123,7 @@ mod tests {
r#" r#"
struct Foo {} struct Foo {}
impl Foo { impl Foo {$0}
$0
}
"#, "#,
); );
} }
@ -134,9 +138,7 @@ mod tests {
r#" r#"
struct Foo<T: Clone> {} struct Foo<T: Clone> {}
impl<T: Clone> Foo<T> { impl<T: Clone> Foo<T> {$0}
$0
}
"#, "#,
); );
} }
@ -151,9 +153,7 @@ mod tests {
r#" r#"
struct Foo<'a, T: Foo<'a>> {} struct Foo<'a, T: Foo<'a>> {}
impl<'a, T: Foo<'a>> Foo<'a, T> { impl<'a, T: Foo<'a>> Foo<'a, T> {$0}
$0
}
"#, "#,
); );
} }
@ -171,9 +171,7 @@ mod tests {
struct Foo<'a, T: Foo<'a>> {} struct Foo<'a, T: Foo<'a>> {}
#[cfg(feature = "foo")] #[cfg(feature = "foo")]
impl<'a, T: Foo<'a>> Foo<'a, T> { impl<'a, T: Foo<'a>> Foo<'a, T> {$0}
$0
}
"#, "#,
); );
} }
@ -188,9 +186,7 @@ mod tests {
r#" r#"
struct Defaulted<T = i32> {} struct Defaulted<T = i32> {}
impl<T> Defaulted<T> { impl<T> Defaulted<T> {$0}
$0
}
"#, "#,
); );
} }
@ -205,9 +201,7 @@ mod tests {
r#" r#"
struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {} struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {}
impl<'a, 'b: 'a, T: Debug + Clone + 'a + 'b, const S: usize> Defaulted<'a, 'b, T, S> { impl<'a, 'b: 'a, T: Debug + Clone + 'a + 'b, const S: usize> Defaulted<'a, 'b, T, S> {$0}
$0
}
"#, "#,
); );
} }
@ -222,9 +216,7 @@ mod tests {
r#" r#"
struct Defaulted<const N: i32 = 0> {} struct Defaulted<const N: i32 = 0> {}
impl<const N: i32> Defaulted<N> { impl<const N: i32> Defaulted<N> {$0}
$0
}
"#, "#,
); );
} }
@ -254,8 +246,7 @@ mod tests {
impl<T> Struct<T> impl<T> Struct<T>
where where
T: Trait, T: Trait,
{ {$0
$0
} }
"#, "#,
); );
@ -285,9 +276,7 @@ mod tests {
r#" r#"
struct Foo {} struct Foo {}
impl $0 for Foo { impl ${0:_} for Foo {}
}
"#, "#,
); );
} }
@ -302,9 +291,7 @@ mod tests {
r#" r#"
struct Foo<T: Clone> {} struct Foo<T: Clone> {}
impl<T: Clone> $0 for Foo<T> { impl<T: Clone> ${0:_} for Foo<T> {}
}
"#, "#,
); );
} }
@ -319,9 +306,7 @@ mod tests {
r#" r#"
struct Foo<'a, T: Foo<'a>> {} struct Foo<'a, T: Foo<'a>> {}
impl<'a, T: Foo<'a>> $0 for Foo<'a, T> { impl<'a, T: Foo<'a>> ${0:_} for Foo<'a, T> {}
}
"#, "#,
); );
} }
@ -339,9 +324,7 @@ mod tests {
struct Foo<'a, T: Foo<'a>> {} struct Foo<'a, T: Foo<'a>> {}
#[cfg(feature = "foo")] #[cfg(feature = "foo")]
impl<'a, T: Foo<'a>> $0 for Foo<'a, T> { impl<'a, T: Foo<'a>> ${0:_} for Foo<'a, T> {}
}
"#, "#,
); );
} }
@ -356,9 +339,7 @@ mod tests {
r#" r#"
struct Defaulted<T = i32> {} struct Defaulted<T = i32> {}
impl<T> $0 for Defaulted<T> { impl<T> ${0:_} for Defaulted<T> {}
}
"#, "#,
); );
} }
@ -373,9 +354,7 @@ mod tests {
r#" r#"
struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {} struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {}
impl<'a, 'b: 'a, T: Debug + Clone + 'a + 'b, const S: usize> $0 for Defaulted<'a, 'b, T, S> { impl<'a, 'b: 'a, T: Debug + Clone + 'a + 'b, const S: usize> ${0:_} for Defaulted<'a, 'b, T, S> {}
}
"#, "#,
); );
} }
@ -390,9 +369,7 @@ mod tests {
r#" r#"
struct Defaulted<const N: i32 = 0> {} struct Defaulted<const N: i32 = 0> {}
impl<const N: i32> $0 for Defaulted<N> { impl<const N: i32> ${0:_} for Defaulted<N> {}
}
"#, "#,
); );
} }
@ -419,11 +396,10 @@ mod tests {
inner: T, inner: T,
} }
impl<T> $0 for Struct<T> impl<T> ${0:_} for Struct<T>
where where
T: Trait, T: Trait,
{ {
} }
"#, "#,
); );

View file

@ -1513,9 +1513,7 @@ struct Ctx<T: Clone> {
data: T, data: T,
} }
impl<T: Clone> Ctx<T> { impl<T: Clone> Ctx<T> {$0}
$0
}
"#####, "#####,
) )
} }
@ -1702,9 +1700,7 @@ struct Ctx<T: Clone> {
data: T, data: T,
} }
impl<T: Clone> $0 for Ctx<T> { impl<T: Clone> ${0:_} for Ctx<T> {}
}
"#####, "#####,
) )
} }