aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-07-16 10:36:52 +0100
committerAleksey Kladov <[email protected]>2020-07-16 10:37:26 +0100
commit1d6cf336630c7b1d7779eb5abb6e84584e41c4d4 (patch)
treeb84ea78d9945e4cd4f0b38d337c39af68c769fe5 /crates/ra_ide
parentbb2613ed4d23f6104d0a2648352bff111292373a (diff)
Simplify
Diffstat (limited to 'crates/ra_ide')
-rw-r--r--crates/ra_ide/src/call_info.rs66
1 files changed, 19 insertions, 47 deletions
diff --git a/crates/ra_ide/src/call_info.rs b/crates/ra_ide/src/call_info.rs
index eabe0cb82..0ef92ed4b 100644
--- a/crates/ra_ide/src/call_info.rs
+++ b/crates/ra_ide/src/call_info.rs
@@ -48,43 +48,40 @@ fn call_info_for_token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Op
48 // Find the calling expression and it's NameRef 48 // Find the calling expression and it's NameRef
49 let calling_node = FnCallNode::with_node(&token.parent())?; 49 let calling_node = FnCallNode::with_node(&token.parent())?;
50 50
51 let (mut call_info, has_self) = match &calling_node { 51 let signature = match &calling_node {
52 FnCallNode::CallExpr(call) => { 52 FnCallNode::CallExpr(call) => {
53 //FIXME: Type::as_callable is broken 53 //FIXME: Type::as_callable is broken
54 let callable_def = sema.type_of_expr(&call.expr()?)?.as_callable()?; 54 let callable_def = sema.type_of_expr(&call.expr()?)?.as_callable()?;
55 match callable_def { 55 match callable_def {
56 hir::CallableDef::FunctionId(it) => { 56 hir::CallableDef::FunctionId(it) => {
57 let fn_def = it.into(); 57 let fn_def = it.into();
58 (CallInfo::with_fn(sema.db, fn_def), fn_def.has_self_param(sema.db)) 58 FunctionSignature::from_hir(sema.db, fn_def)
59 } 59 }
60 hir::CallableDef::StructId(it) => { 60 hir::CallableDef::StructId(it) => {
61 (CallInfo::with_struct(sema.db, it.into())?, false) 61 FunctionSignature::from_struct(sema.db, it.into())?
62 } 62 }
63 hir::CallableDef::EnumVariantId(it) => { 63 hir::CallableDef::EnumVariantId(it) => {
64 (CallInfo::with_enum_variant(sema.db, it.into())?, false) 64 FunctionSignature::from_enum_variant(sema.db, it.into())?
65 } 65 }
66 } 66 }
67 } 67 }
68 FnCallNode::MethodCallExpr(method_call) => { 68 FnCallNode::MethodCallExpr(method_call) => {
69 let function = sema.resolve_method_call(&method_call)?; 69 let function = sema.resolve_method_call(&method_call)?;
70 (CallInfo::with_fn(sema.db, function), function.has_self_param(sema.db)) 70 FunctionSignature::from_hir(sema.db, function)
71 } 71 }
72 FnCallNode::MacroCallExpr(macro_call) => { 72 FnCallNode::MacroCallExpr(macro_call) => {
73 let macro_def = sema.resolve_macro_call(&macro_call)?; 73 let macro_def = sema.resolve_macro_call(&macro_call)?;
74 (CallInfo::with_macro(sema.db, macro_def)?, false) 74 FunctionSignature::from_macro(sema.db, macro_def)?
75 } 75 }
76 }; 76 };
77 77
78 // If we have a calling expression let's find which argument we are on 78 // If we have a calling expression let's find which argument we are on
79 let num_params = call_info.parameters().len(); 79 let num_params = signature.parameters.len();
80 80
81 match num_params { 81 let active_parameter = match num_params {
82 0 => (), 82 0 => None,
83 1 => { 83 1 if signature.has_self_param => None,
84 if !has_self { 84 1 => Some(0),
85 call_info.active_parameter = Some(0);
86 }
87 }
88 _ => { 85 _ => {
89 if let Some(arg_list) = calling_node.arg_list() { 86 if let Some(arg_list) = calling_node.arg_list() {
90 // Number of arguments specified at the call site 87 // Number of arguments specified at the call site
@@ -107,16 +104,18 @@ fn call_info_for_token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Op
107 ); 104 );
108 105
109 // If we are in a method account for `self` 106 // If we are in a method account for `self`
110 if has_self { 107 if signature.has_self_param {
111 param += 1; 108 param += 1;
112 } 109 }
113 110
114 call_info.active_parameter = Some(param); 111 Some(param)
112 } else {
113 None
115 } 114 }
116 } 115 }
117 } 116 };
118 117
119 Some(call_info) 118 Some(CallInfo { signature, active_parameter })
120} 119}
121 120
122#[derive(Debug)] 121#[derive(Debug)]
@@ -189,34 +188,6 @@ impl CallInfo {
189 let res = ActiveParameter { ty, name }; 188 let res = ActiveParameter { ty, name };
190 Some(res) 189 Some(res)
191 } 190 }
192
193 fn with_fn(db: &RootDatabase, function: hir::Function) -> Self {
194 let signature = FunctionSignature::from_hir(db, function);
195
196 CallInfo { signature, active_parameter: None }
197 }
198
199 fn with_struct(db: &RootDatabase, st: hir::Struct) -> Option<Self> {
200 let signature = FunctionSignature::from_struct(db, st)?;
201
202 Some(CallInfo { signature, active_parameter: None })
203 }
204
205 fn with_enum_variant(db: &RootDatabase, variant: hir::EnumVariant) -> Option<Self> {
206 let signature = FunctionSignature::from_enum_variant(db, variant)?;
207
208 Some(CallInfo { signature, active_parameter: None })
209 }
210
211 fn with_macro(db: &RootDatabase, macro_def: hir::MacroDef) -> Option<Self> {
212 let signature = FunctionSignature::from_macro(db, macro_def)?;
213
214 Some(CallInfo { signature, active_parameter: None })
215 }
216
217 fn parameters(&self) -> &[String] {
218 &self.signature.parameters
219 }
220} 191}
221 192
222#[cfg(test)] 193#[cfg(test)]
@@ -236,7 +207,8 @@ mod tests {
236 Some(docs) => format!("{}\n------\n", docs.as_str()), 207 Some(docs) => format!("{}\n------\n", docs.as_str()),
237 }; 208 };
238 let params = call_info 209 let params = call_info
239 .parameters() 210 .signature
211 .parameters
240 .iter() 212 .iter()
241 .enumerate() 213 .enumerate()
242 .map(|(i, param)| { 214 .map(|(i, param)| {