diff options
Diffstat (limited to 'crates/ide_assists/src/utils')
-rw-r--r-- | crates/ide_assists/src/utils/suggest_name.rs | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/crates/ide_assists/src/utils/suggest_name.rs b/crates/ide_assists/src/utils/suggest_name.rs index deafcd630..b3aabeab3 100644 --- a/crates/ide_assists/src/utils/suggest_name.rs +++ b/crates/ide_assists/src/utils/suggest_name.rs | |||
@@ -6,7 +6,7 @@ use itertools::Itertools; | |||
6 | use stdx::to_lower_snake_case; | 6 | use stdx::to_lower_snake_case; |
7 | use syntax::{ | 7 | use syntax::{ |
8 | ast::{self, NameOwner}, | 8 | ast::{self, NameOwner}, |
9 | match_ast, AstNode, | 9 | match_ast, AstNode, SmolStr, |
10 | }; | 10 | }; |
11 | 11 | ||
12 | /// Trait names, that will be ignored when in `impl Trait` and `dyn Trait` | 12 | /// Trait names, that will be ignored when in `impl Trait` and `dyn Trait` |
@@ -57,6 +57,14 @@ const USELESS_METHODS: &[&str] = &[ | |||
57 | "iter_mut", | 57 | "iter_mut", |
58 | ]; | 58 | ]; |
59 | 59 | ||
60 | pub(crate) fn for_generic_parameter(ty: &ast::ImplTraitType) -> SmolStr { | ||
61 | let c = ty | ||
62 | .type_bound_list() | ||
63 | .and_then(|bounds| bounds.syntax().text().char_at(0.into())) | ||
64 | .unwrap_or('T'); | ||
65 | c.encode_utf8(&mut [0; 4]).into() | ||
66 | } | ||
67 | |||
60 | /// Suggest name of variable for given expression | 68 | /// Suggest name of variable for given expression |
61 | /// | 69 | /// |
62 | /// **NOTE**: it is caller's responsibility to guarantee uniqueness of the name. | 70 | /// **NOTE**: it is caller's responsibility to guarantee uniqueness of the name. |
@@ -75,7 +83,8 @@ const USELESS_METHODS: &[&str] = &[ | |||
75 | /// It also applies heuristics to filter out less informative names | 83 | /// It also applies heuristics to filter out less informative names |
76 | /// | 84 | /// |
77 | /// Currently it sticks to the first name found. | 85 | /// Currently it sticks to the first name found. |
78 | pub(crate) fn variable(expr: &ast::Expr, sema: &Semantics<'_, RootDatabase>) -> String { | 86 | // FIXME: Microoptimize and return a `SmolStr` here. |
87 | pub(crate) fn for_variable(expr: &ast::Expr, sema: &Semantics<'_, RootDatabase>) -> String { | ||
79 | // `from_param` does not benifit from stripping | 88 | // `from_param` does not benifit from stripping |
80 | // it need the largest context possible | 89 | // it need the largest context possible |
81 | // so we check firstmost | 90 | // so we check firstmost |
@@ -276,7 +285,7 @@ mod tests { | |||
276 | frange.range, | 285 | frange.range, |
277 | "selection is not an expression(yet contained in one)" | 286 | "selection is not an expression(yet contained in one)" |
278 | ); | 287 | ); |
279 | let name = variable(&expr, &sema); | 288 | let name = for_variable(&expr, &sema); |
280 | assert_eq!(&name, expected); | 289 | assert_eq!(&name, expected); |
281 | } | 290 | } |
282 | 291 | ||