Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide_db/src/active_parameter.rs')
| -rw-r--r-- | crates/ide_db/src/active_parameter.rs | 60 |
1 files changed, 9 insertions, 51 deletions
diff --git a/crates/ide_db/src/active_parameter.rs b/crates/ide_db/src/active_parameter.rs index 67b819c5a5..788a5ed17e 100644 --- a/crates/ide_db/src/active_parameter.rs +++ b/crates/ide_db/src/active_parameter.rs @@ -43,13 +43,21 @@ pub fn callable_for_token( sema: &Semantics<RootDatabase>, token: SyntaxToken, ) -> Option<(hir::Callable, Option<usize>)> { - // Find the calling expression and it's NameRef + // Find the calling expression and its NameRef let parent = token.parent()?; let calling_node = parent.ancestors().filter_map(ast::CallableExpr::cast).find(|it| { it.arg_list() .map_or(false, |it| it.syntax().text_range().contains(token.text_range().start())) })?; + callable_for_node(sema, &calling_node, &token) +} + +pub fn callable_for_node( + sema: &Semantics<RootDatabase>, + calling_node: &ast::CallableExpr, + token: &SyntaxToken, +) -> Option<(hir::Callable, Option<usize>)> { let callable = match &calling_node { ast::CallableExpr::Call(call) => { let expr = call.expr()?; @@ -68,53 +76,3 @@ pub fn callable_for_token( }; Some((callable, active_param)) } - -pub fn generics_for_token( - sema: &Semantics<RootDatabase>, - token: SyntaxToken, -) -> Option<(hir::GenericDef, usize)> { - let parent = token.parent()?; - let arg_list = parent - .ancestors() - .filter_map(ast::GenericArgList::cast) - .find(|list| list.syntax().text_range().contains(token.text_range().start()))?; - - let active_param = arg_list - .generic_args() - .take_while(|arg| arg.syntax().text_range().end() <= token.text_range().start()) - .count(); - - if let Some(path) = arg_list.syntax().ancestors().find_map(ast::Path::cast) { - let res = sema.resolve_path(&path)?; - let generic_def: hir::GenericDef = match res { - hir::PathResolution::Def(hir::ModuleDef::Adt(it)) => it.into(), - hir::PathResolution::Def(hir::ModuleDef::Function(it)) => it.into(), - hir::PathResolution::Def(hir::ModuleDef::Trait(it)) => it.into(), - hir::PathResolution::Def(hir::ModuleDef::TypeAlias(it)) => it.into(), - hir::PathResolution::Def(hir::ModuleDef::Variant(it)) => it.into(), - hir::PathResolution::Def(hir::ModuleDef::BuiltinType(_)) - | hir::PathResolution::Def(hir::ModuleDef::Const(_)) - | hir::PathResolution::Def(hir::ModuleDef::Macro(_)) - | hir::PathResolution::Def(hir::ModuleDef::Module(_)) - | hir::PathResolution::Def(hir::ModuleDef::Static(_)) => return None, - hir::PathResolution::AssocItem(hir::AssocItem::Function(it)) => it.into(), - hir::PathResolution::AssocItem(hir::AssocItem::TypeAlias(it)) => it.into(), - hir::PathResolution::AssocItem(hir::AssocItem::Const(_)) => return None, - hir::PathResolution::BuiltinAttr(_) - | hir::PathResolution::ToolModule(_) - | hir::PathResolution::Local(_) - | hir::PathResolution::TypeParam(_) - | hir::PathResolution::ConstParam(_) - | hir::PathResolution::SelfType(_) => return None, - }; - - Some((generic_def, active_param)) - } else if let Some(method_call) = arg_list.syntax().parent().and_then(ast::MethodCallExpr::cast) - { - // recv.method::<$0>() - let method = sema.resolve_method_call(&method_call)?; - Some((method.into(), active_param)) - } else { - None - } -} |