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,
}
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.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FnSignature {
@ -619,11 +627,11 @@ pub(crate) fn fn_signature_query(
db: &(impl DefDatabase + AstDatabase),
func: Function,
) -> Arc<FnSignature> {
let (_, node) = func.source(db);
let name = node.name().map(|n| n.as_name()).unwrap_or_else(Name::missing);
let src = func.source(db);
let name = src.ast.name().map(|n| n.as_name()).unwrap_or_else(Name::missing);
let mut params = Vec::new();
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() {
let self_type = if let Some(type_ref) = self_param.ascribed_type() {
TypeRef::from_ast(type_ref)
@ -647,7 +655,7 @@ pub(crate) fn fn_signature_query(
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)
} else {
TypeRef::unit()
@ -676,8 +684,8 @@ pub fn has_self_param(&self) -> bool {
}
impl Function {
pub fn source(self, db: &(impl DefDatabase + AstDatabase)) -> (HirFileId, TreeArc<ast::FnDef>) {
self.id.source(db)
pub fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<TreeArc<ast::FnDef>> {
self.id.source(db).into()
}
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::Static(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::Trait(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)
}
DefWithBody::Function(ref f) => {
let (file_id, src) = f.source(db);
collector = ExprCollector::new(def, file_id, def.resolver(db), db);
collector.collect_fn_body(&src)
let src = f.source(db);
collector = ExprCollector::new(def, src.file_id, def.resolver(db), db);
collector.collect_fn_body(&src.ast)
}
DefWithBody::Static(ref s) => {
let (file_id, src) = s.source(db);

View File

@ -71,7 +71,7 @@ fn validate_struct_literal(
return;
}
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;
if let Some(field_list_node) = source_map
.expr_syntax(id)

View File

@ -68,7 +68,7 @@ pub(crate) fn generic_params_query(
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;
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::Union(it) => generics.fill(&*it.source(db).1, start),
GenericDef::Enum(it) => generics.fill(&*it.source(db).ast, start),

View File

@ -1432,7 +1432,7 @@ pub(super) fn add_to(
) {
match self {
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);
sink.push(NoSuchField { file, field })
}

View File

@ -100,7 +100,7 @@ fn add_function_with_name(
) {
let sig = func.signature(ctx.db);
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 mut builder = CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name)

View File

@ -33,7 +33,7 @@ pub(crate) fn with_doc_opt(mut self, doc: Option<Documentation>) -> Self {
pub(crate) fn from_hir(db: &db::RootDatabase, function: hir::Function) -> Self {
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)
}
}

View File

@ -164,13 +164,7 @@ pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> Nav
}
pub(crate) fn from_function(db: &RootDatabase, func: hir::Function) -> NavigationTarget {
let (file_id, fn_def) = func.source(db);
NavigationTarget::from_named(
file_id.original_file(db),
&*fn_def,
fn_def.doc_comment_text(),
fn_def.short_label(),
)
NavigationTarget::from_def_source(db, func)
}
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) {
Some(Method(it)) => {
let it = it.source(db).1;
res.extend(hover_text(it.doc_comment_text(), it.short_label()));
let src = it.source(db);
res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label()));
}
Some(Macro(it)) => {
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 {
hir::ImplItem::Method(it) => {
let it = it.source(db).1;
res.extend(hover_text(it.doc_comment_text(), it.short_label()))
let src = it.source(db);
res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label()))
}
hir::ImplItem::Const(it) => {
let it = it.source(db).1;
@ -132,8 +132,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
}
}
hir::ModuleDef::Function(it) => {
let it = it.source(db).1;
res.extend(hover_text(it.doc_comment_text(), it.short_label()))
let src = it.source(db);
res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label()))
}
hir::ModuleDef::Struct(it) => {
let src = it.source(db);