uncopypaste def sources

This commit is contained in:
Aleksey Kladov 2019-01-11 20:43:10 +03:00
parent 0f9c350812
commit 19136cde00
3 changed files with 26 additions and 40 deletions

View file

@ -2,7 +2,7 @@ use std::sync::Arc;
use relative_path::RelativePathBuf;
use ra_db::{CrateId, Cancelable, FileId};
use ra_syntax::{ast, TreeArc, SyntaxNode, AstNode};
use ra_syntax::{ast, TreeArc, SyntaxNode};
use crate::{
Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId,
@ -12,6 +12,7 @@ use crate::{
expr::BodySyntaxMapping,
ty::InferenceResult,
adt::VariantData,
code_model_impl::def_id_to_ast,
};
/// hir::Crate describes a single crate. It's the main interface with which
@ -186,13 +187,7 @@ impl Struct {
&self,
db: &impl HirDatabase,
) -> Cancelable<(HirFileId, TreeArc<ast::StructDef>)> {
let (file_id, syntax) = self.def_id.source(db);
Ok((
file_id,
ast::StructDef::cast(&syntax)
.expect("struct def should point to StructDef node")
.to_owned(),
))
Ok(def_id_to_ast(db, self.def_id))
}
}
@ -219,13 +214,7 @@ impl Enum {
}
pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::EnumDef>)> {
let (file_id, syntax) = self.def_id.source(db);
Ok((
file_id,
ast::EnumDef::cast(&syntax)
.expect("enum def should point to EnumDef node")
.to_owned(),
))
Ok(def_id_to_ast(db, self.def_id))
}
}
@ -259,13 +248,7 @@ impl EnumVariant {
&self,
db: &impl HirDatabase,
) -> Cancelable<(HirFileId, TreeArc<ast::EnumVariant>)> {
let (file_id, syntax) = self.def_id.source(db);
Ok((
file_id,
ast::EnumVariant::cast(&syntax)
.expect("variant def should point to EnumVariant node")
.to_owned(),
))
Ok(def_id_to_ast(db, self.def_id))
}
}
@ -304,7 +287,7 @@ impl Function {
}
pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::FnDef>)> {
Ok(self.source_impl(db))
Ok(def_id_to_ast(db, self.def_id))
}
pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> {

View file

@ -1,3 +1,18 @@
mod krate; // `crate` is invalid ident :(
mod module;
pub(crate) mod function;
use ra_syntax::{AstNode, TreeArc};
use crate::{HirDatabase, DefId, HirFileId};
pub(crate) fn def_id_to_ast<N: AstNode>(
db: &impl HirDatabase,
def_id: DefId,
) -> (HirFileId, TreeArc<N>) {
let (file_id, syntax) = def_id.source(db);
let ast = N::cast(&syntax)
.unwrap_or_else(|| panic!("def points to wrong source {:?} {:?}", def_id, syntax))
.to_owned();
(file_id, ast)
}

View file

@ -3,16 +3,14 @@ mod scope;
use std::sync::Arc;
use ra_db::Cancelable;
use ra_syntax::{
TreeArc,
ast::{self, AstNode, NameOwner},
};
use ra_syntax::{TreeArc, ast::{self, NameOwner}};
use crate::{
DefId, DefKind, HirDatabase, Name, AsName, Function, FnSignature, Module, HirFileId,
DefId, HirDatabase, Name, AsName, Function, FnSignature, Module,
type_ref::{TypeRef, Mutability},
expr::Body,
impl_block::ImplBlock,
code_model_impl::def_id_to_ast,
};
pub use self::scope::{FnScopes, ScopesWithSyntaxMapping, ScopeEntryWithSyntax};
@ -22,16 +20,6 @@ impl Function {
Function { def_id }
}
pub(crate) fn source_impl(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::FnDef>) {
let def_loc = self.def_id.loc(db);
assert!(def_loc.kind == DefKind::Function);
let syntax = db.file_item(def_loc.source_item_id);
(
def_loc.source_item_id.file_id,
ast::FnDef::cast(&syntax).unwrap().to_owned(),
)
}
pub(crate) fn body(&self, db: &impl HirDatabase) -> Cancelable<Arc<Body>> {
db.body_hir(self.def_id)
}
@ -48,8 +36,8 @@ impl Function {
impl FnSignature {
pub(crate) fn fn_signature_query(db: &impl HirDatabase, def_id: DefId) -> Arc<FnSignature> {
let func = Function::new(def_id);
let node = func.source_impl(db).1; // TODO we're using source_impl here to avoid returning Cancelable... this is a bit hacky
// FIXME: we're using def_id_to_ast here to avoid returning Cancelable... this is a bit hacky
let node: TreeArc<ast::FnDef> = def_id_to_ast(db, def_id).1;
let name = node
.name()
.map(|n| n.as_name())