use Source for Function

This commit is contained in:
Aleksey Kladov 2019-06-11 16:49:56 +03:00
parent 36865adcb9
commit 4f94af3c4a
10 changed files with 30 additions and 28 deletions

View File

@ -603,6 +603,14 @@ pub struct Function {
pub(crate) id: FunctionId, pub(crate) id: FunctionId,
} }
impl HasSource for Function {
type Ast = TreeArc<ast::FnDef>;
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::FnDef>> {
self.id.source(db).into()
}
}
/// The declared signature of a function. /// The declared signature of a function.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct FnSignature { pub struct FnSignature {
@ -619,11 +627,11 @@ impl FnSignature {
db: &(impl DefDatabase + AstDatabase), db: &(impl DefDatabase + AstDatabase),
func: Function, func: Function,
) -> Arc<FnSignature> { ) -> Arc<FnSignature> {
let (_, node) = func.source(db); let src = func.source(db);
let name = node.name().map(|n| n.as_name()).unwrap_or_else(Name::missing); let name = src.ast.name().map(|n| n.as_name()).unwrap_or_else(Name::missing);
let mut params = Vec::new(); let mut params = Vec::new();
let mut has_self_param = false; let mut has_self_param = false;
if let Some(param_list) = node.param_list() { if let Some(param_list) = src.ast.param_list() {
if let Some(self_param) = param_list.self_param() { if let Some(self_param) = param_list.self_param() {
let self_type = if let Some(type_ref) = self_param.ascribed_type() { let self_type = if let Some(type_ref) = self_param.ascribed_type() {
TypeRef::from_ast(type_ref) TypeRef::from_ast(type_ref)
@ -647,7 +655,7 @@ impl FnSignature {
params.push(type_ref); params.push(type_ref);
} }
} }
let ret_type = if let Some(type_ref) = node.ret_type().and_then(|rt| rt.type_ref()) { let ret_type = if let Some(type_ref) = src.ast.ret_type().and_then(|rt| rt.type_ref()) {
TypeRef::from_ast(type_ref) TypeRef::from_ast(type_ref)
} else { } else {
TypeRef::unit() TypeRef::unit()
@ -676,8 +684,8 @@ impl FnSignature {
} }
impl Function { impl Function {
pub fn source(self, db: &(impl DefDatabase + AstDatabase)) -> (HirFileId, TreeArc<ast::FnDef>) { pub fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::FnDef>> {
self.id.source(db) self.id.source(db).into()
} }
pub fn module(self, db: &impl DefDatabase) -> Module { pub fn module(self, db: &impl DefDatabase) -> Module {

View File

@ -81,7 +81,7 @@ pub(crate) fn documentation_query(
DocDef::EnumVariant(it) => docs_from_ast(&*it.source(db).ast), DocDef::EnumVariant(it) => docs_from_ast(&*it.source(db).ast),
DocDef::Static(it) => docs_from_ast(&*it.source(db).1), DocDef::Static(it) => docs_from_ast(&*it.source(db).1),
DocDef::Const(it) => docs_from_ast(&*it.source(db).1), DocDef::Const(it) => docs_from_ast(&*it.source(db).1),
DocDef::Function(it) => docs_from_ast(&*it.source(db).1), DocDef::Function(it) => docs_from_ast(&*it.source(db).ast),
DocDef::Union(it) => docs_from_ast(&*it.source(db).1), DocDef::Union(it) => docs_from_ast(&*it.source(db).1),
DocDef::Trait(it) => docs_from_ast(&*it.source(db).1), DocDef::Trait(it) => docs_from_ast(&*it.source(db).1),
DocDef::TypeAlias(it) => docs_from_ast(&*it.source(db).1), DocDef::TypeAlias(it) => docs_from_ast(&*it.source(db).1),

View File

@ -1023,9 +1023,9 @@ pub(crate) fn body_with_source_map_query(
collector.collect_const_body(&src) collector.collect_const_body(&src)
} }
DefWithBody::Function(ref f) => { DefWithBody::Function(ref f) => {
let (file_id, src) = f.source(db); let src = f.source(db);
collector = ExprCollector::new(def, file_id, def.resolver(db), db); collector = ExprCollector::new(def, src.file_id, def.resolver(db), db);
collector.collect_fn_body(&src) collector.collect_fn_body(&src.ast)
} }
DefWithBody::Static(ref s) => { DefWithBody::Static(ref s) => {
let (file_id, src) = s.source(db); let (file_id, src) = s.source(db);

View File

@ -71,7 +71,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
return; return;
} }
let source_map = self.func.body_source_map(db); let source_map = self.func.body_source_map(db);
let file_id = self.func.source(db).0; let file_id = self.func.source(db).file_id;
let source_file = db.parse(file_id.original_file(db)).tree; let source_file = db.parse(file_id.original_file(db)).tree;
if let Some(field_list_node) = source_map if let Some(field_list_node) = source_map
.expr_syntax(id) .expr_syntax(id)

View File

@ -68,7 +68,7 @@ impl GenericParams {
generics.parent_params = parent.map(|p| db.generic_params(p)); generics.parent_params = parent.map(|p| db.generic_params(p));
let start = generics.parent_params.as_ref().map(|p| p.params.len()).unwrap_or(0) as u32; let start = generics.parent_params.as_ref().map(|p| p.params.len()).unwrap_or(0) as u32;
match def { match def {
GenericDef::Function(it) => generics.fill(&*it.source(db).1, start), GenericDef::Function(it) => generics.fill(&*it.source(db).ast, start),
GenericDef::Struct(it) => generics.fill(&*it.source(db).ast, start), GenericDef::Struct(it) => generics.fill(&*it.source(db).ast, start),
GenericDef::Union(it) => generics.fill(&*it.source(db).1, start), GenericDef::Union(it) => generics.fill(&*it.source(db).1, start),
GenericDef::Enum(it) => generics.fill(&*it.source(db).ast, start), GenericDef::Enum(it) => generics.fill(&*it.source(db).ast, start),

View File

@ -1432,7 +1432,7 @@ mod diagnostics {
) { ) {
match self { match self {
InferenceDiagnostic::NoSuchField { expr, field } => { InferenceDiagnostic::NoSuchField { expr, field } => {
let (file, _) = owner.source(db); let file = owner.source(db).file_id;
let field = owner.body_source_map(db).field_syntax(*expr, *field); let field = owner.body_source_map(db).field_syntax(*expr, *field);
sink.push(NoSuchField { file, field }) sink.push(NoSuchField { file, field })
} }

View File

@ -100,7 +100,7 @@ impl Completions {
) { ) {
let sig = func.signature(ctx.db); let sig = func.signature(ctx.db);
let name = name.unwrap_or_else(|| sig.name().to_string()); let name = name.unwrap_or_else(|| sig.name().to_string());
let (_, ast_node) = func.source(ctx.db); let ast_node = func.source(ctx.db).ast;
let detail = function_label(&ast_node); let detail = function_label(&ast_node);
let mut builder = CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name) let mut builder = CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name)

View File

@ -33,7 +33,7 @@ impl FunctionSignature {
pub(crate) fn from_hir(db: &db::RootDatabase, function: hir::Function) -> Self { pub(crate) fn from_hir(db: &db::RootDatabase, function: hir::Function) -> Self {
let doc = function.docs(db); let doc = function.docs(db);
let (_, ast_node) = function.source(db); let ast_node = function.source(db).ast;
FunctionSignature::from(&*ast_node).with_doc_opt(doc) FunctionSignature::from(&*ast_node).with_doc_opt(doc)
} }
} }

View File

@ -164,13 +164,7 @@ impl NavigationTarget {
} }
pub(crate) fn from_function(db: &RootDatabase, func: hir::Function) -> NavigationTarget { pub(crate) fn from_function(db: &RootDatabase, func: hir::Function) -> NavigationTarget {
let (file_id, fn_def) = func.source(db); NavigationTarget::from_def_source(db, func)
NavigationTarget::from_named(
file_id.original_file(db),
&*fn_def,
fn_def.doc_comment_text(),
fn_def.short_label(),
)
} }
pub(crate) fn from_field(db: &RootDatabase, field: hir::StructField) -> NavigationTarget { pub(crate) fn from_field(db: &RootDatabase, field: hir::StructField) -> NavigationTarget {

View File

@ -96,8 +96,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
match classify_name_ref(db, &analyzer, name_ref) { match classify_name_ref(db, &analyzer, name_ref) {
Some(Method(it)) => { Some(Method(it)) => {
let it = it.source(db).1; let src = it.source(db);
res.extend(hover_text(it.doc_comment_text(), it.short_label())); res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label()));
} }
Some(Macro(it)) => { Some(Macro(it)) => {
let it = it.source(db).1; let it = it.source(db).1;
@ -111,8 +111,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
} }
Some(AssocItem(it)) => match it { Some(AssocItem(it)) => match it {
hir::ImplItem::Method(it) => { hir::ImplItem::Method(it) => {
let it = it.source(db).1; let src = it.source(db);
res.extend(hover_text(it.doc_comment_text(), it.short_label())) res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label()))
} }
hir::ImplItem::Const(it) => { hir::ImplItem::Const(it) => {
let it = it.source(db).1; let it = it.source(db).1;
@ -132,8 +132,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
} }
} }
hir::ModuleDef::Function(it) => { hir::ModuleDef::Function(it) => {
let it = it.source(db).1; let src = it.source(db);
res.extend(hover_text(it.doc_comment_text(), it.short_label())) res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label()))
} }
hir::ModuleDef::Struct(it) => { hir::ModuleDef::Struct(it) => {
let src = it.source(db); let src = it.source(db);