aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis')
-rw-r--r--crates/ra_analysis/src/descriptors/module/nameres.rs69
1 files changed, 44 insertions, 25 deletions
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 {
43#[derive(Debug, Default, PartialEq, Eq)] 43#[derive(Debug, Default, PartialEq, Eq)]
44pub(crate) struct InputModuleItems { 44pub(crate) struct InputModuleItems {
45 items: Vec<ModuleItem>, 45 items: Vec<ModuleItem>,
46 glob_imports: Vec<Path>, 46 imports: Vec<Import>,
47 imports: Vec<Path>, 47}
48
49#[derive(Debug, Clone, PartialEq, Eq)]
50struct Import {
51 path: Path,
52 kind: ImportKind,
53}
54
55#[derive(Debug, Clone, PartialEq, Eq)]
56enum ImportKind {
57 Glob,
58 // TODO: make offset independent
59 Named(LocalSyntaxPtr),
48} 60}
49 61
50#[derive(Debug, Clone, PartialEq, Eq)] 62#[derive(Debug, Clone, PartialEq, Eq)]
51struct Path { 63struct Path {
52 kind: PathKind, 64 kind: PathKind,
53 segments: Vec<(LocalSyntaxPtr, SmolStr)>, 65 segments: Vec<SmolStr>,
54} 66}
55 67
56#[derive(Debug, Clone, Copy, PartialEq, Eq)] 68#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -199,14 +211,15 @@ impl InputModuleItems {
199 self.add_use_tree(prefix.clone(), tree); 211 self.add_use_tree(prefix.clone(), tree);
200 } 212 }
201 } else { 213 } else {
202 if let Some(path) = tree.path() { 214 if let Some(ast_path) = tree.path() {
203 if let Some(path) = convert_path(prefix, path) { 215 if let Some(path) = convert_path(prefix, ast_path) {
204 if tree.has_star() { 216 let kind = if tree.has_star() {
205 &mut self.glob_imports 217 ImportKind::Glob
206 } else { 218 } else {
207 &mut self.imports 219 let ptr = LocalSyntaxPtr::new(ast_path.segment().unwrap().syntax());
208 } 220 ImportKind::Named(ptr)
209 .push(path); 221 };
222 self.imports.push(Import { kind, path })
210 } 223 }
211 } 224 }
212 } 225 }
@@ -226,8 +239,7 @@ fn convert_path(prefix: Option<Path>, path: ast::Path) -> Option<Path> {
226 kind: PathKind::Abs, 239 kind: PathKind::Abs,
227 segments: Vec::with_capacity(1), 240 segments: Vec::with_capacity(1),
228 }); 241 });
229 let ptr = LocalSyntaxPtr::new(name.syntax()); 242 res.segments.push(name.text());
230 res.segments.push((ptr, name.text()));
231 res 243 res
232 } 244 }
233 ast::PathSegmentKind::CrateKw => { 245 ast::PathSegmentKind::CrateKw => {
@@ -307,14 +319,16 @@ where
307 let mut module_items = ModuleScope::default(); 319 let mut module_items = ModuleScope::default();
308 320
309 for import in input.imports.iter() { 321 for import in input.imports.iter() {
310 if let Some((ptr, name)) = import.segments.last() { 322 if let Some(name) = import.path.segments.iter().last() {
311 module_items.items.insert( 323 if let ImportKind::Named(ptr) = import.kind {
312 name.clone(), 324 module_items.items.insert(
313 Resolution { 325 name.clone(),
314 def_id: None, 326 Resolution {
315 import_name: Some(*ptr), 327 def_id: None,
316 }, 328 import_name: Some(ptr),
317 ); 329 },
330 );
331 }
318 } 332 }
319 } 333 }
320 334
@@ -355,8 +369,13 @@ where
355 } 369 }
356 } 370 }
357 371
358 fn resolve_import(&mut self, module_id: ModuleId, import: &Path) { 372 fn resolve_import(&mut self, module_id: ModuleId, import: &Import) {
359 let mut curr = match import.kind { 373 let ptr = match import.kind {
374 ImportKind::Glob => return,
375 ImportKind::Named(ptr) => ptr,
376 };
377
378 let mut curr = match import.path.kind {
360 // TODO: handle extern crates 379 // TODO: handle extern crates
361 PathKind::Abs => return, 380 PathKind::Abs => return,
362 PathKind::Self_ => module_id, 381 PathKind::Self_ => module_id,
@@ -370,8 +389,8 @@ where
370 PathKind::Crate => module_id.crate_root(&self.module_tree), 389 PathKind::Crate => module_id.crate_root(&self.module_tree),
371 }; 390 };
372 391
373 for (i, (ptr, name)) in import.segments.iter().enumerate() { 392 for (i, name) in import.path.segments.iter().enumerate() {
374 let is_last = i == import.segments.len() - 1; 393 let is_last = i == import.path.segments.len() - 1;
375 394
376 let def_id = match self.result.per_module[&curr].items.get(name) { 395 let def_id = match self.result.per_module[&curr].items.get(name) {
377 None => return, 396 None => return,
@@ -390,7 +409,7 @@ where
390 self.update(module_id, |items| { 409 self.update(module_id, |items| {
391 let res = Resolution { 410 let res = Resolution {
392 def_id: Some(def_id), 411 def_id: Some(def_id),
393 import_name: Some(*ptr), 412 import_name: Some(ptr),
394 }; 413 };
395 items.items.insert(name.clone(), res); 414 items.items.insert(name.clone(), res);
396 }) 415 })