From c879f43186a30bd5ab6886600bd6fac8aff035b2 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Wed, 27 Feb 2019 17:52:37 +0200 Subject: Enable hover support for NamedFieldDefs Additionally add type ascription for const and statics as well. --- crates/ra_ide_api/src/hover.rs | 82 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 3 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index ef3b5df29..47913d753 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs @@ -1,6 +1,6 @@ use ra_db::SourceDatabase; use ra_syntax::{ - AstNode, SyntaxNode, TreeArc, ast::{self, NameOwner, VisibilityOwner}, + AstNode, SyntaxNode, TreeArc, ast::{self, NameOwner, VisibilityOwner, TypeAscriptionOwner}, algo::{find_covering_node, find_node_at_offset, find_leaf_at_offset, visit::{visitor, Visitor}}, }; @@ -179,6 +179,7 @@ impl NavigationTarget { .visit(doc_comments::) .visit(doc_comments::) .visit(doc_comments::) + .visit(doc_comments::) .accept(&node)? } @@ -189,6 +190,20 @@ impl NavigationTarget { // TODO: 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); + } + + Some(string) + } + fn visit_node(node: &T, label: &str) -> Option where T: NameOwner + VisibilityOwner, @@ -207,8 +222,9 @@ impl NavigationTarget { .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_node(node, "const ")) - .visit(|node: &ast::StaticDef| visit_node(node, "static ")) + .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, "")) .accept(&node)? } } @@ -320,6 +336,66 @@ mod tests { ); } + #[test] + fn hover_shows_struct_field_info() { + // Hovering over the field when instantiating + check_hover_result( + r#" + //- /main.rs + struct Foo { + field_a: u32, + } + + fn main() { + let foo = Foo { + field_a<|>: 0, + }; + } + "#, + &["field_a: u32"], + ); + + // Hovering over the field in the definition + check_hover_result( + r#" + //- /main.rs + struct Foo { + field_a<|>: u32, + } + + fn main() { + let foo = Foo { + field_a: 0, + }; + } + "#, + &["field_a: u32"], + ); + } + + #[test] + fn hover_const_static() { + check_hover_result( + r#" + //- /main.rs + fn main() { + const foo<|>: u32 = 0; + } + "#, + &["const foo: u32"], + ); + + check_hover_result( + r#" + //- /main.rs + fn main() { + static foo<|>: u32 = 0; + } + "#, + &["static foo: u32"], + ); + } + #[test] fn hover_some() { let (analysis, position) = single_file_with_position( -- cgit v1.2.3