diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-12-21 11:48:12 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-12-21 11:48:12 +0000 |
commit | cdc9d682b066b110e0a44e5f8f1c574b38c16ba9 (patch) | |
tree | d1429d913719f73b4dee771e0c9ce85e871fc54a | |
parent | 90ef070db3dce0a7acb9cd11d0b0d72de13c9d79 (diff) | |
parent | 2d3fdf3fb52f32ececdfa19df7ab2971a24bae71 (diff) |
Merge #2629
2629: Remove imports from hir r=matklad a=matklad
We only used them to avoid self-confirming completions (`use self::foo`), but that can be handled more locally.
bors r+
Co-authored-by: Aleksey Kladov <[email protected]>
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 18 | ||||
-rw-r--r-- | crates/ra_hir/src/db.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir/src/has_source.rs | 16 | ||||
-rw-r--r-- | crates/ra_hir/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir_def/src/db.rs | 11 | ||||
-rw-r--r-- | crates/ra_hir_def/src/item_scope.rs | 35 | ||||
-rw-r--r-- | crates/ra_hir_def/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/collector.rs | 46 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/raw.rs | 68 | ||||
-rw-r--r-- | crates/ra_hir_def/src/trace.rs | 8 | ||||
-rw-r--r-- | crates/ra_ide/src/change.rs | 2 | ||||
-rw-r--r-- | crates/ra_ide/src/completion/complete_path.rs | 17 | ||||
-rw-r--r-- | crates/ra_ide/src/completion/completion_context.rs | 4 |
13 files changed, 71 insertions, 166 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index fca3a2950..e6768ea0b 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 | }; |
18 | use hir_expand::{ | 18 | use 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,10 +227,10 @@ impl Module { | |||
229 | } | 227 | } |
230 | } | 228 | } |
231 | 229 | ||
232 | pub struct Import { | 230 | // pub struct Import { |
233 | pub(crate) parent: Module, | 231 | // pub(crate) parent: Module, |
234 | pub(crate) id: LocalImportId, | 232 | // pub(crate) id: LocalImportId, |
235 | } | 233 | // } |
236 | 234 | ||
237 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 235 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
238 | pub struct StructField { | 236 | pub struct StructField { |
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index bfae3660b..f5ffd64fa 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs | |||
@@ -4,8 +4,8 @@ pub use hir_def::db::{ | |||
4 | BodyQuery, BodyWithSourceMapQuery, ConstDataQuery, CrateDefMapQuery, CrateLangItemsQuery, | 4 | BodyQuery, BodyWithSourceMapQuery, ConstDataQuery, CrateDefMapQuery, CrateLangItemsQuery, |
5 | DefDatabase, DefDatabaseStorage, DocumentationQuery, EnumDataQuery, ExprScopesQuery, | 5 | DefDatabase, DefDatabaseStorage, DocumentationQuery, EnumDataQuery, ExprScopesQuery, |
6 | FunctionDataQuery, GenericParamsQuery, ImplDataQuery, InternDatabase, InternDatabaseStorage, | 6 | FunctionDataQuery, GenericParamsQuery, ImplDataQuery, InternDatabase, InternDatabaseStorage, |
7 | LangItemQuery, ModuleLangItemsQuery, RawItemsQuery, RawItemsWithSourceMapQuery, | 7 | LangItemQuery, ModuleLangItemsQuery, RawItemsQuery, StaticDataQuery, StructDataQuery, |
8 | StaticDataQuery, StructDataQuery, TraitDataQuery, TypeAliasDataQuery, | 8 | TraitDataQuery, TypeAliasDataQuery, |
9 | }; | 9 | }; |
10 | pub use hir_expand::db::{ | 10 | pub use hir_expand::db::{ |
11 | AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery, | 11 | AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery, |
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::{ | |||
9 | use ra_syntax::ast; | 9 | use ra_syntax::ast; |
10 | 10 | ||
11 | use crate::{ | 11 | use 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 | ||
16 | pub use hir_expand::InFile; | 16 | pub 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 | } |
120 | impl 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 | ||
133 | impl HasSource for TypeParam { | 121 | impl 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; | |||
40 | pub use crate::{ | 40 | pub 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/db.rs b/crates/ra_hir_def/src/db.rs index 98bff6cb7..c55fd4111 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs | |||
@@ -13,10 +13,7 @@ use crate::{ | |||
13 | docs::Documentation, | 13 | docs::Documentation, |
14 | generics::GenericParams, | 14 | generics::GenericParams, |
15 | lang_item::{LangItemTarget, LangItems}, | 15 | lang_item::{LangItemTarget, LangItems}, |
16 | nameres::{ | 16 | nameres::{raw::RawItems, CrateDefMap}, |
17 | raw::{ImportSourceMap, RawItems}, | ||
18 | CrateDefMap, | ||
19 | }, | ||
20 | AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId, FunctionLoc, | 17 | AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId, FunctionLoc, |
21 | GenericDefId, ImplId, ImplLoc, ModuleId, StaticId, StaticLoc, StructId, StructLoc, TraitId, | 18 | GenericDefId, ImplId, ImplLoc, ModuleId, StaticId, StaticLoc, StructId, StructLoc, TraitId, |
22 | TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, | 19 | TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, |
@@ -46,12 +43,6 @@ pub trait InternDatabase: SourceDatabase { | |||
46 | 43 | ||
47 | #[salsa::query_group(DefDatabaseStorage)] | 44 | #[salsa::query_group(DefDatabaseStorage)] |
48 | pub trait DefDatabase: InternDatabase + AstDatabase { | 45 | pub trait DefDatabase: InternDatabase + AstDatabase { |
49 | #[salsa::invoke(RawItems::raw_items_with_source_map_query)] | ||
50 | fn raw_items_with_source_map( | ||
51 | &self, | ||
52 | file_id: HirFileId, | ||
53 | ) -> (Arc<RawItems>, Arc<ImportSourceMap>); | ||
54 | |||
55 | #[salsa::invoke(RawItems::raw_items_query)] | 46 | #[salsa::invoke(RawItems::raw_items_query)] |
56 | fn raw_items(&self, file_id: HirFileId) -> Arc<RawItems>; | 47 | fn raw_items(&self, file_id: HirFileId) -> Arc<RawItems>; |
57 | 48 | ||
diff --git a/crates/ra_hir_def/src/item_scope.rs b/crates/ra_hir_def/src/item_scope.rs index 6b9be8325..5c14fefff 100644 --- a/crates/ra_hir_def/src/item_scope.rs +++ b/crates/ra_hir_def/src/item_scope.rs | |||
@@ -5,7 +5,7 @@ use hir_expand::name::Name; | |||
5 | use once_cell::sync::Lazy; | 5 | use once_cell::sync::Lazy; |
6 | use rustc_hash::FxHashMap; | 6 | use rustc_hash::FxHashMap; |
7 | 7 | ||
8 | use crate::{per_ns::PerNs, BuiltinType, ImplId, LocalImportId, MacroDefId, ModuleDefId, TraitId}; | 8 | use crate::{per_ns::PerNs, BuiltinType, ImplId, MacroDefId, ModuleDefId, TraitId}; |
9 | 9 | ||
10 | #[derive(Debug, Default, PartialEq, Eq)] | 10 | #[derive(Debug, Default, PartialEq, Eq)] |
11 | pub struct ItemScope { | 11 | pub struct ItemScope { |
@@ -30,7 +30,7 @@ static BUILTIN_SCOPE: Lazy<FxHashMap<Name, Resolution>> = Lazy::new(|| { | |||
30 | BuiltinType::ALL | 30 | BuiltinType::ALL |
31 | .iter() | 31 | .iter() |
32 | .map(|(name, ty)| { | 32 | .map(|(name, ty)| { |
33 | (name.clone(), Resolution { def: PerNs::types(ty.clone().into()), import: None }) | 33 | (name.clone(), Resolution { def: PerNs::types(ty.clone().into()), declaration: false }) |
34 | }) | 34 | }) |
35 | .collect() | 35 | .collect() |
36 | }); | 36 | }); |
@@ -53,11 +53,9 @@ impl ItemScope { | |||
53 | } | 53 | } |
54 | 54 | ||
55 | pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ { | 55 | pub fn declarations(&self) -> impl Iterator<Item = ModuleDefId> + '_ { |
56 | self.entries() | 56 | self.entries().filter(|(_name, res)| res.declaration).flat_map(|(_name, res)| { |
57 | .filter_map(|(_name, res)| if res.import.is_none() { Some(res.def) } else { None }) | 57 | res.def.take_types().into_iter().chain(res.def.take_values().into_iter()) |
58 | .flat_map(|per_ns| { | 58 | }) |
59 | per_ns.take_types().into_iter().chain(per_ns.take_values().into_iter()) | ||
60 | }) | ||
61 | } | 59 | } |
62 | 60 | ||
63 | pub fn impls(&self) -> impl Iterator<Item = ImplId> + ExactSizeIterator + '_ { | 61 | pub fn impls(&self) -> impl Iterator<Item = ImplId> + ExactSizeIterator + '_ { |
@@ -112,38 +110,26 @@ impl ItemScope { | |||
112 | self.legacy_macros.insert(name, mac); | 110 | self.legacy_macros.insert(name, mac); |
113 | } | 111 | } |
114 | 112 | ||
115 | pub(crate) fn push_res( | 113 | pub(crate) fn push_res(&mut self, name: Name, res: &Resolution, declaration: bool) -> bool { |
116 | &mut self, | ||
117 | name: Name, | ||
118 | res: &Resolution, | ||
119 | import: Option<LocalImportId>, | ||
120 | ) -> bool { | ||
121 | let mut changed = false; | 114 | let mut changed = false; |
122 | let existing = self.items.entry(name.clone()).or_default(); | 115 | let existing = self.items.entry(name.clone()).or_default(); |
123 | 116 | ||
124 | if existing.def.types.is_none() && res.def.types.is_some() { | 117 | if existing.def.types.is_none() && res.def.types.is_some() { |
125 | existing.def.types = res.def.types; | 118 | existing.def.types = res.def.types; |
126 | existing.import = import.or(res.import); | 119 | existing.declaration |= declaration; |
127 | changed = true; | 120 | changed = true; |
128 | } | 121 | } |
129 | if existing.def.values.is_none() && res.def.values.is_some() { | 122 | if existing.def.values.is_none() && res.def.values.is_some() { |
130 | existing.def.values = res.def.values; | 123 | existing.def.values = res.def.values; |
131 | existing.import = import.or(res.import); | 124 | existing.declaration |= declaration; |
132 | changed = true; | 125 | changed = true; |
133 | } | 126 | } |
134 | if existing.def.macros.is_none() && res.def.macros.is_some() { | 127 | if existing.def.macros.is_none() && res.def.macros.is_some() { |
135 | existing.def.macros = res.def.macros; | 128 | existing.def.macros = res.def.macros; |
136 | existing.import = import.or(res.import); | 129 | existing.declaration |= declaration; |
137 | changed = true; | 130 | changed = true; |
138 | } | 131 | } |
139 | 132 | ||
140 | if existing.def.is_none() | ||
141 | && res.def.is_none() | ||
142 | && existing.import.is_none() | ||
143 | && res.import.is_some() | ||
144 | { | ||
145 | existing.import = res.import; | ||
146 | } | ||
147 | changed | 133 | changed |
148 | } | 134 | } |
149 | 135 | ||
@@ -160,6 +146,5 @@ impl ItemScope { | |||
160 | pub struct Resolution { | 146 | pub struct Resolution { |
161 | /// None for unresolved | 147 | /// None for unresolved |
162 | pub def: PerNs, | 148 | pub def: PerNs, |
163 | /// ident by which this is imported into local scope. | 149 | pub declaration: bool, |
164 | pub import: Option<LocalImportId>, | ||
165 | } | 150 | } |
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index acd4f4af1..f6c7f38d1 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs | |||
@@ -52,10 +52,6 @@ use crate::body::Expander; | |||
52 | use crate::builtin_type::BuiltinType; | 52 | use crate::builtin_type::BuiltinType; |
53 | 53 | ||
54 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 54 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
55 | pub struct LocalImportId(RawId); | ||
56 | impl_arena_id!(LocalImportId); | ||
57 | |||
58 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
59 | pub struct ModuleId { | 55 | pub struct ModuleId { |
60 | pub krate: CrateId, | 56 | pub krate: CrateId, |
61 | pub local_id: LocalModuleId, | 57 | pub local_id: LocalModuleId, |
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 45199fa11..9419461a8 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs | |||
@@ -26,8 +26,7 @@ use crate::{ | |||
26 | path::{ModPath, PathKind}, | 26 | path::{ModPath, PathKind}, |
27 | per_ns::PerNs, | 27 | per_ns::PerNs, |
28 | AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern, | 28 | AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern, |
29 | LocalImportId, LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, | 29 | LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, |
30 | TypeAliasLoc, UnionLoc, | ||
31 | }; | 30 | }; |
32 | 31 | ||
33 | pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { | 32 | pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { |
@@ -93,7 +92,7 @@ impl PartialResolvedImport { | |||
93 | #[derive(Clone, Debug, Eq, PartialEq)] | 92 | #[derive(Clone, Debug, Eq, PartialEq)] |
94 | struct ImportDirective { | 93 | struct ImportDirective { |
95 | module_id: LocalModuleId, | 94 | module_id: LocalModuleId, |
96 | import_id: LocalImportId, | 95 | import_id: raw::LocalImportId, |
97 | import: raw::ImportData, | 96 | import: raw::ImportData, |
98 | status: PartialResolvedImport, | 97 | status: PartialResolvedImport, |
99 | } | 98 | } |
@@ -110,7 +109,7 @@ struct MacroDirective { | |||
110 | struct DefCollector<'a, DB> { | 109 | struct DefCollector<'a, DB> { |
111 | db: &'a DB, | 110 | db: &'a DB, |
112 | def_map: CrateDefMap, | 111 | def_map: CrateDefMap, |
113 | glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, LocalImportId)>>, | 112 | glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, raw::LocalImportId)>>, |
114 | unresolved_imports: Vec<ImportDirective>, | 113 | unresolved_imports: Vec<ImportDirective>, |
115 | resolved_imports: Vec<ImportDirective>, | 114 | resolved_imports: Vec<ImportDirective>, |
116 | unexpanded_macros: Vec<MacroDirective>, | 115 | unexpanded_macros: Vec<MacroDirective>, |
@@ -218,8 +217,7 @@ where | |||
218 | if export { | 217 | if export { |
219 | self.update( | 218 | self.update( |
220 | self.def_map.root, | 219 | self.def_map.root, |
221 | None, | 220 | &[(name, Resolution { def: PerNs::macros(macro_), declaration: false })], |
222 | &[(name, Resolution { def: PerNs::macros(macro_), import: None })], | ||
223 | ); | 221 | ); |
224 | } | 222 | } |
225 | } | 223 | } |
@@ -374,7 +372,7 @@ where | |||
374 | // Module scoped macros is included | 372 | // Module scoped macros is included |
375 | let items = scope.collect_resolutions(); | 373 | let items = scope.collect_resolutions(); |
376 | 374 | ||
377 | self.update(module_id, Some(import_id), &items); | 375 | self.update(module_id, &items); |
378 | } else { | 376 | } else { |
379 | // glob import from same crate => we do an initial | 377 | // glob import from same crate => we do an initial |
380 | // import, and then need to propagate any further | 378 | // import, and then need to propagate any further |
@@ -384,7 +382,7 @@ where | |||
384 | // Module scoped macros is included | 382 | // Module scoped macros is included |
385 | let items = scope.collect_resolutions(); | 383 | let items = scope.collect_resolutions(); |
386 | 384 | ||
387 | self.update(module_id, Some(import_id), &items); | 385 | self.update(module_id, &items); |
388 | // record the glob import in case we add further items | 386 | // record the glob import in case we add further items |
389 | let glob = self.glob_imports.entry(m.local_id).or_default(); | 387 | let glob = self.glob_imports.entry(m.local_id).or_default(); |
390 | if !glob.iter().any(|it| *it == (module_id, import_id)) { | 388 | if !glob.iter().any(|it| *it == (module_id, import_id)) { |
@@ -404,12 +402,12 @@ where | |||
404 | let variant = EnumVariantId { parent: e, local_id }; | 402 | let variant = EnumVariantId { parent: e, local_id }; |
405 | let res = Resolution { | 403 | let res = Resolution { |
406 | def: PerNs::both(variant.into(), variant.into()), | 404 | def: PerNs::both(variant.into(), variant.into()), |
407 | import: Some(import_id), | 405 | declaration: false, |
408 | }; | 406 | }; |
409 | (name, res) | 407 | (name, res) |
410 | }) | 408 | }) |
411 | .collect::<Vec<_>>(); | 409 | .collect::<Vec<_>>(); |
412 | self.update(module_id, Some(import_id), &resolutions); | 410 | self.update(module_id, &resolutions); |
413 | } | 411 | } |
414 | Some(d) => { | 412 | Some(d) => { |
415 | log::debug!("glob import {:?} from non-module/enum {:?}", import, d); | 413 | log::debug!("glob import {:?} from non-module/enum {:?}", import, d); |
@@ -431,27 +429,21 @@ where | |||
431 | } | 429 | } |
432 | } | 430 | } |
433 | 431 | ||
434 | let resolution = Resolution { def, import: Some(import_id) }; | 432 | let resolution = Resolution { def, declaration: false }; |
435 | self.update(module_id, Some(import_id), &[(name, resolution)]); | 433 | self.update(module_id, &[(name, resolution)]); |
436 | } | 434 | } |
437 | None => tested_by!(bogus_paths), | 435 | None => tested_by!(bogus_paths), |
438 | } | 436 | } |
439 | } | 437 | } |
440 | } | 438 | } |
441 | 439 | ||
442 | fn update( | 440 | fn update(&mut self, module_id: LocalModuleId, resolutions: &[(Name, Resolution)]) { |
443 | &mut self, | 441 | self.update_recursive(module_id, resolutions, 0) |
444 | module_id: LocalModuleId, | ||
445 | import: Option<LocalImportId>, | ||
446 | resolutions: &[(Name, Resolution)], | ||
447 | ) { | ||
448 | self.update_recursive(module_id, import, resolutions, 0) | ||
449 | } | 442 | } |
450 | 443 | ||
451 | fn update_recursive( | 444 | fn update_recursive( |
452 | &mut self, | 445 | &mut self, |
453 | module_id: LocalModuleId, | 446 | module_id: LocalModuleId, |
454 | import: Option<LocalImportId>, | ||
455 | resolutions: &[(Name, Resolution)], | 447 | resolutions: &[(Name, Resolution)], |
456 | depth: usize, | 448 | depth: usize, |
457 | ) { | 449 | ) { |
@@ -462,7 +454,7 @@ where | |||
462 | let scope = &mut self.def_map.modules[module_id].scope; | 454 | let scope = &mut self.def_map.modules[module_id].scope; |
463 | let mut changed = false; | 455 | let mut changed = false; |
464 | for (name, res) in resolutions { | 456 | for (name, res) in resolutions { |
465 | changed |= scope.push_res(name.clone(), res, import); | 457 | changed |= scope.push_res(name.clone(), res, depth == 0 && res.declaration); |
466 | } | 458 | } |
467 | 459 | ||
468 | if !changed { | 460 | if !changed { |
@@ -475,9 +467,9 @@ where | |||
475 | .flat_map(|v| v.iter()) | 467 | .flat_map(|v| v.iter()) |
476 | .cloned() | 468 | .cloned() |
477 | .collect::<Vec<_>>(); | 469 | .collect::<Vec<_>>(); |
478 | for (glob_importing_module, glob_import) in glob_imports { | 470 | for (glob_importing_module, _glob_import) in glob_imports { |
479 | // We pass the glob import so that the tracked import in those modules is that glob import | 471 | // We pass the glob import so that the tracked import in those modules is that glob import |
480 | self.update_recursive(glob_importing_module, Some(glob_import), resolutions, depth + 1); | 472 | self.update_recursive(glob_importing_module, resolutions, depth + 1); |
481 | } | 473 | } |
482 | } | 474 | } |
483 | 475 | ||
@@ -719,9 +711,9 @@ where | |||
719 | def: PerNs::types( | 711 | def: PerNs::types( |
720 | ModuleId { krate: self.def_collector.def_map.krate, local_id: res }.into(), | 712 | ModuleId { krate: self.def_collector.def_map.krate, local_id: res }.into(), |
721 | ), | 713 | ), |
722 | import: None, | 714 | declaration: true, |
723 | }; | 715 | }; |
724 | self.def_collector.update(self.module_id, None, &[(name, resolution)]); | 716 | self.def_collector.update(self.module_id, &[(name, resolution)]); |
725 | res | 717 | res |
726 | } | 718 | } |
727 | 719 | ||
@@ -791,8 +783,8 @@ where | |||
791 | PerNs::types(def.into()) | 783 | PerNs::types(def.into()) |
792 | } | 784 | } |
793 | }; | 785 | }; |
794 | let resolution = Resolution { def, import: None }; | 786 | let resolution = Resolution { def, declaration: true }; |
795 | self.def_collector.update(self.module_id, None, &[(name, resolution)]) | 787 | self.def_collector.update(self.module_id, &[(name, resolution)]) |
796 | } | 788 | } |
797 | 789 | ||
798 | fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) { | 790 | fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) { |
diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index ecb4d7c03..b10e458a2 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs | |||
@@ -7,24 +7,24 @@ | |||
7 | 7 | ||
8 | use std::{ops::Index, sync::Arc}; | 8 | use std::{ops::Index, sync::Arc}; |
9 | 9 | ||
10 | use either::Either; | ||
11 | use hir_expand::{ | 10 | use hir_expand::{ |
12 | ast_id_map::AstIdMap, | 11 | ast_id_map::AstIdMap, |
13 | db::AstDatabase, | 12 | db::AstDatabase, |
14 | hygiene::Hygiene, | 13 | hygiene::Hygiene, |
15 | name::{AsName, Name}, | 14 | name::{AsName, Name}, |
16 | }; | 15 | }; |
17 | use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId}; | 16 | use ra_arena::{impl_arena_id, Arena, RawId}; |
18 | use ra_syntax::{ | 17 | use ra_syntax::{ |
19 | ast::{self, AttrsOwner, NameOwner}, | 18 | ast::{self, AttrsOwner, NameOwner}, |
20 | AstNode, AstPtr, | 19 | AstNode, |
21 | }; | 20 | }; |
22 | use test_utils::tested_by; | 21 | use test_utils::tested_by; |
23 | 22 | ||
24 | use crate::{ | 23 | use crate::{attr::Attrs, db::DefDatabase, path::ModPath, FileAstId, HirFileId, InFile}; |
25 | attr::Attrs, db::DefDatabase, path::ModPath, trace::Trace, FileAstId, HirFileId, InFile, | 24 | |
26 | LocalImportId, | 25 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
27 | }; | 26 | pub(super) struct LocalImportId(RawId); |
27 | impl_arena_id!(LocalImportId); | ||
28 | 28 | ||
29 | /// `RawItems` is a set of top-level items in a file (except for impls). | 29 | /// `RawItems` is a set of top-level items in a file (except for impls). |
30 | /// | 30 | /// |
@@ -41,35 +41,14 @@ pub struct RawItems { | |||
41 | items: Vec<RawItem>, | 41 | items: Vec<RawItem>, |
42 | } | 42 | } |
43 | 43 | ||
44 | #[derive(Debug, Default, PartialEq, Eq)] | ||
45 | pub struct ImportSourceMap { | ||
46 | map: ArenaMap<LocalImportId, ImportSourcePtr>, | ||
47 | } | ||
48 | |||
49 | type ImportSourcePtr = Either<AstPtr<ast::UseTree>, AstPtr<ast::ExternCrateItem>>; | ||
50 | |||
51 | impl ImportSourceMap { | ||
52 | pub fn get(&self, import: LocalImportId) -> ImportSourcePtr { | ||
53 | self.map[import].clone() | ||
54 | } | ||
55 | } | ||
56 | |||
57 | impl RawItems { | 44 | impl RawItems { |
58 | pub(crate) fn raw_items_query( | 45 | pub(crate) fn raw_items_query( |
59 | db: &(impl DefDatabase + AstDatabase), | 46 | db: &(impl DefDatabase + AstDatabase), |
60 | file_id: HirFileId, | 47 | file_id: HirFileId, |
61 | ) -> Arc<RawItems> { | 48 | ) -> Arc<RawItems> { |
62 | db.raw_items_with_source_map(file_id).0 | ||
63 | } | ||
64 | |||
65 | pub(crate) fn raw_items_with_source_map_query( | ||
66 | db: &(impl DefDatabase + AstDatabase), | ||
67 | file_id: HirFileId, | ||
68 | ) -> (Arc<RawItems>, Arc<ImportSourceMap>) { | ||
69 | let mut collector = RawItemsCollector { | 49 | let mut collector = RawItemsCollector { |
70 | raw_items: RawItems::default(), | 50 | raw_items: RawItems::default(), |
71 | source_ast_id_map: db.ast_id_map(file_id), | 51 | source_ast_id_map: db.ast_id_map(file_id), |
72 | imports: Trace::new(), | ||
73 | file_id, | 52 | file_id, |
74 | hygiene: Hygiene::new(db, file_id), | 53 | hygiene: Hygiene::new(db, file_id), |
75 | }; | 54 | }; |
@@ -80,11 +59,8 @@ impl RawItems { | |||
80 | collector.process_module(None, item_list); | 59 | collector.process_module(None, item_list); |
81 | } | 60 | } |
82 | } | 61 | } |
83 | let mut raw_items = collector.raw_items; | 62 | let raw_items = collector.raw_items; |
84 | let (arena, map) = collector.imports.into_arena_and_map(); | 63 | Arc::new(raw_items) |
85 | raw_items.imports = arena; | ||
86 | let source_map = ImportSourceMap { map }; | ||
87 | (Arc::new(raw_items), Arc::new(source_map)) | ||
88 | } | 64 | } |
89 | 65 | ||
90 | pub(super) fn items(&self) -> &[RawItem] { | 66 | pub(super) fn items(&self) -> &[RawItem] { |
@@ -223,7 +199,6 @@ pub(super) struct ImplData { | |||
223 | 199 | ||
224 | struct RawItemsCollector { | 200 | struct RawItemsCollector { |
225 | raw_items: RawItems, | 201 | raw_items: RawItems, |
226 | imports: Trace<LocalImportId, ImportData, ImportSourcePtr>, | ||
227 | source_ast_id_map: Arc<AstIdMap>, | 202 | source_ast_id_map: Arc<AstIdMap>, |
228 | file_id: HirFileId, | 203 | file_id: HirFileId, |
229 | hygiene: Hygiene, | 204 | hygiene: Hygiene, |
@@ -330,7 +305,7 @@ impl RawItemsCollector { | |||
330 | ModPath::expand_use_item( | 305 | ModPath::expand_use_item( |
331 | InFile { value: use_item, file_id: self.file_id }, | 306 | InFile { value: use_item, file_id: self.file_id }, |
332 | &self.hygiene, | 307 | &self.hygiene, |
333 | |path, use_tree, is_glob, alias| { | 308 | |path, _use_tree, is_glob, alias| { |
334 | let import_data = ImportData { | 309 | let import_data = ImportData { |
335 | path, | 310 | path, |
336 | alias, | 311 | alias, |
@@ -339,11 +314,11 @@ impl RawItemsCollector { | |||
339 | is_extern_crate: false, | 314 | is_extern_crate: false, |
340 | is_macro_use: false, | 315 | is_macro_use: false, |
341 | }; | 316 | }; |
342 | buf.push((import_data, Either::Left(AstPtr::new(use_tree)))); | 317 | buf.push(import_data); |
343 | }, | 318 | }, |
344 | ); | 319 | ); |
345 | for (import_data, ptr) in buf { | 320 | for import_data in buf { |
346 | self.push_import(current_module, attrs.clone(), import_data, ptr); | 321 | self.push_import(current_module, attrs.clone(), import_data); |
347 | } | 322 | } |
348 | } | 323 | } |
349 | 324 | ||
@@ -366,12 +341,7 @@ impl RawItemsCollector { | |||
366 | is_extern_crate: true, | 341 | is_extern_crate: true, |
367 | is_macro_use, | 342 | is_macro_use, |
368 | }; | 343 | }; |
369 | self.push_import( | 344 | self.push_import(current_module, attrs, import_data); |
370 | current_module, | ||
371 | attrs, | ||
372 | import_data, | ||
373 | Either::Right(AstPtr::new(&extern_crate)), | ||
374 | ); | ||
375 | } | 345 | } |
376 | } | 346 | } |
377 | 347 | ||
@@ -402,14 +372,8 @@ impl RawItemsCollector { | |||
402 | self.push_item(current_module, attrs, RawItemKind::Impl(imp)) | 372 | self.push_item(current_module, attrs, RawItemKind::Impl(imp)) |
403 | } | 373 | } |
404 | 374 | ||
405 | fn push_import( | 375 | fn push_import(&mut self, current_module: Option<Module>, attrs: Attrs, data: ImportData) { |
406 | &mut self, | 376 | let import = self.raw_items.imports.alloc(data); |
407 | current_module: Option<Module>, | ||
408 | attrs: Attrs, | ||
409 | data: ImportData, | ||
410 | source: ImportSourcePtr, | ||
411 | ) { | ||
412 | let import = self.imports.alloc(|| source, || data); | ||
413 | self.push_item(current_module, attrs, RawItemKind::Import(import)) | 377 | self.push_item(current_module, attrs, RawItemKind::Import(import)) |
414 | } | 378 | } |
415 | 379 | ||
diff --git a/crates/ra_hir_def/src/trace.rs b/crates/ra_hir_def/src/trace.rs index 2bcd707bc..9769e88df 100644 --- a/crates/ra_hir_def/src/trace.rs +++ b/crates/ra_hir_def/src/trace.rs | |||
@@ -18,10 +18,6 @@ pub(crate) struct Trace<ID: ArenaId, T, V> { | |||
18 | } | 18 | } |
19 | 19 | ||
20 | impl<ID: ra_arena::ArenaId + Copy, T, V> Trace<ID, T, V> { | 20 | impl<ID: ra_arena::ArenaId + Copy, T, V> Trace<ID, T, V> { |
21 | pub(crate) fn new() -> Trace<ID, T, V> { | ||
22 | Trace { arena: Some(Arena::default()), map: Some(ArenaMap::default()), len: 0 } | ||
23 | } | ||
24 | |||
25 | pub(crate) fn new_for_arena() -> Trace<ID, T, V> { | 21 | pub(crate) fn new_for_arena() -> Trace<ID, T, V> { |
26 | Trace { arena: Some(Arena::default()), map: None, len: 0 } | 22 | Trace { arena: Some(Arena::default()), map: None, len: 0 } |
27 | } | 23 | } |
@@ -52,8 +48,4 @@ impl<ID: ra_arena::ArenaId + Copy, T, V> Trace<ID, T, V> { | |||
52 | pub(crate) fn into_map(mut self) -> ArenaMap<ID, V> { | 48 | pub(crate) fn into_map(mut self) -> ArenaMap<ID, V> { |
53 | self.map.take().unwrap() | 49 | self.map.take().unwrap() |
54 | } | 50 | } |
55 | |||
56 | pub(crate) fn into_arena_and_map(mut self) -> (Arena<ID, T>, ArenaMap<ID, V>) { | ||
57 | (self.arena.take().unwrap(), self.map.take().unwrap()) | ||
58 | } | ||
59 | } | 51 | } |
diff --git a/crates/ra_ide/src/change.rs b/crates/ra_ide/src/change.rs index 4a76d1dd8..387a9cafb 100644 --- a/crates/ra_ide/src/change.rs +++ b/crates/ra_ide/src/change.rs | |||
@@ -270,7 +270,6 @@ impl RootDatabase { | |||
270 | 270 | ||
271 | self.query(hir::db::AstIdMapQuery).sweep(sweep); | 271 | self.query(hir::db::AstIdMapQuery).sweep(sweep); |
272 | 272 | ||
273 | self.query(hir::db::RawItemsWithSourceMapQuery).sweep(sweep); | ||
274 | self.query(hir::db::BodyWithSourceMapQuery).sweep(sweep); | 273 | self.query(hir::db::BodyWithSourceMapQuery).sweep(sweep); |
275 | 274 | ||
276 | self.query(hir::db::ExprScopesQuery).sweep(sweep); | 275 | self.query(hir::db::ExprScopesQuery).sweep(sweep); |
@@ -309,7 +308,6 @@ impl RootDatabase { | |||
309 | hir::db::StructDataQuery | 308 | hir::db::StructDataQuery |
310 | hir::db::EnumDataQuery | 309 | hir::db::EnumDataQuery |
311 | hir::db::TraitDataQuery | 310 | hir::db::TraitDataQuery |
312 | hir::db::RawItemsWithSourceMapQuery | ||
313 | hir::db::RawItemsQuery | 311 | hir::db::RawItemsQuery |
314 | hir::db::CrateDefMapQuery | 312 | hir::db::CrateDefMapQuery |
315 | hir::db::GenericParamsQuery | 313 | hir::db::GenericParamsQuery |
diff --git a/crates/ra_ide/src/completion/complete_path.rs b/crates/ra_ide/src/completion/complete_path.rs index 28f94e0a7..8ce86ad7d 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 | ||
3 | use either::Either; | 3 | use hir::{Adt, PathResolution, ScopeDef}; |
4 | use hir::{Adt, HasSource, PathResolution}; | ||
5 | use ra_syntax::AstNode; | 4 | use ra_syntax::AstNode; |
6 | use test_utils::tested_by; | 5 | use 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.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; |
diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs index 4894ea2f6..8f56ce706 100644 --- a/crates/ra_ide/src/completion/completion_context.rs +++ b/crates/ra_ide/src/completion/completion_context.rs | |||
@@ -18,6 +18,7 @@ pub(crate) struct CompletionContext<'a> { | |||
18 | pub(super) analyzer: hir::SourceAnalyzer, | 18 | pub(super) analyzer: hir::SourceAnalyzer, |
19 | pub(super) offset: TextUnit, | 19 | pub(super) offset: TextUnit, |
20 | pub(super) token: SyntaxToken, | 20 | pub(super) token: SyntaxToken, |
21 | pub(super) name_ref: Option<ast::NameRef>, | ||
21 | pub(super) module: Option<hir::Module>, | 22 | pub(super) module: Option<hir::Module>, |
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>, |
@@ -68,6 +69,7 @@ impl<'a> CompletionContext<'a> { | |||
68 | analyzer, | 69 | analyzer, |
69 | token, | 70 | token, |
70 | offset: position.offset, | 71 | offset: position.offset, |
72 | name_ref: None, | ||
71 | module, | 73 | module, |
72 | function_syntax: None, | 74 | function_syntax: None, |
73 | use_item_syntax: None, | 75 | use_item_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 = | ||
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); |