From 647c8f3df8e5f798f940ce0b93e5e0ccd59b0e7f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 11 Jun 2019 17:22:14 +0300 Subject: reduce duplication in hover --- crates/ra_ide_api/src/hover.rs | 80 +++++++++++++----------------------------- 1 file changed, 24 insertions(+), 56 deletions(-) (limited to 'crates/ra_ide_api/src') diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index fe108a589..0f68cacca 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs @@ -1,6 +1,7 @@ use ra_db::SourceDatabase; use ra_syntax::{ - AstNode, ast::{self, DocCommentsOwner}, + AstNode, TreeArc, + ast::{self, DocCommentsOwner}, algo::{find_covering_element, find_node_at_offset, ancestors_at_offset, visit::{visitor, Visitor}}, }; use hir::HirDisplay; @@ -95,10 +96,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option { - let src = it.source(db); - res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label())); - } + Some(Method(it)) => res.extend(from_def_source(db, it)), Some(Macro(it)) => { let it = it.source(db).1; res.extend(hover_text(it.doc_comment_text(), None)); @@ -110,14 +108,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option match it { - hir::ImplItem::Method(it) => { - let src = it.source(db); - res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label())) - } - hir::ImplItem::Const(it) => { - let src = it.source(db); - res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label())) - } + hir::ImplItem::Method(it) => res.extend(from_def_source(db, it)), + hir::ImplItem::Const(it) => res.extend(from_def_source(db, it)), hir::ImplItem::TypeAlias(it) => { let it = it.source(db).1; res.extend(hover_text(it.doc_comment_text(), it.short_label())) @@ -131,34 +123,16 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option { - 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); - res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label())) - } - hir::ModuleDef::Union(it) => { - let it = it.source(db).1; - res.extend(hover_text(it.doc_comment_text(), it.short_label())) - } - hir::ModuleDef::Enum(it) => { - let src = it.source(db); - res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label())) - } + hir::ModuleDef::Function(it) => res.extend(from_def_source(db, it)), + hir::ModuleDef::Struct(it) => res.extend(from_def_source(db, it)), + hir::ModuleDef::Union(it) => res.extend(from_def_source(db, it)), + hir::ModuleDef::Enum(it) => res.extend(from_def_source(db, it)), hir::ModuleDef::EnumVariant(it) => { let src = it.source(db); res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label())) } - hir::ModuleDef::Const(it) => { - let src = it.source(db); - res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label())) - } - hir::ModuleDef::Static(it) => { - let src = it.source(db); - res.extend(hover_text(src.ast.doc_comment_text(), src.ast.short_label())) - } + hir::ModuleDef::Const(it) => res.extend(from_def_source(db, it)), + hir::ModuleDef::Static(it) => res.extend(from_def_source(db, it)), hir::ModuleDef::Trait(it) => { let it = it.source(db).1; res.extend(hover_text(it.doc_comment_text(), it.short_label())) @@ -175,24 +149,9 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option { if let Some((adt_def, _)) = ty.as_adt() { match adt_def { - hir::AdtDef::Struct(it) => { - let src = it.source(db); - res.extend(hover_text( - src.ast.doc_comment_text(), - src.ast.short_label(), - )) - } - hir::AdtDef::Union(it) => { - let it = it.source(db).1; - res.extend(hover_text(it.doc_comment_text(), it.short_label())) - } - hir::AdtDef::Enum(it) => { - let src = it.source(db); - res.extend(hover_text( - src.ast.doc_comment_text(), - src.ast.short_label(), - )) - } + hir::AdtDef::Struct(it) => res.extend(from_def_source(db, it)), + hir::AdtDef::Union(it) => res.extend(from_def_source(db, it)), + hir::AdtDef::Enum(it) => res.extend(from_def_source(db, it)), } } } @@ -276,7 +235,16 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option(db: &RootDatabase, def: D) -> Option + where + D: hir::HasSource>, + A: ast::DocCommentsOwner + ast::NameOwner + ShortLabel, + { + let src = def.source(db); + hover_text(src.ast.doc_comment_text(), src.ast.short_label()) + } } pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option { -- cgit v1.2.3