aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/code_model.rs15
-rw-r--r--crates/ra_hir/src/has_source.rs16
-rw-r--r--crates/ra_hir/src/lib.rs4
-rw-r--r--crates/ra_hir_def/src/item_scope.rs2
-rw-r--r--crates/ra_hir_def/src/lib.rs2
-rw-r--r--crates/ra_hir_def/src/nameres/raw.rs6
-rw-r--r--crates/ra_ide/src/completion/complete_path.rs18
-rw-r--r--crates/ra_ide/src/completion/completion_context.rs4
8 files changed, 22 insertions, 45 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index fca3a2950..4cd28eb4e 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -12,8 +12,8 @@ use hir_def::{
12 resolver::HasResolver, 12 resolver::HasResolver,
13 type_ref::{Mutability, TypeRef}, 13 type_ref::{Mutability, TypeRef},
14 AdtId, ConstId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, LocalEnumVariantId, 14 AdtId, ConstId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, LocalEnumVariantId,
15 LocalImportId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, StructId, 15 LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
16 TraitId, TypeAliasId, TypeParamId, UnionId, 16 TypeParamId, UnionId,
17}; 17};
18use hir_expand::{ 18use hir_expand::{
19 diagnostics::DiagnosticSink, 19 diagnostics::DiagnosticSink,
@@ -180,13 +180,11 @@ impl Module {
180 } 180 }
181 181
182 /// Returns a `ModuleScope`: a set of items, visible in this module. 182 /// Returns a `ModuleScope`: a set of items, visible in this module.
183 pub fn scope(self, db: &impl HirDatabase) -> Vec<(Name, ScopeDef, Option<Import>)> { 183 pub fn scope(self, db: &impl HirDatabase) -> Vec<(Name, ScopeDef)> {
184 db.crate_def_map(self.id.krate)[self.id.local_id] 184 db.crate_def_map(self.id.krate)[self.id.local_id]
185 .scope 185 .scope
186 .entries() 186 .entries()
187 .map(|(name, res)| { 187 .map(|(name, res)| (name.clone(), res.def.into()))
188 (name.clone(), res.def.into(), res.import.map(|id| Import { parent: self, id }))
189 })
190 .collect() 188 .collect()
191 } 189 }
192 190
@@ -229,11 +227,6 @@ impl Module {
229 } 227 }
230} 228}
231 229
232pub struct Import {
233 pub(crate) parent: Module,
234 pub(crate) id: LocalImportId,
235}
236
237#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 230#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
238pub struct StructField { 231pub struct StructField {
239 pub(crate) parent: VariantDef, 232 pub(crate) parent: VariantDef,
diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs
index 72afecf26..5541266e2 100644
--- a/crates/ra_hir/src/has_source.rs
+++ b/crates/ra_hir/src/has_source.rs
@@ -9,8 +9,8 @@ use hir_def::{
9use ra_syntax::ast; 9use ra_syntax::ast;
10 10
11use crate::{ 11use crate::{
12 db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplBlock, Import, MacroDef, 12 db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplBlock, MacroDef, Module,
13 Module, Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union, 13 Static, Struct, StructField, Trait, TypeAlias, TypeParam, Union,
14}; 14};
15 15
16pub use hir_expand::InFile; 16pub use hir_expand::InFile;
@@ -117,18 +117,6 @@ impl HasSource for ImplBlock {
117 self.id.lookup(db).source(db) 117 self.id.lookup(db).source(db)
118 } 118 }
119} 119}
120impl HasSource for Import {
121 type Ast = Either<ast::UseTree, ast::ExternCrateItem>;
122
123 /// Returns the syntax of the last path segment corresponding to this import
124 fn source(self, db: &impl DefDatabase) -> InFile<Self::Ast> {
125 let src = self.parent.definition_source(db);
126 let (_, source_map) = db.raw_items_with_source_map(src.file_id);
127 let root = db.parse_or_expand(src.file_id).unwrap();
128 let ptr = source_map.get(self.id);
129 src.with_value(ptr.map_left(|it| it.to_node(&root)).map_right(|it| it.to_node(&root)))
130 }
131}
132 120
133impl HasSource for TypeParam { 121impl HasSource for TypeParam {
134 type Ast = Either<ast::TraitDef, ast::TypeParam>; 122 type Ast = Either<ast::TraitDef, ast::TypeParam>;
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index 7f9aef770..0008a8858 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -40,8 +40,8 @@ mod from_source;
40pub use crate::{ 40pub use crate::{
41 code_model::{ 41 code_model::{
42 Adt, AssocItem, AttrDef, Const, Crate, CrateDependency, DefWithBody, Docs, Enum, 42 Adt, AssocItem, AttrDef, Const, Crate, CrateDependency, DefWithBody, Docs, Enum,
43 EnumVariant, FieldSource, Function, GenericDef, HasAttrs, ImplBlock, Import, Local, 43 EnumVariant, FieldSource, Function, GenericDef, HasAttrs, ImplBlock, Local, MacroDef,
44 MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, StructField, Trait, Type, TypeAlias, 44 Module, ModuleDef, ScopeDef, Static, Struct, StructField, Trait, Type, TypeAlias,
45 TypeParam, Union, VariantDef, 45 TypeParam, Union, VariantDef,
46 }, 46 },
47 from_source::FromSource, 47 from_source::FromSource,
diff --git a/crates/ra_hir_def/src/item_scope.rs b/crates/ra_hir_def/src/item_scope.rs
index 6b9be8325..ad104bb3d 100644
--- a/crates/ra_hir_def/src/item_scope.rs
+++ b/crates/ra_hir_def/src/item_scope.rs
@@ -161,5 +161,5 @@ pub struct Resolution {
161 /// None for unresolved 161 /// None for unresolved
162 pub def: PerNs, 162 pub def: PerNs,
163 /// ident by which this is imported into local scope. 163 /// ident by which this is imported into local scope.
164 pub import: Option<LocalImportId>, 164 pub(crate) import: Option<LocalImportId>,
165} 165}
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index acd4f4af1..8cb5ab8d0 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -52,7 +52,7 @@ use crate::body::Expander;
52use crate::builtin_type::BuiltinType; 52use crate::builtin_type::BuiltinType;
53 53
54#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 54#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
55pub struct LocalImportId(RawId); 55pub(crate) struct LocalImportId(RawId);
56impl_arena_id!(LocalImportId); 56impl_arena_id!(LocalImportId);
57 57
58#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 58#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs
index ecb4d7c03..df5dac88a 100644
--- a/crates/ra_hir_def/src/nameres/raw.rs
+++ b/crates/ra_hir_def/src/nameres/raw.rs
@@ -48,12 +48,6 @@ pub struct ImportSourceMap {
48 48
49type ImportSourcePtr = Either<AstPtr<ast::UseTree>, AstPtr<ast::ExternCrateItem>>; 49type ImportSourcePtr = Either<AstPtr<ast::UseTree>, AstPtr<ast::ExternCrateItem>>;
50 50
51impl ImportSourceMap {
52 pub fn get(&self, import: LocalImportId) -> ImportSourcePtr {
53 self.map[import].clone()
54 }
55}
56
57impl RawItems { 51impl RawItems {
58 pub(crate) fn raw_items_query( 52 pub(crate) fn raw_items_query(
59 db: &(impl DefDatabase + AstDatabase), 53 db: &(impl DefDatabase + AstDatabase),
diff --git a/crates/ra_ide/src/completion/complete_path.rs b/crates/ra_ide/src/completion/complete_path.rs
index 28f94e0a7..cc1f7c830 100644
--- a/crates/ra_ide/src/completion/complete_path.rs
+++ b/crates/ra_ide/src/completion/complete_path.rs
@@ -1,7 +1,6 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use either::Either; 3use hir::{Adt, PathResolution, ScopeDef};
4use hir::{Adt, HasSource, PathResolution};
5use ra_syntax::AstNode; 4use ra_syntax::AstNode;
6use test_utils::tested_by; 5use test_utils::tested_by;
7 6
@@ -19,17 +18,15 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
19 match def { 18 match def {
20 hir::ModuleDef::Module(module) => { 19 hir::ModuleDef::Module(module) => {
21 let module_scope = module.scope(ctx.db); 20 let module_scope = module.scope(ctx.db);
22 for (name, def, import) in module_scope { 21 for (name, def) in module_scope {
23 if let hir::ScopeDef::ModuleDef(hir::ModuleDef::BuiltinType(..)) = def { 22 if ctx.use_item_syntax.is_some() {
24 if ctx.use_item_syntax.is_some() { 23 if let hir::ScopeDef::ModuleDef(hir::ModuleDef::BuiltinType(..)) = def {
25 tested_by!(dont_complete_primitive_in_use); 24 tested_by!(dont_complete_primitive_in_use);
26 continue; 25 continue;
27 } 26 }
28 } 27 if let ScopeDef::Unknown = def {
29 if Some(module) == ctx.module { 28 if let Some(name_ref) = ctx.name_ref_syntax.as_ref() {
30 if let Some(import) = import { 29 if &name_ref.syntax().text() == name.to_string().as_str() {
31 if let Either::Left(use_tree) = import.source(ctx.db).value {
32 if use_tree.syntax().text_range().contains_inclusive(ctx.offset) {
33 // for `use self::foo<|>`, don't suggest `foo` as a completion 30 // for `use self::foo<|>`, don't suggest `foo` as a completion
34 tested_by!(dont_complete_current_use); 31 tested_by!(dont_complete_current_use);
35 continue; 32 continue;
@@ -37,6 +34,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
37 } 34 }
38 } 35 }
39 } 36 }
37
40 acc.add_resolution(ctx, name.to_string(), &def); 38 acc.add_resolution(ctx, name.to_string(), &def);
41 } 39 }
42 } 40 }
diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs
index 4894ea2f6..48d69f7e5 100644
--- a/crates/ra_ide/src/completion/completion_context.rs
+++ b/crates/ra_ide/src/completion/completion_context.rs
@@ -19,6 +19,7 @@ pub(crate) struct CompletionContext<'a> {
19 pub(super) offset: TextUnit, 19 pub(super) offset: TextUnit,
20 pub(super) token: SyntaxToken, 20 pub(super) token: SyntaxToken,
21 pub(super) module: Option<hir::Module>, 21 pub(super) module: Option<hir::Module>,
22 pub(super) name_ref_syntax: Option<ast::NameRef>,
22 pub(super) function_syntax: Option<ast::FnDef>, 23 pub(super) function_syntax: Option<ast::FnDef>,
23 pub(super) use_item_syntax: Option<ast::UseItem>, 24 pub(super) use_item_syntax: Option<ast::UseItem>,
24 pub(super) record_lit_syntax: Option<ast::RecordLit>, 25 pub(super) record_lit_syntax: Option<ast::RecordLit>,
@@ -69,6 +70,7 @@ impl<'a> CompletionContext<'a> {
69 token, 70 token,
70 offset: position.offset, 71 offset: position.offset,
71 module, 72 module,
73 name_ref_syntax: None,
72 function_syntax: None, 74 function_syntax: None,
73 use_item_syntax: None, 75 use_item_syntax: None,
74 record_lit_syntax: None, 76 record_lit_syntax: None,
@@ -142,6 +144,8 @@ impl<'a> CompletionContext<'a> {
142 } 144 }
143 145
144 fn classify_name_ref(&mut self, original_file: SourceFile, name_ref: ast::NameRef) { 146 fn classify_name_ref(&mut self, original_file: SourceFile, name_ref: ast::NameRef) {
147 self.name_ref_syntax =
148 find_node_at_offset(original_file.syntax(), name_ref.syntax().text_range().start());
145 let name_range = name_ref.syntax().text_range(); 149 let name_range = name_ref.syntax().text_range();
146 if name_ref.syntax().parent().and_then(ast::RecordField::cast).is_some() { 150 if name_ref.syntax().parent().and_then(ast::RecordField::cast).is_some() {
147 self.record_lit_syntax = find_node_at_offset(original_file.syntax(), self.offset); 151 self.record_lit_syntax = find_node_at_offset(original_file.syntax(), self.offset);