Move source-related traits to a separate module

This commit is contained in:
Aleksey Kladov 2019-11-28 18:05:28 +03:00
parent 2c7f6b573e
commit 8f1f5a783a
10 changed files with 77 additions and 60 deletions

View file

@ -1,6 +1,9 @@
//! FIXME: write short doc here //! FIXME: write short doc here
use hir_def::{AstItemDef, HasChildSource, HasSource as _, Lookup, VariantId}; use hir_def::{
src::{HasChildSource, HasSource as _},
AstItemDef, Lookup, VariantId,
};
use hir_expand::either::Either; use hir_expand::either::Either;
use ra_syntax::ast; use ra_syntax::ast;

View file

@ -11,7 +11,7 @@ use ra_arena::{map::ArenaMap, Arena};
use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
use crate::{ use crate::{
db::DefDatabase, trace::Trace, type_ref::TypeRef, AstItemDef, EnumId, HasChildSource, db::DefDatabase, src::HasChildSource, trace::Trace, type_ref::TypeRef, AstItemDef, EnumId,
LocalEnumVariantId, LocalStructFieldId, StructId, UnionId, VariantId, LocalEnumVariantId, LocalStructFieldId, StructId, UnionId, VariantId,
}; };

View file

@ -11,7 +11,8 @@ use ra_syntax::{
use tt::Subtree; use tt::Subtree;
use crate::{ use crate::{
db::DefDatabase, path::Path, AdtId, AstItemDef, AttrDefId, HasChildSource, HasSource, Lookup, db::DefDatabase, path::Path, src::HasChildSource, src::HasSource, AdtId, AstItemDef, AttrDefId,
Lookup,
}; };
#[derive(Default, Debug, Clone, PartialEq, Eq)] #[derive(Default, Debug, Clone, PartialEq, Eq)]

View file

@ -17,7 +17,8 @@ use crate::{
expr::{Expr, ExprId, Pat, PatId}, expr::{Expr, ExprId, Pat, PatId},
nameres::CrateDefMap, nameres::CrateDefMap,
path::Path, path::Path,
DefWithBodyId, HasModule, HasSource, Lookup, ModuleId, src::HasSource,
DefWithBodyId, HasModule, Lookup, ModuleId,
}; };
struct Expander { struct Expander {

View file

@ -10,9 +10,10 @@ use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
use crate::{ use crate::{
db::DefDatabase, db::DefDatabase,
src::HasSource,
type_ref::{Mutability, TypeRef}, type_ref::{Mutability, TypeRef},
AssocItemId, AstItemDef, ConstId, ConstLoc, ContainerId, FunctionId, FunctionLoc, HasSource, AssocItemId, AstItemDef, ConstId, ConstLoc, ContainerId, FunctionId, FunctionLoc, ImplId,
ImplId, Intern, Lookup, StaticId, TraitId, TypeAliasId, TypeAliasLoc, Intern, Lookup, StaticId, TraitId, TypeAliasId, TypeAliasLoc,
}; };
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]

View file

@ -8,7 +8,11 @@ use std::sync::Arc;
use hir_expand::either::Either; use hir_expand::either::Either;
use ra_syntax::ast; use ra_syntax::ast;
use crate::{db::DefDatabase, AdtId, AstItemDef, AttrDefId, HasChildSource, HasSource, Lookup}; use crate::{
db::DefDatabase,
src::{HasChildSource, HasSource},
AdtId, AstItemDef, AttrDefId, Lookup,
};
/// Holds documentation /// Holds documentation
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]

View file

@ -9,8 +9,9 @@ use ra_syntax::ast::{self, NameOwner, TypeBoundsOwner, TypeParamsOwner};
use crate::{ use crate::{
db::DefDatabase, db::DefDatabase,
src::HasSource,
type_ref::{TypeBound, TypeRef}, type_ref::{TypeBound, TypeRef},
AdtId, AstItemDef, ContainerId, GenericDefId, HasSource, Lookup, AdtId, AstItemDef, ContainerId, GenericDefId, Lookup,
}; };
/// Data about a generic parameter (to a function, struct, impl, ...). /// Data about a generic parameter (to a function, struct, impl, ...).

View file

