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(
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));
}
}
}

View File

@ -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<ScopeData>;
@ -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<ScopeEntry> {
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::<Vec<_>>();
assert_eq!(actual.as_slice(), expected);
.map(|it| it.name().to_string())
.collect::<Vec<_>>()
.join("\n");
let expected = expected.join("\n");
assert_eq_text!(&actual, &expected);
}
#[test]

View File

@ -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<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.
pub fn module_from_position(
db: &impl HirDatabase,