diff options
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r-- | crates/ra_hir_def/src/find_path.rs | 12 | ||||
-rw-r--r-- | crates/ra_hir_def/src/keys.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir_def/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres.rs | 43 | ||||
-rw-r--r-- | crates/ra_hir_def/src/path.rs | 11 | ||||
-rw-r--r-- | crates/ra_hir_def/src/path/lower/lower_use.rs | 8 |
6 files changed, 27 insertions, 53 deletions
diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs index f7dc8acb7..8cc2fb160 100644 --- a/crates/ra_hir_def/src/find_path.rs +++ b/crates/ra_hir_def/src/find_path.rs | |||
@@ -35,7 +35,7 @@ fn find_path_inner( | |||
35 | let def_map = db.crate_def_map(from.krate); | 35 | let def_map = db.crate_def_map(from.krate); |
36 | let from_scope: &crate::item_scope::ItemScope = &def_map.modules[from.local_id].scope; | 36 | let from_scope: &crate::item_scope::ItemScope = &def_map.modules[from.local_id].scope; |
37 | if let Some((name, _)) = from_scope.name_of(item) { | 37 | if let Some((name, _)) = from_scope.name_of(item) { |
38 | return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()])); | 38 | return Some(ModPath::from_segments(PathKind::Plain, vec![name.clone()])); |
39 | } | 39 | } |
40 | 40 | ||
41 | // - if the item is the crate root, return `crate` | 41 | // - if the item is the crate root, return `crate` |
@@ -45,12 +45,12 @@ fn find_path_inner( | |||
45 | local_id: def_map.root, | 45 | local_id: def_map.root, |
46 | })) | 46 | })) |
47 | { | 47 | { |
48 | return Some(ModPath::from_simple_segments(PathKind::Crate, Vec::new())); | 48 | return Some(ModPath::from_segments(PathKind::Crate, Vec::new())); |
49 | } | 49 | } |
50 | 50 | ||
51 | // - if the item is the module we're in, use `self` | 51 | // - if the item is the module we're in, use `self` |
52 | if item == ItemInNs::Types(from.into()) { | 52 | if item == ItemInNs::Types(from.into()) { |
53 | return Some(ModPath::from_simple_segments(PathKind::Super(0), Vec::new())); | 53 | return Some(ModPath::from_segments(PathKind::Super(0), Vec::new())); |
54 | } | 54 | } |
55 | 55 | ||
56 | // - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly) | 56 | // - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly) |
@@ -61,14 +61,14 @@ fn find_path_inner( | |||
61 | local_id: parent_id, | 61 | local_id: parent_id, |
62 | })) | 62 | })) |
63 | { | 63 | { |
64 | return Some(ModPath::from_simple_segments(PathKind::Super(1), Vec::new())); | 64 | return Some(ModPath::from_segments(PathKind::Super(1), Vec::new())); |
65 | } | 65 | } |
66 | } | 66 | } |
67 | 67 | ||
68 | // - if the item is the crate root of a dependency crate, return the name from the extern prelude | 68 | // - if the item is the crate root of a dependency crate, return the name from the extern prelude |
69 | for (name, def_id) in &def_map.extern_prelude { | 69 | for (name, def_id) in &def_map.extern_prelude { |
70 | if item == ItemInNs::Types(*def_id) { | 70 | if item == ItemInNs::Types(*def_id) { |
71 | return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()])); | 71 | return Some(ModPath::from_segments(PathKind::Plain, vec![name.clone()])); |
72 | } | 72 | } |
73 | } | 73 | } |
74 | 74 | ||
@@ -79,7 +79,7 @@ fn find_path_inner( | |||
79 | &prelude_def_map.modules[prelude_module.local_id].scope; | 79 | &prelude_def_map.modules[prelude_module.local_id].scope; |
80 | if let Some((name, vis)) = prelude_scope.name_of(item) { | 80 | if let Some((name, vis)) = prelude_scope.name_of(item) { |
81 | if vis.is_visible_from(db, from) { | 81 | if vis.is_visible_from(db, from) { |
82 | return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()])); | 82 | return Some(ModPath::from_segments(PathKind::Plain, vec![name.clone()])); |
83 | } | 83 | } |
84 | } | 84 | } |
85 | } | 85 | } |
diff --git a/crates/ra_hir_def/src/keys.rs b/crates/ra_hir_def/src/keys.rs index d844f7a62..5913f12b1 100644 --- a/crates/ra_hir_def/src/keys.rs +++ b/crates/ra_hir_def/src/keys.rs | |||
@@ -2,7 +2,7 @@ | |||
2 | 2 | ||
3 | use std::marker::PhantomData; | 3 | use std::marker::PhantomData; |
4 | 4 | ||
5 | use hir_expand::InFile; | 5 | use hir_expand::{InFile, MacroDefId}; |
6 | use ra_syntax::{ast, AstNode, AstPtr}; | 6 | use ra_syntax::{ast, AstNode, AstPtr}; |
7 | use rustc_hash::FxHashMap; | 7 | use rustc_hash::FxHashMap; |
8 | 8 | ||
@@ -29,6 +29,8 @@ pub const TUPLE_FIELD: Key<ast::TupleFieldDef, StructFieldId> = Key::new(); | |||
29 | pub const RECORD_FIELD: Key<ast::RecordFieldDef, StructFieldId> = Key::new(); | 29 | pub const RECORD_FIELD: Key<ast::RecordFieldDef, StructFieldId> = Key::new(); |
30 | pub const TYPE_PARAM: Key<ast::TypeParam, TypeParamId> = Key::new(); | 30 | pub const TYPE_PARAM: Key<ast::TypeParam, TypeParamId> = Key::new(); |
31 | 31 | ||
32 | pub const MACRO: Key<ast::MacroCall, MacroDefId> = Key::new(); | ||
33 | |||
32 | /// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are | 34 | /// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are |
33 | /// equal if they point to exactly the same object. | 35 | /// equal if they point to exactly the same object. |
34 | /// | 36 | /// |
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index ebc12e891..feb3a300d 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs | |||
@@ -332,7 +332,7 @@ pub enum VariantId { | |||
332 | StructId(StructId), | 332 | StructId(StructId), |
333 | UnionId(UnionId), | 333 | UnionId(UnionId), |
334 | } | 334 | } |
335 | impl_froms!(VariantId: EnumVariantId, StructId); | 335 | impl_froms!(VariantId: EnumVariantId, StructId, UnionId); |
336 | 336 | ||
337 | trait Intern { | 337 | trait Intern { |
338 | type ID; | 338 | type ID; |
diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index 4d210eab1..27c12e46c 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs | |||
@@ -59,12 +59,9 @@ use std::sync::Arc; | |||
59 | 59 | ||
60 | use hir_expand::{diagnostics::DiagnosticSink, name::Name, InFile}; | 60 | use hir_expand::{diagnostics::DiagnosticSink, name::Name, InFile}; |
61 | use ra_arena::Arena; | 61 | use ra_arena::Arena; |
62 | use ra_db::{CrateId, Edition, FileId, FilePosition}; | 62 | use ra_db::{CrateId, Edition, FileId}; |
63 | use ra_prof::profile; | 63 | use ra_prof::profile; |
64 | use ra_syntax::{ | 64 | use ra_syntax::ast; |
65 | ast::{self, AstNode}, | ||
66 | SyntaxNode, | ||
67 | }; | ||
68 | use rustc_hash::FxHashMap; | 65 | use rustc_hash::FxHashMap; |
69 | 66 | ||
70 | use crate::{ | 67 | use crate::{ |
@@ -145,6 +142,13 @@ impl ModuleOrigin { | |||
145 | } | 142 | } |
146 | } | 143 | } |
147 | 144 | ||
145 | pub fn is_inline(&self) -> bool { | ||
146 | match self { | ||
147 | ModuleOrigin::Inline { .. } => true, | ||
148 | ModuleOrigin::CrateRoot { .. } | ModuleOrigin::File { .. } => false, | ||
149 | } | ||
150 | } | ||
151 | |||
148 | /// Returns a node which defines this module. | 152 | /// Returns a node which defines this module. |
149 | /// That is, a file or a `mod foo {}` with items. | 153 | /// That is, a file or a `mod foo {}` with items. |
150 | fn definition_source(&self, db: &impl DefDatabase) -> InFile<ModuleSource> { | 154 | fn definition_source(&self, db: &impl DefDatabase) -> InFile<ModuleSource> { |
@@ -248,35 +252,6 @@ pub enum ModuleSource { | |||
248 | Module(ast::Module), | 252 | Module(ast::Module), |
249 | } | 253 | } |
250 | 254 | ||
251 | impl ModuleSource { | ||
252 | // FIXME: this methods do not belong here | ||
253 | pub fn from_position(db: &impl DefDatabase, position: FilePosition) -> ModuleSource { | ||
254 | let parse = db.parse(position.file_id); | ||
255 | match &ra_syntax::algo::find_node_at_offset::<ast::Module>( | ||
256 | parse.tree().syntax(), | ||
257 | position.offset, | ||
258 | ) { | ||
259 | Some(m) if !m.has_semi() => ModuleSource::Module(m.clone()), | ||
260 | _ => { | ||
261 | let source_file = parse.tree(); | ||
262 | ModuleSource::SourceFile(source_file) | ||
263 | } | ||
264 | } | ||
265 | } | ||
266 | |||
267 | pub fn from_child_node(db: &impl DefDatabase, child: InFile<&SyntaxNode>) -> ModuleSource { | ||
268 | if let Some(m) = | ||
269 | child.value.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi()) | ||
270 | { | ||
271 | ModuleSource::Module(m) | ||
272 | } else { | ||
273 | let file_id = child.file_id.original_file(db); | ||
274 | let source_file = db.parse(file_id).tree(); | ||
275 | ModuleSource::SourceFile(source_file) | ||
276 | } | ||
277 | } | ||
278 | } | ||
279 | |||
280 | mod diagnostics { | 255 | mod diagnostics { |
281 | use hir_expand::diagnostics::DiagnosticSink; | 256 | use hir_expand::diagnostics::DiagnosticSink; |
282 | use ra_db::RelativePathBuf; | 257 | use ra_db::RelativePathBuf; |
diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index 9f93a5424..ab290e2c9 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs | |||
@@ -39,10 +39,7 @@ impl ModPath { | |||
39 | lower::lower_path(path, hygiene).map(|it| it.mod_path) | 39 | lower::lower_path(path, hygiene).map(|it| it.mod_path) |
40 | } | 40 | } |
41 | 41 | ||
42 | pub fn from_simple_segments( | 42 | pub fn from_segments(kind: PathKind, segments: impl IntoIterator<Item = Name>) -> ModPath { |
43 | kind: PathKind, | ||
44 | segments: impl IntoIterator<Item = Name>, | ||
45 | ) -> ModPath { | ||
46 | let segments = segments.into_iter().collect::<Vec<_>>(); | 43 | let segments = segments.into_iter().collect::<Vec<_>>(); |
47 | ModPath { kind, segments } | 44 | ModPath { kind, segments } |
48 | } | 45 | } |
@@ -240,7 +237,7 @@ impl From<Name> for Path { | |||
240 | fn from(name: Name) -> Path { | 237 | fn from(name: Name) -> Path { |
241 | Path { | 238 | Path { |
242 | type_anchor: None, | 239 | type_anchor: None, |
243 | mod_path: ModPath::from_simple_segments(PathKind::Plain, iter::once(name)), | 240 | mod_path: ModPath::from_segments(PathKind::Plain, iter::once(name)), |
244 | generic_args: vec![None], | 241 | generic_args: vec![None], |
245 | } | 242 | } |
246 | } | 243 | } |
@@ -248,7 +245,7 @@ impl From<Name> for Path { | |||
248 | 245 | ||
249 | impl From<Name> for ModPath { | 246 | impl From<Name> for ModPath { |
250 | fn from(name: Name) -> ModPath { | 247 | fn from(name: Name) -> ModPath { |
251 | ModPath::from_simple_segments(PathKind::Plain, iter::once(name)) | 248 | ModPath::from_segments(PathKind::Plain, iter::once(name)) |
252 | } | 249 | } |
253 | } | 250 | } |
254 | 251 | ||
@@ -311,7 +308,7 @@ macro_rules! __known_path { | |||
311 | macro_rules! __path { | 308 | macro_rules! __path { |
312 | ($start:ident $(:: $seg:ident)*) => ({ | 309 | ($start:ident $(:: $seg:ident)*) => ({ |
313 | $crate::__known_path!($start $(:: $seg)*); | 310 | $crate::__known_path!($start $(:: $seg)*); |
314 | $crate::path::ModPath::from_simple_segments($crate::path::PathKind::Abs, vec![ | 311 | $crate::path::ModPath::from_segments($crate::path::PathKind::Abs, vec![ |
315 | $crate::path::__name![$start], $($crate::path::__name![$seg],)* | 312 | $crate::path::__name![$start], $($crate::path::__name![$seg],)* |
316 | ]) | 313 | ]) |
317 | }); | 314 | }); |
diff --git a/crates/ra_hir_def/src/path/lower/lower_use.rs b/crates/ra_hir_def/src/path/lower/lower_use.rs index 3218eaf0a..531878174 100644 --- a/crates/ra_hir_def/src/path/lower/lower_use.rs +++ b/crates/ra_hir_def/src/path/lower/lower_use.rs | |||
@@ -84,7 +84,7 @@ fn convert_path(prefix: Option<ModPath>, path: ast::Path, hygiene: &Hygiene) -> | |||
84 | res | 84 | res |
85 | } | 85 | } |
86 | Either::Right(crate_id) => { | 86 | Either::Right(crate_id) => { |
87 | return Some(ModPath::from_simple_segments( | 87 | return Some(ModPath::from_segments( |
88 | PathKind::DollarCrate(crate_id), | 88 | PathKind::DollarCrate(crate_id), |
89 | iter::empty(), | 89 | iter::empty(), |
90 | )) | 90 | )) |
@@ -95,19 +95,19 @@ fn convert_path(prefix: Option<ModPath>, path: ast::Path, hygiene: &Hygiene) -> | |||
95 | if prefix.is_some() { | 95 | if prefix.is_some() { |
96 | return None; | 96 | return None; |
97 | } | 97 | } |
98 | ModPath::from_simple_segments(PathKind::Crate, iter::empty()) | 98 | ModPath::from_segments(PathKind::Crate, iter::empty()) |
99 | } | 99 | } |
100 | ast::PathSegmentKind::SelfKw => { | 100 | ast::PathSegmentKind::SelfKw => { |
101 | if prefix.is_some() { | 101 | if prefix.is_some() { |
102 | return None; | 102 | return None; |
103 | } | 103 | } |
104 | ModPath::from_simple_segments(PathKind::Super(0), iter::empty()) | 104 | ModPath::from_segments(PathKind::Super(0), iter::empty()) |
105 | } | 105 | } |
106 | ast::PathSegmentKind::SuperKw => { | 106 | ast::PathSegmentKind::SuperKw => { |
107 | if prefix.is_some() { | 107 | if prefix.is_some() { |
108 | return None; | 108 | return None; |
109 | } | 109 | } |
110 | ModPath::from_simple_segments(PathKind::Super(1), iter::empty()) | 110 | ModPath::from_segments(PathKind::Super(1), iter::empty()) |
111 | } | 111 | } |
112 | ast::PathSegmentKind::Type { .. } => { | 112 | ast::PathSegmentKind::Type { .. } => { |
113 | // not allowed in imports | 113 | // not allowed in imports |