mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
uncopypaste def sources
This commit is contained in:
parent
0f9c350812
commit
19136cde00
3 changed files with 26 additions and 40 deletions
|
@ -2,7 +2,7 @@ use std::sync::Arc;
|
||||||
|
|
||||||
use relative_path::RelativePathBuf;
|
use relative_path::RelativePathBuf;
|
||||||
use ra_db::{CrateId, Cancelable, FileId};
|
use ra_db::{CrateId, Cancelable, FileId};
|
||||||
use ra_syntax::{ast, TreeArc, SyntaxNode, AstNode};
|
use ra_syntax::{ast, TreeArc, SyntaxNode};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId,
|
Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId,
|
||||||
|
@ -12,6 +12,7 @@ use crate::{
|
||||||
expr::BodySyntaxMapping,
|
expr::BodySyntaxMapping,
|
||||||
ty::InferenceResult,
|
ty::InferenceResult,
|
||||||
adt::VariantData,
|
adt::VariantData,
|
||||||
|
code_model_impl::def_id_to_ast,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// hir::Crate describes a single crate. It's the main interface with which
|
/// hir::Crate describes a single crate. It's the main interface with which
|
||||||
|
@ -186,13 +187,7 @@ impl Struct {
|
||||||
&self,
|
&self,
|
||||||
db: &impl HirDatabase,
|
db: &impl HirDatabase,
|
||||||
) -> Cancelable<(HirFileId, TreeArc<ast::StructDef>)> {
|
) -> Cancelable<(HirFileId, TreeArc<ast::StructDef>)> {
|
||||||
let (file_id, syntax) = self.def_id.source(db);
|
Ok(def_id_to_ast(db, self.def_id))
|
||||||
Ok((
|
|
||||||
file_id,
|
|
||||||
ast::StructDef::cast(&syntax)
|
|
||||||
.expect("struct def should point to StructDef node")
|
|
||||||
.to_owned(),
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -219,13 +214,7 @@ impl Enum {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::EnumDef>)> {
|
pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::EnumDef>)> {
|
||||||
let (file_id, syntax) = self.def_id.source(db);
|
Ok(def_id_to_ast(db, self.def_id))
|
||||||
Ok((
|
|
||||||
file_id,
|
|
||||||
ast::EnumDef::cast(&syntax)
|
|
||||||
.expect("enum def should point to EnumDef node")
|
|
||||||
.to_owned(),
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -259,13 +248,7 @@ impl EnumVariant {
|
||||||
&self,
|
&self,
|
||||||
db: &impl HirDatabase,
|
db: &impl HirDatabase,
|
||||||
) -> Cancelable<(HirFileId, TreeArc<ast::EnumVariant>)> {
|
) -> Cancelable<(HirFileId, TreeArc<ast::EnumVariant>)> {
|
||||||
let (file_id, syntax) = self.def_id.source(db);
|
Ok(def_id_to_ast(db, self.def_id))
|
||||||
Ok((
|
|
||||||
file_id,
|
|
||||||
ast::EnumVariant::cast(&syntax)
|
|
||||||
.expect("variant def should point to EnumVariant node")
|
|
||||||
.to_owned(),
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -304,7 +287,7 @@ impl Function {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::FnDef>)> {
|
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>> {
|
pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> {
|
||||||
|
|
|
@ -1,3 +1,18 @@
|
||||||
mod krate; // `crate` is invalid ident :(
|
mod krate; // `crate` is invalid ident :(
|
||||||
mod module;
|
mod module;
|
||||||
pub(crate) mod function;
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -3,16 +3,14 @@ mod scope;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use ra_db::Cancelable;
|
use ra_db::Cancelable;
|
||||||
use ra_syntax::{
|
use ra_syntax::{TreeArc, ast::{self, NameOwner}};
|
||||||
TreeArc,
|
|
||||||
ast::{self, AstNode, NameOwner},
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
DefId, DefKind, HirDatabase, Name, AsName, Function, FnSignature, Module, HirFileId,
|
DefId, HirDatabase, Name, AsName, Function, FnSignature, Module,
|
||||||
type_ref::{TypeRef, Mutability},
|
type_ref::{TypeRef, Mutability},
|
||||||
expr::Body,
|
expr::Body,
|
||||||
impl_block::ImplBlock,
|
impl_block::ImplBlock,
|
||||||
|
code_model_impl::def_id_to_ast,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use self::scope::{FnScopes, ScopesWithSyntaxMapping, ScopeEntryWithSyntax};
|
pub use self::scope::{FnScopes, ScopesWithSyntaxMapping, ScopeEntryWithSyntax};
|
||||||
|
@ -22,16 +20,6 @@ impl Function {
|
||||||
Function { def_id }
|
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>> {
|
pub(crate) fn body(&self, db: &impl HirDatabase) -> Cancelable<Arc<Body>> {
|
||||||
db.body_hir(self.def_id)
|
db.body_hir(self.def_id)
|
||||||
}
|
}
|
||||||
|
@ -48,8 +36,8 @@ impl Function {
|
||||||
|
|
||||||
impl FnSignature {
|
impl FnSignature {
|
||||||
pub(crate) fn fn_signature_query(db: &impl HirDatabase, def_id: DefId) -> Arc<FnSignature> {
|
pub(crate) fn fn_signature_query(db: &impl HirDatabase, def_id: DefId) -> Arc<FnSignature> {
|
||||||
let func = Function::new(def_id);
|
// FIXME: we're using def_id_to_ast here to avoid returning Cancelable... this is a bit hacky
|
||||||
let node = func.source_impl(db).1; // TODO we're using source_impl 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
|
let name = node
|
||||||
.name()
|
.name()
|
||||||
.map(|n| n.as_name())
|
.map(|n| n.as_name())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue