use names everywhere

This commit is contained in:
Aleksey Kladov 2018-12-27 21:21:10 +03:00
parent 63f54d234f
commit e066050671
3 changed files with 51 additions and 30 deletions

View file

@ -243,7 +243,7 @@ impl AnalysisImpl {
rr.add_resolution( rr.add_resolution(
position.file_id, position.file_id,
FileSymbol { FileSymbol {
name: entry.name().clone(), name: entry.name().to_string().into(),
node_range: entry.ptr().range(), node_range: entry.ptr().range(),
kind: NAME, kind: NAME,
}, },
@ -261,15 +261,16 @@ impl AnalysisImpl {
let mut rr = ReferenceResolution::new(name.syntax().range()); let mut rr = ReferenceResolution::new(name.syntax().range());
if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) { if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) {
if module.has_semi() { if module.has_semi() {
let parent_module = if let Some(child_module) =
source_binder::module_from_file_id(&*self.db, position.file_id)?; source_binder::module_from_declaration(&*self.db, position.file_id, module)?
let child_name = module.name(); {
match (parent_module, child_name) { let file_id = child_module.source().file_id();
(Some(parent_module), Some(child_name)) => { let name = match child_module.name() {
if let Some(child) = parent_module.child(&child_name.text()) { Some(name) => name.to_string().into(),
let file_id = child.source().file_id(); None => "".into(),
};
let symbol = FileSymbol { let symbol = FileSymbol {
name: child_name.text(), name,
node_range: TextRange::offset_len(0.into(), 0.into()), node_range: TextRange::offset_len(0.into(), 0.into()),
kind: MODULE, kind: MODULE,
}; };
@ -277,9 +278,6 @@ impl AnalysisImpl {
return Ok(Some(rr)); return Ok(Some(rr));
} }
} }
_ => (),
}
}
} }
} }
Ok(None) Ok(None)

View file

@ -1,7 +1,7 @@
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
use ra_syntax::{ use ra_syntax::{
AstNode, SmolStr, SyntaxNodeRef, TextUnit, TextRange, AstNode, SyntaxNodeRef, TextUnit, TextRange,
algo::generate, algo::generate,
ast::{self, ArgListOwner, LoopBodyOwner, NameOwner}, ast::{self, ArgListOwner, LoopBodyOwner, NameOwner},
}; };
@ -9,6 +9,7 @@ use ra_db::LocalSyntaxPtr;
use crate::{ use crate::{
arena::{Arena, Id}, arena::{Arena, Id},
Name, AsName,
}; };
pub(crate) type ScopeId = Id<ScopeData>; pub(crate) type ScopeId = Id<ScopeData>;
@ -22,7 +23,7 @@ pub struct FnScopes {
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub struct ScopeEntry { pub struct ScopeEntry {
name: SmolStr, name: Name,
ptr: LocalSyntaxPtr, ptr: LocalSyntaxPtr,
} }
@ -101,11 +102,12 @@ impl FnScopes {
pub fn resolve_local_name<'a>(&'a self, name_ref: ast::NameRef) -> Option<&'a ScopeEntry> { pub fn resolve_local_name<'a>(&'a self, name_ref: ast::NameRef) -> Option<&'a ScopeEntry> {
let mut shadowed = FxHashSet::default(); let mut shadowed = FxHashSet::default();
let name = name_ref.as_name();
let ret = self let ret = self
.scope_chain(name_ref.syntax()) .scope_chain(name_ref.syntax())
.flat_map(|scope| self.entries(scope).iter()) .flat_map(|scope| self.entries(scope).iter())
.filter(|entry| shadowed.insert(entry.name())) .filter(|entry| shadowed.insert(entry.name()))
.filter(|entry| entry.name() == &name_ref.text()) .filter(|entry| entry.name() == &name)
.nth(0); .nth(0);
ret ret
} }
@ -170,14 +172,14 @@ impl FnScopes {
impl ScopeEntry { impl ScopeEntry {
fn new(pat: ast::BindPat) -> Option<ScopeEntry> { fn new(pat: ast::BindPat) -> Option<ScopeEntry> {
let name = pat.name()?; let name = pat.name()?.as_name();
let res = ScopeEntry { let res = ScopeEntry {
name: name.text(), name,
ptr: LocalSyntaxPtr::new(pat.syntax()), ptr: LocalSyntaxPtr::new(pat.syntax()),
}; };
Some(res) Some(res)
} }
pub fn name(&self) -> &SmolStr { pub fn name(&self) -> &Name {
&self.name &self.name
} }
pub fn ptr(&self) -> LocalSyntaxPtr { pub fn ptr(&self) -> LocalSyntaxPtr {
@ -334,7 +336,7 @@ pub struct ReferenceDescriptor {
mod tests { mod tests {
use ra_editor::find_node_at_offset; use ra_editor::find_node_at_offset;
use ra_syntax::SourceFileNode; use ra_syntax::SourceFileNode;
use test_utils::extract_offset; use test_utils::{extract_offset, assert_eq_text};
use super::*; use super::*;
@ -355,9 +357,11 @@ mod tests {
let actual = scopes let actual = scopes
.scope_chain(marker.syntax()) .scope_chain(marker.syntax())
.flat_map(|scope| scopes.entries(scope)) .flat_map(|scope| scopes.entries(scope))
.map(|it| it.name()) .map(|it| it.name().to_string())
.collect::<Vec<_>>(); .collect::<Vec<_>>()
assert_eq!(actual.as_slice(), expected); .join("\n");
let expected = expected.join("\n");
assert_eq_text!(&actual, &expected);
} }
#[test] #[test]

View file

@ -8,14 +8,14 @@
use ra_db::{FileId, FilePosition, Cancelable}; use ra_db::{FileId, FilePosition, Cancelable};
use ra_editor::find_node_at_offset; use ra_editor::find_node_at_offset;
use ra_syntax::{ use ra_syntax::{
ast::{self, AstNode}, ast::{self, AstNode, NameOwner},
SyntaxNodeRef, SyntaxNodeRef,
}; };
use crate::{ use crate::{
HirDatabase, Module, Function, SourceItemId, HirDatabase, Module, Function, SourceItemId,
module::ModuleSource, module::ModuleSource,
DefKind, DefLoc DefKind, DefLoc, AsName,
}; };
/// Locates the module by `FileId`. Picks topmost module in the file. /// 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) 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<Option<Module>> {
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. /// Locates the module by position in the source code.
pub fn module_from_position( pub fn module_from_position(
db: &impl HirDatabase, db: &impl HirDatabase,