From d46278d320fbc580db81a60be26d8efa6b024433 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 8 Jun 2019 22:26:27 +0800 Subject: Remove node function in NavTarget --- crates/ra_ide_api/src/display.rs | 5 +- crates/ra_ide_api/src/display/navigation_target.rs | 149 +++++++++++---------- crates/ra_ide_api/src/goto_definition.rs | 4 +- crates/ra_ide_api/src/hover.rs | 6 +- crates/ra_ide_api/src/lib.rs | 2 +- 5 files changed, 86 insertions(+), 80 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide_api/src/display.rs b/crates/ra_ide_api/src/display.rs index 1b06abf94..0eef11464 100644 --- a/crates/ra_ide_api/src/display.rs +++ b/crates/ra_ide_api/src/display.rs @@ -5,7 +5,6 @@ mod function_signature; mod navigation_target; mod structure; -use crate::db::RootDatabase; use ra_syntax::{ast::{self, AstNode, TypeParamsOwner}, SyntaxKind::{ATTR, COMMENT}}; pub use navigation_target::NavigationTarget; @@ -73,8 +72,8 @@ where // FIXME: this should not really use navigation target. Rather, approximately // resolved symbol should return a `DefId`. -pub(crate) fn doc_text_for(db: &RootDatabase, nav: NavigationTarget) -> Option { - match (nav.description(db), nav.docs(db)) { +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), _ => None, diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index 45002d098..f8a7bd3ad 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -1,6 +1,6 @@ use ra_db::{FileId, SourceDatabase}; use ra_syntax::{ - SyntaxNode, AstNode, SmolStr, TextRange, TreeArc, AstPtr, + SyntaxNode, AstNode, SmolStr, TextRange, AstPtr, SyntaxKind::{self, NAME}, ast::{self, NameOwner, VisibilityOwner, TypeAscriptionOwner}, algo::visit::{visitor, Visitor}, @@ -22,6 +22,9 @@ pub struct NavigationTarget { full_range: TextRange, focus_range: Option, container_name: Option, + + pub(crate) description: Option, + pub(crate) docs: Option, } impl NavigationTarget { @@ -63,7 +66,10 @@ impl NavigationTarget { NavigationTarget::from_named(file_id, pat) } - pub(crate) fn from_symbol(symbol: FileSymbol) -> NavigationTarget { + pub(crate) fn from_symbol(db: &RootDatabase, symbol: FileSymbol) -> NavigationTarget { + let file = db.parse(symbol.file_id).tree; + let node = symbol.ptr.to_node(file.syntax()).to_owned(); + NavigationTarget { file_id: symbol.file_id, name: symbol.name.clone(), @@ -71,6 +77,8 @@ impl NavigationTarget { full_range: symbol.ptr.range(), focus_range: symbol.name_range, container_name: symbol.container_name.clone(), + description: description_inner(&node), + docs: docs_inner(&node), } } @@ -84,6 +92,8 @@ impl NavigationTarget { ast::PatKind::BindPat(pat) => return NavigationTarget::from_bind_pat(file_id, &pat), _ => ("_".into(), pat.syntax_node_ptr().range()), }; + let node = pat.to_node(file.syntax()).syntax().to_owned(); + NavigationTarget { file_id, name, @@ -91,14 +101,20 @@ impl NavigationTarget { focus_range: None, kind: NAME, container_name: None, + description: description_inner(&node), + docs: docs_inner(&node), } } pub(crate) fn from_self_param( + db: &RootDatabase, file_id: FileId, par: AstPtr, ) -> NavigationTarget { let (name, full_range) = ("self".into(), par.syntax_node_ptr().range()); + let file = db.parse(file_id).tree; + let node = par.to_node(file.syntax()).syntax().to_owned(); + NavigationTarget { file_id, name, @@ -106,6 +122,8 @@ impl NavigationTarget { focus_range: None, kind: NAME, container_name: None, + description: description_inner(&node), + docs: docs_inner(&node), } } @@ -290,83 +308,72 @@ impl NavigationTarget { focus_range, // ptr: Some(LocalSyntaxPtr::new(node)), container_name: None, + description: description_inner(node), + docs: docs_inner(node), } } +} - pub(crate) fn node(&self, db: &RootDatabase) -> Option> { - let source_file = db.parse(self.file_id()).tree; - let source_file = source_file.syntax(); - let node = source_file - .descendants() - .find(|node| node.kind() == self.kind() && node.range() == self.full_range())? - .to_owned(); - Some(node) - } - - pub(crate) fn docs(&self, db: &RootDatabase) -> Option { - let node = self.node(db)?; - fn doc_comments(node: &N) -> Option { - node.doc_comment_text() - } - - visitor() - .visit(doc_comments::) - .visit(doc_comments::) - .visit(doc_comments::) - .visit(doc_comments::) - .visit(doc_comments::) - .visit(doc_comments::) - .visit(doc_comments::) - .visit(doc_comments::) - .visit(doc_comments::) - .visit(doc_comments::) - .visit(doc_comments::) - .accept(&node)? +fn docs_inner(node: &SyntaxNode) -> Option { + fn doc_comments(node: &N) -> Option { + node.doc_comment_text() } - /// Get a description of this node. - /// - /// e.g. `struct Name`, `enum Name`, `fn Name` - pub(crate) fn description(&self, db: &RootDatabase) -> Option { - // FIXME: After type inference is done, add type information to improve the output - let node = self.node(db)?; - - fn visit_ascribed_node(node: &T, prefix: &str) -> Option - where - T: NameOwner + VisibilityOwner + TypeAscriptionOwner, - { - let mut string = visit_node(node, prefix)?; - - if let Some(type_ref) = node.ascribed_type() { - string.push_str(": "); - type_ref.syntax().text().push_to(&mut string); - } + visitor() + .visit(doc_comments::) + .visit(doc_comments::) + .visit(doc_comments::) + .visit(doc_comments::) + .visit(doc_comments::) + .visit(doc_comments::) + .visit(doc_comments::) + .visit(doc_comments::) + .visit(doc_comments::) + .visit(doc_comments::) + .visit(doc_comments::) + .accept(&node)? +} - Some(string) +/// Get a description of this node. +/// +/// e.g. `struct Name`, `enum Name`, `fn Name` +fn description_inner(node: &SyntaxNode) -> Option { + // FIXME: After type inference is done, add type information to improve the output + fn visit_ascribed_node(node: &T, prefix: &str) -> Option + where + T: NameOwner + VisibilityOwner + TypeAscriptionOwner, + { + let mut string = visit_node(node, prefix)?; + + if let Some(type_ref) = node.ascribed_type() { + string.push_str(": "); + type_ref.syntax().text().push_to(&mut string); } - fn visit_node(node: &T, label: &str) -> Option - where - T: NameOwner + VisibilityOwner, - { - let mut string = - node.visibility().map(|v| format!("{} ", v.syntax().text())).unwrap_or_default(); - string.push_str(label); - string.push_str(node.name()?.text().as_str()); - Some(string) - } + Some(string) + } - visitor() - .visit(|node: &ast::FnDef| Some(crate::display::function_label(node))) - .visit(|node: &ast::StructDef| visit_node(node, "struct ")) - .visit(|node: &ast::EnumDef| visit_node(node, "enum ")) - .visit(|node: &ast::TraitDef| visit_node(node, "trait ")) - .visit(|node: &ast::Module| visit_node(node, "mod ")) - .visit(|node: &ast::TypeAliasDef| visit_node(node, "type ")) - .visit(|node: &ast::ConstDef| visit_ascribed_node(node, "const ")) - .visit(|node: &ast::StaticDef| visit_ascribed_node(node, "static ")) - .visit(|node: &ast::NamedFieldDef| visit_ascribed_node(node, "")) - .visit(|node: &ast::EnumVariant| Some(node.name()?.text().to_string())) - .accept(&node)? + fn visit_node(node: &T, label: &str) -> Option + where + T: NameOwner + VisibilityOwner, + { + let mut string = + node.visibility().map(|v| format!("{} ", v.syntax().text())).unwrap_or_default(); + string.push_str(label); + string.push_str(node.name()?.text().as_str()); + Some(string) } + + visitor() + .visit(|node: &ast::FnDef| Some(crate::display::function_label(node))) + .visit(|node: &ast::StructDef| visit_node(node, "struct ")) + .visit(|node: &ast::EnumDef| visit_node(node, "enum ")) + .visit(|node: &ast::TraitDef| visit_node(node, "trait ")) + .visit(|node: &ast::Module| visit_node(node, "mod ")) + .visit(|node: &ast::TypeAliasDef| visit_node(node, "type ")) + .visit(|node: &ast::ConstDef| visit_ascribed_node(node, "const ")) + .visit(|node: &ast::StaticDef| visit_ascribed_node(node, "static ")) + .visit(|node: &ast::NamedFieldDef| visit_ascribed_node(node, "")) + .visit(|node: &ast::EnumVariant| Some(node.name()?.text().to_string())) + .accept(&node)? } diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index e72b7a6e7..31b6679ae 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -72,7 +72,7 @@ pub(crate) fn reference_definition( } } Some(Pat(pat)) => return Exact(NavigationTarget::from_pat(db, file_id, pat)), - Some(SelfParam(par)) => return Exact(NavigationTarget::from_self_param(file_id, par)), + Some(SelfParam(par)) => return Exact(NavigationTarget::from_self_param(db, file_id, par)), Some(GenericParam(_)) => { // FIXME: go to the generic param def } @@ -82,7 +82,7 @@ pub(crate) fn reference_definition( // Fallback index based approach: let navs = crate::symbol_index::index_resolve(db, name_ref) .into_iter() - .map(NavigationTarget::from_symbol) + .map(|s| NavigationTarget::from_symbol(db, s)) .collect(); Approximate(navs) } diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index f56965ef5..cb676eb12 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs @@ -86,13 +86,13 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option res.extend(doc_text_for(db, nav)), + Exact(nav) => res.extend(doc_text_for(nav)), Approximate(navs) => { // We are no longer exact res.exact = false; for nav in navs { - res.extend(doc_text_for(db, nav)) + res.extend(doc_text_for(nav)) } } } @@ -104,7 +104,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option>() }) } -- cgit v1.2.3