7708: Added the updated implementation of is_default_implemented.
The implementation uses hir create to find the implemented trait.
This commit is contained in:
parent
b8e6d6a606
commit
9a84daf47d
@ -2,7 +2,7 @@ use crate::{
|
|||||||
assist_context::{AssistContext, Assists},
|
assist_context::{AssistContext, Assists},
|
||||||
AssistId,
|
AssistId,
|
||||||
};
|
};
|
||||||
use hir::TypeRef;
|
use ide_db::helpers::FamousDefs;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{self, Impl, NameOwner},
|
ast::{self, Impl, NameOwner},
|
||||||
AstNode,
|
AstNode,
|
||||||
@ -53,7 +53,8 @@ pub(crate) fn generate_default_from_new(acc: &mut Assists, ctx: &AssistContext)
|
|||||||
}
|
}
|
||||||
|
|
||||||
let impl_ = fn_node.syntax().ancestors().into_iter().find_map(ast::Impl::cast)?;
|
let impl_ = fn_node.syntax().ancestors().into_iter().find_map(ast::Impl::cast)?;
|
||||||
if is_default_implemented(ctx, &impl_).is_some() {
|
let implements_default = is_default_implemented(ctx, &impl_)?;
|
||||||
|
if implements_default {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,29 +86,25 @@ impl Default for {} {{
|
|||||||
|
|
||||||
fn is_default_implemented(ctx: &AssistContext, impl_: &Impl) -> Option<bool> {
|
fn is_default_implemented(ctx: &AssistContext, impl_: &Impl) -> Option<bool> {
|
||||||
let db = ctx.sema.db;
|
let db = ctx.sema.db;
|
||||||
let module = impl_.syntax().parent()?;
|
let impl_def = ctx.sema.to_def(impl_)?;
|
||||||
let sema_scope = ctx.sema.scope(&module);
|
let ty = impl_def.target_ty(db);
|
||||||
let impls = sema_scope.module()?.impl_defs(db);
|
let krate = impl_def.module(db).krate();
|
||||||
let mut name = None;
|
let default_trait = FamousDefs(&ctx.sema, Some(krate)).core_default_Default()?;
|
||||||
for i in impls {
|
let implements_default = ty.impls_trait(db, default_trait, &[]);
|
||||||
if let Some(TypeRef::Path(p)) = i.target_trait(db) {
|
Some(implements_default)
|
||||||
name = p.segments().iter().map(|s| s.name.to_string()).find(|n| n == "Default");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
name.map(|n| !n.is_empty())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use ide_db::helpers::FamousDefs;
|
||||||
|
|
||||||
use crate::tests::{check_assist, check_assist_not_applicable};
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn generate_default() {
|
fn generate_default() {
|
||||||
check_assist(
|
check_pass(
|
||||||
generate_default_from_new,
|
|
||||||
r#"
|
r#"
|
||||||
struct Example { _inner: () }
|
struct Example { _inner: () }
|
||||||
|
|
||||||
@ -141,8 +138,7 @@ fn main() {}
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn generate_default2() {
|
fn generate_default2() {
|
||||||
check_assist(
|
check_pass(
|
||||||
generate_default_from_new,
|
|
||||||
r#"
|
r#"
|
||||||
struct Test { value: u32 }
|
struct Test { value: u32 }
|
||||||
|
|
||||||
@ -173,8 +169,7 @@ impl Default for Test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn new_function_with_parameters() {
|
fn new_function_with_parameters() {
|
||||||
mark::check!(new_function_with_parameters);
|
mark::check!(new_function_with_parameters);
|
||||||
check_assist_not_applicable(
|
check_not_applicable(
|
||||||
generate_default_from_new,
|
|
||||||
r#"
|
r#"
|
||||||
struct Example { _inner: () }
|
struct Example { _inner: () }
|
||||||
|
|
||||||
@ -190,8 +185,7 @@ impl Example {
|
|||||||
#[test]
|
#[test]
|
||||||
fn other_function_than_new() {
|
fn other_function_than_new() {
|
||||||
mark::check!(other_function_than_new);
|
mark::check!(other_function_than_new);
|
||||||
check_assist_not_applicable(
|
check_not_applicable(
|
||||||
generate_default_from_new,
|
|
||||||
r#"
|
r#"
|
||||||
struct Example { _inner: () }
|
struct Example { _inner: () }
|
||||||
|
|
||||||
@ -207,8 +201,7 @@ impl Exmaple {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn default_block_is_already_present() {
|
fn default_block_is_already_present() {
|
||||||
check_assist_not_applicable(
|
check_not_applicable(
|
||||||
generate_default_from_new,
|
|
||||||
r#"
|
r#"
|
||||||
struct Example { _inner: () }
|
struct Example { _inner: () }
|
||||||
|
|
||||||
@ -229,8 +222,7 @@ impl Default for Example {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn standalone_new_function() {
|
fn standalone_new_function() {
|
||||||
check_assist_not_applicable(
|
check_not_applicable(
|
||||||
generate_default_from_new,
|
|
||||||
r#"
|
r#"
|
||||||
fn n$0ew() -> u32 {
|
fn n$0ew() -> u32 {
|
||||||
0
|
0
|
||||||
@ -241,8 +233,7 @@ fn n$0ew() -> u32 {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn multiple_struct_blocks() {
|
fn multiple_struct_blocks() {
|
||||||
check_assist(
|
check_pass(
|
||||||
generate_default_from_new,
|
|
||||||
r#"
|
r#"
|
||||||
struct Example { _inner: () }
|
struct Example { _inner: () }
|
||||||
struct Test { value: u32 }
|
struct Test { value: u32 }
|
||||||
@ -274,8 +265,7 @@ impl Default for Example {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn when_struct_is_after_impl() {
|
fn when_struct_is_after_impl() {
|
||||||
check_assist(
|
check_pass(
|
||||||
generate_default_from_new,
|
|
||||||
r#"
|
r#"
|
||||||
impl Example {
|
impl Example {
|
||||||
pub fn $0new() -> Self {
|
pub fn $0new() -> Self {
|
||||||
@ -305,8 +295,7 @@ struct Example { _inner: () }
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn struct_in_module() {
|
fn struct_in_module() {
|
||||||
check_assist(
|
check_pass(
|
||||||
generate_default_from_new,
|
|
||||||
r#"
|
r#"
|
||||||
mod test {
|
mod test {
|
||||||
struct Example { _inner: () }
|
struct Example { _inner: () }
|
||||||
@ -340,8 +329,7 @@ impl Default for Example {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn struct_in_module_with_default() {
|
fn struct_in_module_with_default() {
|
||||||
check_assist_not_applicable(
|
check_not_applicable(
|
||||||
generate_default_from_new,
|
|
||||||
r#"
|
r#"
|
||||||
mod test {
|
mod test {
|
||||||
struct Example { _inner: () }
|
struct Example { _inner: () }
|
||||||
@ -361,4 +349,14 @@ mod test {
|
|||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn check_pass(before: &str, after: &str) {
|
||||||
|
let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE);
|
||||||
|
check_assist(generate_default_from_new, before, after);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_not_applicable(before: &str) {
|
||||||
|
let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE);
|
||||||
|
check_assist_not_applicable(generate_default_from_new, before);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user