diff options
Diffstat (limited to 'crates/ra_ide/src/call_info.rs')
-rw-r--r-- | crates/ra_ide/src/call_info.rs | 789 |
1 files changed, 422 insertions, 367 deletions
diff --git a/crates/ra_ide/src/call_info.rs b/crates/ra_ide/src/call_info.rs index a6bdf1c9d..ff602202f 100644 --- a/crates/ra_ide/src/call_info.rs +++ b/crates/ra_ide/src/call_info.rs | |||
@@ -1,13 +1,43 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | use hir::Semantics; | 2 | use either::Either; |
3 | use hir::{Docs, HirDisplay, Semantics, Type}; | ||
3 | use ra_ide_db::RootDatabase; | 4 | use ra_ide_db::RootDatabase; |
4 | use ra_syntax::{ | 5 | use ra_syntax::{ |
5 | ast::{self, ArgListOwner}, | 6 | ast::{self, ArgListOwner}, |
6 | match_ast, AstNode, SyntaxNode, SyntaxToken, | 7 | match_ast, AstNode, SyntaxNode, SyntaxToken, TextRange, TextSize, |
7 | }; | 8 | }; |
9 | use stdx::format_to; | ||
8 | use test_utils::mark; | 10 | use test_utils::mark; |
9 | 11 | ||
10 | use crate::{CallInfo, FilePosition, FunctionSignature}; | 12 | use crate::FilePosition; |
13 | |||
14 | /// Contains information about a call site. Specifically the | ||
15 | /// `FunctionSignature`and current parameter. | ||
16 | #[derive(Debug)] | ||
17 | pub struct CallInfo { | ||
18 | pub doc: Option<String>, | ||
19 | pub signature: String, | ||
20 | pub active_parameter: Option<usize>, | ||
21 | parameters: Vec<TextRange>, | ||
22 | } | ||
23 | |||
24 | impl CallInfo { | ||
25 | pub fn parameter_labels(&self) -> impl Iterator<Item = &str> + '_ { | ||
26 | self.parameters.iter().map(move |&it| &self.signature[it]) | ||
27 | } | ||
28 | pub fn parameter_ranges(&self) -> &[TextRange] { | ||
29 | &self.parameters | ||
30 | } | ||
31 | fn push_param(&mut self, param: &str) { | ||
32 | if !self.signature.ends_with('(') { | ||
33 | self.signature.push_str(", "); | ||
34 | } | ||
35 | let start = TextSize::of(&self.signature); | ||
36 | self.signature.push_str(param); | ||
37 | let end = TextSize::of(&self.signature); | ||
38 | self.parameters.push(TextRange::new(start, end)) | ||
39 | } | ||
40 | } | ||
11 | 41 | ||
12 | /// Computes parameter information for the given call expression. | 42 | /// Computes parameter information for the given call expression. |
13 | pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<CallInfo> { | 43 | pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<CallInfo> { |
@@ -16,106 +46,135 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<Cal | |||
16 | let file = file.syntax(); | 46 | let file = file.syntax(); |
17 | let token = file.token_at_offset(position.offset).next()?; | 47 | let token = file.token_at_offset(position.offset).next()?; |
18 | let token = sema.descend_into_macros(token); | 48 | let token = sema.descend_into_macros(token); |
19 | call_info_for_token(&sema, token) | ||
20 | } | ||
21 | 49 | ||
22 | #[derive(Debug)] | 50 | let (callable, active_parameter) = call_info_impl(&sema, token)?; |
23 | pub(crate) struct ActiveParameter { | ||
24 | /// FIXME: should be `Type` and `Name | ||
25 | pub(crate) ty: String, | ||
26 | pub(crate) name: String, | ||
27 | } | ||
28 | 51 | ||
29 | impl ActiveParameter { | 52 | let mut res = |
30 | pub(crate) fn at(db: &RootDatabase, position: FilePosition) -> Option<Self> { | 53 | CallInfo { doc: None, signature: String::new(), parameters: vec![], active_parameter }; |
31 | call_info(db, position)?.into_active_parameter() | 54 | |
55 | match callable.kind() { | ||
56 | hir::CallableKind::Function(func) => { | ||
57 | res.doc = func.docs(db).map(|it| it.as_str().to_string()); | ||
58 | format_to!(res.signature, "fn {}", func.name(db)); | ||
59 | } | ||
60 | hir::CallableKind::TupleStruct(strukt) => { | ||
61 | res.doc = strukt.docs(db).map(|it| it.as_str().to_string()); | ||
62 | format_to!(res.signature, "struct {}", strukt.name(db)); | ||
63 | } | ||
64 | hir::CallableKind::TupleEnumVariant(variant) => { | ||
65 | res.doc = variant.docs(db).map(|it| it.as_str().to_string()); | ||
66 | format_to!( | ||
67 | res.signature, | ||
68 | "enum {}::{}", | ||
69 | variant.parent_enum(db).name(db), | ||
70 | variant.name(db) | ||
71 | ); | ||
72 | } | ||
73 | hir::CallableKind::Closure => (), | ||
32 | } | 74 | } |
33 | 75 | ||
34 | pub(crate) fn at_token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Option<Self> { | 76 | res.signature.push('('); |
35 | call_info_for_token(sema, token)?.into_active_parameter() | 77 | { |
78 | if let Some(self_param) = callable.receiver_param(db) { | ||
79 | format_to!(res.signature, "{}", self_param) | ||
80 | } | ||
81 | let mut buf = String::new(); | ||
82 | for (pat, ty) in callable.params(db) { | ||
83 | buf.clear(); | ||
84 | if let Some(pat) = pat { | ||
85 | match pat { | ||
86 | Either::Left(_self) => format_to!(buf, "self: "), | ||
87 | Either::Right(pat) => format_to!(buf, "{}: ", pat), | ||
88 | } | ||
89 | } | ||
90 | format_to!(buf, "{}", ty.display(db)); | ||
91 | res.push_param(&buf); | ||
92 | } | ||
36 | } | 93 | } |
94 | res.signature.push(')'); | ||
95 | |||
96 | match callable.kind() { | ||
97 | hir::CallableKind::Function(_) | hir::CallableKind::Closure => { | ||
98 | let ret_type = callable.return_type(); | ||
99 | if !ret_type.is_unit() { | ||
100 | format_to!(res.signature, " -> {}", ret_type.display(db)); | ||
101 | } | ||
102 | } | ||
103 | hir::CallableKind::TupleStruct(_) | hir::CallableKind::TupleEnumVariant(_) => {} | ||
104 | } | ||
105 | Some(res) | ||
37 | } | 106 | } |
38 | 107 | ||
39 | fn call_info_for_token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Option<CallInfo> { | 108 | fn call_info_impl( |
109 | sema: &Semantics<RootDatabase>, | ||
110 | token: SyntaxToken, | ||
111 | ) -> Option<(hir::Callable, Option<usize>)> { | ||
40 | // Find the calling expression and it's NameRef | 112 | // Find the calling expression and it's NameRef |
41 | let calling_node = FnCallNode::with_node(&token.parent())?; | 113 | let calling_node = FnCallNode::with_node(&token.parent())?; |
42 | 114 | ||
43 | let (mut call_info, has_self) = match &calling_node { | 115 | let callable = match &calling_node { |
44 | FnCallNode::CallExpr(call) => { | 116 | FnCallNode::CallExpr(call) => sema.type_of_expr(&call.expr()?)?.as_callable(sema.db)?, |
45 | //FIXME: Type::as_callable is broken | 117 | FnCallNode::MethodCallExpr(call) => sema.resolve_method_call_as_callable(call)?, |
46 | let callable_def = sema.type_of_expr(&call.expr()?)?.as_callable()?; | 118 | }; |
47 | match callable_def { | 119 | let active_param = if let Some(arg_list) = calling_node.arg_list() { |
48 | hir::CallableDef::FunctionId(it) => { | 120 | // Number of arguments specified at the call site |
49 | let fn_def = it.into(); | 121 | let num_args_at_callsite = arg_list.args().count(); |
50 | (CallInfo::with_fn(sema.db, fn_def), fn_def.has_self_param(sema.db)) | 122 | |
51 | } | 123 | let arg_list_range = arg_list.syntax().text_range(); |
52 | hir::CallableDef::StructId(it) => { | 124 | if !arg_list_range.contains_inclusive(token.text_range().start()) { |
53 | (CallInfo::with_struct(sema.db, it.into())?, false) | 125 | mark::hit!(call_info_bad_offset); |
54 | } | 126 | return None; |
55 | hir::CallableDef::EnumVariantId(it) => { | ||
56 | (CallInfo::with_enum_variant(sema.db, it.into())?, false) | ||
57 | } | ||
58 | } | ||
59 | } | ||
60 | FnCallNode::MethodCallExpr(method_call) => { | ||
61 | let function = sema.resolve_method_call(&method_call)?; | ||
62 | (CallInfo::with_fn(sema.db, function), function.has_self_param(sema.db)) | ||
63 | } | ||
64 | FnCallNode::MacroCallExpr(macro_call) => { | ||
65 | let macro_def = sema.resolve_macro_call(¯o_call)?; | ||
66 | (CallInfo::with_macro(sema.db, macro_def)?, false) | ||
67 | } | 127 | } |
128 | let param = std::cmp::min( | ||
129 | num_args_at_callsite, | ||
130 | arg_list | ||
131 | .args() | ||
132 | .take_while(|arg| arg.syntax().text_range().end() <= token.text_range().start()) | ||
133 | .count(), | ||
134 | ); | ||
135 | |||
136 | Some(param) | ||
137 | } else { | ||
138 | None | ||
68 | }; | 139 | }; |
140 | Some((callable, active_param)) | ||
141 | } | ||
69 | 142 | ||
70 | // If we have a calling expression let's find which argument we are on | 143 | #[derive(Debug)] |
71 | let num_params = call_info.parameters().len(); | 144 | pub(crate) struct ActiveParameter { |
145 | pub(crate) ty: Type, | ||
146 | pub(crate) name: String, | ||
147 | } | ||
72 | 148 | ||
73 | match num_params { | 149 | impl ActiveParameter { |
74 | 0 => (), | 150 | pub(crate) fn at(db: &RootDatabase, position: FilePosition) -> Option<Self> { |
75 | 1 => { | 151 | let sema = Semantics::new(db); |
76 | if !has_self { | 152 | let file = sema.parse(position.file_id); |
77 | call_info.active_parameter = Some(0); | 153 | let file = file.syntax(); |
78 | } | 154 | let token = file.token_at_offset(position.offset).next()?; |
79 | } | 155 | let token = sema.descend_into_macros(token); |
80 | _ => { | 156 | Self::at_token(&sema, token) |
81 | if let Some(arg_list) = calling_node.arg_list() { | 157 | } |
82 | // Number of arguments specified at the call site | ||
83 | let num_args_at_callsite = arg_list.args().count(); | ||
84 | |||
85 | let arg_list_range = arg_list.syntax().text_range(); | ||
86 | if !arg_list_range.contains_inclusive(token.text_range().start()) { | ||
87 | mark::hit!(call_info_bad_offset); | ||
88 | return None; | ||
89 | } | ||
90 | 158 | ||
91 | let mut param = std::cmp::min( | 159 | pub(crate) fn at_token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Option<Self> { |
92 | num_args_at_callsite, | 160 | let (signature, active_parameter) = call_info_impl(&sema, token)?; |
93 | arg_list | ||
94 | .args() | ||
95 | .take_while(|arg| { | ||
96 | arg.syntax().text_range().end() < token.text_range().start() | ||
97 | }) | ||
98 | .count(), | ||
99 | ); | ||
100 | |||
101 | // If we are in a method account for `self` | ||
102 | if has_self { | ||
103 | param += 1; | ||
104 | } | ||
105 | 161 | ||
106 | call_info.active_parameter = Some(param); | 162 | let idx = active_parameter?; |
107 | } | 163 | let mut params = signature.params(sema.db); |
164 | if !(idx < params.len()) { | ||
165 | mark::hit!(too_many_arguments); | ||
166 | return None; | ||
108 | } | 167 | } |
168 | let (pat, ty) = params.swap_remove(idx); | ||
169 | let name = pat?.to_string(); | ||
170 | Some(ActiveParameter { ty, name }) | ||
109 | } | 171 | } |
110 | |||
111 | Some(call_info) | ||
112 | } | 172 | } |
113 | 173 | ||
114 | #[derive(Debug)] | 174 | #[derive(Debug)] |
115 | pub(crate) enum FnCallNode { | 175 | pub(crate) enum FnCallNode { |
116 | CallExpr(ast::CallExpr), | 176 | CallExpr(ast::CallExpr), |
117 | MethodCallExpr(ast::MethodCallExpr), | 177 | MethodCallExpr(ast::MethodCallExpr), |
118 | MacroCallExpr(ast::MacroCall), | ||
119 | } | 178 | } |
120 | 179 | ||
121 | impl FnCallNode { | 180 | impl FnCallNode { |
@@ -131,7 +190,6 @@ impl FnCallNode { | |||
131 | } | 190 | } |
132 | Some(FnCallNode::MethodCallExpr(it)) | 191 | Some(FnCallNode::MethodCallExpr(it)) |
133 | }, | 192 | }, |
134 | ast::MacroCall(it) => Some(FnCallNode::MacroCallExpr(it)), | ||
135 | _ => None, | 193 | _ => None, |
136 | } | 194 | } |
137 | } | 195 | } |
@@ -143,7 +201,6 @@ impl FnCallNode { | |||
143 | match node { | 201 | match node { |
144 | ast::CallExpr(it) => Some(FnCallNode::CallExpr(it)), | 202 | ast::CallExpr(it) => Some(FnCallNode::CallExpr(it)), |
145 | ast::MethodCallExpr(it) => Some(FnCallNode::MethodCallExpr(it)), | 203 | ast::MethodCallExpr(it) => Some(FnCallNode::MethodCallExpr(it)), |
146 | ast::MacroCall(it) => Some(FnCallNode::MacroCallExpr(it)), | ||
147 | _ => None, | 204 | _ => None, |
148 | } | 205 | } |
149 | } | 206 | } |
@@ -159,8 +216,6 @@ impl FnCallNode { | |||
159 | FnCallNode::MethodCallExpr(call_expr) => { | 216 | FnCallNode::MethodCallExpr(call_expr) => { |
160 | call_expr.syntax().children().filter_map(ast::NameRef::cast).next() | 217 | call_expr.syntax().children().filter_map(ast::NameRef::cast).next() |
161 | } | 218 | } |
162 | |||
163 | FnCallNode::MacroCallExpr(call_expr) => call_expr.path()?.segment()?.name_ref(), | ||
164 | } | 219 | } |
165 | } | 220 | } |
166 | 221 | ||
@@ -168,214 +223,209 @@ impl FnCallNode { | |||
168 | match self { | 223 | match self { |
169 | FnCallNode::CallExpr(expr) => expr.arg_list(), | 224 | FnCallNode::CallExpr(expr) => expr.arg_list(), |
170 | FnCallNode::MethodCallExpr(expr) => expr.arg_list(), | 225 | FnCallNode::MethodCallExpr(expr) => expr.arg_list(), |
171 | FnCallNode::MacroCallExpr(_) => None, | ||
172 | } | 226 | } |
173 | } | 227 | } |
174 | } | 228 | } |
175 | 229 | ||
176 | impl CallInfo { | ||
177 | fn into_active_parameter(self) -> Option<ActiveParameter> { | ||
178 | let idx = self.active_parameter?; | ||
179 | let ty = self.signature.parameter_types.get(idx)?.clone(); | ||
180 | let name = self.signature.parameter_names.get(idx)?.clone(); | ||
181 | let res = ActiveParameter { ty, name }; | ||
182 | Some(res) | ||
183 | } | ||
184 | |||
185 | fn with_fn(db: &RootDatabase, function: hir::Function) -> Self { | ||
186 | let signature = FunctionSignature::from_hir(db, function); | ||
187 | |||
188 | CallInfo { signature, active_parameter: None } | ||
189 | } | ||
190 | |||
191 | fn with_struct(db: &RootDatabase, st: hir::Struct) -> Option<Self> { | ||
192 | let signature = FunctionSignature::from_struct(db, st)?; | ||
193 | |||
194 | Some(CallInfo { signature, active_parameter: None }) | ||
195 | } | ||
196 | |||
197 | fn with_enum_variant(db: &RootDatabase, variant: hir::EnumVariant) -> Option<Self> { | ||
198 | let signature = FunctionSignature::from_enum_variant(db, variant)?; | ||
199 | |||
200 | Some(CallInfo { signature, active_parameter: None }) | ||
201 | } | ||
202 | |||
203 | fn with_macro(db: &RootDatabase, macro_def: hir::MacroDef) -> Option<Self> { | ||
204 | let signature = FunctionSignature::from_macro(db, macro_def)?; | ||
205 | |||
206 | Some(CallInfo { signature, active_parameter: None }) | ||
207 | } | ||
208 | |||
209 | fn parameters(&self) -> &[String] { | ||
210 | &self.signature.parameters | ||
211 | } | ||
212 | } | ||
213 | |||
214 | #[cfg(test)] | 230 | #[cfg(test)] |
215 | mod tests { | 231 | mod tests { |
232 | use expect::{expect, Expect}; | ||
216 | use test_utils::mark; | 233 | use test_utils::mark; |
217 | 234 | ||
218 | use crate::mock_analysis::analysis_and_position; | 235 | use crate::mock_analysis::analysis_and_position; |
219 | 236 | ||
220 | use super::*; | 237 | fn check(ra_fixture: &str, expect: Expect) { |
221 | 238 | let (analysis, position) = analysis_and_position(ra_fixture); | |
222 | // These are only used when testing | 239 | let call_info = analysis.call_info(position).unwrap(); |
223 | impl CallInfo { | 240 | let actual = match call_info { |
224 | fn doc(&self) -> Option<hir::Documentation> { | 241 | Some(call_info) => { |
225 | self.signature.doc.clone() | 242 | let docs = match &call_info.doc { |
226 | } | 243 | None => "".to_string(), |
227 | 244 | Some(docs) => format!("{}\n------\n", docs.as_str()), | |
228 | fn label(&self) -> String { | 245 | }; |
229 | self.signature.to_string() | 246 | let params = call_info |
230 | } | 247 | .parameter_labels() |
231 | } | 248 | .enumerate() |
232 | 249 | .map(|(i, param)| { | |
233 | fn call_info_helper(text: &str) -> Option<CallInfo> { | 250 | if Some(i) == call_info.active_parameter { |
234 | let (analysis, position) = analysis_and_position(text); | 251 | format!("<{}>", param) |
235 | analysis.call_info(position).unwrap() | 252 | } else { |
236 | } | 253 | param.to_string() |
237 | 254 | } | |
238 | fn call_info(text: &str) -> CallInfo { | 255 | }) |
239 | let info = call_info_helper(text); | 256 | .collect::<Vec<_>>() |
240 | assert!(info.is_some()); | 257 | .join(", "); |
241 | info.unwrap() | 258 | format!("{}{}\n({})\n", docs, call_info.signature, params) |
242 | } | 259 | } |
243 | 260 | None => String::new(), | |
244 | fn no_call_info(text: &str) { | 261 | }; |
245 | let info = call_info_helper(text); | 262 | expect.assert_eq(&actual); |
246 | assert!(info.is_none()); | ||
247 | } | 263 | } |
248 | 264 | ||
249 | #[test] | 265 | #[test] |
250 | fn test_fn_signature_two_args_firstx() { | 266 | fn test_fn_signature_two_args() { |
251 | let info = call_info( | 267 | check( |
252 | r#"fn foo(x: u32, y: u32) -> u32 {x + y} | 268 | r#" |
253 | fn bar() { foo(<|>3, ); }"#, | 269 | fn foo(x: u32, y: u32) -> u32 {x + y} |
270 | fn bar() { foo(<|>3, ); } | ||
271 | "#, | ||
272 | expect![[r#" | ||
273 | fn foo(x: u32, y: u32) -> u32 | ||
274 | (<x: u32>, y: u32) | ||
275 | "#]], | ||
254 | ); | 276 | ); |
255 | 277 | check( | |
256 | assert_eq!(info.parameters(), ["x: u32", "y: u32"]); | 278 | r#" |
257 | assert_eq!(info.active_parameter, Some(0)); | 279 | fn foo(x: u32, y: u32) -> u32 {x + y} |
258 | } | 280 | fn bar() { foo(3<|>, ); } |
259 | 281 | "#, | |
260 | #[test] | 282 | expect![[r#" |
261 | fn test_fn_signature_two_args_second() { | 283 | fn foo(x: u32, y: u32) -> u32 |
262 | let info = call_info( | 284 | (<x: u32>, y: u32) |
263 | r#"fn foo(x: u32, y: u32) -> u32 {x + y} | 285 | "#]], |
264 | fn bar() { foo(3, <|>); }"#, | 286 | ); |
287 | check( | ||
288 | r#" | ||
289 | fn foo(x: u32, y: u32) -> u32 {x + y} | ||
290 | fn bar() { foo(3,<|> ); } | ||
291 | "#, | ||
292 | expect![[r#" | ||
293 | fn foo(x: u32, y: u32) -> u32 | ||
294 | (x: u32, <y: u32>) | ||
295 | "#]], | ||
296 | ); | ||
297 | check( | ||
298 | r#" | ||
299 | fn foo(x: u32, y: u32) -> u32 {x + y} | ||
300 | fn bar() { foo(3, <|>); } | ||
301 | "#, | ||
302 | expect![[r#" | ||
303 | fn foo(x: u32, y: u32) -> u32 | ||
304 | (x: u32, <y: u32>) | ||
305 | "#]], | ||
265 | ); | 306 | ); |
266 | |||
267 | assert_eq!(info.parameters(), ["x: u32", "y: u32"]); | ||
268 | assert_eq!(info.active_parameter, Some(1)); | ||
269 | } | 307 | } |
270 | 308 | ||
271 | #[test] | 309 | #[test] |
272 | fn test_fn_signature_two_args_empty() { | 310 | fn test_fn_signature_two_args_empty() { |
273 | let info = call_info( | 311 | check( |
274 | r#"fn foo(x: u32, y: u32) -> u32 {x + y} | 312 | r#" |
275 | fn bar() { foo(<|>); }"#, | 313 | fn foo(x: u32, y: u32) -> u32 {x + y} |
314 | fn bar() { foo(<|>); } | ||
315 | "#, | ||
316 | expect![[r#" | ||
317 | fn foo(x: u32, y: u32) -> u32 | ||
318 | (<x: u32>, y: u32) | ||
319 | "#]], | ||
276 | ); | 320 | ); |
277 | |||
278 | assert_eq!(info.parameters(), ["x: u32", "y: u32"]); | ||
279 | assert_eq!(info.active_parameter, Some(0)); | ||
280 | } | 321 | } |
281 | 322 | ||
282 | #[test] | 323 | #[test] |
283 | fn test_fn_signature_two_args_first_generics() { | 324 | fn test_fn_signature_two_args_first_generics() { |
284 | let info = call_info( | 325 | check( |
285 | r#"fn foo<T, U: Copy + Display>(x: T, y: U) -> u32 where T: Copy + Display, U: Debug {x + y} | ||
286 | fn bar() { foo(<|>3, ); }"#, | ||
287 | ); | ||
288 | |||
289 | assert_eq!(info.parameters(), ["x: T", "y: U"]); | ||
290 | assert_eq!( | ||
291 | info.label(), | ||
292 | r#" | 326 | r#" |
293 | fn foo<T, U: Copy + Display>(x: T, y: U) -> u32 | 327 | fn foo<T, U: Copy + Display>(x: T, y: U) -> u32 |
294 | where T: Copy + Display, | 328 | where T: Copy + Display, U: Debug |
295 | U: Debug | 329 | { x + y } |
296 | "# | 330 | |
297 | .trim() | 331 | fn bar() { foo(<|>3, ); } |
332 | "#, | ||
333 | expect![[r#" | ||
334 | fn foo(x: i32, y: {unknown}) -> u32 | ||
335 | (<x: i32>, y: {unknown}) | ||
336 | "#]], | ||
298 | ); | 337 | ); |
299 | assert_eq!(info.active_parameter, Some(0)); | ||
300 | } | 338 | } |
301 | 339 | ||
302 | #[test] | 340 | #[test] |
303 | fn test_fn_signature_no_params() { | 341 | fn test_fn_signature_no_params() { |
304 | let info = call_info( | 342 | check( |
305 | r#"fn foo<T>() -> T where T: Copy + Display {} | ||
306 | fn bar() { foo(<|>); }"#, | ||
307 | ); | ||
308 | |||
309 | assert!(info.parameters().is_empty()); | ||
310 | assert_eq!( | ||
311 | info.label(), | ||
312 | r#" | 343 | r#" |
313 | fn foo<T>() -> T | 344 | fn foo<T>() -> T where T: Copy + Display {} |
314 | where T: Copy + Display | 345 | fn bar() { foo(<|>); } |
315 | "# | 346 | "#, |
316 | .trim() | 347 | expect![[r#" |
348 | fn foo() -> {unknown} | ||
349 | () | ||
350 | "#]], | ||
317 | ); | 351 | ); |
318 | assert!(info.active_parameter.is_none()); | ||
319 | } | 352 | } |
320 | 353 | ||
321 | #[test] | 354 | #[test] |
322 | fn test_fn_signature_for_impl() { | 355 | fn test_fn_signature_for_impl() { |
323 | let info = call_info( | 356 | check( |
324 | r#"struct F; impl F { pub fn new() { F{}} } | 357 | r#" |
325 | fn bar() {let _ : F = F::new(<|>);}"#, | 358 | struct F; |
359 | impl F { pub fn new() { } } | ||
360 | fn bar() { | ||
361 | let _ : F = F::new(<|>); | ||
362 | } | ||
363 | "#, | ||
364 | expect![[r#" | ||
365 | fn new() | ||
366 | () | ||
367 | "#]], | ||
326 | ); | 368 | ); |
327 | |||
328 | assert!(info.parameters().is_empty()); | ||
329 | assert_eq!(info.active_parameter, None); | ||
330 | } | 369 | } |
331 | 370 | ||
332 | #[test] | 371 | #[test] |
333 | fn test_fn_signature_for_method_self() { | 372 | fn test_fn_signature_for_method_self() { |
334 | let info = call_info( | 373 | check( |
335 | r#"struct F; | 374 | r#" |
336 | impl F { | 375 | struct S; |
337 | pub fn new() -> F{ | 376 | impl S { pub fn do_it(&self) {} } |
338 | F{} | ||
339 | } | ||
340 | |||
341 | pub fn do_it(&self) {} | ||
342 | } | ||
343 | 377 | ||
344 | fn bar() { | 378 | fn bar() { |
345 | let f : F = F::new(); | 379 | let s: S = S; |
346 | f.do_it(<|>); | 380 | s.do_it(<|>); |
347 | }"#, | 381 | } |
382 | "#, | ||
383 | expect![[r#" | ||
384 | fn do_it(&self) | ||
385 | () | ||
386 | "#]], | ||
348 | ); | 387 | ); |
349 | |||
350 | assert_eq!(info.parameters(), ["&self"]); | ||
351 | assert_eq!(info.active_parameter, None); | ||
352 | } | 388 | } |
353 | 389 | ||
354 | #[test] | 390 | #[test] |
355 | fn test_fn_signature_for_method_with_arg() { | 391 | fn test_fn_signature_for_method_with_arg() { |
356 | let info = call_info( | 392 | check( |
357 | r#"struct F; | 393 | r#" |
358 | impl F { | 394 | struct S; |
359 | pub fn new() -> F{ | 395 | impl S { |
360 | F{} | 396 | fn foo(&self, x: i32) {} |
397 | } | ||
398 | |||
399 | fn main() { S.foo(<|>); } | ||
400 | "#, | ||
401 | expect![[r#" | ||
402 | fn foo(&self, x: i32) | ||
403 | (<x: i32>) | ||
404 | "#]], | ||
405 | ); | ||
361 | } | 406 | } |
362 | 407 | ||
363 | pub fn do_it(&self, x: i32) {} | 408 | #[test] |
409 | fn test_fn_signature_for_method_with_arg_as_assoc_fn() { | ||
410 | check( | ||
411 | r#" | ||
412 | struct S; | ||
413 | impl S { | ||
414 | fn foo(&self, x: i32) {} | ||
364 | } | 415 | } |
365 | 416 | ||
366 | fn bar() { | 417 | fn main() { S::foo(<|>); } |
367 | let f : F = F::new(); | 418 | "#, |
368 | f.do_it(<|>); | 419 | expect![[r#" |
369 | }"#, | 420 | fn foo(self: &S, x: i32) |
421 | (<self: &S>, x: i32) | ||
422 | "#]], | ||
370 | ); | 423 | ); |
371 | |||
372 | assert_eq!(info.parameters(), ["&self", "x: i32"]); | ||
373 | assert_eq!(info.active_parameter, Some(1)); | ||
374 | } | 424 | } |
375 | 425 | ||
376 | #[test] | 426 | #[test] |
377 | fn test_fn_signature_with_docs_simple() { | 427 | fn test_fn_signature_with_docs_simple() { |
378 | let info = call_info( | 428 | check( |
379 | r#" | 429 | r#" |
380 | /// test | 430 | /// test |
381 | // non-doc-comment | 431 | // non-doc-comment |
@@ -387,17 +437,18 @@ fn bar() { | |||
387 | let _ = foo(<|>); | 437 | let _ = foo(<|>); |
388 | } | 438 | } |
389 | "#, | 439 | "#, |
440 | expect![[r#" | ||
441 | test | ||
442 | ------ | ||
443 | fn foo(j: u32) -> u32 | ||
444 | (<j: u32>) | ||
445 | "#]], | ||
390 | ); | 446 | ); |
391 | |||
392 | assert_eq!(info.parameters(), ["j: u32"]); | ||
393 | assert_eq!(info.active_parameter, Some(0)); | ||
394 | assert_eq!(info.label(), "fn foo(j: u32) -> u32"); | ||
395 | assert_eq!(info.doc().map(|it| it.into()), Some("test".to_string())); | ||
396 | } | 447 | } |
397 | 448 | ||
398 | #[test] | 449 | #[test] |
399 | fn test_fn_signature_with_docs() { | 450 | fn test_fn_signature_with_docs() { |
400 | let info = call_info( | 451 | check( |
401 | r#" | 452 | r#" |
402 | /// Adds one to the number given. | 453 | /// Adds one to the number given. |
403 | /// | 454 | /// |
@@ -415,31 +466,26 @@ pub fn add_one(x: i32) -> i32 { | |||
415 | pub fn do() { | 466 | pub fn do() { |
416 | add_one(<|> | 467 | add_one(<|> |
417 | }"#, | 468 | }"#, |
418 | ); | 469 | expect![[r##" |
419 | 470 | Adds one to the number given. | |
420 | assert_eq!(info.parameters(), ["x: i32"]); | ||
421 | assert_eq!(info.active_parameter, Some(0)); | ||
422 | assert_eq!(info.label(), "pub fn add_one(x: i32) -> i32"); | ||
423 | assert_eq!( | ||
424 | info.doc().map(|it| it.into()), | ||
425 | Some( | ||
426 | r#"Adds one to the number given. | ||
427 | 471 | ||
428 | # Examples | 472 | # Examples |
429 | 473 | ||
430 | ``` | 474 | ``` |
431 | let five = 5; | 475 | let five = 5; |
432 | 476 | ||
433 | assert_eq!(6, my_crate::add_one(5)); | 477 | assert_eq!(6, my_crate::add_one(5)); |
434 | ```"# | 478 | ``` |
435 | .to_string() | 479 | ------ |
436 | ) | 480 | fn add_one(x: i32) -> i32 |
481 | (<x: i32>) | ||
482 | "##]], | ||
437 | ); | 483 | ); |
438 | } | 484 | } |
439 | 485 | ||
440 | #[test] | 486 | #[test] |
441 | fn test_fn_signature_with_docs_impl() { | 487 | fn test_fn_signature_with_docs_impl() { |
442 | let info = call_info( | 488 | check( |
443 | r#" | 489 | r#" |
444 | struct addr; | 490 | struct addr; |
445 | impl addr { | 491 | impl addr { |
@@ -460,32 +506,28 @@ impl addr { | |||
460 | pub fn do_it() { | 506 | pub fn do_it() { |
461 | addr {}; | 507 | addr {}; |
462 | addr::add_one(<|>); | 508 | addr::add_one(<|>); |
463 | }"#, | 509 | } |
464 | ); | 510 | "#, |
465 | 511 | expect![[r##" | |
466 | assert_eq!(info.parameters(), ["x: i32"]); | 512 | Adds one to the number given. |
467 | assert_eq!(info.active_parameter, Some(0)); | ||
468 | assert_eq!(info.label(), "pub fn add_one(x: i32) -> i32"); | ||
469 | assert_eq!( | ||
470 | info.doc().map(|it| it.into()), | ||
471 | Some( | ||
472 | r#"Adds one to the number given. | ||
473 | 513 | ||
474 | # Examples | 514 | # Examples |
475 | 515 | ||
476 | ``` | 516 | ``` |
477 | let five = 5; | 517 | let five = 5; |
478 | 518 | ||
479 | assert_eq!(6, my_crate::add_one(5)); | 519 | assert_eq!(6, my_crate::add_one(5)); |
480 | ```"# | 520 | ``` |
481 | .to_string() | 521 | ------ |
482 | ) | 522 | fn add_one(x: i32) -> i32 |
523 | (<x: i32>) | ||
524 | "##]], | ||
483 | ); | 525 | ); |
484 | } | 526 | } |
485 | 527 | ||
486 | #[test] | 528 | #[test] |
487 | fn test_fn_signature_with_docs_from_actix() { | 529 | fn test_fn_signature_with_docs_from_actix() { |
488 | let info = call_info( | 530 | check( |
489 | r#" | 531 | r#" |
490 | struct WriteHandler<E>; | 532 | struct WriteHandler<E>; |
491 | 533 | ||
@@ -509,101 +551,89 @@ impl<E> WriteHandler<E> { | |||
509 | pub fn foo(mut r: WriteHandler<()>) { | 551 | pub fn foo(mut r: WriteHandler<()>) { |
510 | r.finished(<|>); | 552 | r.finished(<|>); |
511 | } | 553 | } |
512 | |||
513 | "#, | 554 | "#, |
514 | ); | 555 | expect![[r#" |
515 | 556 | Method is called when writer finishes. | |
516 | assert_eq!(info.label(), "fn finished(&mut self, ctx: &mut Self::Context)".to_string()); | 557 | |
517 | assert_eq!(info.parameters(), ["&mut self", "ctx: &mut Self::Context"]); | 558 | By default this method stops actor's `Context`. |
518 | assert_eq!(info.active_parameter, Some(1)); | 559 | ------ |
519 | assert_eq!( | 560 | fn finished(&mut self, ctx: &mut {unknown}) |
520 | info.doc().map(|it| it.into()), | 561 | (<ctx: &mut {unknown}>) |
521 | Some( | 562 | "#]], |
522 | r#"Method is called when writer finishes. | ||
523 | |||
524 | By default this method stops actor's `Context`."# | ||
525 | .to_string() | ||
526 | ) | ||
527 | ); | 563 | ); |
528 | } | 564 | } |
529 | 565 | ||
530 | #[test] | 566 | #[test] |
531 | fn call_info_bad_offset() { | 567 | fn call_info_bad_offset() { |
532 | mark::check!(call_info_bad_offset); | 568 | mark::check!(call_info_bad_offset); |
533 | let (analysis, position) = analysis_and_position( | 569 | check( |
534 | r#"fn foo(x: u32, y: u32) -> u32 {x + y} | 570 | r#" |
535 | fn bar() { foo <|> (3, ); }"#, | 571 | fn foo(x: u32, y: u32) -> u32 {x + y} |
572 | fn bar() { foo <|> (3, ); } | ||
573 | "#, | ||
574 | expect![[""]], | ||
536 | ); | 575 | ); |
537 | let call_info = analysis.call_info(position).unwrap(); | ||
538 | assert!(call_info.is_none()); | ||
539 | } | 576 | } |
540 | 577 | ||
541 | #[test] | 578 | #[test] |
542 | fn test_nested_method_in_lamba() { | 579 | fn test_nested_method_in_lambda() { |
543 | let info = call_info( | 580 | check( |
544 | r#"struct Foo; | 581 | r#" |
545 | 582 | struct Foo; | |
546 | impl Foo { | 583 | impl Foo { fn bar(&self, _: u32) { } } |
547 | fn bar(&self, _: u32) { } | ||
548 | } | ||
549 | 584 | ||
550 | fn bar(_: u32) { } | 585 | fn bar(_: u32) { } |
551 | 586 | ||
552 | fn main() { | 587 | fn main() { |
553 | let foo = Foo; | 588 | let foo = Foo; |
554 | std::thread::spawn(move || foo.bar(<|>)); | 589 | std::thread::spawn(move || foo.bar(<|>)); |
555 | }"#, | 590 | } |
591 | "#, | ||
592 | expect![[r#" | ||
593 | fn bar(&self, _: u32) | ||
594 | (<_: u32>) | ||
595 | "#]], | ||
556 | ); | 596 | ); |
557 | |||
558 | assert_eq!(info.parameters(), ["&self", "_: u32"]); | ||
559 | assert_eq!(info.active_parameter, Some(1)); | ||
560 | assert_eq!(info.label(), "fn bar(&self, _: u32)"); | ||
561 | } | 597 | } |
562 | 598 | ||
563 | #[test] | 599 | #[test] |
564 | fn works_for_tuple_structs() { | 600 | fn works_for_tuple_structs() { |
565 | let info = call_info( | 601 | check( |
566 | r#" | 602 | r#" |
567 | /// A cool tuple struct | 603 | /// A cool tuple struct |
568 | struct TS(u32, i32); | 604 | struct S(u32, i32); |
569 | fn main() { | 605 | fn main() { |
570 | let s = TS(0, <|>); | 606 | let s = S(0, <|>); |
571 | }"#, | 607 | } |
608 | "#, | ||
609 | expect![[r#" | ||
610 | A cool tuple struct | ||
611 | ------ | ||
612 | struct S(u32, i32) | ||
613 | (u32, <i32>) | ||
614 | "#]], | ||
572 | ); | 615 | ); |
573 | |||
574 | assert_eq!(info.label(), "struct TS(u32, i32) -> TS"); | ||
575 | assert_eq!(info.doc().map(|it| it.into()), Some("A cool tuple struct".to_string())); | ||
576 | assert_eq!(info.active_parameter, Some(1)); | ||
577 | } | 616 | } |
578 | 617 | ||
579 | #[test] | 618 | #[test] |
580 | fn generic_struct() { | 619 | fn generic_struct() { |
581 | let info = call_info( | 620 | check( |
582 | r#" | 621 | r#" |
583 | struct TS<T>(T); | 622 | struct S<T>(T); |
584 | fn main() { | 623 | fn main() { |
585 | let s = TS(<|>); | 624 | let s = S(<|>); |
586 | }"#, | 625 | } |
587 | ); | 626 | "#, |
588 | 627 | expect![[r#" | |
589 | assert_eq!(info.label(), "struct TS<T>(T) -> TS"); | 628 | struct S({unknown}) |
590 | assert_eq!(info.active_parameter, Some(0)); | 629 | (<{unknown}>) |
591 | } | 630 | "#]], |
592 | |||
593 | #[test] | ||
594 | fn cant_call_named_structs() { | ||
595 | no_call_info( | ||
596 | r#" | ||
597 | struct TS { x: u32, y: i32 } | ||
598 | fn main() { | ||
599 | let s = TS(<|>); | ||
600 | }"#, | ||
601 | ); | 631 | ); |
602 | } | 632 | } |
603 | 633 | ||
604 | #[test] | 634 | #[test] |
605 | fn works_for_enum_variants() { | 635 | fn works_for_enum_variants() { |
606 | let info = call_info( | 636 | check( |
607 | r#" | 637 | r#" |
608 | enum E { | 638 | enum E { |
609 | /// A Variant | 639 | /// A Variant |
@@ -617,17 +647,32 @@ enum E { | |||
617 | fn main() { | 647 | fn main() { |
618 | let a = E::A(<|>); | 648 | let a = E::A(<|>); |
619 | } | 649 | } |
620 | "#, | 650 | "#, |
651 | expect![[r#" | ||
652 | A Variant | ||
653 | ------ | ||
654 | enum E::A(i32) | ||
655 | (<i32>) | ||
656 | "#]], | ||
621 | ); | 657 | ); |
658 | } | ||
622 | 659 | ||
623 | assert_eq!(info.label(), "E::A(0: i32)"); | 660 | #[test] |
624 | assert_eq!(info.doc().map(|it| it.into()), Some("A Variant".to_string())); | 661 | fn cant_call_struct_record() { |
625 | assert_eq!(info.active_parameter, Some(0)); | 662 | check( |
663 | r#" | ||
664 | struct S { x: u32, y: i32 } | ||
665 | fn main() { | ||
666 | let s = S(<|>); | ||
667 | } | ||
668 | "#, | ||
669 | expect![[""]], | ||
670 | ); | ||
626 | } | 671 | } |
627 | 672 | ||
628 | #[test] | 673 | #[test] |
629 | fn cant_call_enum_records() { | 674 | fn cant_call_enum_record() { |
630 | no_call_info( | 675 | check( |
631 | r#" | 676 | r#" |
632 | enum E { | 677 | enum E { |
633 | /// A Variant | 678 | /// A Variant |
@@ -641,47 +686,57 @@ enum E { | |||
641 | fn main() { | 686 | fn main() { |
642 | let a = E::C(<|>); | 687 | let a = E::C(<|>); |
643 | } | 688 | } |
644 | "#, | 689 | "#, |
690 | expect![[""]], | ||
645 | ); | 691 | ); |
646 | } | 692 | } |
647 | 693 | ||
648 | #[test] | 694 | #[test] |
649 | fn fn_signature_for_macro() { | 695 | fn fn_signature_for_call_in_macro() { |
650 | let info = call_info( | 696 | check( |
651 | r#" | 697 | r#" |
652 | /// empty macro | 698 | macro_rules! id { ($($tt:tt)*) => { $($tt)* } } |
653 | macro_rules! foo { | 699 | fn foo() { } |
654 | () => {} | 700 | id! { |
701 | fn bar() { foo(<|>); } | ||
655 | } | 702 | } |
703 | "#, | ||
704 | expect![[r#" | ||
705 | fn foo() | ||
706 | () | ||
707 | "#]], | ||
708 | ); | ||
709 | } | ||
656 | 710 | ||
657 | fn f() { | 711 | #[test] |
658 | foo!(<|>); | 712 | fn call_info_for_lambdas() { |
713 | check( | ||
714 | r#" | ||
715 | struct S; | ||
716 | fn foo(s: S) -> i32 { 92 } | ||
717 | fn main() { | ||
718 | (|s| foo(s))(<|>) | ||
659 | } | 719 | } |
660 | "#, | 720 | "#, |
661 | ); | 721 | expect![[r#" |
662 | 722 | (S) -> i32 | |
663 | assert_eq!(info.label(), "foo!()"); | 723 | (<S>) |
664 | assert_eq!(info.doc().map(|it| it.into()), Some("empty macro".to_string())); | 724 | "#]], |
725 | ) | ||
665 | } | 726 | } |
666 | 727 | ||
667 | #[test] | 728 | #[test] |
668 | fn fn_signature_for_call_in_macro() { | 729 | fn call_info_for_fn_ptr() { |
669 | let info = call_info( | 730 | check( |
670 | r#" | 731 | r#" |
671 | macro_rules! id { | 732 | fn main(f: fn(i32, f64) -> char) { |
672 | ($($tt:tt)*) => { $($tt)* } | 733 | f(0, <|>) |
673 | } | 734 | } |
674 | fn foo() { | 735 | "#, |
675 | 736 | expect![[r#" | |
676 | } | 737 | (i32, f64) -> char |
677 | id! { | 738 | (i32, <f64>) |
678 | fn bar() { | 739 | "#]], |
679 | foo(<|>); | 740 | ) |
680 | } | ||
681 | } | ||
682 | "#, | ||
683 | ); | ||
684 | |||
685 | assert_eq!(info.label(), "fn foo()"); | ||
686 | } | 741 | } |
687 | } | 742 | } |