diff options
Diffstat (limited to 'crates/ra_analysis')
-rw-r--r-- | crates/ra_analysis/src/call_info.rs | 249 | ||||
-rw-r--r-- | crates/ra_analysis/tests/test/main.rs | 261 |
2 files changed, 250 insertions, 260 deletions
diff --git a/crates/ra_analysis/src/call_info.rs b/crates/ra_analysis/src/call_info.rs index 8da19a648..2fcd03c9b 100644 --- a/crates/ra_analysis/src/call_info.rs +++ b/crates/ra_analysis/src/call_info.rs | |||
@@ -128,3 +128,252 @@ impl<'a> FnCallNode<'a> { | |||
128 | } | 128 | } |
129 | } | 129 | } |
130 | } | 130 | } |
131 | |||
132 | #[cfg(test)] | ||
133 | mod tests { | ||
134 | use super::*; | ||
135 | |||
136 | use crate::mock_analysis::single_file_with_position; | ||
137 | |||
138 | fn call_info(text: &str) -> CallInfo { | ||
139 | let (analysis, position) = single_file_with_position(text); | ||
140 | analysis.call_info(position).unwrap().unwrap() | ||
141 | } | ||
142 | |||
143 | #[test] | ||
144 | fn test_fn_signature_two_args_first() { | ||
145 | let info = call_info( | ||
146 | r#"fn foo(x: u32, y: u32) -> u32 {x + y} | ||
147 | fn bar() { foo(<|>3, ); }"#, | ||
148 | ); | ||
149 | |||
150 | assert_eq!(info.parameters, vec!("x".to_string(), "y".to_string())); | ||
151 | assert_eq!(info.active_parameter, Some(0)); | ||
152 | } | ||
153 | |||
154 | #[test] | ||
155 | fn test_fn_signature_two_args_second() { | ||
156 | let info = call_info( | ||
157 | r#"fn foo(x: u32, y: u32) -> u32 {x + y} | ||
158 | fn bar() { foo(3, <|>); }"#, | ||
159 | ); | ||
160 | |||
161 | assert_eq!(info.parameters, vec!("x".to_string(), "y".to_string())); | ||
162 | assert_eq!(info.active_parameter, Some(1)); | ||
163 | } | ||
164 | |||
165 | #[test] | ||
166 | fn test_fn_signature_for_impl() { | ||
167 | let info = call_info( | ||
168 | r#"struct F; impl F { pub fn new() { F{}} } | ||
169 | fn bar() {let _ : F = F::new(<|>);}"#, | ||
170 | ); | ||
171 | |||
172 | assert_eq!(info.parameters, Vec::<String>::new()); | ||
173 | assert_eq!(info.active_parameter, None); | ||
174 | } | ||
175 | |||
176 | #[test] | ||
177 | fn test_fn_signature_for_method_self() { | ||
178 | let info = call_info( | ||
179 | r#"struct F; | ||
180 | impl F { | ||
181 | pub fn new() -> F{ | ||
182 | F{} | ||
183 | } | ||
184 | |||
185 | pub fn do_it(&self) {} | ||
186 | } | ||
187 | |||
188 | fn bar() { | ||
189 | let f : F = F::new(); | ||
190 | f.do_it(<|>); | ||
191 | }"#, | ||
192 | ); | ||
193 | |||
194 | assert_eq!(info.parameters, vec!["&self".to_string()]); | ||
195 | assert_eq!(info.active_parameter, None); | ||
196 | } | ||
197 | |||
198 | #[test] | ||
199 | fn test_fn_signature_for_method_with_arg() { | ||
200 | let info = call_info( | ||
201 | r#"struct F; | ||
202 | impl F { | ||
203 | pub fn new() -> F{ | ||
204 | F{} | ||
205 | } | ||
206 | |||
207 | pub fn do_it(&self, x: i32) {} | ||
208 | } | ||
209 | |||
210 | fn bar() { | ||
211 | let f : F = F::new(); | ||
212 | f.do_it(<|>); | ||
213 | }"#, | ||
214 | ); | ||
215 | |||
216 | assert_eq!(info.parameters, vec!["&self".to_string(), "x".to_string()]); | ||
217 | assert_eq!(info.active_parameter, Some(1)); | ||
218 | } | ||
219 | |||
220 | #[test] | ||
221 | fn test_fn_signature_with_docs_simple() { | ||
222 | let info = call_info( | ||
223 | r#" | ||
224 | /// test | ||
225 | // non-doc-comment | ||
226 | fn foo(j: u32) -> u32 { | ||
227 | j | ||
228 | } | ||
229 | |||
230 | fn bar() { | ||
231 | let _ = foo(<|>); | ||
232 | } | ||
233 | "#, | ||
234 | ); | ||
235 | |||
236 | assert_eq!(info.parameters, vec!["j".to_string()]); | ||
237 | assert_eq!(info.active_parameter, Some(0)); | ||
238 | assert_eq!(info.label, "fn foo(j: u32) -> u32".to_string()); | ||
239 | assert_eq!(info.doc, Some("test".into())); | ||
240 | } | ||
241 | |||
242 | #[test] | ||
243 | fn test_fn_signature_with_docs() { | ||
244 | let info = call_info( | ||
245 | r#" | ||
246 | /// Adds one to the number given. | ||
247 | /// | ||
248 | /// # Examples | ||
249 | /// | ||
250 | /// ``` | ||
251 | /// let five = 5; | ||
252 | /// | ||
253 | /// assert_eq!(6, my_crate::add_one(5)); | ||
254 | /// ``` | ||
255 | pub fn add_one(x: i32) -> i32 { | ||
256 | x + 1 | ||
257 | } | ||
258 | |||
259 | pub fn do() { | ||
260 | add_one(<|> | ||
261 | }"#, | ||
262 | ); | ||
263 | |||
264 | assert_eq!(info.parameters, vec!["x".to_string()]); | ||
265 | assert_eq!(info.active_parameter, Some(0)); | ||
266 | assert_eq!(info.label, "pub fn add_one(x: i32) -> i32".to_string()); | ||
267 | assert_eq!( | ||
268 | info.doc, | ||
269 | Some( | ||
270 | r#"Adds one to the number given. | ||
271 | |||
272 | # Examples | ||
273 | |||
274 | ```rust | ||
275 | let five = 5; | ||
276 | |||
277 | assert_eq!(6, my_crate::add_one(5)); | ||
278 | ```"# | ||
279 | .into() | ||
280 | ) | ||
281 | ); | ||
282 | } | ||
283 | |||
284 | #[test] | ||
285 | fn test_fn_signature_with_docs_impl() { | ||
286 | let info = call_info( | ||
287 | r#" | ||
288 | struct addr; | ||
289 | impl addr { | ||
290 | /// Adds one to the number given. | ||
291 | /// | ||
292 | /// # Examples | ||
293 | /// | ||
294 | /// ``` | ||
295 | /// let five = 5; | ||
296 | /// | ||
297 | /// assert_eq!(6, my_crate::add_one(5)); | ||
298 | /// ``` | ||
299 | pub fn add_one(x: i32) -> i32 { | ||
300 | x + 1 | ||
301 | } | ||
302 | } | ||
303 | |||
304 | pub fn do_it() { | ||
305 | addr {}; | ||
306 | addr::add_one(<|>); | ||
307 | }"#, | ||
308 | ); | ||
309 | |||
310 | assert_eq!(info.parameters, vec!["x".to_string()]); | ||
311 | assert_eq!(info.active_parameter, Some(0)); | ||
312 | assert_eq!(info.label, "pub fn add_one(x: i32) -> i32".to_string()); | ||
313 | assert_eq!( | ||
314 | info.doc, | ||
315 | Some( | ||
316 | r#"Adds one to the number given. | ||
317 | |||
318 | # Examples | ||
319 | |||
320 | ```rust | ||
321 | let five = 5; | ||
322 | |||
323 | assert_eq!(6, my_crate::add_one(5)); | ||
324 | ```"# | ||
325 | .into() | ||
326 | ) | ||
327 | ); | ||
328 | } | ||
329 | |||
330 | #[test] | ||
331 | fn test_fn_signature_with_docs_from_actix() { | ||
332 | let info = call_info( | ||
333 | r#" | ||
334 | pub trait WriteHandler<E> | ||
335 | where | ||
336 | Self: Actor, | ||
337 | Self::Context: ActorContext, | ||
338 | { | ||
339 | /// Method is called when writer emits error. | ||
340 | /// | ||
341 | /// If this method returns `ErrorAction::Continue` writer processing | ||
342 | /// continues otherwise stream processing stops. | ||
343 | fn error(&mut self, err: E, ctx: &mut Self::Context) -> Running { | ||
344 | Running::Stop | ||
345 | } | ||
346 | |||
347 | /// Method is called when writer finishes. | ||
348 | /// | ||
349 | /// By default this method stops actor's `Context`. | ||
350 | fn finished(&mut self, ctx: &mut Self::Context) { | ||
351 | ctx.stop() | ||
352 | } | ||
353 | } | ||
354 | |||
355 | pub fn foo() { | ||
356 | WriteHandler r; | ||
357 | r.finished(<|>); | ||
358 | } | ||
359 | |||
360 | "#, | ||
361 | ); | ||
362 | |||
363 | assert_eq!( | ||
364 | info.parameters, | ||
365 | vec!["&mut self".to_string(), "ctx".to_string()] | ||
366 | ); | ||
367 | assert_eq!(info.active_parameter, Some(1)); | ||
368 | assert_eq!( | ||
369 | info.doc, | ||
370 | Some( | ||
371 | r#"Method is called when writer finishes. | ||
372 | |||
373 | By default this method stops actor's `Context`."# | ||
374 | .into() | ||
375 | ) | ||
376 | ); | ||
377 | } | ||
378 | |||
379 | } | ||
diff --git a/crates/ra_analysis/tests/test/main.rs b/crates/ra_analysis/tests/test/main.rs index 1f70af12a..2c0735cb5 100644 --- a/crates/ra_analysis/tests/test/main.rs +++ b/crates/ra_analysis/tests/test/main.rs | |||
@@ -5,14 +5,9 @@ use test_utils::{assert_eq_dbg, assert_eq_text}; | |||
5 | 5 | ||
6 | use ra_analysis::{ | 6 | use ra_analysis::{ |
7 | mock_analysis::{analysis_and_position, single_file, single_file_with_position, MockAnalysis}, | 7 | mock_analysis::{analysis_and_position, single_file, single_file_with_position, MockAnalysis}, |
8 | AnalysisChange, CrateGraph, FileId, FnSignatureInfo, Query | 8 | AnalysisChange, CrateGraph, FileId, Query |
9 | }; | 9 | }; |
10 | 10 | ||
11 | fn get_signature(text: &str) -> (FnSignatureInfo, Option<usize>) { | ||
12 | let (analysis, position) = single_file_with_position(text); | ||
13 | analysis.resolve_callable(position).unwrap().unwrap() | ||
14 | } | ||
15 | |||
16 | #[test] | 11 | #[test] |
17 | fn test_unresolved_module_diagnostic() { | 12 | fn test_unresolved_module_diagnostic() { |
18 | let (analysis, file_id) = single_file("mod foo;"); | 13 | let (analysis, file_id) = single_file("mod foo;"); |
@@ -99,260 +94,6 @@ fn test_resolve_crate_root() { | |||
99 | assert_eq!(host.analysis().crate_for(mod_file).unwrap(), vec![crate_id]); | 94 | assert_eq!(host.analysis().crate_for(mod_file).unwrap(), vec![crate_id]); |
100 | } | 95 | } |
101 | 96 | ||
102 | #[test] | ||
103 | fn test_fn_signature_two_args_first() { | ||
104 | let (desc, param) = get_signature( | ||
105 | r#"fn foo(x: u32, y: u32) -> u32 {x + y} | ||
106 | fn bar() { foo(<|>3, ); }"#, | ||
107 | ); | ||
108 | |||
109 | assert_eq!(desc.name, "foo".to_string()); | ||
110 | assert_eq!(desc.params, vec!("x".to_string(), "y".to_string())); | ||
111 | assert_eq!(desc.ret_type, Some("-> u32".into())); | ||
112 | assert_eq!(param, Some(0)); | ||
113 | } | ||
114 | |||
115 | #[test] | ||
116 | fn test_fn_signature_two_args_second() { | ||
117 | let (desc, param) = get_signature( | ||
118 | r#"fn foo(x: u32, y: u32) -> u32 {x + y} | ||
119 | fn bar() { foo(3, <|>); }"#, | ||
120 | ); | ||
121 | |||
122 | assert_eq!(desc.name, "foo".to_string()); | ||
123 | assert_eq!(desc.params, vec!("x".to_string(), "y".to_string())); | ||
124 | assert_eq!(desc.ret_type, Some("-> u32".into())); | ||
125 | assert_eq!(param, Some(1)); | ||
126 | } | ||
127 | |||
128 | #[test] | ||
129 | fn test_fn_signature_for_impl() { | ||
130 | let (desc, param) = get_signature( | ||
131 | r#"struct F; impl F { pub fn new() { F{}} } | ||
132 | fn bar() {let _ : F = F::new(<|>);}"#, | ||
133 | ); | ||
134 | |||
135 | assert_eq!(desc.name, "new".to_string()); | ||
136 | assert_eq!(desc.params, Vec::<String>::new()); | ||
137 | assert_eq!(desc.ret_type, None); | ||
138 | assert_eq!(param, None); | ||
139 | } | ||
140 | |||
141 | #[test] | ||
142 | fn test_fn_signature_for_method_self() { | ||
143 | let (desc, param) = get_signature( | ||
144 | r#"struct F; | ||
145 | impl F { | ||
146 | pub fn new() -> F{ | ||
147 | F{} | ||
148 | } | ||
149 | |||
150 | pub fn do_it(&self) {} | ||
151 | } | ||
152 | |||
153 | fn bar() { | ||
154 | let f : F = F::new(); | ||
155 | f.do_it(<|>); | ||
156 | }"#, | ||
157 | ); | ||
158 | |||
159 | assert_eq!(desc.name, "do_it".to_string()); | ||
160 | assert_eq!(desc.params, vec!["&self".to_string()]); | ||
161 | assert_eq!(desc.ret_type, None); | ||
162 | assert_eq!(param, None); | ||
163 | } | ||
164 | |||
165 | #[test] | ||
166 | fn test_fn_signature_for_method_with_arg() { | ||
167 | let (desc, param) = get_signature( | ||
168 | r#"struct F; | ||
169 | impl F { | ||
170 | pub fn new() -> F{ | ||
171 | F{} | ||
172 | } | ||
173 | |||
174 | pub fn do_it(&self, x: i32) {} | ||
175 | } | ||
176 | |||
177 | fn bar() { | ||
178 | let f : F = F::new(); | ||
179 | f.do_it(<|>); | ||
180 | }"#, | ||
181 | ); | ||
182 | |||
183 | assert_eq!(desc.name, "do_it".to_string()); | ||
184 | assert_eq!(desc.params, vec!["&self".to_string(), "x".to_string()]); | ||
185 | assert_eq!(desc.ret_type, None); | ||
186 | assert_eq!(param, Some(1)); | ||
187 | } | ||
188 | |||
189 | #[test] | ||
190 | fn test_fn_signature_with_docs_simple() { | ||
191 | let (desc, param) = get_signature( | ||
192 | r#" | ||
193 | /// test | ||
194 | // non-doc-comment | ||
195 | fn foo(j: u32) -> u32 { | ||
196 | j | ||
197 | } | ||
198 | |||
199 | fn bar() { | ||
200 | let _ = foo(<|>); | ||
201 | } | ||
202 | "#, | ||
203 | ); | ||
204 | |||
205 | assert_eq!(desc.name, "foo".to_string()); | ||
206 | assert_eq!(desc.params, vec!["j".to_string()]); | ||
207 | assert_eq!(desc.ret_type, Some("-> u32".to_string())); | ||
208 | assert_eq!(param, Some(0)); | ||
209 | assert_eq!(desc.label, "fn foo(j: u32) -> u32".to_string()); | ||
210 | assert_eq!(desc.doc, Some("test".into())); | ||
211 | } | ||
212 | |||
213 | #[test] | ||
214 | fn test_fn_signature_with_docs() { | ||
215 | let (desc, param) = get_signature( | ||
216 | r#" | ||
217 | /// Adds one to the number given. | ||
218 | /// | ||
219 | /// # Examples | ||
220 | /// | ||
221 | /// ``` | ||
222 | /// let five = 5; | ||
223 | /// | ||
224 | /// assert_eq!(6, my_crate::add_one(5)); | ||
225 | /// ``` | ||
226 | pub fn add_one(x: i32) -> i32 { | ||
227 | x + 1 | ||
228 | } | ||
229 | |||
230 | pub fn do() { | ||
231 | add_one(<|> | ||
232 | }"#, | ||
233 | ); | ||
234 | |||
235 | assert_eq!(desc.name, "add_one".to_string()); | ||
236 | assert_eq!(desc.params, vec!["x".to_string()]); | ||
237 | assert_eq!(desc.ret_type, Some("-> i32".to_string())); | ||
238 | assert_eq!(param, Some(0)); | ||
239 | assert_eq!(desc.label, "pub fn add_one(x: i32) -> i32".to_string()); | ||
240 | assert_eq!( | ||
241 | desc.doc, | ||
242 | Some( | ||
243 | r#"Adds one to the number given. | ||
244 | |||
245 | # Examples | ||
246 | |||
247 | ```rust | ||
248 | let five = 5; | ||
249 | |||
250 | assert_eq!(6, my_crate::add_one(5)); | ||
251 | ```"# | ||
252 | .into() | ||
253 | ) | ||
254 | ); | ||
255 | } | ||
256 | |||
257 | #[test] | ||
258 | fn test_fn_signature_with_docs_impl() { | ||
259 | let (desc, param) = get_signature( | ||
260 | r#" | ||
261 | struct addr; | ||
262 | impl addr { | ||
263 | /// Adds one to the number given. | ||
264 | /// | ||
265 | /// # Examples | ||
266 | /// | ||
267 | /// ``` | ||
268 | /// let five = 5; | ||
269 | /// | ||
270 | /// assert_eq!(6, my_crate::add_one(5)); | ||
271 | /// ``` | ||
272 | pub fn add_one(x: i32) -> i32 { | ||
273 | x + 1 | ||
274 | } | ||
275 | } | ||
276 | |||
277 | pub fn do_it() { | ||
278 | addr {}; | ||
279 | addr::add_one(<|>); | ||
280 | }"#, | ||
281 | ); | ||
282 | |||
283 | assert_eq!(desc.name, "add_one".to_string()); | ||
284 | assert_eq!(desc.params, vec!["x".to_string()]); | ||
285 | assert_eq!(desc.ret_type, Some("-> i32".to_string())); | ||
286 | assert_eq!(param, Some(0)); | ||
287 | assert_eq!(desc.label, "pub fn add_one(x: i32) -> i32".to_string()); | ||
288 | assert_eq!( | ||
289 | desc.doc, | ||
290 | Some( | ||
291 | r#"Adds one to the number given. | ||
292 | |||
293 | # Examples | ||
294 | |||
295 | ```rust | ||
296 | let five = 5; | ||
297 | |||
298 | assert_eq!(6, my_crate::add_one(5)); | ||
299 | ```"# | ||
300 | .into() | ||
301 | ) | ||
302 | ); | ||
303 | } | ||
304 | |||
305 | #[test] | ||
306 | fn test_fn_signature_with_docs_from_actix() { | ||
307 | let (desc, param) = get_signature( | ||
308 | r#" | ||
309 | pub trait WriteHandler<E> | ||
310 | where | ||
311 | Self: Actor, | ||
312 | Self::Context: ActorContext, | ||
313 | { | ||
314 | /// Method is called when writer emits error. | ||
315 | /// | ||
316 | /// If this method returns `ErrorAction::Continue` writer processing | ||
317 | /// continues otherwise stream processing stops. | ||
318 | fn error(&mut self, err: E, ctx: &mut Self::Context) -> Running { | ||
319 | Running::Stop | ||
320 | } | ||
321 | |||
322 | /// Method is called when writer finishes. | ||
323 | /// | ||
324 | /// By default this method stops actor's `Context`. | ||
325 | fn finished(&mut self, ctx: &mut Self::Context) { | ||
326 | ctx.stop() | ||
327 | } | ||
328 | } | ||
329 | |||
330 | pub fn foo() { | ||
331 | WriteHandler r; | ||
332 | r.finished(<|>); | ||
333 | } | ||
334 | |||
335 | "#, | ||
336 | ); | ||
337 | |||
338 | assert_eq!(desc.name, "finished".to_string()); | ||
339 | assert_eq!( | ||
340 | desc.params, | ||
341 | vec!["&mut self".to_string(), "ctx".to_string()] | ||
342 | ); | ||
343 | assert_eq!(desc.ret_type, None); | ||
344 | assert_eq!(param, Some(1)); | ||
345 | assert_eq!( | ||
346 | desc.doc, | ||
347 | Some( | ||
348 | r#"Method is called when writer finishes. | ||
349 | |||
350 | By default this method stops actor's `Context`."# | ||
351 | .into() | ||
352 | ) | ||
353 | ); | ||
354 | } | ||
355 | |||
356 | fn get_all_refs(text: &str) -> Vec<(FileId, TextRange)> { | 97 | fn get_all_refs(text: &str) -> Vec<(FileId, TextRange)> { |
357 | let (analysis, position) = single_file_with_position(text); | 98 | let (analysis, position) = single_file_with_position(text); |
358 | analysis.find_all_refs(position).unwrap() | 99 | analysis.find_all_refs(position).unwrap() |