From ccd1b0800a5de5e046e6e9a4b6f49030c1ce3639 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 28 Nov 2019 12:50:26 +0300 Subject: Rename Source -> InFile --- crates/ra_hir/src/code_model.rs | 6 ++-- crates/ra_hir/src/code_model/src.rs | 36 ++++++++++----------- crates/ra_hir/src/from_source.rs | 62 ++++++++++++++++++------------------- crates/ra_hir/src/lib.rs | 2 +- crates/ra_hir/src/source_binder.rs | 28 ++++++++--------- 5 files changed, 67 insertions(+), 67 deletions(-) (limited to 'crates/ra_hir/src') 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::{ db::{DefDatabase, HirDatabase}, ty::display::HirFormatter, ty::{self, InEnvironment, InferenceResult, TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk}, - CallableDef, Either, HirDisplay, Name, Source, + CallableDef, Either, HirDisplay, InFile, Name, }; /// hir::Crate describes a single crate. It's the main interface with which @@ -118,7 +118,7 @@ impl ModuleSource { } } - pub fn from_child_node(db: &impl DefDatabase, child: Source<&SyntaxNode>) -> ModuleSource { + pub fn from_child_node(db: &impl DefDatabase, child: InFile<&SyntaxNode>) -> ModuleSource { if let Some(m) = child.value.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi()) { @@ -901,7 +901,7 @@ impl Local { Type { krate, ty: InEnvironment { value: ty, environment } } } - pub fn source(self, db: &impl HirDatabase) -> Source> { + pub fn source(self, db: &impl HirDatabase) -> InFile> { let (_body, source_map) = db.body_with_source_map(self.parent.into()); let src = source_map.pat_syntax(self.pat_id).unwrap(); // Hmm... 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..6fd4e7987 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -9,18 +9,18 @@ use crate::{ Module, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, }; -pub use hir_expand::Source; +pub use hir_expand::InFile; pub trait HasSource { type Ast; - fn source(self, db: &impl DefDatabase) -> Source; + fn source(self, db: &impl DefDatabase) -> InFile; } /// 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) -> Source { + pub fn definition_source(self, db: &impl DefDatabase) -> InFile { let def_map = db.crate_def_map(self.id.krate); let src = def_map[self.id.local_id].definition_source(db); src.map(|it| match it { @@ -31,7 +31,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) -> Option> { + pub fn declaration_source(self, db: &impl DefDatabase) -> Option> { let def_map = db.crate_def_map(self.id.krate); def_map[self.id.local_id].declaration_source(db) } @@ -39,7 +39,7 @@ impl Module { impl HasSource for StructField { type Ast = FieldSource; - fn source(self, db: &impl DefDatabase) -> Source { + fn source(self, db: &impl DefDatabase) -> InFile { let var = VariantId::from(self.parent); let src = var.child_source(db); src.map(|it| match it[self.id].clone() { @@ -50,67 +50,67 @@ impl HasSource for StructField { } impl HasSource for Struct { type Ast = ast::StructDef; - fn source(self, db: &impl DefDatabase) -> Source { + fn source(self, db: &impl DefDatabase) -> InFile { self.id.source(db) } } impl HasSource for Union { type Ast = ast::UnionDef; - fn source(self, db: &impl DefDatabase) -> Source { + fn source(self, db: &impl DefDatabase) -> InFile { self.id.source(db) } } impl HasSource for Enum { type Ast = ast::EnumDef; - fn source(self, db: &impl DefDatabase) -> Source { + fn source(self, db: &impl DefDatabase) -> InFile { self.id.source(db) } } impl HasSource for EnumVariant { type Ast = ast::EnumVariant; - fn source(self, db: &impl DefDatabase) -> Source { + fn source(self, db: &impl DefDatabase) -> InFile { 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) -> Source { + fn source(self, db: &impl DefDatabase) -> InFile { self.id.lookup(db).source(db) } } impl HasSource for Const { type Ast = ast::ConstDef; - fn source(self, db: &impl DefDatabase) -> Source { + fn source(self, db: &impl DefDatabase) -> InFile { self.id.lookup(db).source(db) } } impl HasSource for Static { type Ast = ast::StaticDef; - fn source(self, db: &impl DefDatabase) -> Source { + fn source(self, db: &impl DefDatabase) -> InFile { self.id.lookup(db).source(db) } } impl HasSource for Trait { type Ast = ast::TraitDef; - fn source(self, db: &impl DefDatabase) -> Source { + fn source(self, db: &impl DefDatabase) -> InFile { self.id.source(db) } } impl HasSource for TypeAlias { type Ast = ast::TypeAliasDef; - fn source(self, db: &impl DefDatabase) -> Source { + fn source(self, db: &impl DefDatabase) -> InFile { self.id.lookup(db).source(db) } } impl HasSource for MacroDef { type Ast = ast::MacroCall; - fn source(self, db: &impl DefDatabase) -> Source { - Source { file_id: self.id.ast_id.file_id(), value: self.id.ast_id.to_node(db) } + fn source(self, db: &impl DefDatabase) -> InFile { + InFile { 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 { + fn source(self, db: &impl DefDatabase) -> InFile { self.id.source(db) } } @@ -118,7 +118,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) -> Source { + fn source(self, db: &impl DefDatabase) -> InFile { let src = self.parent.definition_source(db); let (_, source_map) = db.raw_items_with_source_map(src.file_id); 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::{ use crate::{ db::{AstDatabase, DefDatabase, HirDatabase}, AssocItem, Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasSource, ImplBlock, - Local, MacroDef, Module, ModuleDef, ModuleSource, Source, Static, Struct, StructField, Trait, + InFile, Local, MacroDef, Module, ModuleDef, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, VariantDef, }; pub trait FromSource: Sized { type Ast; - fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option; + fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option; } impl FromSource for Struct { type Ast = ast::StructDef; - fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { let id = from_source(db, src)?; Some(Struct { id }) } } impl FromSource for Union { type Ast = ast::UnionDef; - fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { let id = from_source(db, src)?; Some(Union { id }) } } impl FromSource for Enum { type Ast = ast::EnumDef; - fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { let id = from_source(db, src)?; Some(Enum { id }) } } impl FromSource for Trait { type Ast = ast::TraitDef; - fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { let id = from_source(db, src)?; Some(Trait { id }) } } impl FromSource for Function { type Ast = ast::FnDef; - fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { let items = match Container::find(db, src.as_ref().map(|it| it.syntax()))? { Container::Trait(it) => it.items(db), Container::ImplBlock(it) => it.items(db), @@ -76,7 +76,7 @@ impl FromSource for Function { impl FromSource for Const { type Ast = ast::ConstDef; - fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { let items = match Container::find(db, src.as_ref().map(|it| it.syntax()))? { Container::Trait(it) => it.items(db), Container::ImplBlock(it) => it.items(db), @@ -102,7 +102,7 @@ impl FromSource for Const { } impl FromSource for Static { type Ast = ast::StaticDef; - fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { let module = match Container::find(db, src.as_ref().map(|it| it.syntax()))? { Container::Module(it) => it, Container::Trait(_) | Container::ImplBlock(_) => return None, @@ -120,7 +120,7 @@ impl FromSource for Static { impl FromSource for TypeAlias { type Ast = ast::TypeAliasDef; - fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { let items = match Container::find(db, src.as_ref().map(|it| it.syntax()))? { Container::Trait(it) => it.items(db), Container::ImplBlock(it) => it.items(db), @@ -147,11 +147,11 @@ impl FromSource for TypeAlias { impl FromSource for MacroDef { type Ast = ast::MacroCall; - fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { let kind = MacroDefKind::Declarative; let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax())); - let module = Module::from_definition(db, Source::new(src.file_id, module_src))?; + let module = Module::from_definition(db, InFile::new(src.file_id, module_src))?; let krate = module.krate().crate_id(); 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 { impl FromSource for ImplBlock { type Ast = ast::ImplBlock; - fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { let id = from_source(db, src)?; Some(ImplBlock { id }) } @@ -171,9 +171,9 @@ impl FromSource for ImplBlock { impl FromSource for EnumVariant { type Ast = ast::EnumVariant; - fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { let parent_enum = src.value.parent_enum(); - let src_enum = Source { file_id: src.file_id, value: parent_enum }; + let src_enum = InFile { file_id: src.file_id, value: parent_enum }; let variants = Enum::from_source(db, src_enum)?.variants(db); variants.into_iter().find(|v| same_source(&v.source(db), &src)) } @@ -181,17 +181,17 @@ impl FromSource for EnumVariant { impl FromSource for StructField { type Ast = FieldSource; - fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option { + fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { let variant_def: VariantDef = match src.value { FieldSource::Named(ref field) => { let value = field.syntax().ancestors().find_map(ast::StructDef::cast)?; - let src = Source { file_id: src.file_id, value }; + let src = InFile { file_id: src.file_id, value }; let def = Struct::from_source(db, src)?; VariantDef::from(def) } FieldSource::Pos(ref field) => { let value = field.syntax().ancestors().find_map(ast::EnumVariant::cast)?; - let src = Source { file_id: src.file_id, value }; + let src = InFile { file_id: src.file_id, value }; let def = EnumVariant::from_source(db, src)?; VariantDef::from(def) } @@ -206,14 +206,14 @@ impl FromSource for StructField { } impl Local { - pub fn from_source(db: &impl HirDatabase, src: Source) -> Option { + pub fn from_source(db: &impl HirDatabase, src: InFile) -> Option { let file_id = src.file_id; let parent: DefWithBody = src.value.syntax().ancestors().find_map(|it| { let res = match_ast! { match it { - ast::ConstDef(value) => { Const::from_source(db, Source { value, file_id})?.into() }, - ast::StaticDef(value) => { Static::from_source(db, Source { value, file_id})?.into() }, - ast::FnDef(value) => { Function::from_source(db, Source { value, file_id})?.into() }, + ast::ConstDef(value) => { Const::from_source(db, InFile { value, file_id})?.into() }, + ast::StaticDef(value) => { Static::from_source(db, InFile { value, file_id})?.into() }, + ast::FnDef(value) => { Function::from_source(db, InFile { value, file_id})?.into() }, _ => return None, } }; @@ -227,16 +227,16 @@ impl Local { } impl Module { - pub fn from_declaration(db: &impl DefDatabase, src: Source) -> Option { + pub fn from_declaration(db: &impl DefDatabase, src: InFile) -> Option { let parent_declaration = src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast); let parent_module = match parent_declaration { Some(parent_declaration) => { - let src_parent = Source { file_id: src.file_id, value: parent_declaration }; + let src_parent = InFile { file_id: src.file_id, value: parent_declaration }; Module::from_declaration(db, src_parent) } _ => { - let src_parent = Source { + let src_parent = InFile { file_id: src.file_id, value: ModuleSource::new(db, Some(src.file_id.original_file(db)), None), }; @@ -248,13 +248,13 @@ impl Module { parent_module.child(db, &child_name.as_name()) } - pub fn from_definition(db: &impl DefDatabase, src: Source) -> Option { + pub fn from_definition(db: &impl DefDatabase, src: InFile) -> Option { match src.value { ModuleSource::Module(ref module) => { assert!(!module.has_semi()); return Module::from_declaration( db, - Source { file_id: src.file_id, value: module.clone() }, + InFile { file_id: src.file_id, value: module.clone() }, ); } ModuleSource::SourceFile(_) => (), @@ -271,13 +271,13 @@ impl Module { } } -fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source) -> Option +fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option where N: AstNode, DEF: AstItemDef, { let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax())); - let module = Module::from_definition(db, Source::new(src.file_id, module_src))?; + let module = Module::from_definition(db, InFile::new(src.file_id, module_src))?; let ctx = LocationCtx::new(db, module.id, src.file_id); let items = db.ast_id_map(src.file_id); let item_id = items.ast_id(&src.value); @@ -291,7 +291,7 @@ enum Container { } impl Container { - fn find(db: &impl DefDatabase, src: Source<&SyntaxNode>) -> Option { + fn find(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> Option { // FIXME: this doesn't try to handle nested declarations for container in src.value.ancestors() { let res = match_ast! { @@ -322,6 +322,6 @@ impl Container { /// In general, we do not guarantee that we have exactly one instance of a /// syntax tree for each file. We probably should add such guarantee, but, for /// the time being, we will use identity-less AstPtr comparison. -fn same_source(s1: &Source, s2: &Source) -> bool { +fn same_source(s1: &InFile, s2: &InFile) -> bool { s1.as_ref().map(AstPtr::new) == s2.as_ref().map(AstPtr::new) } 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::{ type_ref::Mutability, }; pub use hir_expand::{ - either::Either, name::Name, HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, Source, + either::Either, name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, }; diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 76c493f1a..1661d92a2 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -18,7 +18,7 @@ use hir_def::{ AssocItemId, DefWithBodyId, }; use hir_expand::{ - hygiene::Hygiene, name::AsName, AstId, HirFileId, MacroCallId, MacroFileKind, Source, + hygiene::Hygiene, name::AsName, AstId, HirFileId, InFile, MacroCallId, MacroFileKind, }; use ra_syntax::{ ast::{self, AstNode}, @@ -37,7 +37,7 @@ use crate::{ GenericParam, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias, }; -fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -> Option { +fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -> Option { match_ast! { match (node.value) { ast::Module(it) => { @@ -71,7 +71,7 @@ fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) - fn def_with_body_from_child_node( db: &impl HirDatabase, - child: Source<&SyntaxNode>, + child: InFile<&SyntaxNode>, ) -> Option { child.value.ancestors().find_map(|node| { match_ast! { @@ -141,8 +141,8 @@ impl Expansion { pub fn map_token_down( &self, db: &impl HirDatabase, - token: Source<&SyntaxToken>, - ) -> Option> { + token: InFile<&SyntaxToken>, + ) -> Option> { let exp_info = self.file_id().expansion_info(db)?; exp_info.map_token_down(token) } @@ -155,7 +155,7 @@ impl Expansion { impl SourceAnalyzer { pub fn new( db: &impl HirDatabase, - node: Source<&SyntaxNode>, + node: InFile<&SyntaxNode>, offset: Option, ) -> SourceAnalyzer { let def_with_body = def_with_body_from_child_node(db, node); @@ -192,12 +192,12 @@ impl SourceAnalyzer { } fn expr_id(&self, expr: &ast::Expr) -> Option { - let src = Source { file_id: self.file_id, value: expr }; + let src = InFile { file_id: self.file_id, value: expr }; self.body_source_map.as_ref()?.node_expr(src) } fn pat_id(&self, pat: &ast::Pat) -> Option { - let src = Source { file_id: self.file_id, value: pat }; + let src = InFile { file_id: self.file_id, value: pat }; self.body_source_map.as_ref()?.node_pat(src) } @@ -243,7 +243,7 @@ impl SourceAnalyzer { pub fn resolve_macro_call( &self, db: &impl HirDatabase, - macro_call: Source<&ast::MacroCall>, + macro_call: InFile<&ast::MacroCall>, ) -> Option { let hygiene = Hygiene::new(db, macro_call.file_id); let path = macro_call.value.path().and_then(|ast| Path::from_src(ast, &hygiene))?; @@ -318,7 +318,7 @@ impl SourceAnalyzer { let name = name_ref.as_name(); let source_map = self.body_source_map.as_ref()?; let scopes = self.scopes.as_ref()?; - let scope = scope_for(scopes, source_map, Source::new(self.file_id, name_ref.syntax()))?; + let scope = scope_for(scopes, source_map, InFile::new(self.file_id, name_ref.syntax()))?; let entry = scopes.resolve_name_in_scope(scope, &name)?; Some(ScopeEntryWithSyntax { name: entry.name().clone(), @@ -446,7 +446,7 @@ impl SourceAnalyzer { pub fn expand( &self, db: &impl HirDatabase, - macro_call: Source<&ast::MacroCall>, + macro_call: InFile<&ast::MacroCall>, ) -> Option { let def = self.resolve_macro_call(db, macro_call)?.id; let ast_id = AstId::new( @@ -463,19 +463,19 @@ impl SourceAnalyzer { fn scope_for( scopes: &ExprScopes, source_map: &BodySourceMap, - node: Source<&SyntaxNode>, + node: InFile<&SyntaxNode>, ) -> Option { node.value .ancestors() .filter_map(ast::Expr::cast) - .filter_map(|it| source_map.node_expr(Source::new(node.file_id, &it))) + .filter_map(|it| source_map.node_expr(InFile::new(node.file_id, &it))) .find_map(|it| scopes.scope_for(it)) } fn scope_for_offset( scopes: &ExprScopes, source_map: &BodySourceMap, - offset: Source, + offset: InFile, ) -> Option { scopes .scope_by_expr() -- cgit v1.2.3