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 ++++++++++++++++++++------------- crates/ide_db/src/helpers/insert_use.rs | 10 ++++++-- crates/ide_db/src/search.rs | 2 +- 3 files changed, 36 insertions(+), 21 deletions(-) (limited to 'crates/ide_db') 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))?; diff --git a/crates/ide_db/src/helpers/insert_use.rs b/crates/ide_db/src/helpers/insert_use.rs index d6b498be3..0c180e9bc 100644 --- a/crates/ide_db/src/helpers/insert_use.rs +++ b/crates/ide_db/src/helpers/insert_use.rs @@ -444,8 +444,14 @@ fn use_tree_path_cmp(a: &ast::Path, a_has_tl: bool, b: &ast::Path, b_has_tl: boo } fn path_segment_cmp(a: &ast::PathSegment, b: &ast::PathSegment) -> Ordering { - let a = a.name_ref(); - let b = b.name_ref(); + let a = a.kind().and_then(|kind| match kind { + PathSegmentKind::Name(name_ref) => Some(name_ref), + _ => None, + }); + let b = b.kind().and_then(|kind| match kind { + PathSegmentKind::Name(name_ref) => Some(name_ref), + _ => None, + }); a.as_ref().map(ast::NameRef::text).cmp(&b.as_ref().map(ast::NameRef::text)) } diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs index b5fa46642..0ecb13a64 100644 --- a/crates/ide_db/src/search.rs +++ b/crates/ide_db/src/search.rs @@ -65,7 +65,7 @@ pub enum ReferenceKind { FieldShorthandForLocal, StructLiteral, RecordFieldExprOrPat, - SelfKw, + SelfParam, EnumLiteral, Lifetime, Other, -- cgit v1.2.3