From e0660506719476a0546e10bee816d7220be85440 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 27 Dec 2018 21:21:10 +0300 Subject: use names everywhere --- crates/ra_analysis/src/imp.rs | 34 ++++++++++++++++------------------ crates/ra_hir/src/function/scope.rs | 24 ++++++++++++++---------- crates/ra_hir/src/source_binder.rs | 23 +++++++++++++++++++++-- 3 files changed, 51 insertions(+), 30 deletions(-) diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 38a5c1a7d..0471a2fca 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -243,7 +243,7 @@ impl AnalysisImpl { rr.add_resolution( position.file_id, FileSymbol { - name: entry.name().clone(), + name: entry.name().to_string().into(), node_range: entry.ptr().range(), kind: NAME, }, @@ -261,23 +261,21 @@ impl AnalysisImpl { let mut rr = ReferenceResolution::new(name.syntax().range()); if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) { if module.has_semi() { - let parent_module = - source_binder::module_from_file_id(&*self.db, position.file_id)?; - let child_name = module.name(); - match (parent_module, child_name) { - (Some(parent_module), Some(child_name)) => { - if let Some(child) = parent_module.child(&child_name.text()) { - let file_id = child.source().file_id(); - let symbol = FileSymbol { - name: child_name.text(), - node_range: TextRange::offset_len(0.into(), 0.into()), - kind: MODULE, - }; - rr.add_resolution(file_id, symbol); - return Ok(Some(rr)); - } - } - _ => (), + if let Some(child_module) = + source_binder::module_from_declaration(&*self.db, position.file_id, module)? + { + let file_id = child_module.source().file_id(); + let name = match child_module.name() { + Some(name) => name.to_string().into(), + None => "".into(), + }; + let symbol = FileSymbol { + name, + node_range: TextRange::offset_len(0.into(), 0.into()), + kind: MODULE, + }; + rr.add_resolution(file_id, symbol); + return Ok(Some(rr)); } } } diff --git a/crates/ra_hir/src/function/scope.rs b/crates/ra_hir/src/function/scope.rs index a1a580979..3e4cfad0c 100644 --- a/crates/ra_hir/src/function/scope.rs +++ b/crates/ra_hir/src/function/scope.rs @@ -1,7 +1,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; use ra_syntax::{ - AstNode, SmolStr, SyntaxNodeRef, TextUnit, TextRange, + AstNode, SyntaxNodeRef, TextUnit, TextRange, algo::generate, ast::{self, ArgListOwner, LoopBodyOwner, NameOwner}, }; @@ -9,6 +9,7 @@ use ra_db::LocalSyntaxPtr; use crate::{ arena::{Arena, Id}, + Name, AsName, }; pub(crate) type ScopeId = Id; @@ -22,7 +23,7 @@ pub struct FnScopes { #[derive(Debug, PartialEq, Eq)] pub struct ScopeEntry { - name: SmolStr, + name: Name, ptr: LocalSyntaxPtr, } @@ -101,11 +102,12 @@ impl FnScopes { pub fn resolve_local_name<'a>(&'a self, name_ref: ast::NameRef) -> Option<&'a ScopeEntry> { let mut shadowed = FxHashSet::default(); + let name = name_ref.as_name(); let ret = self .scope_chain(name_ref.syntax()) .flat_map(|scope| self.entries(scope).iter()) .filter(|entry| shadowed.insert(entry.name())) - .filter(|entry| entry.name() == &name_ref.text()) + .filter(|entry| entry.name() == &name) .nth(0); ret } @@ -170,14 +172,14 @@ impl FnScopes { impl ScopeEntry { fn new(pat: ast::BindPat) -> Option { - let name = pat.name()?; + let name = pat.name()?.as_name(); let res = ScopeEntry { - name: name.text(), + name, ptr: LocalSyntaxPtr::new(pat.syntax()), }; Some(res) } - pub fn name(&self) -> &SmolStr { + pub fn name(&self) -> &Name { &self.name } pub fn ptr(&self) -> LocalSyntaxPtr { @@ -334,7 +336,7 @@ pub struct ReferenceDescriptor { mod tests { use ra_editor::find_node_at_offset; use ra_syntax::SourceFileNode; - use test_utils::extract_offset; + use test_utils::{extract_offset, assert_eq_text}; use super::*; @@ -355,9 +357,11 @@ mod tests { let actual = scopes .scope_chain(marker.syntax()) .flat_map(|scope| scopes.entries(scope)) - .map(|it| it.name()) - .collect::>(); - assert_eq!(actual.as_slice(), expected); + .map(|it| it.name().to_string()) + .collect::>() + .join("\n"); + let expected = expected.join("\n"); + assert_eq_text!(&actual, &expected); } #[test] diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index a0165aef2..a0d1daf71 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -8,14 +8,14 @@ use ra_db::{FileId, FilePosition, Cancelable}; use ra_editor::find_node_at_offset; use ra_syntax::{ - ast::{self, AstNode}, + ast::{self, AstNode, NameOwner}, SyntaxNodeRef, }; use crate::{ HirDatabase, Module, Function, SourceItemId, module::ModuleSource, - DefKind, DefLoc + DefKind, DefLoc, AsName, }; /// Locates the module by `FileId`. Picks topmost module in the file. @@ -24,6 +24,25 @@ pub fn module_from_file_id(db: &impl HirDatabase, file_id: FileId) -> Cancelable module_from_source(db, module_source) } +/// Locates the child module by `mod child;` declaration. +pub fn module_from_declaration( + db: &impl HirDatabase, + file_id: FileId, + decl: ast::Module, +) -> Cancelable> { + let parent_module = module_from_file_id(db, file_id)?; + let child_name = decl.name(); + match (parent_module, child_name) { + (Some(parent_module), Some(child_name)) => { + if let Some(child) = parent_module.child(&child_name.as_name()) { + return Ok(Some(child)); + } + } + _ => (), + } + Ok(None) +} + /// Locates the module by position in the source code. pub fn module_from_position( db: &impl HirDatabase, -- cgit v1.2.3