mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
feat: ide-assist, generate single field struct From
This commit is contained in:
parent
2bafe9d96c
commit
3de532937a
1000
crates/ide-assists/src/handlers/generate_single_field_struct_from.rs
Normal file
1000
crates/ide-assists/src/handlers/generate_single_field_struct_from.rs
Normal file
File diff suppressed because it is too large
Load Diff
@ -171,6 +171,7 @@ mod handlers {
|
||||
mod generate_is_empty_from_len;
|
||||
mod generate_mut_trait_impl;
|
||||
mod generate_new;
|
||||
mod generate_single_field_struct_from;
|
||||
mod generate_trait_from_impl;
|
||||
mod inline_call;
|
||||
mod inline_const_as_literal;
|
||||
@ -304,6 +305,7 @@ mod handlers {
|
||||
generate_mut_trait_impl::generate_mut_trait_impl,
|
||||
generate_new::generate_new,
|
||||
generate_trait_from_impl::generate_trait_from_impl,
|
||||
generate_single_field_struct_from::generate_single_field_struct_from,
|
||||
inline_call::inline_call,
|
||||
inline_call::inline_into_callers,
|
||||
inline_const_as_literal::inline_const_as_literal,
|
||||
|
@ -1953,6 +1953,34 @@ impl Person {
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_generate_single_field_struct_from() {
|
||||
check_doc_test(
|
||||
"generate_single_field_struct_from",
|
||||
r#####"
|
||||
//- minicore: from, phantom_data
|
||||
use core::marker::PhantomData;
|
||||
struct $0Foo<T> {
|
||||
id: i32,
|
||||
_phantom_data: PhantomData<T>,
|
||||
}
|
||||
"#####,
|
||||
r#####"
|
||||
use core::marker::PhantomData;
|
||||
struct Foo<T> {
|
||||
id: i32,
|
||||
_phantom_data: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T> From<i32> for Foo<T> {
|
||||
fn from(id: i32) -> Self {
|
||||
Self { id, _phantom_data: PhantomData }
|
||||
}
|
||||
}
|
||||
"#####,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doctest_generate_trait_from_impl() {
|
||||
check_doc_test(
|
||||
|
@ -747,16 +747,23 @@ fn generate_impl_inner(
|
||||
.clone_for_update();
|
||||
|
||||
// Copy any cfg attrs from the original adt
|
||||
let cfg_attrs = adt
|
||||
.attrs()
|
||||
.filter(|attr| attr.as_simple_call().map(|(name, _arg)| name == "cfg").unwrap_or(false));
|
||||
for attr in cfg_attrs {
|
||||
impl_.add_attr(attr.clone_for_update());
|
||||
}
|
||||
add_cfg_attrs_to(adt, &impl_);
|
||||
|
||||
impl_
|
||||
}
|
||||
|
||||
pub(crate) fn add_cfg_attrs_to<T, U>(from: &T, to: &U)
|
||||
where
|
||||
T: HasAttrs,
|
||||
U: AttrsOwnerEdit,
|
||||
{
|
||||
let cfg_attrs =
|
||||
from.attrs().filter(|attr| attr.as_simple_call().is_some_and(|(name, _arg)| name == "cfg"));
|
||||
for attr in cfg_attrs {
|
||||
to.add_attr(attr.clone_for_update());
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn add_method_to_adt(
|
||||
builder: &mut SourceChangeBuilder,
|
||||
adt: &ast::Adt,
|
||||
|
Loading…
x
Reference in New Issue
Block a user