From d8caf56dfc9eb3cdddff05b58b954a78cf1b9476 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 22 Nov 2019 21:52:06 +0300 Subject: Uniformalize naming --- crates/ra_hir/src/code_model/src.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir/src/code_model/src.rs') diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index 4aa427de4..fc466c1f0 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -63,7 +63,7 @@ impl HasSource for StructField { let field_sources = match struct_kind { ast::StructKind::Tuple(fl) => fl.fields().map(|it| FieldSource::Pos(it)).collect(), - ast::StructKind::Named(fl) => fl.fields().map(|it| FieldSource::Named(it)).collect(), + ast::StructKind::Record(fl) => fl.fields().map(|it| FieldSource::Named(it)).collect(), ast::StructKind::Unit => Vec::new(), }; let value = field_sources -- cgit v1.2.3 From 0f415dd4b30289117fe76d071293e9bdd3556336 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 22 Nov 2019 21:43:36 +0300 Subject: More principled sources for enums and fields --- crates/ra_hir/src/code_model/src.rs | 50 +++++++------------------------------ 1 file changed, 9 insertions(+), 41 deletions(-) (limited to 'crates/ra_hir/src/code_model/src.rs') diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index fc466c1f0..a45c062bf 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -1,13 +1,13 @@ //! FIXME: write short doc here -use hir_def::{HasSource as _, Lookup}; +use hir_def::{HasChildSource, HasSource as _, Lookup, VariantId}; use ra_syntax::ast::{self, AstNode}; use crate::{ db::{AstDatabase, DefDatabase, HirDatabase}, ids::AstItemDef, Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef, Module, - ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, VariantDef, + ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, }; pub use hir_expand::Source; @@ -46,33 +46,12 @@ impl Module { impl HasSource for StructField { type Ast = FieldSource; fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { - let var_data = self.parent.variant_data(db); - let fields = var_data.fields().unwrap(); - let ss; - let es; - let (file_id, struct_kind) = match self.parent { - VariantDef::Struct(s) => { - ss = s.source(db); - (ss.file_id, ss.value.kind()) - } - VariantDef::EnumVariant(e) => { - es = e.source(db); - (es.file_id, es.value.kind()) - } - }; - - let field_sources = match struct_kind { - ast::StructKind::Tuple(fl) => fl.fields().map(|it| FieldSource::Pos(it)).collect(), - ast::StructKind::Record(fl) => fl.fields().map(|it| FieldSource::Named(it)).collect(), - ast::StructKind::Unit => Vec::new(), - }; - let value = field_sources - .into_iter() - .zip(fields.iter()) - .find(|(_syntax, (id, _))| *id == self.id) - .unwrap() - .0; - Source { file_id, value } + let var = VariantId::from(self.parent); + let src = var.child_source(db); + src.map(|it| match it[self.id].clone() { + Either::A(it) => FieldSource::Pos(it), + Either::B(it) => FieldSource::Named(it), + }) } } impl HasSource for Struct { @@ -96,18 +75,7 @@ impl HasSource for Enum { impl HasSource for EnumVariant { type Ast = ast::EnumVariant; fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { - let enum_data = db.enum_data(self.parent.id); - let src = self.parent.id.source(db); - let value = src - .value - .variant_list() - .into_iter() - .flat_map(|it| it.variants()) - .zip(enum_data.variants.iter()) - .find(|(_syntax, (id, _))| *id == self.id) - .unwrap() - .0; - Source { file_id: src.file_id, value } + self.parent.id.child_source(db).map(|map| map[self.id].clone()) } } impl HasSource for Function { -- cgit v1.2.3 From 552ba868afc8f72202ac834d07bbeb330aca007d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 11:14:10 +0300 Subject: Move attrs query to hir_def --- crates/ra_hir/src/code_model/src.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'crates/ra_hir/src/code_model/src.rs') diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index a45c062bf..09bacf579 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -1,13 +1,14 @@ //! FIXME: write short doc here use hir_def::{HasChildSource, HasSource as _, Lookup, VariantId}; +use hir_expand::either::Either; use ra_syntax::ast::{self, AstNode}; use crate::{ db::{AstDatabase, DefDatabase, HirDatabase}, ids::AstItemDef, - Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef, Module, - ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, + Const, Enum, EnumVariant, FieldSource, Function, HasBody, MacroDef, Module, ModuleSource, + Static, Struct, StructField, Trait, TypeAlias, Union, }; pub use hir_expand::Source; @@ -23,11 +24,11 @@ impl Module { /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. pub fn definition_source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { let def_map = db.crate_def_map(self.id.krate); - let decl_id = def_map[self.id.module_id].declaration; - let file_id = def_map[self.id.module_id].definition; - let value = ModuleSource::new(db, file_id, decl_id); - let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id()); - Source { file_id, value } + let src = def_map[self.id.module_id].definition_source(db); + src.map(|it| match it { + Either::A(it) => ModuleSource::SourceFile(it), + Either::B(it) => ModuleSource::Module(it), + }) } /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. @@ -37,9 +38,7 @@ impl Module { db: &(impl DefDatabase + AstDatabase), ) -> Option> { let def_map = db.crate_def_map(self.id.krate); - let decl = def_map[self.id.module_id].declaration?; - let value = decl.to_node(db); - Some(Source { file_id: decl.file_id(), value }) + def_map[self.id.module_id].declaration_source(db) } } -- cgit v1.2.3 From e5bcb69e4f88c7536b1039a881816cb8f1c1f867 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 16:39:53 +0300 Subject: Hide ImportId --- crates/ra_hir/src/code_model/src.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir/src/code_model/src.rs') diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index 09bacf579..ceb3daa27 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -8,7 +8,7 @@ use crate::{ db::{AstDatabase, DefDatabase, HirDatabase}, ids::AstItemDef, Const, Enum, EnumVariant, FieldSource, Function, HasBody, MacroDef, Module, ModuleSource, - Static, Struct, StructField, Trait, TypeAlias, Union, + Static, Struct, StructField, Trait, TypeAlias, Union, Import }; pub use hir_expand::Source; @@ -113,6 +113,16 @@ impl HasSource for MacroDef { Source { file_id: self.id.ast_id.file_id(), value: self.id.ast_id.to_node(db) } } } +impl HasSource for Import { + type Ast = Either; + + /// Returns the syntax of the last path segment corresponding to this import + fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { + let src = self.parent.definition_source(db); + let (_, source_map) = db.raw_items_with_source_map(src.file_id); + src.with_value(source_map.get(&src.value, self.id)) + } +} pub trait HasBodySource: HasBody + HasSource where -- cgit v1.2.3 From 338ff052c2fe658d6fcf88888918b2e372129e21 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 16:41:04 +0300 Subject: Simplify HasSource --- crates/ra_hir/src/code_model/src.rs | 39 +++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 21 deletions(-) (limited to 'crates/ra_hir/src/code_model/src.rs') diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index ceb3daa27..402f821bf 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -5,24 +5,24 @@ use hir_expand::either::Either; use ra_syntax::ast::{self, AstNode}; use crate::{ - db::{AstDatabase, DefDatabase, HirDatabase}, + db::{DefDatabase, HirDatabase}, ids::AstItemDef, - Const, Enum, EnumVariant, FieldSource, Function, HasBody, MacroDef, Module, ModuleSource, - Static, Struct, StructField, Trait, TypeAlias, Union, Import + Const, Enum, EnumVariant, FieldSource, Function, HasBody, Import, MacroDef, Module, + ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, }; pub use hir_expand::Source; pub trait HasSource { type Ast; - fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source; + fn source(self, db: &impl DefDatabase) -> Source; } /// NB: Module is !HasSource, because it has two source nodes at the same time: /// definition and declaration. impl Module { /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. - pub fn definition_source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { + pub fn definition_source(self, db: &impl DefDatabase) -> Source { let def_map = db.crate_def_map(self.id.krate); let src = def_map[self.id.module_id].definition_source(db); src.map(|it| match it { @@ -33,10 +33,7 @@ impl Module { /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. /// `None` for the crate root. - pub fn declaration_source( - self, - db: &(impl DefDatabase + AstDatabase), - ) -> Option> { + pub fn declaration_source(self, db: &impl DefDatabase) -> Option> { let def_map = db.crate_def_map(self.id.krate); def_map[self.id.module_id].declaration_source(db) } @@ -44,7 +41,7 @@ impl Module { impl HasSource for StructField { type Ast = FieldSource; - fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { + fn source(self, db: &impl DefDatabase) -> Source { let var = VariantId::from(self.parent); let src = var.child_source(db); src.map(|it| match it[self.id].clone() { @@ -55,61 +52,61 @@ impl HasSource for StructField { } impl HasSource for Struct { type Ast = ast::StructDef; - fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { + fn source(self, db: &impl DefDatabase) -> Source { self.id.0.source(db) } } impl HasSource for Union { type Ast = ast::StructDef; - fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { + fn source(self, db: &impl DefDatabase) -> Source { self.id.0.source(db) } } impl HasSource for Enum { type Ast = ast::EnumDef; - fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { + fn source(self, db: &impl DefDatabase) -> Source { self.id.source(db) } } impl HasSource for EnumVariant { type Ast = ast::EnumVariant; - fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { + fn source(self, db: &impl DefDatabase) -> Source { self.parent.id.child_source(db).map(|map| map[self.id].clone()) } } impl HasSource for Function { type Ast = ast::FnDef; - fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { + fn source(self, db: &impl DefDatabase) -> Source { self.id.lookup(db).source(db) } } impl HasSource for Const { type Ast = ast::ConstDef; - fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { + fn source(self, db: &impl DefDatabase) -> Source { self.id.lookup(db).source(db) } } impl HasSource for Static { type Ast = ast::StaticDef; - fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { + fn source(self, db: &impl DefDatabase) -> Source { self.id.source(db) } } impl HasSource for Trait { type Ast = ast::TraitDef; - fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { + fn source(self, db: &impl DefDatabase) -> Source { self.id.source(db) } } impl HasSource for TypeAlias { type Ast = ast::TypeAliasDef; - fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { + fn source(self, db: &impl DefDatabase) -> Source { self.id.lookup(db).source(db) } } impl HasSource for MacroDef { type Ast = ast::MacroCall; - fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { + fn source(self, db: &impl DefDatabase) -> Source { Source { file_id: self.id.ast_id.file_id(), value: self.id.ast_id.to_node(db) } } } @@ -117,7 +114,7 @@ impl HasSource for Import { type Ast = Either; /// Returns the syntax of the last path segment corresponding to this import - fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { + fn source(self, db: &impl DefDatabase) -> Source { let src = self.parent.definition_source(db); let (_, source_map) = db.raw_items_with_source_map(src.file_id); src.with_value(source_map.get(&src.value, self.id)) -- cgit v1.2.3 From ffc2325d194d2523456484a7dec1f175c729c1b5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 17:06:04 +0300 Subject: Move ModuleSource back to hir --- crates/ra_hir/src/code_model/src.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir/src/code_model/src.rs') diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index 402f821bf..b7bafe23d 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -117,7 +117,9 @@ impl HasSource for Import { fn source(self, db: &impl DefDatabase) -> Source { let src = self.parent.definition_source(db); let (_, source_map) = db.raw_items_with_source_map(src.file_id); - src.with_value(source_map.get(&src.value, self.id)) + let root = db.parse_or_expand(src.file_id).unwrap(); + let ptr = source_map.get(self.id); + src.with_value(ptr.map(|it| it.to_node(&root), |it| it.to_node(&root))) } } -- cgit v1.2.3 From f11237561c391eecba39c5ba57defa2dc7a27b21 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 14:02:08 +0300 Subject: Cleanup imports --- crates/ra_hir/src/code_model/src.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'crates/ra_hir/src/code_model/src.rs') diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index b7bafe23d..59cda2e89 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -1,12 +1,11 @@ //! FIXME: write short doc here -use hir_def::{HasChildSource, HasSource as _, Lookup, VariantId}; +use hir_def::{AstItemDef, HasChildSource, HasSource as _, Lookup, VariantId}; use hir_expand::either::Either; use ra_syntax::ast::{self, AstNode}; use crate::{ db::{DefDatabase, HirDatabase}, - ids::AstItemDef, Const, Enum, EnumVariant, FieldSource, Function, HasBody, Import, MacroDef, Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, }; -- cgit v1.2.3 From e0b06cb672b7aae770fea24e4a5efdbec8cbf5c6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 15:13:56 +0300 Subject: Switch to StaticLoc for statics --- crates/ra_hir/src/code_model/src.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir/src/code_model/src.rs') diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index 59cda2e89..b9d21bdd7 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -88,7 +88,7 @@ impl HasSource for Const { impl HasSource for Static { type Ast = ast::StaticDef; fn source(self, db: &impl DefDatabase) -> Source { - self.id.source(db) + self.id.lookup(db).source(db) } } impl HasSource for Trait { -- cgit v1.2.3 From e48430cbae32249e50dda1b39e0c98a31f118250 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 20:53:42 +0300 Subject: Simplify --- crates/ra_hir/src/code_model/src.rs | 29 ++--------------------------- 1 file changed, 2 insertions(+), 27 deletions(-) (limited to 'crates/ra_hir/src/code_model/src.rs') diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index b9d21bdd7..a0e605603 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -2,11 +2,10 @@ use hir_def::{AstItemDef, HasChildSource, HasSource as _, Lookup, VariantId}; use hir_expand::either::Either; -use ra_syntax::ast::{self, AstNode}; +use ra_syntax::ast; use crate::{ - db::{DefDatabase, HirDatabase}, - Const, Enum, EnumVariant, FieldSource, Function, HasBody, Import, MacroDef, Module, + db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, Import, MacroDef, Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, }; @@ -121,27 +120,3 @@ impl HasSource for Import { src.with_value(ptr.map(|it| it.to_node(&root), |it| it.to_node(&root))) } } - -pub trait HasBodySource: HasBody + HasSource -where - Self::Ast: AstNode, -{ - fn expr_source( - self, - db: &impl HirDatabase, - expr_id: crate::expr::ExprId, - ) -> Option>> { - let source_map = self.body_source_map(db); - let source_ptr = source_map.expr_syntax(expr_id)?; - let root = source_ptr.file_syntax(db); - let source = source_ptr.map(|ast| ast.map(|it| it.to_node(&root), |it| it.to_node(&root))); - Some(source) - } -} - -impl HasBodySource for T -where - T: HasBody + HasSource, - T::Ast: AstNode, -{ -} -- cgit v1.2.3 From 191b1d238fd1594ab74b1ab6a17dbe0430fc6b1a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 21:03:24 +0300 Subject: Remove impl_block module --- crates/ra_hir/src/code_model/src.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir/src/code_model/src.rs') diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index a0e605603..a4e317c20 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -5,8 +5,8 @@ use hir_expand::either::Either; use ra_syntax::ast; use crate::{ - db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, Import, MacroDef, Module, - ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, + db::DefDatabase, Const, Enum, EnumVariant, FieldSource, Function, ImplBlock, Import, MacroDef, + Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, }; pub use hir_expand::Source; @@ -108,6 +108,12 @@ impl HasSource for MacroDef { Source { file_id: self.id.ast_id.file_id(), value: self.id.ast_id.to_node(db) } } } +impl HasSource for ImplBlock { + type Ast = ast::ImplBlock; + fn source(self, db: &impl DefDatabase) -> Source { + self.id.source(db) + } +} impl HasSource for Import { type Ast = Either; -- cgit v1.2.3