|
| 1 | +use syntax::ast::{self, AstNode, HasName}; |
| 2 | + |
| 3 | +use crate::{utils::generate_trait_impl_text, AssistContext, AssistId, AssistKind, Assists}; |
| 4 | + |
| 5 | +// Assist: generate_trait_impl |
| 6 | +// |
| 7 | +// Adds a new trait impl for a type. |
| 8 | +// |
| 9 | +// ``` |
| 10 | +// struct $0Ctx<T: Clone> { |
| 11 | +// data: T, |
| 12 | +// } |
| 13 | +// ``` |
| 14 | +// -> |
| 15 | +// ``` |
| 16 | +// struct Ctx<T: Clone> { |
| 17 | +// data: T, |
| 18 | +// } |
| 19 | +// |
| 20 | +// impl<T: Clone> $0 for Ctx<T> { |
| 21 | +// |
| 22 | +// } |
| 23 | +// ``` |
| 24 | +pub(crate) fn generate_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { |
| 25 | + let nominal = ctx.find_node_at_offset::<ast::Adt>()?; |
| 26 | + let name = nominal.name()?; |
| 27 | + let target = nominal.syntax().text_range(); |
| 28 | + |
| 29 | + if let Some(_) = ctx.find_node_at_offset::<ast::RecordFieldList>() { |
| 30 | + return None; |
| 31 | + } |
| 32 | + |
| 33 | + acc.add( |
| 34 | + AssistId("generate_trait_impl", AssistKind::Generate), |
| 35 | + format!("Generate trait impl for `{name}`"), |
| 36 | + target, |
| 37 | + |edit| { |
| 38 | + let start_offset = nominal.syntax().text_range().end(); |
| 39 | + match ctx.config.snippet_cap { |
| 40 | + Some(cap) => { |
| 41 | + let snippet = generate_trait_impl_text(&nominal, "$0", ""); |
| 42 | + edit.insert_snippet(cap, start_offset, snippet); |
| 43 | + } |
| 44 | + None => { |
| 45 | + let text = generate_trait_impl_text(&nominal, "", ""); |
| 46 | + edit.insert(start_offset, text); |
| 47 | + } |
| 48 | + } |
| 49 | + }, |
| 50 | + ) |
| 51 | +} |
| 52 | + |
| 53 | +#[cfg(test)] |
| 54 | +mod tests { |
| 55 | + use crate::tests::{check_assist, check_assist_target}; |
| 56 | + |
| 57 | + use super::*; |
| 58 | + |
| 59 | + #[test] |
| 60 | + fn test_add_trait_impl() { |
| 61 | + check_assist( |
| 62 | + generate_trait_impl, |
| 63 | + r#" |
| 64 | + struct Foo$0 {} |
| 65 | + "#, |
| 66 | + r#" |
| 67 | + struct Foo {} |
| 68 | +
|
| 69 | + impl $0 for Foo { |
| 70 | +
|
| 71 | + } |
| 72 | + "#, |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn test_add_trait_impl_with_generics() { |
| 78 | + check_assist( |
| 79 | + generate_trait_impl, |
| 80 | + r#" |
| 81 | + struct Foo$0<T: Clone> {} |
| 82 | + "#, |
| 83 | + r#" |
| 84 | + struct Foo<T: Clone> {} |
| 85 | +
|
| 86 | + impl<T: Clone> $0 for Foo<T> { |
| 87 | +
|
| 88 | + } |
| 89 | + "#, |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn test_add_trait_impl_with_generics_and_lifetime_parameters() { |
| 95 | + check_assist( |
| 96 | + generate_trait_impl, |
| 97 | + r#" |
| 98 | + struct Foo<'a, T: Foo<'a>>$0 {} |
| 99 | + "#, |
| 100 | + r#" |
| 101 | + struct Foo<'a, T: Foo<'a>> {} |
| 102 | +
|
| 103 | + impl<'a, T: Foo<'a>> $0 for Foo<'a, T> { |
| 104 | +
|
| 105 | + } |
| 106 | + "#, |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn test_add_trait_impl_with_attributes() { |
| 112 | + check_assist( |
| 113 | + generate_trait_impl, |
| 114 | + r#" |
| 115 | + #[cfg(feature = "foo")] |
| 116 | + struct Foo<'a, T: Foo$0<'a>> {} |
| 117 | + "#, |
| 118 | + r#" |
| 119 | + #[cfg(feature = "foo")] |
| 120 | + struct Foo<'a, T: Foo<'a>> {} |
| 121 | +
|
| 122 | + #[cfg(feature = "foo")] |
| 123 | + impl<'a, T: Foo<'a>> $0 for Foo<'a, T> { |
| 124 | +
|
| 125 | + } |
| 126 | + "#, |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + #[test] |
| 131 | + fn test_add_trait_impl_with_default_generic() { |
| 132 | + check_assist( |
| 133 | + generate_trait_impl, |
| 134 | + r#" |
| 135 | + struct Defaulted$0<T = i32> {} |
| 136 | + "#, |
| 137 | + r#" |
| 138 | + struct Defaulted<T = i32> {} |
| 139 | +
|
| 140 | + impl<T> $0 for Defaulted<T> { |
| 141 | +
|
| 142 | + } |
| 143 | + "#, |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + #[test] |
| 148 | + fn test_add_trait_impl_with_constrained_default_generic() { |
| 149 | + check_assist( |
| 150 | + generate_trait_impl, |
| 151 | + r#" |
| 152 | + struct Defaulted$0<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {} |
| 153 | + "#, |
| 154 | + r#" |
| 155 | + struct Defaulted<'a, 'b: 'a, T: Debug + Clone + 'a + 'b = String, const S: usize> {} |
| 156 | +
|
| 157 | + impl<'a, 'b: 'a, T: Debug + Clone + 'a + 'b, const S: usize> $0 for Defaulted<'a, 'b, T, S> { |
| 158 | +
|
| 159 | + } |
| 160 | + "#, |
| 161 | + ); |
| 162 | + } |
| 163 | + |
| 164 | + #[test] |
| 165 | + fn test_add_trait_impl_with_const_defaulted_generic() { |
| 166 | + check_assist( |
| 167 | + generate_trait_impl, |
| 168 | + r#" |
| 169 | + struct Defaulted$0<const N: i32 = 0> {} |
| 170 | + "#, |
| 171 | + r#" |
| 172 | + struct Defaulted<const N: i32 = 0> {} |
| 173 | +
|
| 174 | + impl<const N: i32> $0 for Defaulted<N> { |
| 175 | +
|
| 176 | + } |
| 177 | + "#, |
| 178 | + ); |
| 179 | + } |
| 180 | + |
| 181 | + #[test] |
| 182 | + fn test_add_trait_impl_with_trait_constraint() { |
| 183 | + check_assist( |
| 184 | + generate_trait_impl, |
| 185 | + r#" |
| 186 | + pub trait Trait {} |
| 187 | + struct Struct$0<T> |
| 188 | + where |
| 189 | + T: Trait, |
| 190 | + { |
| 191 | + inner: T, |
| 192 | + } |
| 193 | + "#, |
| 194 | + r#" |
| 195 | + pub trait Trait {} |
| 196 | + struct Struct<T> |
| 197 | + where |
| 198 | + T: Trait, |
| 199 | + { |
| 200 | + inner: T, |
| 201 | + } |
| 202 | +
|
| 203 | + impl<T> $0 for Struct<T> |
| 204 | + where |
| 205 | + T: Trait, |
| 206 | + { |
| 207 | +
|
| 208 | + } |
| 209 | + "#, |
| 210 | + ); |
| 211 | + } |
| 212 | + |
| 213 | + #[test] |
| 214 | + fn add_trait_impl_target() { |
| 215 | + check_assist_target( |
| 216 | + generate_trait_impl, |
| 217 | + r#" |
| 218 | + struct SomeThingIrrelevant; |
| 219 | + /// Has a lifetime parameter |
| 220 | + struct Foo$0<'a, T: Foo<'a>> {} |
| 221 | + struct EvenMoreIrrelevant; |
| 222 | + "#, |
| 223 | + "/// Has a lifetime parameter\nstruct Foo<'a, T: Foo<'a>> {}", |
| 224 | + ); |
| 225 | + } |
| 226 | +} |
0 commit comments