]> git.lizzy.rs Git - rust.git/commitdiff
generate function assist favors deref cmpt types
authormahdi-frms <mahdif1380@outlook.com>
Sat, 31 Jul 2021 11:13:22 +0000 (15:43 +0430)
committermahdi-frms <mahdif1380@outlook.com>
Sat, 31 Jul 2021 12:04:09 +0000 (16:34 +0430)
crates/ide_assists/src/handlers/generate_function.rs
crates/ide_assists/src/handlers/generate_getter.rs
crates/ide_assists/src/utils.rs

index c620cfac486b7b18a109ddc0e79d7c926c9be76c..d06e3f70942421c27ce5289c9b779395070e441e 100644 (file)
@@ -12,6 +12,7 @@
 };
 
 use crate::{
+    utils::useless_type_special_case,
     utils::{render_snippet, Cursor},
     AssistContext, AssistId, AssistKind, Assists,
 };
@@ -257,7 +258,17 @@ fn fn_args(
             None => String::from("arg"),
         });
         arg_types.push(match fn_arg_type(ctx, target_module, &arg) {
-            Some(ty) => ty,
+            Some(ty) => {
+                if ty.len() > 0 && ty.starts_with('&') {
+                    if let Some((new_ty, _)) = useless_type_special_case("", &ty[1..].to_owned()) {
+                        new_ty
+                    } else {
+                        ty
+                    }
+                } else {
+                    ty
+                }
+            }
             None => String::from("()"),
         });
     }
index cc020c92c522ba868a5649bdbad93d4347b2cd68..739f3e592fff15c76f27d9290a39c4f1a57417bc 100644 (file)
@@ -2,6 +2,7 @@
 use syntax::ast::{self, AstNode, NameOwner, VisibilityOwner};
 
 use crate::{
+    utils::useless_type_special_case,
     utils::{find_impl_block_end, find_struct_impl, generate_impl_text},
     AssistContext, AssistId, AssistKind, Assists, GroupLabel,
 };
@@ -99,7 +100,7 @@ pub(crate) fn generate_getter_impl(
             let (ty, body) = if mutable {
                 (format!("&mut {}", field_ty), format!("&mut self.{}", field_name))
             } else {
-                useless_type_special_case(&field_name.to_string(), &field_ty)
+                useless_type_special_case(&field_name.to_string(), &field_ty.to_string())
                     .unwrap_or_else(|| (format!("&{}", field_ty), format!("&self.{}", field_name)))
             };
 
@@ -136,29 +137,6 @@ pub(crate) fn generate_getter_impl(
     )
 }
 
-fn useless_type_special_case(field_name: &str, field_ty: &ast::Type) -> Option<(String, String)> {
-    if field_ty.to_string() == "String" {
-        cov_mark::hit!(useless_type_special_case);
-        return Some(("&str".to_string(), format!("self.{}.as_str()", field_name)));
-    }
-    if let Some(arg) = ty_ctor(field_ty, "Vec") {
-        return Some((format!("&[{}]", arg), format!("self.{}.as_slice()", field_name)));
-    }
-    if let Some(arg) = ty_ctor(field_ty, "Box") {
-        return Some((format!("&{}", arg), format!("self.{}.as_ref()", field_name)));
-    }
-    if let Some(arg) = ty_ctor(field_ty, "Option") {
-        return Some((format!("Option<&{}>", arg), format!("self.{}.as_ref()", field_name)));
-    }
-    None
-}
-
-// FIXME: This should rely on semantic info.
-fn ty_ctor(ty: &ast::Type, ctor: &str) -> Option<String> {
-    let res = ty.to_string().strip_prefix(ctor)?.strip_prefix('<')?.strip_suffix('>')?.to_string();
-    Some(res)
-}
-
 #[cfg(test)]
 mod tests {
     use crate::tests::{check_assist, check_assist_not_applicable};
index 99c91463b98c7aacf04157ea7346a3726f406244..81463745a4618b9e4781ad9d20b8be8a783e21db 100644 (file)
@@ -493,3 +493,26 @@ pub(crate) fn add_method_to_adt(
 
     builder.insert(start_offset, buf);
 }
+
+pub fn useless_type_special_case(field_name: &str, field_ty: &String) -> Option<(String, String)> {
+    if field_ty.to_string() == "String" {
+        cov_mark::hit!(useless_type_special_case);
+        return Some(("&str".to_string(), format!("self.{}.as_str()", field_name)));
+    }
+    if let Some(arg) = ty_ctor(field_ty, "Vec") {
+        return Some((format!("&[{}]", arg), format!("self.{}.as_slice()", field_name)));
+    }
+    if let Some(arg) = ty_ctor(field_ty, "Box") {
+        return Some((format!("&{}", arg), format!("self.{}.as_ref()", field_name)));
+    }
+    if let Some(arg) = ty_ctor(field_ty, "Option") {
+        return Some((format!("Option<&{}>", arg), format!("self.{}.as_ref()", field_name)));
+    }
+    None
+}
+
+// FIXME: This should rely on semantic info.
+fn ty_ctor(ty: &String, ctor: &str) -> Option<String> {
+    let res = ty.to_string().strip_prefix(ctor)?.strip_prefix('<')?.strip_suffix('>')?.to_string();
+    Some(res)
+}