@ -29,6 +29,8 @@ pub mod resolver;
mod trace; mod trace;
pub mod nameres; pub mod nameres;
pub mod src;
#[cfg(test)] #[cfg(test)]
mod test_db; mod test_db;
#[cfg(test)] #[cfg(test)]
@ -37,7 +39,7 @@ mod marks;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId, InFile, MacroDefId}; use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId, InFile, MacroDefId};
use ra_arena::{impl_arena_id, map::ArenaMap, RawId}; use ra_arena::{impl_arena_id, RawId};
use ra_db::{impl_intern_key, salsa, CrateId}; use ra_db::{impl_intern_key, salsa, CrateId};
use ra_syntax::{ast, AstNode}; use ra_syntax::{ast, AstNode};
@ -514,53 +516,3 @@ impl HasModule for StaticLoc {
self.container self.container
} }
} }
pub trait HasSource {
type Value;
fn source(&self, db: &impl db::DefDatabase) -> InFile<Self::Value>;
}
impl HasSource for FunctionLoc {
type Value = ast::FnDef;
fn source(&self, db: &impl db::DefDatabase) -> InFile<ast::FnDef> {
let node = self.ast_id.to_node(db);
InFile::new(self.ast_id.file_id, node)
}
}
impl HasSource for TypeAliasLoc {
type Value = ast::TypeAliasDef;
fn source(&self, db: &impl db::DefDatabase) -> InFile<ast::TypeAliasDef> {
let node = self.ast_id.to_node(db);
InFile::new(self.ast_id.file_id, node)
}
}
impl HasSource for ConstLoc {
type Value = ast::ConstDef;
fn source(&self, db: &impl db::DefDatabase) -> InFile<ast::ConstDef> {
let node = self.ast_id.to_node(db);
InFile::new(self.ast_id.file_id, node)
}
}
impl HasSource for StaticLoc {
type Value = ast::StaticDef;
fn source(&self, db: &impl db::DefDatabase) -> InFile<ast::StaticDef> {
let node = self.ast_id.to_node(db);
InFile::new(self.ast_id.file_id, node)
}
}
pub trait HasChildSource {
type ChildId;
type Value;
fn child_source(
&self,
db: &impl db::DefDatabase,
) -> InFile<ArenaMap<Self::ChildId, Self::Value>>;
}

View file

@ -0,0 +1,54 @@
//! Utilities for mapping between hir IDs and the surface syntax.
use hir_expand::InFile;
use ra_arena::map::ArenaMap;
use ra_syntax::ast;
use crate::{db::DefDatabase, ConstLoc, FunctionLoc, StaticLoc, TypeAliasLoc};
pub trait HasSource {
type Value;
fn source(&self, db: &impl DefDatabase) -> InFile<Self::Value>;
}
impl HasSource for FunctionLoc {
type Value = ast::FnDef;
fn source(&self, db: &impl DefDatabase) -> InFile<ast::FnDef> {
let node = self.ast_id.to_node(db);
InFile::new(self.ast_id.file_id, node)
}
}
impl HasSource for TypeAliasLoc {
type Value = ast::TypeAliasDef;
fn source(&self, db: &impl DefDatabase) -> InFile<ast::TypeAliasDef> {
let node = self.ast_id.to_node(db);
InFile::new(self.ast_id.file_id, node)
}
}
impl HasSource for ConstLoc {
type Value = ast::ConstDef;
fn source(&self, db: &impl DefDatabase) -> InFile<ast::ConstDef> {
let node = self.ast_id.to_node(db);
InFile::new(self.ast_id.file_id, node)
}
}
impl HasSource for StaticLoc {
type Value = ast::StaticDef;
fn source(&self, db: &impl DefDatabase) -> InFile<ast::StaticDef> {
let node = self.ast_id.to_node(db);
InFile::new(self.ast_id.file_id, node)
}
}
pub trait HasChildSource {
type ChildId;
type Value;
fn child_source(&self, db: &impl DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>>;
}

View file

@ -693,7 +693,7 @@ impl Expectation {
} }
mod diagnostics { mod diagnostics {
use hir_def::{expr::ExprId, FunctionId, HasSource, Lookup}; use hir_def::{expr::ExprId, src::HasSource, FunctionId, Lookup};
use hir_expand::diagnostics::DiagnosticSink; use hir_expand::diagnostics::DiagnosticSink;
use crate::{db::HirDatabase, diagnostics::NoSuchField}; use crate::{db::HirDatabase, diagnostics::NoSuchField};