aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/from_source.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/from_source.rs')
-rw-r--r--crates/ra_hir/src/from_source.rs62
1 files changed, 31 insertions, 31 deletions
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::{
10use crate::{ 10use 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
17pub trait FromSource: Sized { 17pub 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
22impl FromSource for Struct { 22impl 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}
29impl FromSource for Union { 29impl 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}
36impl FromSource for Enum { 36impl 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}
43impl FromSource for Trait { 43impl 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}
50impl FromSource for Function { 50impl 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
77impl FromSource for Const { 77impl 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}
103impl FromSource for Static { 103impl 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
121impl FromSource for TypeAlias { 121impl 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
148impl FromSource for MacroDef { 148impl 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
164impl FromSource for ImplBlock { 164impl 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
172impl FromSource for EnumVariant { 172impl 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
182impl FromSource for StructField { 182impl 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
208impl Local { 208impl 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
229impl Module { 229impl 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
274fn from_source<N, DEF>(db: &(impl DefDatabase + AstDatabase), src: Source<N>) -> Option<DEF> 274fn from_source<N, DEF>(db: &(impl DefDatabase + AstDatabase), src: InFile<N>) -> Option<DEF>
275where 275where
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
293impl Container { 293impl 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.
325fn same_source<N: AstNode>(s1: &Source<N>, s2: &Source<N>) -> bool { 325fn 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}