]> git.lizzy.rs Git - rust.git/commitdiff
7709: Updated the implementation.
authorChetan Khilosiya <chetan.khilosiya@gmail.com>
Mon, 15 Mar 2021 17:18:50 +0000 (22:48 +0530)
committerChetan Khilosiya <chetan.khilosiya@gmail.com>
Mon, 15 Mar 2021 17:18:50 +0000 (22:48 +0530)
The get function from impl method is updated.
and now same method used to get len and is_empty function.

crates/hir_expand/src/name.rs
crates/ide_assists/src/handlers/generate_is_empty_from_len.rs
crates/ide_assists/src/tests/generated.rs

index e833e032c08582288d48e0194d17438becca16b0..43de9edd65a80bdc804831aa84eeb30ca99253c4 100644 (file)
@@ -191,6 +191,8 @@ macro_rules! known_names {
         filter_map,
         next,
         iter_mut,
+        len,
+        is_empty,
         // Builtin macros
         file,
         column,
index bd29dddb3b16c53da4313223b5bd979cb2f9db71..aa7072f25ccd3add105a3f644c96a3a102bf225b 100644 (file)
@@ -1,4 +1,4 @@
-use hir::{AssocItem, HasSource, Impl};
+use hir::{known, HasSource, Name};
 use syntax::{
     ast::{self, NameOwner},
     AstNode, TextRange,
@@ -14,6 +14,8 @@
 // Generates is_empty implementation from the len method.
 //
 // ```
+// struct MyStruct { data: Vec<String> }
+//
 // impl MyStruct {
 //     p$0ub fn len(&self) -> usize {
 //         self.data.len()
@@ -22,6 +24,8 @@
 // ```
 // ->
 // ```
+// struct MyStruct { data: Vec<String> }
+//
 // impl MyStruct {
 //     pub fn len(&self) -> usize {
 //         self.data.len()
@@ -46,60 +50,50 @@ pub(crate) fn generate_is_empty_from_len(acc: &mut Assists, ctx: &AssistContext)
         return None;
     }
 
-    let impl_ = fn_node.syntax().ancestors().into_iter().find_map(ast::Impl::cast)?;
-    let impl_def = ctx.sema.to_def(&impl_)?;
-    if is_empty_implemented(ctx, &impl_def) {
+    let impl_ = fn_node.syntax().ancestors().find_map(ast::Impl::cast)?;
+    if get_impl_method(ctx, &impl_, &known::is_empty).is_some() {
         cov_mark::hit!(is_empty_already_implemented);
         return None;
     }
 
-    let range = get_text_range_of_len_function(ctx, &impl_def)?;
+    let range = get_text_range_of_len_function(ctx, &impl_)?;
 
     acc.add(
         AssistId("generate_is_empty_from_len", AssistKind::Generate),
         "Generate a is_empty impl from a len function",
         range,
         |builder| {
-            let code = get_is_empty_code();
+            let code = r#"
+
+    pub fn is_empty(&self) -> bool {
+        self.len() == 0
+    }"#
+            .to_string();
             builder.insert(range.end(), code)
         },
     )
 }
 
-fn get_function_from_impl(ctx: &AssistContext, impl_def: &Impl, name: &str) -> Option<AssocItem> {
+fn get_impl_method(
+    ctx: &AssistContext,
+    impl_: &ast::Impl,
+    fn_name: &Name,
+) -> Option<hir::Function> {
     let db = ctx.sema.db;
-    impl_def.items(db).into_iter().filter(|item| matches!(item, AssocItem::Function(_value))).find(
-        |func| match func.name(db) {
-            Some(fn_name) => fn_name.to_string() == name,
-            None => false,
-        },
-    )
-}
+    let impl_def: hir::Impl = ctx.sema.to_def(impl_)?;
 
-fn is_empty_implemented(ctx: &AssistContext, impl_def: &Impl) -> bool {
-    get_function_from_impl(ctx, impl_def, "is_empty").is_some()
+    let scope = ctx.sema.scope(impl_.syntax());
+    let krate = impl_def.module(db).krate();
+    let ty = impl_def.target_ty(db);
+    let traits_in_scope = scope.traits_in_scope();
+    ty.iterate_method_candidates(db, krate, &traits_in_scope, Some(fn_name), |_, func| Some(func))
 }
 
-fn get_text_range_of_len_function(ctx: &AssistContext, impl_def: &Impl) -> Option<TextRange> {
+fn get_text_range_of_len_function(ctx: &AssistContext, impl_: &ast::Impl) -> Option<TextRange> {
     let db = ctx.sema.db;
-    let len_fn = get_function_from_impl(ctx, impl_def, "len")?;
-
-    let mut range = None;
-    if let AssocItem::Function(node) = len_fn {
-        let node = node.source(db)?;
-        range = Some(node.syntax().value.text_range());
-    }
-
-    range
-}
-
-fn get_is_empty_code() -> String {
-    r#"
-
-    pub fn is_empty(&self) -> bool {
-        self.len() == 0
-    }"#
-    .to_string()
+    let func = get_impl_method(ctx, impl_, &known::len)?;
+    let node = func.source(db)?;
+    Some(node.syntax().value.text_range())
 }
 
 #[cfg(test)]
@@ -114,6 +108,8 @@ fn len_function_not_present() {
         check_assist_not_applicable(
             generate_is_empty_from_len,
             r#"
+struct MyStruct { data: Vec<String> }
+
 impl MyStruct {
     p$0ub fn test(&self) -> usize {
             self.data.len()
@@ -129,6 +125,8 @@ fn len_function_with_parameters() {
         check_assist_not_applicable(
             generate_is_empty_from_len,
             r#"
+struct MyStruct { data: Vec<String> }
+
 impl MyStruct {
     p$0ub fn len(&self, _i: bool) -> usize {
         self.data.len()
@@ -144,6 +142,8 @@ fn is_empty_already_implemented() {
         check_assist_not_applicable(
             generate_is_empty_from_len,
             r#"
+struct MyStruct { data: Vec<String> }
+
 impl MyStruct {
     p$0ub fn len(&self) -> usize {
         self.data.len()
@@ -162,6 +162,8 @@ fn generate_is_empty() {
         check_assist(
             generate_is_empty_from_len,
             r#"
+struct MyStruct { data: Vec<String> }
+
 impl MyStruct {
     p$0ub fn len(&self) -> usize {
         self.data.len()
@@ -169,6 +171,8 @@ impl MyStruct {
 }
 "#,
             r#"
+struct MyStruct { data: Vec<String> }
+
 impl MyStruct {
     pub fn len(&self) -> usize {
         self.data.len()
@@ -187,6 +191,8 @@ fn multiple_functions_in_impl() {
         check_assist(
             generate_is_empty_from_len,
             r#"
+struct MyStruct { data: Vec<String> }
+
 impl MyStruct {
     pub fn new() -> Self {
         Self { data: 0 }
@@ -197,11 +203,13 @@ pub fn new() -> Self {
     }
 
     pub fn work(&self) -> Option<usize> {
-        // do some work
+
     }
 }
 "#,
             r#"
+struct MyStruct { data: Vec<String> }
+
 impl MyStruct {
     pub fn new() -> Self {
         Self { data: 0 }
@@ -216,7 +224,29 @@ pub fn is_empty(&self) -> bool {
     }
 
     pub fn work(&self) -> Option<usize> {
-        // do some work
+
+    }
+}
+"#,
+        );
+    }
+
+    #[test]
+    fn multiple_impls() {
+        check_assist_not_applicable(
+            generate_is_empty_from_len,
+            r#"
+struct MyStruct { data: Vec<String> }
+
+impl MyStruct {
+    p$0ub fn len(&self) -> usize {
+        self.data.len()
+    }
+}
+
+impl MyStruct {
+    pub fn is_empty(&self) -> bool {
+        self.len() == 0
     }
 }
 "#,
index 66fbcc968943cbdfd58148092c9d2d9577a20479..736027ff00b6785dc4abaeec09d8ba1a89a0388c 100644 (file)
@@ -726,6 +726,8 @@ fn doctest_generate_is_empty_from_len() {
     check_doc_test(
         "generate_is_empty_from_len",
         r#####"
+struct MyStruct { data: Vec<String> }
+
 impl MyStruct {
     p$0ub fn len(&self) -> usize {
         self.data.len()
@@ -733,6 +735,8 @@ impl MyStruct {
 }
 "#####,
         r#####"
+struct MyStruct { data: Vec<String> }
+
 impl MyStruct {
     pub fn len(&self) -> usize {
         self.data.len()