From cb863390f23bc2eac6561d55def9bd3ba54605fc Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 15 Jan 2021 18:57:32 +0100 Subject: Handle self/super/crate in PathSegment as NameRef --- crates/ide_db/src/defs.rs | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) (limited to 'crates/ide_db/src/defs.rs') diff --git a/crates/ide_db/src/defs.rs b/crates/ide_db/src/defs.rs index d68fe42b0..231e886a9 100644 --- a/crates/ide_db/src/defs.rs +++ b/crates/ide_db/src/defs.rs @@ -10,7 +10,7 @@ use hir::{ Module, ModuleDef, Name, PathResolution, Semantics, Visibility, }; use syntax::{ - ast::{self, AstNode}, + ast::{self, AstNode, PathSegmentKind}, match_ast, SyntaxKind, SyntaxNode, }; @@ -117,6 +117,13 @@ impl NameClass { } } + pub fn classify_self_param( + sema: &Semantics, + self_param: &ast::SelfParam, + ) -> Option { + sema.to_def(self_param).map(Definition::Local).map(NameClass::Definition) + } + pub fn classify(sema: &Semantics, name: &ast::Name) -> Option { let _p = profile::span("classify_name"); @@ -135,24 +142,26 @@ impl NameClass { let path = use_tree.path()?; let path_segment = path.segment()?; let name_ref_class = path_segment - .name_ref() - // The rename might be from a `self` token, so fallback to the name higher - // in the use tree. - .or_else(||{ - if path_segment.self_token().is_none() { - return None; + .kind() + .and_then(|kind| { + match kind { + // The rename might be from a `self` token, so fallback to the name higher + // in the use tree. + PathSegmentKind::SelfKw => { + let use_tree = use_tree + .syntax() + .parent() + .as_ref() + // Skip over UseTreeList + .and_then(SyntaxNode::parent) + .and_then(ast::UseTree::cast)?; + let path = use_tree.path()?; + let path_segment = path.segment()?; + path_segment.name_ref() + }, + PathSegmentKind::Name(name_ref) => Some(name_ref), + _ => return None, } - - let use_tree = use_tree - .syntax() - .parent() - .as_ref() - // Skip over UseTreeList - .and_then(SyntaxNode::parent) - .and_then(ast::UseTree::cast)?; - let path = use_tree.path()?; - let path_segment = path.segment()?; - path_segment.name_ref() }) .and_then(|name_ref| NameRefClass::classify(sema, &name_ref))?; -- cgit v1.2.3