aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/code_model.rs26
-rw-r--r--crates/ra_hir/src/code_model/src.rs12
-rw-r--r--crates/ra_hir/src/lib.rs5
-rw-r--r--crates/ra_ide_api/src/completion/complete_path.rs4
4 files changed, 26 insertions, 21 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 4b3ec5457..50c9a79fc 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -9,7 +9,7 @@ use hir_def::{
9 body::scope::ExprScopes, 9 body::scope::ExprScopes,
10 builtin_type::BuiltinType, 10 builtin_type::BuiltinType,
11 docs::Documentation, 11 docs::Documentation,
12 nameres::per_ns::PerNs, 12 nameres::{per_ns::PerNs, raw::ImportId},
13 resolver::{HasResolver, TypeNs}, 13 resolver::{HasResolver, TypeNs},
14 type_ref::TypeRef, 14 type_ref::TypeRef,
15 ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, LocalStructFieldId, Lookup, 15 ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, LocalStructFieldId, Lookup,
@@ -30,7 +30,7 @@ use crate::{
30 TypeAliasId, 30 TypeAliasId,
31 }, 31 },
32 ty::{InferenceResult, Namespace, TraitRef}, 32 ty::{InferenceResult, Namespace, TraitRef},
33 Either, HasSource, ImportId, Name, Source, Ty, 33 Either, HasSource, Name, Source, Ty,
34}; 34};
35 35
36/// hir::Crate describes a single crate. It's the main interface with which 36/// hir::Crate describes a single crate. It's the main interface with which
@@ -129,17 +129,6 @@ impl Module {
129 }) 129 })
130 } 130 }
131 131
132 /// Returns the syntax of the last path segment corresponding to this import
133 pub fn import_source(
134 self,
135 db: &impl HirDatabase,
136 import: ImportId,
137 ) -> Either<ast::UseTree, ast::ExternCrateItem> {
138 let src = self.definition_source(db);
139 let (_, source_map) = db.raw_items_with_source_map(src.file_id);
140 source_map.get(&src.value, import)
141 }
142
143 /// Returns the crate this module is part of. 132 /// Returns the crate this module is part of.
144 pub fn krate(self) -> Crate { 133 pub fn krate(self) -> Crate {
145 Crate { crate_id: self.id.krate } 134 Crate { crate_id: self.id.krate }
@@ -189,11 +178,13 @@ impl Module {
189 } 178 }
190 179
191 /// Returns a `ModuleScope`: a set of items, visible in this module. 180 /// Returns a `ModuleScope`: a set of items, visible in this module.
192 pub fn scope(self, db: &impl HirDatabase) -> Vec<(Name, ScopeDef, Option<ImportId>)> { 181 pub fn scope(self, db: &impl HirDatabase) -> Vec<(Name, ScopeDef, Option<Import>)> {
193 db.crate_def_map(self.id.krate)[self.id.module_id] 182 db.crate_def_map(self.id.krate)[self.id.module_id]
194 .scope 183 .scope
195 .entries() 184 .entries()
196 .map(|(name, res)| (name.clone(), res.def.into(), res.import)) 185 .map(|(name, res)| {
186 (name.clone(), res.def.into(), res.import.map(|id| Import { parent: self, id }))
187 })
197 .collect() 188 .collect()
198 } 189 }
199 190
@@ -236,6 +227,11 @@ impl Module {
236 } 227 }
237} 228}
238 229
230pub struct Import {
231 pub(crate) parent: Module,
232 pub(crate) id: ImportId,
233}
234
239#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 235#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
240pub struct StructField { 236pub struct StructField {
241 pub(crate) parent: VariantDef, 237 pub(crate) parent: VariantDef,
diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs
index 09bacf579..ceb3daa27 100644
--- a/crates/ra_hir/src/code_model/src.rs
+++ b/crates/ra_hir/src/code_model/src.rs
@@ -8,7 +8,7 @@ use crate::{
8 db::{AstDatabase, DefDatabase, HirDatabase}, 8 db::{AstDatabase, DefDatabase, HirDatabase},
9 ids::AstItemDef, 9 ids::AstItemDef,
10 Const, Enum, EnumVariant, FieldSource, Function, HasBody, MacroDef, Module, ModuleSource, 10 Const, Enum, EnumVariant, FieldSource, Function, HasBody, MacroDef, Module, ModuleSource,
11 Static, Struct, StructField, Trait, TypeAlias, Union, 11 Static, Struct, StructField, Trait, TypeAlias, Union, Import
12}; 12};
13 13
14pub use hir_expand::Source; 14pub use hir_expand::Source;
@@ -113,6 +113,16 @@ impl HasSource for MacroDef {
113 Source { file_id: self.id.ast_id.file_id(), value: self.id.ast_id.to_node(db) } 113 Source { file_id: self.id.ast_id.file_id(), value: self.id.ast_id.to_node(db) }
114 } 114 }
115} 115}
116impl HasSource for Import {
117 type Ast = Either<ast::UseTree, ast::ExternCrateItem>;
118
119 /// Returns the syntax of the last path segment corresponding to this import
120 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<Self::Ast> {
121 let src = self.parent.definition_source(db);
122 let (_, source_map) = db.raw_items_with_source_map(src.file_id);
123 src.with_value(source_map.get(&src.value, self.id))
124 }
125}
116 126
117pub trait HasBodySource: HasBody + HasSource 127pub trait HasBodySource: HasBody + HasSource
118where 128where
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index d42161612..e51d4d063 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -53,8 +53,8 @@ pub use crate::{
53 src::{HasBodySource, HasSource}, 53 src::{HasBodySource, HasSource},
54 Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, DefWithBody, Docs, Enum, 54 Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, DefWithBody, Docs, Enum,
55 EnumVariant, FieldSource, Function, GenericDef, GenericParam, HasAttrs, HasBody, ImplBlock, 55 EnumVariant, FieldSource, Function, GenericDef, GenericParam, HasAttrs, HasBody, ImplBlock,
56 Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, StructField, 56 Import, Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct,
57 Trait, TypeAlias, Union, VariantDef, 57 StructField, Trait, TypeAlias, Union, VariantDef,
58 }, 58 },
59 expr::ExprScopes, 59 expr::ExprScopes,
60 from_source::FromSource, 60 from_source::FromSource,
@@ -70,7 +70,6 @@ pub use crate::{
70pub use hir_def::{ 70pub use hir_def::{
71 builtin_type::BuiltinType, 71 builtin_type::BuiltinType,
72 docs::Documentation, 72 docs::Documentation,
73 nameres::raw::ImportId,
74 path::{Path, PathKind}, 73 path::{Path, PathKind},
75 type_ref::Mutability, 74 type_ref::Mutability,
76}; 75};
diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs
index 802c7701a..63e25e0bf 100644
--- a/crates/ra_ide_api/src/completion/complete_path.rs
+++ b/crates/ra_ide_api/src/completion/complete_path.rs
@@ -1,6 +1,6 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use hir::{Adt, Either, PathResolution}; 3use hir::{Adt, Either, HasSource, PathResolution};
4use ra_syntax::AstNode; 4use ra_syntax::AstNode;
5use test_utils::tested_by; 5use test_utils::tested_by;
6 6
@@ -27,7 +27,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
27 } 27 }
28 if Some(module) == ctx.module { 28 if Some(module) == ctx.module {
29 if let Some(import) = import { 29 if let Some(import) = import {
30 if let Either::A(use_tree) = module.import_source(ctx.db, import) { 30 if let Either::A(use_tree) = import.source(ctx.db).value {
31 if use_tree.syntax().text_range().contains_inclusive(ctx.offset) { 31 if use_tree.syntax().text_range().contains_inclusive(ctx.offset) {
32 // for `use self::foo<|>`, don't suggest `foo` as a completion 32 // for `use self::foo<|>`, don't suggest `foo` as a completion
33 tested_by!(dont_complete_current_use); 33 tested_by!(dont_complete_current_use);