mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-11-01 12:24:29 +00:00
Merge pull request #19783 from A4-Tacks/generate-single-field-from
Some checks are pending
metrics / build_metrics (push) Waiting to run
metrics / other_metrics (diesel-1.4.8) (push) Blocked by required conditions
metrics / other_metrics (hyper-0.14.18) (push) Blocked by required conditions
metrics / other_metrics (ripgrep-13.0.0) (push) Blocked by required conditions
metrics / other_metrics (self) (push) Blocked by required conditions
metrics / other_metrics (webrender-2022) (push) Blocked by required conditions
metrics / generate_final_metrics (push) Blocked by required conditions
rustdoc / rustdoc (push) Waiting to run
Some checks are pending
metrics / build_metrics (push) Waiting to run
metrics / other_metrics (diesel-1.4.8) (push) Blocked by required conditions
metrics / other_metrics (hyper-0.14.18) (push) Blocked by required conditions
metrics / other_metrics (ripgrep-13.0.0) (push) Blocked by required conditions
metrics / other_metrics (self) (push) Blocked by required conditions
metrics / other_metrics (webrender-2022) (push) Blocked by required conditions
metrics / generate_final_metrics (push) Blocked by required conditions
rustdoc / rustdoc (push) Waiting to run
Add ide-assist, generate single field struct From
This commit is contained in:
commit
4f294fc2a1
4 changed files with 1043 additions and 6 deletions
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
|
|
@ -172,6 +172,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;
|
||||
|
|
@ -305,6 +306,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,
|
||||
|
|
|
|||
|
|
@ -1994,6 +1994,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(
|
||||
|
|
|
|||
|
|
@ -743,16 +743,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…
Add table
Add a link
Reference in a new issue