Skip to content

Commit 93873b0

Browse files
bors[bot]mahdifrmz
andauthored
Merge #9739
9739: generate function assist favors deref cmpt types r=matklad a=mahdi-frms Fixes #9713 Although that's still not relying on sematic info. Co-authored-by: mahdi-frms <[email protected]>
2 parents a423b30 + a5edf6d commit 93873b0

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

crates/ide_assists/src/handlers/generate_function.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use syntax::{
1212
};
1313

1414
use crate::{
15+
utils::useless_type_special_case,
1516
utils::{render_snippet, Cursor},
1617
AssistContext, AssistId, AssistKind, Assists,
1718
};
@@ -257,7 +258,17 @@ fn fn_args(
257258
None => String::from("arg"),
258259
});
259260
arg_types.push(match fn_arg_type(ctx, target_module, &arg) {
260-
Some(ty) => ty,
261+
Some(ty) => {
262+
if ty.len() > 0 && ty.starts_with('&') {
263+
if let Some((new_ty, _)) = useless_type_special_case("", &ty[1..].to_owned()) {
264+
new_ty
265+
} else {
266+
ty
267+
}
268+
} else {
269+
ty
270+
}
271+
}
261272
None => String::from("()"),
262273
});
263274
}

crates/ide_assists/src/handlers/generate_getter.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use stdx::{format_to, to_lower_snake_case};
22
use syntax::ast::{self, AstNode, NameOwner, VisibilityOwner};
33

44
use crate::{
5+
utils::useless_type_special_case,
56
utils::{find_impl_block_end, find_struct_impl, generate_impl_text},
67
AssistContext, AssistId, AssistKind, Assists, GroupLabel,
78
};
@@ -99,7 +100,7 @@ pub(crate) fn generate_getter_impl(
99100
let (ty, body) = if mutable {
100101
(format!("&mut {}", field_ty), format!("&mut self.{}", field_name))
101102
} else {
102-
useless_type_special_case(&field_name.to_string(), &field_ty)
103+
useless_type_special_case(&field_name.to_string(), &field_ty.to_string())
103104
.unwrap_or_else(|| (format!("&{}", field_ty), format!("&self.{}", field_name)))
104105
};
105106

@@ -136,29 +137,6 @@ pub(crate) fn generate_getter_impl(
136137
)
137138
}
138139

139-
fn useless_type_special_case(field_name: &str, field_ty: &ast::Type) -> Option<(String, String)> {
140-
if field_ty.to_string() == "String" {
141-
cov_mark::hit!(useless_type_special_case);
142-
return Some(("&str".to_string(), format!("self.{}.as_str()", field_name)));
143-
}
144-
if let Some(arg) = ty_ctor(field_ty, "Vec") {
145-
return Some((format!("&[{}]", arg), format!("self.{}.as_slice()", field_name)));
146-
}
147-
if let Some(arg) = ty_ctor(field_ty, "Box") {
148-
return Some((format!("&{}", arg), format!("self.{}.as_ref()", field_name)));
149-
}
150-
if let Some(arg) = ty_ctor(field_ty, "Option") {
151-
return Some((format!("Option<&{}>", arg), format!("self.{}.as_ref()", field_name)));
152-
}
153-
None
154-
}
155-
156-
// FIXME: This should rely on semantic info.
157-
fn ty_ctor(ty: &ast::Type, ctor: &str) -> Option<String> {
158-
let res = ty.to_string().strip_prefix(ctor)?.strip_prefix('<')?.strip_suffix('>')?.to_string();
159-
Some(res)
160-
}
161-
162140
#[cfg(test)]
163141
mod tests {
164142
use crate::tests::{check_assist, check_assist_not_applicable};

crates/ide_assists/src/utils.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,3 +493,26 @@ pub(crate) fn add_method_to_adt(
493493

494494
builder.insert(start_offset, buf);
495495
}
496+
497+
pub fn useless_type_special_case(field_name: &str, field_ty: &String) -> Option<(String, String)> {
498+
if field_ty.to_string() == "String" {
499+
cov_mark::hit!(useless_type_special_case);
500+
return Some(("&str".to_string(), format!("self.{}.as_str()", field_name)));
501+
}
502+
if let Some(arg) = ty_ctor(field_ty, "Vec") {
503+
return Some((format!("&[{}]", arg), format!("self.{}.as_slice()", field_name)));
504+
}
505+
if let Some(arg) = ty_ctor(field_ty, "Box") {
506+
return Some((format!("&{}", arg), format!("self.{}.as_ref()", field_name)));
507+
}
508+
if let Some(arg) = ty_ctor(field_ty, "Option") {
509+
return Some((format!("Option<&{}>", arg), format!("self.{}.as_ref()", field_name)));
510+
}
511+
None
512+
}
513+
514+
// FIXME: This should rely on semantic info.
515+
fn ty_ctor(ty: &String, ctor: &str) -> Option<String> {
516+
let res = ty.to_string().strip_prefix(ctor)?.strip_prefix('<')?.strip_suffix('>')?.to_string();
517+
Some(res)
518+
}

0 commit comments

Comments
 (0)