From 2d7b4cc548a5b72c8b268a774444a59dd1ce669e Mon Sep 17 00:00:00 2001 From: Jeremy Kolb Date: Sun, 13 Jan 2019 20:08:33 -0500 Subject: Add visibility to hover --- crates/ra_ide_api/src/hover.rs | 50 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 9 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 c445d4fa1..4dafefa2c 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs @@ -1,7 +1,7 @@ use ra_db::{Cancelable, SyntaxDatabase}; use ra_syntax::{ AstNode, SyntaxNode, TreeArc, - ast::{self, NameOwner}, + ast::{self, NameOwner, VisibilityOwner}, algo::{find_covering_node, find_node_at_offset, find_leaf_at_offset, visit::{visitor, Visitor}}, }; @@ -140,42 +140,74 @@ impl NavigationTarget { // TODO: Refactor to be have less repetition visitor() .visit(|node: &ast::FnDef| { - let mut string = "fn ".to_string(); + let mut string = node + .visibility() + .map(|v| format!("{} ", v.syntax().text())) + .unwrap_or_default(); + string.push_str("fn "); node.name()?.syntax().text().push_to(&mut string); Some(string) }) .visit(|node: &ast::StructDef| { - let mut string = "struct ".to_string(); + let mut string = node + .visibility() + .map(|v| format!("{} ", v.syntax().text())) + .unwrap_or_default(); + string.push_str("struct "); node.name()?.syntax().text().push_to(&mut string); Some(string) }) .visit(|node: &ast::EnumDef| { - let mut string = "enum ".to_string(); + let mut string = node + .visibility() + .map(|v| format!("{} ", v.syntax().text())) + .unwrap_or_default(); + string.push_str("enum "); node.name()?.syntax().text().push_to(&mut string); Some(string) }) .visit(|node: &ast::TraitDef| { - let mut string = "trait ".to_string(); + let mut string = node + .visibility() + .map(|v| format!("{} ", v.syntax().text())) + .unwrap_or_default(); + string.push_str("trait "); node.name()?.syntax().text().push_to(&mut string); Some(string) }) .visit(|node: &ast::Module| { - let mut string = "mod ".to_string(); + let mut string = node + .visibility() + .map(|v| format!("{} ", v.syntax().text())) + .unwrap_or_default(); + string.push_str("mod "); node.name()?.syntax().text().push_to(&mut string); Some(string) }) .visit(|node: &ast::TypeDef| { - let mut string = "type ".to_string(); + let mut string = node + .visibility() + .map(|v| format!("{} ", v.syntax().text())) + .unwrap_or_default(); + string.push_str("type "); node.name()?.syntax().text().push_to(&mut string); Some(string) }) .visit(|node: &ast::ConstDef| { - let mut string = "const ".to_string(); + let mut string = node + .visibility() + .map(|v| format!("{} ", v.syntax().text())) + .unwrap_or_default(); + string.push_str("const "); node.name()?.syntax().text().push_to(&mut string); Some(string) }) .visit(|node: &ast::StaticDef| { - let mut string = "static ".to_string(); + let mut string = node + .visibility() + .map(|v| format!("{} ", v.syntax().text())) + .unwrap_or_default(); + string.push_str("static "); node.name()?.syntax().text().push_to(&mut string); Some(string) }) -- cgit v1.2.3 From da326dca6005fa23fa6f8524bde1abd3a156c0c7 Mon Sep 17 00:00:00 2001 From: Jeremy Kolb Date: Sun, 13 Jan 2019 20:25:14 -0500 Subject: Remove duplication --- crates/ra_ide_api/src/hover.rs | 97 ++++++++++-------------------------------- 1 file changed, 23 insertions(+), 74 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 4dafefa2c..d73c5bc31 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs @@ -1,7 +1,7 @@ use ra_db::{Cancelable, SyntaxDatabase}; use ra_syntax::{ AstNode, SyntaxNode, TreeArc, - ast::{self, NameOwner, VisibilityOwner}, + ast::self, algo::{find_covering_node, find_node_at_offset, find_leaf_at_offset, visit::{visitor, Visitor}}, }; @@ -137,80 +137,29 @@ impl NavigationTarget { fn description(&self, db: &RootDatabase) -> Option { // TODO: After type inference is done, add type information to improve the output let node = self.node(db)?; - // TODO: Refactor to be have less repetition + + fn visit_node(node: &T, label: &str) -> Option + where + T: ast::NameOwner + ast::VisibilityOwner, + { + let mut string = node + .visibility() + .map(|v| format!("{} ", v.syntax().text())) + .unwrap_or_default(); + string.push_str(label); + node.name()?.syntax().text().push_to(&mut string); + Some(string) + } + visitor() - .visit(|node: &ast::FnDef| { - let mut string = node - .visibility() - .map(|v| format!("{} ", v.syntax().text())) - .unwrap_or_default(); - string.push_str("fn "); - node.name()?.syntax().text().push_to(&mut string); - Some(string) - }) - .visit(|node: &ast::StructDef| { - let mut string = node - .visibility() - .map(|v| format!("{} ", v.syntax().text())) - .unwrap_or_default(); - string.push_str("struct "); - node.name()?.syntax().text().push_to(&mut string); - Some(string) - }) - .visit(|node: &ast::EnumDef| { - let mut string = node - .visibility() - .map(|v| format!("{} ", v.syntax().text())) - .unwrap_or_default(); - string.push_str("enum "); - node.name()?.syntax().text().push_to(&mut string); - Some(string) - }) - .visit(|node: &ast::TraitDef| { - let mut string = node - .visibility() - .map(|v| format!("{} ", v.syntax().text())) - .unwrap_or_default(); - string.push_str("trait "); - node.name()?.syntax().text().push_to(&mut string); - Some(string) - }) - .visit(|node: &ast::Module| { - let mut string = node - .visibility() - .map(|v| format!("{} ", v.syntax().text())) - .unwrap_or_default(); - string.push_str("mod "); - node.name()?.syntax().text().push_to(&mut string); - Some(string) - }) - .visit(|node: &ast::TypeDef| { - let mut string = node - .visibility() - .map(|v| format!("{} ", v.syntax().text())) - .unwrap_or_default(); - string.push_str("type "); - node.name()?.syntax().text().push_to(&mut string); - Some(string) - }) - .visit(|node: &ast::ConstDef| { - let mut string = node - .visibility() - .map(|v| format!("{} ", v.syntax().text())) - .unwrap_or_default(); - string.push_str("const "); - node.name()?.syntax().text().push_to(&mut string); - Some(string) - }) - .visit(|node: &ast::StaticDef| { - let mut string = node - .visibility() - .map(|v| format!("{} ", v.syntax().text())) - .unwrap_or_default(); - string.push_str("static "); - node.name()?.syntax().text().push_to(&mut string); - Some(string) - }) + .visit(|node: &ast::FnDef| visit_node(node, "fn ")) + .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::TypeDef| visit_node(node, "type ")) + .visit(|node: &ast::ConstDef| visit_node(node, "const ")) + .visit(|node: &ast::StaticDef| visit_node(node, "static ")) .accept(&node)? } } -- cgit v1.2.3