From 8954d4dc67fc3cf519a9855b974846cfcb8c53b2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 21 Nov 2018 16:15:15 +0300 Subject: Introduce Import struct --- .../ra_analysis/src/descriptors/module/nameres.rs | 69 ++++++++++++++-------- 1 file changed, 44 insertions(+), 25 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/descriptors/module/nameres.rs b/crates/ra_analysis/src/descriptors/module/nameres.rs index d69cc6e4d..fb315a870 100644 --- a/crates/ra_analysis/src/descriptors/module/nameres.rs +++ b/crates/ra_analysis/src/descriptors/module/nameres.rs @@ -43,14 +43,26 @@ pub(crate) struct ModuleScope { #[derive(Debug, Default, PartialEq, Eq)] pub(crate) struct InputModuleItems { items: Vec, - glob_imports: Vec, - imports: Vec, + imports: Vec, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +struct Import { + path: Path, + kind: ImportKind, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +enum ImportKind { + Glob, + // TODO: make offset independent + Named(LocalSyntaxPtr), } #[derive(Debug, Clone, PartialEq, Eq)] struct Path { kind: PathKind, - segments: Vec<(LocalSyntaxPtr, SmolStr)>, + segments: Vec, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -199,14 +211,15 @@ impl InputModuleItems { self.add_use_tree(prefix.clone(), tree); } } else { - if let Some(path) = tree.path() { - if let Some(path) = convert_path(prefix, path) { - if tree.has_star() { - &mut self.glob_imports + if let Some(ast_path) = tree.path() { + if let Some(path) = convert_path(prefix, ast_path) { + let kind = if tree.has_star() { + ImportKind::Glob } else { - &mut self.imports - } - .push(path); + let ptr = LocalSyntaxPtr::new(ast_path.segment().unwrap().syntax()); + ImportKind::Named(ptr) + }; + self.imports.push(Import { kind, path }) } } } @@ -226,8 +239,7 @@ fn convert_path(prefix: Option, path: ast::Path) -> Option { kind: PathKind::Abs, segments: Vec::with_capacity(1), }); - let ptr = LocalSyntaxPtr::new(name.syntax()); - res.segments.push((ptr, name.text())); + res.segments.push(name.text()); res } ast::PathSegmentKind::CrateKw => { @@ -307,14 +319,16 @@ where let mut module_items = ModuleScope::default(); for import in input.imports.iter() { - if let Some((ptr, name)) = import.segments.last() { - module_items.items.insert( - name.clone(), - Resolution { - def_id: None, - import_name: Some(*ptr), - }, - ); + if let Some(name) = import.path.segments.iter().last() { + if let ImportKind::Named(ptr) = import.kind { + module_items.items.insert( + name.clone(), + Resolution { + def_id: None, + import_name: Some(ptr), + }, + ); + } } } @@ -355,8 +369,13 @@ where } } - fn resolve_import(&mut self, module_id: ModuleId, import: &Path) { - let mut curr = match import.kind { + fn resolve_import(&mut self, module_id: ModuleId, import: &Import) { + let ptr = match import.kind { + ImportKind::Glob => return, + ImportKind::Named(ptr) => ptr, + }; + + let mut curr = match import.path.kind { // TODO: handle extern crates PathKind::Abs => return, PathKind::Self_ => module_id, @@ -370,8 +389,8 @@ where PathKind::Crate => module_id.crate_root(&self.module_tree), }; - for (i, (ptr, name)) in import.segments.iter().enumerate() { - let is_last = i == import.segments.len() - 1; + for (i, name) in import.path.segments.iter().enumerate() { + let is_last = i == import.path.segments.len() - 1; let def_id = match self.result.per_module[&curr].items.get(name) { None => return, @@ -390,7 +409,7 @@ where self.update(module_id, |items| { let res = Resolution { def_id: Some(def_id), - import_name: Some(*ptr), + import_name: Some(ptr), }; items.items.insert(name.clone(), res); }) -- cgit v1.2.3