diff options
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model/src.rs | 41 | ||||
-rw-r--r-- | crates/ra_hir/src/from_source.rs | 62 | ||||
-rw-r--r-- | crates/ra_hir/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 30 |
5 files changed, 72 insertions, 69 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 38d66c2a7..dddac915b 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -30,7 +30,7 @@ use crate::{ | |||
30 | db::{DefDatabase, HirDatabase}, | 30 | db::{DefDatabase, HirDatabase}, |
31 | ty::display::HirFormatter, | 31 | ty::display::HirFormatter, |
32 | ty::{self, InEnvironment, InferenceResult, TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk}, | 32 | ty::{self, InEnvironment, InferenceResult, TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk}, |
33 | CallableDef, Either, HirDisplay, Name, Source, | 33 | CallableDef, Either, HirDisplay, InFile, Name, |
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 |
@@ -118,7 +118,7 @@ impl ModuleSource { | |||
118 | } | 118 | } |
119 | } | 119 | } |
120 | 120 | ||
121 | pub fn from_child_node(db: &impl DefDatabase, child: Source<&SyntaxNode>) -> ModuleSource { | 121 | pub fn from_child_node(db: &impl DefDatabase, child: InFile<&SyntaxNode>) -> ModuleSource { |
122 | if let Some(m) = | 122 | if let Some(m) = |
123 | child.value.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi()) | 123 | child.value.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi()) |
124 | { | 124 | { |
@@ -901,7 +901,7 @@ impl Local { | |||
901 | Type { krate, ty: InEnvironment { value: ty, environment } } | 901 | Type { krate, ty: InEnvironment { value: ty, environment } } |
902 | } | 902 | } |
903 | 903 | ||
904 | pub fn source(self, db: &impl HirDatabase) -> Source<Either<ast::BindPat, ast::SelfParam>> { | 904 | pub fn source(self, db: &impl HirDatabase) -> InFile<Either<ast::BindPat, ast::SelfParam>> { |
905 | let (_body, source_map) = db.body_with_source_map(self.parent.into()); | 905 | let (_body, source_map) = db.body_with_source_map(self.parent.into()); |
906 | let src = source_map.pat_syntax(self.pat_id).unwrap(); // Hmm... | 906 | let src = source_map.pat_syntax(self.pat_id).unwrap(); // Hmm... |
907 | let root = src.file_syntax(db); | 907 | let root = src.file_syntax(db); |
diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index bf3ee0834..2cf210349 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs | |||
@@ -1,6 +1,9 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use hir_def::{AstItemDef, HasChildSource, HasSource as _, Lookup, VariantId}; | 3 | use hir_def::{ |
4 | src::{HasChildSource, HasSource as _}, | ||
5 | AstItemDef, Lookup, VariantId, | ||
6 | }; | ||
4 | use hir_expand::either::Either; | 7 | use hir_expand::either::Either; |
5 | use ra_syntax::ast; | 8 | use ra_syntax::ast; |
6 | 9 | ||
@@ -9,18 +12,18 @@ use crate::{ | |||
9 | Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, | 12 | Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, |
10 | }; | 13 | }; |
11 | 14 | ||
12 | pub use hir_expand::Source; | 15 | pub use hir_expand::InFile; |
13 | 16 | ||
14 | pub trait HasSource { | 17 | pub trait HasSource { |
15 | type Ast; | 18 | type Ast; |
16 | fn source(self, db: &impl DefDatabase) -> Source<Self::Ast>; | 19 | fn source(self, db: &impl DefDatabase) -> InFile<Self::Ast>; |
17 | } | 20 | } |
18 | 21 | ||
19 | /// NB: Module is !HasSource, because it has two source nodes at the same time: | 22 | /// NB: Module is !HasSource, because it has two source nodes at the same time: |
20 | /// definition and declaration. | 23 | /// definition and declaration. |
21 | impl Module { | 24 | impl Module { |
22 | /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. | 25 | /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. |
23 | pub fn definition_source(self, db: &impl DefDatabase) -> Source<ModuleSource> { | 26 | pub fn definition_source(self, db: &impl DefDatabase) -> InFile<ModuleSource> { |
24 | let def_map = db.crate_def_map(self.id.krate); | 27 | let def_map = db.crate_def_map(self.id.krate); |
25 | let src = def_map[self.id.local_id].definition_source(db); | 28 | let src = def_map[self.id.local_id].definition_source(db); |
26 | src.map(|it| match it { | 29 | src.map(|it| match it { |
@@ -31,7 +34,7 @@ impl Module { | |||
31 | 34 | ||
32 | /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. | 35 | /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. |
33 | /// `None` for the crate root. | 36 | /// `None` for the crate root. |
34 | pub fn declaration_source(self, db: &impl DefDatabase) -> Option<Source<ast::Module>> { | 37 | pub fn declaration_source(self, db: &impl DefDatabase) -> Option<InFile<ast::Module>> { |
35 | let def_map = db.crate_def_map(self.id.krate); | 38 | let def_map = db.crate_def_map(self.id.krate); |
36 | def_map[self.id.local_id].declaration_source(db) | 39 | def_map[self.id.local_id].declaration_source(db) |
37 | } | 40 | } |
@@ -39,7 +42,7 @@ impl Module { | |||
39 | 42 | ||
40 | impl HasSource for StructField { | 43 | impl HasSource for StructField { |
41 | type Ast = FieldSource; | 44 | type Ast = FieldSource; |
42 | fn source(self, db: &impl DefDatabase) -> Source<FieldSource> { | 45 | fn source(self, db: &impl DefDatabase) -> InFile<FieldSource> { |
43 | let var = VariantId::from(self.parent); | 46 | let var = VariantId::from(self.parent); |
44 | let src = var.child_source(db); | 47 | let src = var.child_source(db); |
45 | src.map(|it| match it[self.id].clone() { | 48 | src.map(|it| match it[self.id].clone() { |
@@ -50,67 +53,67 @@ impl HasSource for StructField { | |||
50 | } | 53 | } |
51 | impl HasSource for Struct { | 54 | impl HasSource for Struct { |
52 | type Ast = ast::StructDef; | 55 | type Ast = ast::StructDef; |
53 | fn source(self, db: &impl DefDatabase) -> Source<ast::StructDef> { | 56 | fn source(self, db: &impl DefDatabase) -> InFile<ast::StructDef> { |
54 | self.id.source(db) | 57 | self.id.source(db) |
55 | } | 58 | } |
56 | } | 59 | } |
57 | impl HasSource for Union { | 60 | impl HasSource for Union { |
58 | type Ast = ast::UnionDef; | 61 | type Ast = ast::UnionDef; |
59 | fn source(self, db: &impl DefDatabase) -> Source<ast::UnionDef> { | 62 | fn source(self, db: &impl DefDatabase) -> InFile<ast::UnionDef> { |
60 | self.id.source(db) | 63 | self.id.source(db) |
61 | } | 64 | } |
62 | } | 65 | } |
63 | impl HasSource for Enum { | 66 | impl HasSource for Enum { |
64 | type Ast = ast::EnumDef; | 67 | type Ast = ast::EnumDef; |
65 | fn source(self, db: &impl DefDatabase) -> Source<ast::EnumDef> { | 68 | fn source(self, db: &impl DefDatabase) -> InFile<ast::EnumDef> { |
66 | self.id.source(db) | 69 | self.id.source(db) |
67 | } | 70 | } |
68 | } | 71 | } |
69 | impl HasSource for EnumVariant { | 72 | impl HasSource for EnumVariant { |
70 | type Ast = ast::EnumVariant; | 73 | type Ast = ast::EnumVariant; |
71 | fn source(self, db: &impl DefDatabase) -> Source<ast::EnumVariant> { | 74 | fn source(self, db: &impl DefDatabase) -> InFile<ast::EnumVariant> { |
72 | self.parent.id.child_source(db).map(|map| map[self.id].clone()) | 75 | self.parent.id.child_source(db).map(|map| map[self.id].clone()) |
73 | } | 76 | } |
74 | } | 77 | } |
75 | impl HasSource for Function { | 78 | impl HasSource for Function { |
76 | type Ast = ast::FnDef; | 79 | type Ast = ast::FnDef; |
77 | fn source(self, db: &impl DefDatabase) -> Source<ast::FnDef> { | 80 | fn source(self, db: &impl DefDatabase) -> InFile<ast::FnDef> { |
78 | self.id.lookup(db).source(db) | 81 | self.id.lookup(db).source(db) |
79 | } | 82 | } |
80 | } | 83 | } |
81 | impl HasSource for Const { | 84 | impl HasSource for Const { |
82 | type Ast = ast::ConstDef; | 85 | type Ast = ast::ConstDef; |
83 | fn source(self, db: &impl DefDatabase) -> Source<ast::ConstDef> { | 86 | fn source(self, db: &impl DefDatabase) -> InFile<ast::ConstDef> { |
84 | self.id.lookup(db).source(db) | 87 | self.id.lookup(db).source(db) |
85 | } | 88 | } |
86 | } | 89 | } |
87 | impl HasSource for Static { | 90 | impl HasSource for Static { |
88 | type Ast = ast::StaticDef; | 91 | type Ast = ast::StaticDef; |
89 | fn source(self, db: &impl DefDatabase) -> Source<ast::StaticDef> { | 92 | fn source(self, db: &impl DefDatabase) -> InFile<ast::StaticDef> { |
90 | self.id.lookup(db).source(db) | 93 | self.id.lookup(db).source(db) |
91 | } | 94 | } |
92 | } | 95 | } |
93 | impl HasSource for Trait { | 96 | impl HasSource for Trait { |
94 | type Ast = ast::TraitDef; | 97 | type Ast = ast::TraitDef; |
95 | fn source(self, db: &impl DefDatabase) -> Source<ast::TraitDef> { | 98 | fn source(self, db: &impl DefDatabase) -> InFile<ast::TraitDef> { |
96 | self.id.source(db) | 99 | self.id.source(db) |
97 | } | 100 | } |
98 | } | 101 | } |
99 | impl HasSource for TypeAlias { | 102 | impl HasSource for TypeAlias { |
100 | type Ast = ast::TypeAliasDef; | 103 | type Ast = ast::TypeAliasDef; |
101 | fn source(self, db: &impl DefDatabase) -> Source<ast::TypeAliasDef> { | 104 | fn source(self, db: &impl DefDatabase) -> InFile<ast::TypeAliasDef> { |
102 | self.id.lookup(db).source(db) | 105 | self.id.lookup(db).source(db) |
103 | } | 106 | } |
104 | } | 107 | } |
105 | impl HasSource for MacroDef { | 108 | impl HasSource for MacroDef { |
106 | type Ast = ast::MacroCall; | 109 | type Ast = ast::MacroCall; |
107 | fn source(self, db: &impl DefDatabase) -> Source<ast::MacroCall> { | 110 | fn source(self, db: &impl DefDatabase) -> InFile<ast::MacroCall> { |
108 | Source { file_id: self.id.ast_id.file_id(), value: self.id.ast_id.to_node(db) } | 111 | InFile { file_id: self.id.ast_id.file_id, value: self.id.ast_id.to_node(db) } |
109 | } | 112 | } |
110 | } | 113 | } |
111 | impl HasSource for ImplBlock { | 114 | impl HasSource for ImplBlock { |
112 | type Ast = ast::ImplBlock; | 115 | type Ast = ast::ImplBlock; |
113 | fn source(self, db: &impl DefDatabase) -> Source<ast::ImplBlock> { | 116 | fn source(self, db: &impl DefDatabase) -> InFile<ast::ImplBlock> { |
114 | self.id.source(db) | 117 | self.id.source(db) |
115 | } | 118 | } |
116 | } | 119 | } |
@@ -118,7 +121,7 @@ impl HasSource for Import { | |||
118 | type Ast = Either<ast::UseTree, ast::ExternCrateItem>; | 121 | type Ast = Either<ast::UseTree, ast::ExternCrateItem>; |
119 | 122 | ||
120 | /// Returns the syntax of the last path segment corresponding to this import | 123 | /// Returns the syntax of the last path segment corresponding to this import |
121 | fn source(self, db: &impl DefDatabase) -> Source<Self::Ast> { | 124 | fn source(self, db: &impl DefDatabase) -> InFile<Self::Ast> { |
122 | let src = self.parent.definition_source(db); | 125 | let src = self.parent.definition_source(db); |
123 | let (_, source_map) = db.raw_items_with_source_map(src.file_id); | 126 | let (_, source_map) = db.raw_items_with_source_map(src.file_id); |
124 | let root = db.parse_or_expand(src.file_id).unwrap(); | 127 | let root = db.parse_or_expand(src.file_id).unwrap(); |
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 9f7c22b21..82bf641dc 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs | |||
@@ -10,46 +10,46 @@ use ra_syntax::{ | |||
10 | use crate::{ | 10 | use crate::{ |
11 | db::{AstDatabase, DefDatabase, HirDatabase}, | 11 | db::{AstDatabase, DefDatabase, HirDatabase}, |
12 | AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock, | 12 | AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock, |
13 | Local, MacroDef, Module, ModuleDef, ModuleSource, Source, Static, Struct, StructField, Trait, | 13 | InFile, Local, MacroDef, Module, ModuleDef, ModuleSource, Static, Struct, StructField, Trait, |
14 | TypeAlias, Union, VariantDef, | 14 | TypeAlias, Union, VariantDef, |
15 | }; | 15 | }; |
16 | 16 | ||
17 | pub trait FromSource: Sized { | 17 | pub trait FromSource: Sized { |
18 | type Ast; | 18 | type Ast; |
19 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self>; | 19 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self>; |
20 | } | 20 | } |
21 | 21 | ||
22 | impl FromSource for Struct { | 22 | impl FromSource for Struct { |
23 | type Ast = ast::StructDef; | 23 | type Ast = ast::StructDef; |
24 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> { | 24 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { |
25 | let id = from_source(db, src)?; | 25 | let id = from_source(db, src)?; |
26 | Some(Struct { id }) | 26 | Some(Struct { id }) |
27 | } | 27 | } |
28 | } | 28 | } |
29 | impl FromSource for Union { | 29 | impl FromSource for Union { |
30 | type Ast = ast::UnionDef; | 30 | type Ast = ast::UnionDef; |
31 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> { | 31 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { |
32 | let id = from_source(db, src)?; | 32 | let id = from_source(db, src)?; |
33 | Some(Union { id }) | 33 | Some(Union { id }) |
34 | } | 34 | } |
35 | } | 35 | } |
36 | impl FromSource for Enum { | 36 | impl FromSource for Enum { |
37 | type Ast = ast::EnumDef; | 37 | type Ast = ast::EnumDef; |
38 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> { | 38 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { |
39 | let id = from_source(db, src)?; | 39 | let id = from_source(db, src)?; |
40 | Some(Enum { id }) | 40 | Some(Enum { id }) |
41 | } | 41 | } |
42 | } | 42 | } |
43 | impl FromSource for Trait { | 43 | impl FromSource for Trait { |
44 | type Ast = ast::TraitDef; | 44 | type Ast = ast::TraitDef; |
45 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> { | 45 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { |
46 | let id = from_source(db, src)?; | 46 | let id = from_source(db, src)?; |
47 | Some(Trait { id }) | 47 | Some(Trait { id }) |
48 | } | 48 | } |
49 | } | 49 | } |
50 | impl FromSource for Function { | 50 | impl FromSource for Function { |
51 | type Ast = ast::FnDef; | 51 | type Ast = ast::FnDef; |
52 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> { | 52 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { |
53 | let items = match Container::find(db, src.as_ref().map(|it| it.syntax()))? { | 53 | let items = match Container::find(db, src.as_ref().map(|it| it.syntax()))? { |
54 | Container::Trait(it) => it.items(db), | 54 | Container::Trait(it) => it.items(db), |
55 | Container::ImplBlock(it) => it.items(db), | 55 | Container::ImplBlock(it) => it.items(db), |
@@ -76,7 +76,7 @@ impl FromSource for Function { | |||
76 | 76 | ||
77 | impl FromSource for Const { | 77 | impl FromSource for Const { |
78 | type Ast = ast::ConstDef; | 78 | type Ast = ast::ConstDef; |
79 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> { | 79 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { |
80 | let items = match Container::find(db, src.as_ref().map(|it| it.syntax()))? { | 80 | let items = match Container::find(db, src.as_ref().map(|it| it.syntax()))? { |
81 | Container::Trait(it) => it.items(db), | 81 | Container::Trait(it) => it.items(db), |
82 | Container::ImplBlock(it) => it.items(db), | 82 | Container::ImplBlock(it) => it.items(db), |
@@ -102,7 +102,7 @@ impl FromSource for Const { | |||
102 | } | 102 | } |
103 | impl FromSource for Static { | 103 | impl FromSource for Static { |
104 | type Ast = ast::StaticDef; | 104 | type Ast = ast::StaticDef; |
105 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> { | 105 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { |
106 | let module = match Container::find(db, src.as_ref().map(|it| it.syntax()))? { | 106 | let module = match Container::find(db, src.as_ref().map(|it| it.syntax()))? { |
107 | Container::Module(it) => it, | 107 | Container::Module(it) => it, |
108 | Container::Trait(_) | Container::ImplBlock(_) => return None, | 108 | Container::Trait(_) | Container::ImplBlock(_) => return None, |
@@ -120,7 +120,7 @@ impl FromSource for Static { | |||
120 | 120 | ||
121 | impl FromSource for TypeAlias { | 121 | impl FromSource for TypeAlias { |
122 | type Ast = ast::TypeAliasDef; | 122 | type Ast = ast::TypeAliasDef; |
123 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> { | 123 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { |
124 | let items = match Container::find(db, src.as_ref().map(|it| it.syntax()))? { | 124 | let items = match Container::find(db, src.as_ref().map(|it| it.syntax()))? { |
125 | Container::Trait(it) => it.items(db), | 125 | Container::Trait(it) => it.items(db), |
126 | Container::ImplBlock(it) => it.items(db), | 126 | Container::ImplBlock(it) => it.items(db), |
@@ -147,11 +147,11 @@ impl FromSource for TypeAlias { | |||
147 | 147 | ||
148 | impl FromSource for MacroDef { | 148 | impl FromSource for MacroDef { |
149 | type Ast = ast::MacroCall; | 149 | type Ast = ast::MacroCall; |
150 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> { | 150 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { |
151 | let kind = MacroDefKind::Declarative; | 151 | let kind = MacroDefKind::Declarative; |
152 | 152 | ||
153 | let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax())); | 153 | let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax())); |
154 | let module = Module::from_definition(db, Source::new(src.file_id, module_src))?; | 154 | let module = Module::from_definition(db, InFile::new(src.file_id, module_src))?; |
155 | let krate = module.krate().crate_id(); | 155 | let krate = module.krate().crate_id(); |
156 | 156 | ||
157 | let ast_id = AstId::new(src.file_id, db.ast_id_map(src.file_id).ast_id(&src.value)); | 157 | let ast_id = AstId::new(src.file_id, db.ast_id_map(src.file_id).ast_id(&src.value)); |
@@ -163,7 +163,7 @@ impl FromSource for MacroDef { | |||
163 | 163 | ||
164 | impl FromSource for ImplBlock { | 164 | impl FromSource for ImplBlock { |
165 | type Ast = ast::ImplBlock; | 165 | type Ast = ast::ImplBlock; |
166 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> { | 166 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { |
167 | let id = from_source(db, src)?; | 167 | let id = from_source(db, src)?; |
168 | Some(ImplBlock { id }) | 168 | Some(ImplBlock { id }) |
169 | } | 169 | } |
@@ -171,9 +171,9 @@ impl FromSource for ImplBlock { | |||
171 | 171 | ||
172 | impl FromSource for EnumVariant { | 172 | impl FromSource for EnumVariant { |
173 | type Ast = ast::EnumVariant; | 173 | type Ast = ast::EnumVariant; |
174 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> { | 174 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { |
175 | let parent_enum = src.value.parent_enum(); | 175 | let parent_enum = src.value.parent_enum(); |
176 | let src_enum = Source { file_id: src.file_id, value: parent_enum }; | 176 | let src_enum = InFile { file_id: src.file_id, value: parent_enum }; |
177 | let variants = Enum::from_source(db, src_enum)?.variants(db); | 177 | let variants = Enum::from_source(db, src_enum)?.variants(db); |
178 | variants.into_iter().find(|v| same_source(&v.source(db), &src)) | 178 | variants.into_iter().find(|v| same_source(&v.source(db), &src)) |
179 | } | 179 | } |
@@ -181,17 +181,17 @@ impl FromSource for EnumVariant { | |||
181 | 181 | ||
182 | impl FromSource for StructField { | 182 | impl FromSource for StructField { |
183 | type Ast = FieldSource; | 183 | type Ast = FieldSource; |
184 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> { | 184 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { |
185 | let variant_def: VariantDef = match src.value { | 185 | let variant_def: VariantDef = match src.value { |
186 | FieldSource::Named(ref field) => { | 186 | FieldSource::Named(ref field) => { |
187 | let value = field.syntax().ancestors().find_map(ast::StructDef::cast)?; | 187 | let value = field.syntax().ancestors().find_map(ast::StructDef::cast)?; |
188 | let src = Source { file_id: src.file_id, value }; | 188 | let src = InFile { file_id: src.file_id, value }; |
189 | let def = Struct::from_source(db, src)?; | 189 | let def = Struct::from_source(db, src)?; |
190 | VariantDef::from(def) | 190 | VariantDef::from(def) |
191 | } | 191 | } |
192 | FieldSource::Pos(ref field) => { | 192 | FieldSource::Pos(ref field) => { |
193 | let value = field.syntax().ancestors().find_map(ast::EnumVariant::cast)?; | 193 | let value = field.syntax().ancestors().find_map(ast::EnumVariant::cast)?; |
194 | let src = Source { file_id: src.file_id, value }; | 194 | let src = InFile { file_id: src.file_id, value }; |
195 | let def = EnumVariant::from_source(db, src)?; | 195 | let def = EnumVariant::from_source(db, src)?; |
196 | VariantDef::from(def) | 196 | VariantDef::from(def) |
197 | } | 197 | } |
@@ -206,14 +206,14 @@ impl FromSource for StructField { | |||
206 | } | 206 | } |
207 | 207 | ||
208 | impl Local { | 208 | impl Local { |
209 | pub fn from_source(db: &impl HirDatabase, src: Source<ast::BindPat>) -> Option<Self> { | 209 | pub fn from_source(db: &impl HirDatabase, src: InFile<ast::BindPat>) -> Option<Self> { |
210 | let file_id = src.file_id; | 210 | let file_id = src.file_id; |
211 | let parent: DefWithBody = src.value.syntax().ancestors().find_map(|it| { | 211 | let parent: DefWithBody = src.value.syntax().ancestors().find_map(|it| { |
212 | let res = match_ast! { | 212 | let res = match_ast! { |
213 | match it { | 213 | match it { |
214 | ast::ConstDef(value) => { Const::from_source(db, Source { value, file_id})?.into() }, | 214 | ast::ConstDef(value) => { Const::from_source(db, InFile { value, file_id})?.into() }, |
215 | ast::StaticDef(value) => { Static::from_source(db, Source { value, file_id})?.into() }, | 215 | ast::StaticDef(value) => { Static::from_source(db, InFile { value, file_id})?.into() }, |
216 | ast::FnDef(value) => { Function::from_source(db, Source { value, file_id})?.into() }, | 216 | ast::FnDef(value) => { Function::from_source(db, InFile { value, file_id})?.into() }, |
217 | _ => return None, | 217 | _ => return None, |
218 | } | 218 | } |
219 | }; | 219 | }; |
@@ -227,16 +227,16 @@ impl Local { | |||
227 | } | 227 | } |
228 | 228 | ||
229 | impl Module { | 229 | impl Module { |
230 | pub fn from_declaration(db: &impl DefDatabase, src: Source<ast::Module>) -> Option<Self> { | 230 | pub fn from_declaration(db: &impl DefDatabase, src: InFile<ast::Module>) -> Option<Self> { |
231 | let parent_declaration = src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast); | 231 | let parent_declaration = src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast); |
232 | 232 | ||
233 | let parent_module = match parent_declaration { | 233 | let parent_module = match parent_declaration { |
234 | Some(parent_declaration) => { | 234 | Some(parent_declaration) => { |
235 | let src_parent = Source { file_id: src.file_id, value: parent_declaration }; | 235 | let src_parent = InFile { file_id: src.file_id, value: parent_declaration }; |
236 | Module::from_declaration(db, src_parent) | 236 | Module::from_declaration(db, src_parent) |
237 | } | 237 | } |
238 | _ => { | 238 | _ => { |
239 | let src_parent = Source { | 239 | let src_parent = InFile { |
240 | file_id: src.file_id, | 240 | file_id: src.file_id, |
241 | value: ModuleSource::new(db, Some(src.file_id.original_file(db)), None), | 241 | value: ModuleSource::new(db, Some(src.file_id.original_file(db)), None), |
242 | }; | 242 | }; |
@@ -248,13 +248,13 @@ impl Module { | |||
248 | parent_module.child(db, &child_name.as_name()) | 248 | parent_module.child(db, &child_name.as_name()) |
249 | } | 249 | } |
250 | 250 | ||
251 | pub fn from_definition(db: &impl DefDatabase, src: Source<ModuleSource>) -> Option<Self> { | 251 | pub fn from_definition(db: &impl DefDatabase, src: InFile<ModuleSource>) -> Option<Self> { |
252 | match src.value { | 252 | match src.value { |
253 | ModuleSource::Module(ref module) => { | 253 | ModuleSource::Module(ref module) => { |
254 | assert!(!module.has_semi()); | 254 | assert!(!module.has_semi()); |
255 | return Module::from_declaration( | 255 | return Module::from_declaration( |
256 | db, | 256 | db, |
257 | Source { file_id: src.file_id, value: module.clone() }, | 257 | InFile { file_id: src.file_id, value: module.clone() }, |
258 | ); | 258 | ); |
259 | } | 259 | } |
260 | ModuleSource::SourceFile(_) => (), | 260 | ModuleSource::SourceFile(_) => (), |
@@ -271,13 +271,13 @@ impl Module { | |||
271 | } | 271 | } |
272 | } | 272 | } |
273 | 273 | ||
274 | fn from_source<N, DEF>(db: &(impl DefDatabase + AstDatabase), src: Source<N>) -> Option<DEF> | 274 | fn from_source<N, DEF>(db: &(impl DefDatabase + AstDatabase), src: InFile<N>) -> Option<DEF> |
275 | where | 275 | where |
276 | N: AstNode, | 276 | N: AstNode, |
277 | DEF: AstItemDef<N>, | 277 | DEF: AstItemDef<N>, |
278 | { | 278 | { |
279 | let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax())); | 279 | let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax())); |
280 | let module = Module::from_definition(db, Source::new(src.file_id, module_src))?; | 280 | let module = Module::from_definition(db, InFile::new(src.file_id, module_src))?; |
281 | let ctx = LocationCtx::new(db, module.id, src.file_id); | 281 | let ctx = LocationCtx::new(db, module.id, src.file_id); |
282 | let items = db.ast_id_map(src.file_id); | 282 | let items = db.ast_id_map(src.file_id); |
283 | let item_id = items.ast_id(&src.value); | 283 | let item_id = items.ast_id(&src.value); |
@@ -291,7 +291,7 @@ enum Container { | |||
291 | } | 291 | } |
292 | 292 | ||
293 | impl Container { | 293 | impl Container { |
294 | fn find(db: &impl DefDatabase, src: Source<&SyntaxNode>) -> Option<Container> { | 294 | fn find(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> Option<Container> { |
295 | // FIXME: this doesn't try to handle nested declarations | 295 | // FIXME: this doesn't try to handle nested declarations |
296 | for container in src.value.ancestors() { | 296 | for container in src.value.ancestors() { |
297 | let res = match_ast! { | 297 | let res = match_ast! { |
@@ -322,6 +322,6 @@ impl Container { | |||
322 | /// In general, we do not guarantee that we have exactly one instance of a | 322 | /// In general, we do not guarantee that we have exactly one instance of a |
323 | /// syntax tree for each file. We probably should add such guarantee, but, for | 323 | /// syntax tree for each file. We probably should add such guarantee, but, for |
324 | /// the time being, we will use identity-less AstPtr comparison. | 324 | /// the time being, we will use identity-less AstPtr comparison. |
325 | fn same_source<N: AstNode>(s1: &Source<N>, s2: &Source<N>) -> bool { | 325 | fn same_source<N: AstNode>(s1: &InFile<N>, s2: &InFile<N>) -> bool { |
326 | s1.as_ref().map(AstPtr::new) == s2.as_ref().map(AstPtr::new) | 326 | s1.as_ref().map(AstPtr::new) == s2.as_ref().map(AstPtr::new) |
327 | } | 327 | } |
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 3c12c61f0..88d2f6e02 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -63,5 +63,5 @@ pub use hir_def::{ | |||
63 | type_ref::Mutability, | 63 | type_ref::Mutability, |
64 | }; | 64 | }; |
65 | pub use hir_expand::{ | 65 | pub use hir_expand::{ |
66 | either::Either, name::Name, HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, Source, | 66 | either::Either, name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, |
67 | }; | 67 | }; |
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 76c493f1a..0df7a7cb4 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -18,7 +18,7 @@ use hir_def::{ | |||
18 | AssocItemId, DefWithBodyId, | 18 | AssocItemId, DefWithBodyId, |
19 | }; | 19 | }; |
20 | use hir_expand::{ | 20 | use hir_expand::{ |
21 | hygiene::Hygiene, name::AsName, AstId, HirFileId, MacroCallId, MacroFileKind, Source, | 21 | hygiene::Hygiene, name::AsName, AstId, HirFileId, InFile, MacroCallId, MacroFileKind, |
22 | }; | 22 | }; |
23 | use ra_syntax::{ | 23 | use ra_syntax::{ |
24 | ast::{self, AstNode}, | 24 | ast::{self, AstNode}, |
@@ -37,7 +37,7 @@ use crate::{ | |||
37 | GenericParam, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias, | 37 | GenericParam, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias, |
38 | }; | 38 | }; |
39 | 39 | ||
40 | fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option<Resolver> { | 40 | fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -> Option<Resolver> { |
41 | match_ast! { | 41 | match_ast! { |
42 | match (node.value) { | 42 | match (node.value) { |
43 | ast::Module(it) => { | 43 | ast::Module(it) => { |
@@ -71,7 +71,7 @@ fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) - | |||
71 | 71 | ||
72 | fn def_with_body_from_child_node( | 72 | fn def_with_body_from_child_node( |
73 | db: &impl HirDatabase, | 73 | db: &impl HirDatabase, |
74 | child: Source<&SyntaxNode>, | 74 | child: InFile<&SyntaxNode>, |
75 | ) -> Option<DefWithBody> { | 75 | ) -> Option<DefWithBody> { |
76 | child.value.ancestors().find_map(|node| { | 76 | child.value.ancestors().find_map(|node| { |
77 | match_ast! { | 77 | match_ast! { |
@@ -141,8 +141,8 @@ impl Expansion { | |||
141 | pub fn map_token_down( | 141 | pub fn map_token_down( |
142 | &self, | 142 | &self, |
143 | db: &impl HirDatabase, | 143 | db: &impl HirDatabase, |
144 | token: Source<&SyntaxToken>, | 144 | token: InFile<&SyntaxToken>, |
145 | ) -> Option<Source<SyntaxToken>> { | 145 | ) -> Option<InFile<SyntaxToken>> { |
146 | let exp_info = self.file_id().expansion_info(db)?; | 146 | let exp_info = self.file_id().expansion_info(db)?; |
147 | exp_info.map_token_down(token) | 147 | exp_info.map_token_down(token) |
148 | } | 148 | } |
@@ -155,7 +155,7 @@ impl Expansion { | |||
155 | impl SourceAnalyzer { | 155 | impl SourceAnalyzer { |
156 | pub fn new( | 156 | pub fn new( |
157 | db: &impl HirDatabase, | 157 | db: &impl HirDatabase, |
158 | node: Source<&SyntaxNode>, | 158 | node: InFile<&SyntaxNode>, |
159 | offset: Option<TextUnit>, | 159 | offset: Option<TextUnit>, |
160 | ) -> SourceAnalyzer { | 160 | ) -> SourceAnalyzer { |
161 | let def_with_body = def_with_body_from_child_node(db, node); | 161 | let def_with_body = def_with_body_from_child_node(db, node); |
@@ -192,12 +192,12 @@ impl SourceAnalyzer { | |||
192 | } | 192 | } |
193 | 193 | ||
194 | fn expr_id(&self, expr: &ast::Expr) -> Option<ExprId> { | 194 | fn expr_id(&self, expr: &ast::Expr) -> Option<ExprId> { |
195 | let src = Source { file_id: self.file_id, value: expr }; | 195 | let src = InFile { file_id: self.file_id, value: expr }; |
196 | self.body_source_map.as_ref()?.node_expr(src) | 196 | self.body_source_map.as_ref()?.node_expr(src) |
197 | } | 197 | } |
198 | 198 | ||
199 | fn pat_id(&self, pat: &ast::Pat) -> Option<PatId> { | 199 | fn pat_id(&self, pat: &ast::Pat) -> Option<PatId> { |
200 | let src = Source { file_id: self.file_id, value: pat }; | 200 | let src = InFile { file_id: self.file_id, value: pat }; |
201 | self.body_source_map.as_ref()?.node_pat(src) | 201 | self.body_source_map.as_ref()?.node_pat(src) |
202 | } | 202 | } |
203 | 203 | ||
@@ -243,7 +243,7 @@ impl SourceAnalyzer { | |||
243 | pub fn resolve_macro_call( | 243 | pub fn resolve_macro_call( |
244 | &self, | 244 | &self, |
245 | db: &impl HirDatabase, | 245 | db: &impl HirDatabase, |
246 | macro_call: Source<&ast::MacroCall>, | 246 | macro_call: InFile<&ast::MacroCall>, |
247 | ) -> Option<MacroDef> { | 247 | ) -> Option<MacroDef> { |
248 | let hygiene = Hygiene::new(db, macro_call.file_id); | 248 | let hygiene = Hygiene::new(db, macro_call.file_id); |
249 | let path = macro_call.value.path().and_then(|ast| Path::from_src(ast, &hygiene))?; | 249 | let path = macro_call.value.path().and_then(|ast| Path::from_src(ast, &hygiene))?; |
@@ -286,7 +286,7 @@ impl SourceAnalyzer { | |||
286 | 286 | ||
287 | let items = self | 287 | let items = self |
288 | .resolver | 288 | .resolver |
289 | .resolve_module_path(db, &path) | 289 | .resolve_module_path_in_items(db, &path) |
290 | .take_types() | 290 | .take_types() |
291 | .map(|it| PathResolution::Def(it.into())); | 291 | .map(|it| PathResolution::Def(it.into())); |
292 | types.or(values).or(items).or_else(|| { | 292 | types.or(values).or(items).or_else(|| { |
@@ -318,7 +318,7 @@ impl SourceAnalyzer { | |||
318 | let name = name_ref.as_name(); | 318 | let name = name_ref.as_name(); |
319 | let source_map = self.body_source_map.as_ref()?; | 319 | let source_map = self.body_source_map.as_ref()?; |
320 | let scopes = self.scopes.as_ref()?; | 320 | let scopes = self.scopes.as_ref()?; |
321 | let scope = scope_for(scopes, source_map, Source::new(self.file_id, name_ref.syntax()))?; | 321 | let scope = scope_for(scopes, source_map, InFile::new(self.file_id, name_ref.syntax()))?; |
322 | let entry = scopes.resolve_name_in_scope(scope, &name)?; | 322 | let entry = scopes.resolve_name_in_scope(scope, &name)?; |
323 | Some(ScopeEntryWithSyntax { | 323 | Some(ScopeEntryWithSyntax { |
324 | name: entry.name().clone(), | 324 | name: entry.name().clone(), |
@@ -446,7 +446,7 @@ impl SourceAnalyzer { | |||
446 | pub fn expand( | 446 | pub fn expand( |
447 | &self, | 447 | &self, |
448 | db: &impl HirDatabase, | 448 | db: &impl HirDatabase, |
449 | macro_call: Source<&ast::MacroCall>, | 449 | macro_call: InFile<&ast::MacroCall>, |
450 | ) -> Option<Expansion> { | 450 | ) -> Option<Expansion> { |
451 | let def = self.resolve_macro_call(db, macro_call)?.id; | 451 | let def = self.resolve_macro_call(db, macro_call)?.id; |
452 | let ast_id = AstId::new( | 452 | let ast_id = AstId::new( |
@@ -463,19 +463,19 @@ impl SourceAnalyzer { | |||
463 | fn scope_for( | 463 | fn scope_for( |
464 | scopes: &ExprScopes, | 464 | scopes: &ExprScopes, |
465 | source_map: &BodySourceMap, | 465 | source_map: &BodySourceMap, |
466 | node: Source<&SyntaxNode>, | 466 | node: InFile<&SyntaxNode>, |
467 | ) -> Option<ScopeId> { | 467 | ) -> Option<ScopeId> { |
468 | node.value | 468 | node.value |
469 | .ancestors() | 469 | .ancestors() |
470 | .filter_map(ast::Expr::cast) | 470 | .filter_map(ast::Expr::cast) |
471 | .filter_map(|it| source_map.node_expr(Source::new(node.file_id, &it))) | 471 | .filter_map(|it| source_map.node_expr(InFile::new(node.file_id, &it))) |
472 | .find_map(|it| scopes.scope_for(it)) | 472 | .find_map(|it| scopes.scope_for(it)) |
473 | } | 473 | } |
474 | 474 | ||
475 | fn scope_for_offset( | 475 | fn scope_for_offset( |
476 | scopes: &ExprScopes, | 476 | scopes: &ExprScopes, |
477 | source_map: &BodySourceMap, | 477 | source_map: &BodySourceMap, |
478 | offset: Source<TextUnit>, | 478 | offset: InFile<TextUnit>, |
479 | ) -> Option<ScopeId> { | 479 | ) -> Option<ScopeId> { |
480 | scopes | 480 | scopes |
481 | .scope_by_expr() | 481 | .scope_by_expr() |