From 656a0fa9f99298123d7dcee8c65a8a5ed7043bc4 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 11 Jun 2019 01:18:32 +0800 Subject: Simpliy hover on ast::name --- crates/ra_ide_api/src/display.rs | 10 ---------- crates/ra_ide_api/src/hover.rs | 42 +++++++++++++++++++++++++++++++++------- 2 files changed, 35 insertions(+), 17 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/display.rs b/crates/ra_ide_api/src/display.rs index b81f4db0c..882518838 100644 --- a/crates/ra_ide_api/src/display.rs +++ b/crates/ra_ide_api/src/display.rs @@ -73,13 +73,3 @@ where format!("```rust\n{}\n```", val.as_ref()) } } - -// FIXME: this should not really use navigation target. Rather, approximately -// resolved symbol should return a `DefId`. -pub(crate) fn doc_text_for(nav: NavigationTarget) -> Option { - match (nav.description(), nav.docs()) { - (Some(desc), docs) => Some(rust_code_markup_with_doc(desc, docs)), - (None, Some(docs)) => Some(docs.to_string()), - _ => None, - } -} diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index 1f454be21..fbabeb194 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs @@ -1,14 +1,14 @@ use ra_db::SourceDatabase; use ra_syntax::{ AstNode, ast::{self, DocCommentsOwner}, - algo::{find_covering_element, find_node_at_offset, ancestors_at_offset}, + algo::{find_covering_element, find_node_at_offset, ancestors_at_offset, visit::{visitor, Visitor}}, }; use hir::HirDisplay; use crate::{ db::RootDatabase, RangeInfo, FilePosition, FileRange, - display::{rust_code_markup, doc_text_for, rust_code_markup_with_doc, ShortLabel, docs_from_symbol, description_from_symbol}, + display::{rust_code_markup, rust_code_markup_with_doc, ShortLabel, docs_from_symbol, description_from_symbol}, name_ref_kind::{NameRefKind::*, classify_name_ref}, }; @@ -216,11 +216,39 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option(file.syntax(), position.offset) { - let navs = crate::goto_definition::name_definition(db, position.file_id, name); - - if let Some(navs) = navs { - for nav in navs { - res.extend(doc_text_for(nav)) + if let Some(parent) = name.syntax().parent() { + let text = visitor() + .visit(|node: &ast::StructDef| { + hover_text(node.doc_comment_text(), node.short_label()) + }) + .visit(|node: &ast::EnumDef| { + hover_text(node.doc_comment_text(), node.short_label()) + }) + .visit(|node: &ast::EnumVariant| { + hover_text(node.doc_comment_text(), node.short_label()) + }) + .visit(|node: &ast::FnDef| hover_text(node.doc_comment_text(), node.short_label())) + .visit(|node: &ast::TypeAliasDef| { + hover_text(node.doc_comment_text(), node.short_label()) + }) + .visit(|node: &ast::ConstDef| { + hover_text(node.doc_comment_text(), node.short_label()) + }) + .visit(|node: &ast::StaticDef| { + hover_text(node.doc_comment_text(), node.short_label()) + }) + .visit(|node: &ast::TraitDef| { + hover_text(node.doc_comment_text(), node.short_label()) + }) + .visit(|node: &ast::NamedFieldDef| { + hover_text(node.doc_comment_text(), node.short_label()) + }) + .visit(|node: &ast::Module| hover_text(node.doc_comment_text(), node.short_label())) + .visit(|node: &ast::MacroCall| hover_text(node.doc_comment_text(), None)) + .accept(parent); + + if let Some(text) = text { + res.extend(text); } } -- cgit v1.2.3