From 4f94af3c4aaa57ebb4cb01f7e4edfb3a0821b09b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 11 Jun 2019 16:49:56 +0300 Subject: [PATCH] use Source for Function --- crates/ra_hir/src/code_model.rs | 20 +++++++++++++------ crates/ra_hir/src/docs.rs | 2 +- crates/ra_hir/src/expr.rs | 6 +++--- crates/ra_hir/src/expr/validation.rs | 2 +- crates/ra_hir/src/generics.rs | 2 +- crates/ra_hir/src/ty/infer.rs | 2 +- .../ra_ide_api/src/completion/presentation.rs | 2 +- .../src/display/function_signature.rs | 2 +- .../src/display/navigation_target.rs | 8 +------- crates/ra_ide_api/src/hover.rs | 12 +++++------ 10 files changed, 30 insertions(+), 28 deletions(-) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 7658504883e..aa6eb741b74 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -603,6 +603,14 @@ pub struct Function { pub(crate) id: FunctionId, } +impl HasSource for Function { + type Ast = TreeArc; + + fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { + 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 { - 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) { - self.id.source(db) + pub fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source> { + self.id.source(db).into() } pub fn module(self, db: &impl DefDatabase) -> Module { diff --git a/crates/ra_hir/src/docs.rs b/crates/ra_hir/src/docs.rs index 70b9d13b2ed..8d7a5255f4a 100644 --- a/crates/ra_hir/src/docs.rs +++ b/crates/ra_hir/src/docs.rs @@ -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), diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 012f374ec84..46d51e0db35 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -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); diff --git a/crates/ra_hir/src/expr/validation.rs b/crates/ra_hir/src/expr/validation.rs index a1b2641da1b..ff5e5f68ee9 100644 --- a/crates/ra_hir/src/expr/validation.rs +++ b/crates/ra_hir/src/expr/validation.rs @@ -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) diff --git a/crates/ra_hir/src/generics.rs b/crates/ra_hir/src/generics.rs index 2a92d594507..fcccd67c8e4 100644 --- a/crates/ra_hir/src/generics.rs +++ b/crates/ra_hir/src/generics.rs @@ -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), diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 6aa727ea104..a534c233670 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -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 }) } diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index d405161d63a..e9ae2022508 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -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) diff --git a/crates/ra_ide_api/src/display/function_signature.rs b/crates/ra_ide_api/src/display/function_signature.rs index d09950bce02..d0c155de712 100644 --- a/crates/ra_ide_api/src/display/function_signature.rs +++ b/crates/ra_ide_api/src/display/function_signature.rs @@ -33,7 +33,7 @@ pub(crate) fn with_doc_opt(mut self, doc: Option) -> 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) } } diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index bef5f0980a1..3333771ab5a 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -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 { diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index ee0f2dde880..4dd3c1b9f9d 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs @@ -96,8 +96,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option { - 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 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 { - 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);