From c5be0b2589e5ed732ae5001122e453f0903bfdf2 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 9 Feb 2021 16:03:39 +0100 Subject: Use NameLike in FileReference directly as its not exported from ide anymore --- crates/ide_db/src/search.rs | 63 ++++++++++++++------------------------------- 1 file changed, 20 insertions(+), 43 deletions(-) (limited to 'crates/ide_db/src') diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs index d0aed26f7..38b20f2dc 100644 --- a/crates/ide_db/src/search.rs +++ b/crates/ide_db/src/search.rs @@ -10,9 +10,7 @@ use base_db::{FileId, FileRange, SourceDatabaseExt}; use hir::{DefWithBody, HasSource, Module, ModuleSource, Semantics, Visibility}; use once_cell::unsync::Lazy; use rustc_hash::FxHashMap; -use syntax::{ - ast, match_ast, AstNode, NodeOrToken, SyntaxElement, SyntaxNode, TextRange, TextSize, -}; +use syntax::{ast, match_ast, AstNode, TextRange, TextSize}; use crate::defs::NameClass; use crate::{ @@ -20,13 +18,6 @@ use crate::{ RootDatabase, }; -#[derive(Debug, Clone)] -pub enum NameKind { - Name, - NameRef, - Lifetime, -} - #[derive(Debug, Default, Clone)] pub struct UsageSearchResult { pub references: FxHashMap>, @@ -68,6 +59,15 @@ pub enum NameLike { Lifetime(ast::Lifetime), } +impl NameLike { + pub fn as_name_ref(&self) -> Option<&ast::NameRef> { + match self { + NameLike::NameRef(name_ref) => Some(name_ref), + _ => None, + } + } +} + mod __ { use super::{ ast::{Lifetime, Name, NameRef}, @@ -79,37 +79,10 @@ mod __ { #[derive(Debug, Clone)] pub struct FileReference { pub range: TextRange, - pub name: NameKind, + pub name: NameLike, pub access: Option, } -impl FileReference { - pub fn name_from_syntax(&self, root: &SyntaxNode) -> Option { - let node = node_or_parent(root.covering_element(self.range)); - match self.name { - NameKind::Name => ast::Name::cast(node).map(Into::into), - NameKind::NameRef => ast::NameRef::cast(node).map(Into::into), - NameKind::Lifetime => ast::Lifetime::cast(node).map(Into::into), - } - } - - pub fn as_name_ref(&self, root: &SyntaxNode) -> Option { - match self.name { - NameKind::NameRef => { - ast::NameRef::cast(node_or_parent(root.covering_element(self.range))) - } - _ => None, - } - } -} - -fn node_or_parent(ele: SyntaxElement) -> SyntaxNode { - match ele { - NodeOrToken::Node(node) => node, - NodeOrToken::Token(token) => token.parent(), - } -} - #[derive(Debug, Copy, Clone, PartialEq)] pub enum ReferenceAccess { Read, @@ -408,7 +381,11 @@ impl<'a> FindUsages<'a> { match NameRefClass::classify_lifetime(self.sema, lifetime) { Some(NameRefClass::Definition(def)) if &def == self.def => { let FileRange { file_id, range } = self.sema.original_range(lifetime.syntax()); - let reference = FileReference { range, name: NameKind::Lifetime, access: None }; + let reference = FileReference { + range, + name: NameLike::Lifetime(lifetime.clone()), + access: None, + }; sink(file_id, reference) } _ => false, // not a usage @@ -425,7 +402,7 @@ impl<'a> FindUsages<'a> { let FileRange { file_id, range } = self.sema.original_range(name_ref.syntax()); let reference = FileReference { range, - name: NameKind::NameRef, + name: NameLike::NameRef(name_ref.clone()), access: reference_access(&def, &name_ref), }; sink(file_id, reference) @@ -435,12 +412,12 @@ impl<'a> FindUsages<'a> { let reference = match self.def { Definition::Field(_) if &field == self.def => FileReference { range, - name: NameKind::NameRef, + name: NameLike::NameRef(name_ref.clone()), access: reference_access(&field, &name_ref), }, Definition::Local(l) if &local == l => FileReference { range, - name: NameKind::NameRef, + name: NameLike::NameRef(name_ref.clone()), access: reference_access(&Definition::Local(local), &name_ref), }, _ => return false, // not a usage @@ -464,7 +441,7 @@ impl<'a> FindUsages<'a> { let FileRange { file_id, range } = self.sema.original_range(name.syntax()); let reference = FileReference { range, - name: NameKind::Name, + name: NameLike::Name(name.clone()), // FIXME: mutable patterns should have `Write` access access: Some(ReferenceAccess::Read), }; -- cgit v1.2.3