1
1
//! This module contains functions to suggest names for expressions, functions and other items
2
2
3
3
use hir:: Semantics ;
4
- use ide_db:: { FxHashSet , RootDatabase } ;
5
4
use itertools:: Itertools ;
5
+ use rustc_hash:: FxHashSet ;
6
6
use stdx:: to_lower_snake_case;
7
7
use syntax:: {
8
8
ast:: { self , HasName } ,
9
9
match_ast, AstNode , Edition , SmolStr ,
10
10
} ;
11
11
12
+ use crate :: RootDatabase ;
13
+
12
14
/// Trait names, that will be ignored when in `impl Trait` and `dyn Trait`
13
15
const USELESS_TRAITS : & [ & str ] = & [ "Send" , "Sync" , "Copy" , "Clone" , "Eq" , "PartialEq" ] ;
14
16
@@ -58,6 +60,21 @@ const USELESS_METHODS: &[&str] = &[
58
60
"into_future" ,
59
61
] ;
60
62
63
+ /// Suggest a name for given type.
64
+ ///
65
+ /// The function will strip references first, and suggest name from the inner type.
66
+ ///
67
+ /// - If `ty` is an ADT, it will suggest the name of the ADT.
68
+ /// + If `ty` is wrapped in `Box`, `Option` or `Result`, it will suggest the name from the inner type.
69
+ /// - If `ty` is a trait, it will suggest the name of the trait.
70
+ /// - If `ty` is an `impl Trait`, it will suggest the name of the first trait.
71
+ ///
72
+ /// If the suggested name conflicts with reserved keywords, it will return `None`.
73
+ pub fn for_type ( ty : & hir:: Type , db : & RootDatabase , edition : Edition ) -> Option < String > {
74
+ let ty = ty. strip_references ( ) ;
75
+ name_of_type ( & ty, db, edition)
76
+ }
77
+
61
78
/// Suggest a unique name for generic parameter.
62
79
///
63
80
/// `existing_params` is used to check if the name conflicts with existing
@@ -66,10 +83,7 @@ const USELESS_METHODS: &[&str] = &[
66
83
/// The function checks if the name conflicts with existing generic parameters.
67
84
/// If so, it will try to resolve the conflict by adding a number suffix, e.g.
68
85
/// `T`, `T0`, `T1`, ...
69
- pub ( crate ) fn for_unique_generic_name (
70
- name : & str ,
71
- existing_params : & ast:: GenericParamList ,
72
- ) -> SmolStr {
86
+ pub fn for_unique_generic_name ( name : & str , existing_params : & ast:: GenericParamList ) -> SmolStr {
73
87
let param_names = existing_params
74
88
. generic_params ( )
75
89
. map ( |param| match param {
@@ -101,7 +115,7 @@ pub(crate) fn for_unique_generic_name(
101
115
///
102
116
/// If the name conflicts with existing generic parameters, it will try to
103
117
/// resolve the conflict with `for_unique_generic_name`.
104
- pub ( crate ) fn for_impl_trait_as_generic (
118
+ pub fn for_impl_trait_as_generic (
105
119
ty : & ast:: ImplTraitType ,
106
120
existing_params : & ast:: GenericParamList ,
107
121
) -> SmolStr {
@@ -132,7 +146,7 @@ pub(crate) fn for_impl_trait_as_generic(
132
146
///
133
147
/// Currently it sticks to the first name found.
134
148
// FIXME: Microoptimize and return a `SmolStr` here.
135
- pub ( crate ) fn for_variable ( expr : & ast:: Expr , sema : & Semantics < ' _ , RootDatabase > ) -> String {
149
+ pub fn for_variable ( expr : & ast:: Expr , sema : & Semantics < ' _ , RootDatabase > ) -> String {
136
150
// `from_param` does not benefit from stripping
137
151
// it need the largest context possible
138
152
// so we check firstmost
@@ -184,7 +198,7 @@ fn normalize(name: &str) -> Option<String> {
184
198
185
199
fn is_valid_name ( name : & str ) -> bool {
186
200
matches ! (
187
- ide_db :: syntax_helpers :: LexedStr :: single_token( syntax:: Edition :: CURRENT_FIXME , name) ,
201
+ super :: LexedStr :: single_token( syntax:: Edition :: CURRENT_FIXME , name) ,
188
202
Some ( ( syntax:: SyntaxKind :: IDENT , _error) )
189
203
)
190
204
}
@@ -270,10 +284,9 @@ fn var_name_from_pat(pat: &ast::Pat) -> Option<ast::Name> {
270
284
271
285
fn from_type ( expr : & ast:: Expr , sema : & Semantics < ' _ , RootDatabase > ) -> Option < String > {
272
286
let ty = sema. type_of_expr ( expr) ?. adjusted ( ) ;
273
- let ty = ty. remove_ref ( ) . unwrap_or ( ty) ;
274
287
let edition = sema. scope ( expr. syntax ( ) ) ?. krate ( ) . edition ( sema. db ) ;
275
288
276
- name_of_type ( & ty, sema. db , edition)
289
+ for_type ( & ty, sema. db , edition)
277
290
}
278
291
279
292
fn name_of_type ( ty : & hir:: Type , db : & RootDatabase , edition : Edition ) -> Option < String > {
0 commit comments