diff options
author | Conrad Ludgate <[email protected]> | 2021-03-01 10:51:47 +0000 |
---|---|---|
committer | Conrad Ludgate <[email protected]> | 2021-03-02 14:30:10 +0000 |
commit | 705712993ffe24898e3c1fe006e1108b7d02d6bc (patch) | |
tree | e3f40b0432e0dff4e1f53f37851c56ac9a94fc7e /crates/ide_assists/src | |
parent | f8152171bbe160c4273d692d42c06eb7c6d66e1a (diff) |
feat: add type ascription assist
Diffstat (limited to 'crates/ide_assists/src')
-rw-r--r-- | crates/ide_assists/src/handlers/add_type_ascription.rs | 198 | ||||
-rw-r--r-- | crates/ide_assists/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ide_assists/src/tests/generated.rs | 19 |
3 files changed, 219 insertions, 0 deletions
diff --git a/crates/ide_assists/src/handlers/add_type_ascription.rs b/crates/ide_assists/src/handlers/add_type_ascription.rs new file mode 100644 index 000000000..e9dc37150 --- /dev/null +++ b/crates/ide_assists/src/handlers/add_type_ascription.rs | |||
@@ -0,0 +1,198 @@ | |||
1 | use ide_db::defs::{Definition, NameRefClass}; | ||
2 | use syntax::{ast, AstNode, SyntaxKind, T}; | ||
3 | use test_utils::mark; | ||
4 | |||
5 | use crate::{ | ||
6 | assist_context::{AssistContext, Assists}, | ||
7 | AssistId, AssistKind, | ||
8 | }; | ||
9 | |||
10 | // Assist: add_type_ascription | ||
11 | // | ||
12 | // Adds `: _` before the assignment operator to prompt the user for a type | ||
13 | // | ||
14 | // ``` | ||
15 | // fn make<T>() -> T { todo!() } | ||
16 | // fn main() { | ||
17 | // let x = make$0(); | ||
18 | // } | ||
19 | // ``` | ||
20 | // -> | ||
21 | // ``` | ||
22 | // fn make<T>() -> T { todo!() } | ||
23 | // fn main() { | ||
24 | // let x: ${0:_} = make(); | ||
25 | // } | ||
26 | // ``` | ||
27 | pub(crate) fn add_type_ascription(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | ||
28 | let let_stmt = ctx.find_node_at_offset::<ast::LetStmt>()?; | ||
29 | if let_stmt.colon_token().is_some() { | ||
30 | mark::hit!(add_type_ascription_already_typed); | ||
31 | return None | ||
32 | } | ||
33 | |||
34 | let ident = ctx.find_token_syntax_at_offset(SyntaxKind::IDENT).or_else(|| { | ||
35 | let arg_list = ctx.find_node_at_offset::<ast::ArgList>()?; | ||
36 | if arg_list.args().count() > 0 { | ||
37 | return None; | ||
38 | } | ||
39 | mark::hit!(add_type_ascription_after_call); | ||
40 | arg_list.l_paren_token()?.prev_token().filter(|it| it.kind() == SyntaxKind::IDENT) | ||
41 | })?; | ||
42 | let next_token = ident.next_token()?; | ||
43 | if next_token.kind() == T![::] { | ||
44 | mark::hit!(add_type_ascription_turbofished); | ||
45 | return None; | ||
46 | } | ||
47 | let name_ref = ast::NameRef::cast(ident.parent())?; | ||
48 | let def = match NameRefClass::classify(&ctx.sema, &name_ref)? { | ||
49 | NameRefClass::Definition(def) => def, | ||
50 | NameRefClass::ExternCrate(_) | NameRefClass::FieldShorthand { .. } => return None, | ||
51 | }; | ||
52 | let fun = match def { | ||
53 | Definition::ModuleDef(hir::ModuleDef::Function(it)) => it, | ||
54 | _ => return None, | ||
55 | }; | ||
56 | let generics = hir::GenericDef::Function(fun).params(ctx.sema.db); | ||
57 | if generics.is_empty() { | ||
58 | mark::hit!(add_type_ascription_non_generic); | ||
59 | return None; | ||
60 | } | ||
61 | let pat = let_stmt.pat()?.syntax().last_token()?.text_range().end(); | ||
62 | acc.add( | ||
63 | AssistId("add_type_ascription", AssistKind::RefactorRewrite), | ||
64 | "Add `: _` before assignment operator", | ||
65 | ident.text_range(), | ||
66 | |builder| match ctx.config.snippet_cap { | ||
67 | Some(cap) => builder.insert_snippet(cap, pat, ": ${0:_}"), | ||
68 | None => builder.insert(pat, ": _"), | ||
69 | }, | ||
70 | ) | ||
71 | } | ||
72 | |||
73 | #[cfg(test)] | ||
74 | mod tests { | ||
75 | use crate::tests::{check_assist, check_assist_not_applicable}; | ||
76 | |||
77 | use super::*; | ||
78 | use test_utils::mark; | ||
79 | |||
80 | #[test] | ||
81 | fn add_type_ascription_function() { | ||
82 | check_assist( | ||
83 | add_type_ascription, | ||
84 | r#" | ||
85 | fn make<T>() -> T {} | ||
86 | fn main() { | ||
87 | let x = make$0(); | ||
88 | } | ||
89 | "#, | ||
90 | r#" | ||
91 | fn make<T>() -> T {} | ||
92 | fn main() { | ||
93 | let x: ${0:_} = make(); | ||
94 | } | ||
95 | "#, | ||
96 | ); | ||
97 | } | ||
98 | |||
99 | #[test] | ||
100 | fn add_type_ascription_after_call() { | ||
101 | mark::check!(add_type_ascription_after_call); | ||
102 | check_assist( | ||
103 | add_type_ascription, | ||
104 | r#" | ||
105 | fn make<T>() -> T {} | ||
106 | fn main() { | ||
107 | let x = make()$0; | ||
108 | } | ||
109 | "#, | ||
110 | r#" | ||
111 | fn make<T>() -> T {} | ||
112 | fn main() { | ||
113 | let x: ${0:_} = make(); | ||
114 | } | ||
115 | "#, | ||
116 | ); | ||
117 | } | ||
118 | |||
119 | #[test] | ||
120 | fn add_type_ascription_method() { | ||
121 | check_assist( | ||
122 | add_type_ascription, | ||
123 | r#" | ||
124 | struct S; | ||
125 | impl S { | ||
126 | fn make<T>(&self) -> T {} | ||
127 | } | ||
128 | fn main() { | ||
129 | let x = S.make$0(); | ||
130 | } | ||
131 | "#, | ||
132 | r#" | ||
133 | struct S; | ||
134 | impl S { | ||
135 | fn make<T>(&self) -> T {} | ||
136 | } | ||
137 | fn main() { | ||
138 | let x: ${0:_} = S.make(); | ||
139 | } | ||
140 | "#, | ||
141 | ); | ||
142 | } | ||
143 | |||
144 | #[test] | ||
145 | fn add_type_ascription_turbofished() { | ||
146 | mark::check!(add_type_ascription_turbofished); | ||
147 | check_assist_not_applicable( | ||
148 | add_type_ascription, | ||
149 | r#" | ||
150 | fn make<T>() -> T {} | ||
151 | fn main() { | ||
152 | let x = make$0::<()>(); | ||
153 | } | ||
154 | "#, | ||
155 | ); | ||
156 | } | ||
157 | |||
158 | #[test] | ||
159 | fn add_type_ascription_already_typed() { | ||
160 | mark::check!(add_type_ascription_already_typed); | ||
161 | check_assist_not_applicable( | ||
162 | add_type_ascription, | ||
163 | r#" | ||
164 | fn make<T>() -> T {} | ||
165 | fn main() { | ||
166 | let x: () = make$0(); | ||
167 | } | ||
168 | "#, | ||
169 | ); | ||
170 | } | ||
171 | |||
172 | #[test] | ||
173 | fn add_type_ascription_non_generic() { | ||
174 | mark::check!(add_type_ascription_non_generic); | ||
175 | check_assist_not_applicable( | ||
176 | add_type_ascription, | ||
177 | r#" | ||
178 | fn make() -> () {} | ||
179 | fn main() { | ||
180 | let x = make$0(); | ||
181 | } | ||
182 | "#, | ||
183 | ); | ||
184 | } | ||
185 | |||
186 | #[test] | ||
187 | fn add_type_ascription_no_let() { | ||
188 | check_assist_not_applicable( | ||
189 | add_type_ascription, | ||
190 | r#" | ||
191 | fn make<T>() -> T {} | ||
192 | fn main() { | ||
193 | make$0(); | ||
194 | } | ||
195 | "#, | ||
196 | ); | ||
197 | } | ||
198 | } | ||
diff --git a/crates/ide_assists/src/lib.rs b/crates/ide_assists/src/lib.rs index 9c8148462..0248cb9c0 100644 --- a/crates/ide_assists/src/lib.rs +++ b/crates/ide_assists/src/lib.rs | |||
@@ -111,6 +111,7 @@ mod handlers { | |||
111 | mod add_lifetime_to_type; | 111 | mod add_lifetime_to_type; |
112 | mod add_missing_impl_members; | 112 | mod add_missing_impl_members; |
113 | mod add_turbo_fish; | 113 | mod add_turbo_fish; |
114 | mod add_type_ascription; | ||
114 | mod apply_demorgan; | 115 | mod apply_demorgan; |
115 | mod auto_import; | 116 | mod auto_import; |
116 | mod change_visibility; | 117 | mod change_visibility; |
@@ -175,6 +176,7 @@ mod handlers { | |||
175 | add_explicit_type::add_explicit_type, | 176 | add_explicit_type::add_explicit_type, |
176 | add_lifetime_to_type::add_lifetime_to_type, | 177 | add_lifetime_to_type::add_lifetime_to_type, |
177 | add_turbo_fish::add_turbo_fish, | 178 | add_turbo_fish::add_turbo_fish, |
179 | add_type_ascription::add_type_ascription, | ||
178 | apply_demorgan::apply_demorgan, | 180 | apply_demorgan::apply_demorgan, |
179 | auto_import::auto_import, | 181 | auto_import::auto_import, |
180 | change_visibility::change_visibility, | 182 | change_visibility::change_visibility, |
diff --git a/crates/ide_assists/src/tests/generated.rs b/crates/ide_assists/src/tests/generated.rs index 4f007aa48..439ee8b22 100644 --- a/crates/ide_assists/src/tests/generated.rs +++ b/crates/ide_assists/src/tests/generated.rs | |||
@@ -142,6 +142,25 @@ fn main() { | |||
142 | } | 142 | } |
143 | 143 | ||
144 | #[test] | 144 | #[test] |
145 | fn doctest_add_type_ascription() { | ||
146 | check_doc_test( | ||
147 | "add_type_ascription", | ||
148 | r#####" | ||
149 | fn make<T>() -> T { todo!() } | ||
150 | fn main() { | ||
151 | let x = make$0(); | ||
152 | } | ||
153 | "#####, | ||
154 | r#####" | ||
155 | fn make<T>() -> T { todo!() } | ||
156 | fn main() { | ||
157 | let x: ${0:_} = make(); | ||
158 | } | ||
159 | "#####, | ||
160 | ) | ||
161 | } | ||
162 | |||
163 | #[test] | ||
145 | fn doctest_apply_demorgan() { | 164 | fn doctest_apply_demorgan() { |
146 | check_doc_test( | 165 | check_doc_test( |
147 | "apply_demorgan", | 166 | "apply_demorgan", |