1 use ra_db::SourceDatabase;
3 AstNode, SyntaxNode, TreeArc, ast::{self, NameOwner, VisibilityOwner, TypeAscriptionOwner},
4 algo::{find_covering_node, find_node_at_offset, find_leaf_at_offset, visit::{visitor, Visitor}},
7 use crate::{db::RootDatabase, RangeInfo, FilePosition, FileRange, NavigationTarget};
9 /// Contains the results when hovering over an item
10 #[derive(Debug, Clone)]
11 pub struct HoverResult {
17 pub fn new() -> HoverResult {
20 // We assume exact by default
25 pub fn extend(&mut self, item: Option<String>) {
26 self.results.extend(item);
29 pub fn is_exact(&self) -> bool {
33 pub fn is_empty(&self) -> bool {
34 self.results.is_empty()
37 pub fn len(&self) -> usize {
41 pub fn first(&self) -> Option<&str> {
42 self.results.first().map(String::as_str)
45 pub fn results(&self) -> &[String] {
49 /// Returns the results converted into markup
50 /// for displaying in a UI
51 pub fn to_markup(&self) -> String {
52 let mut markup = if !self.exact {
53 let mut msg = String::from("Failed to exactly resolve the symbol. This is probably because rust_analyzer does not yet support traits.");
54 if !self.results.is_empty() {
55 msg.push_str(" \nThese items were found instead:");
57 msg.push_str("\n\n---\n");
63 markup.push_str(&self.results.join("\n\n---\n"));
69 pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeInfo<HoverResult>> {
70 let file = db.parse(position.file_id);
71 let mut res = HoverResult::new();
74 if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset) {
75 use crate::goto_definition::{ReferenceResult::*, reference_definition};
76 let ref_result = reference_definition(db, position.file_id, name_ref);
78 Exact(nav) => res.extend(doc_text_for(db, nav)),
79 Approximate(navs) => {
80 // We are no longer exact
84 res.extend(doc_text_for(db, nav))
89 range = Some(name_ref.syntax().range())
91 } else if let Some(name) = find_node_at_offset::<ast::Name>(file.syntax(), position.offset) {
92 let navs = crate::goto_definition::name_definition(db, position.file_id, name);
94 if let Some(navs) = navs {
96 res.extend(doc_text_for(db, nav))
100 if !res.is_empty() && range.is_none() {
101 range = Some(name.syntax().range());
106 let node = find_leaf_at_offset(file.syntax(), position.offset).find_map(|leaf| {
107 leaf.ancestors().find(|n| ast::Expr::cast(*n).is_some() || ast::Pat::cast(*n).is_some())
109 let frange = FileRange { file_id: position.file_id, range: node.range() };
110 res.extend(type_of(db, frange).map(rust_code_markup));
111 range = Some(node.range());
118 let res = RangeInfo::new(range, res);
122 pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option<String> {
123 let file = db.parse(frange.file_id);
124 let syntax = file.syntax();
125 let leaf_node = find_covering_node(syntax, frange.range);
126 // if we picked identifier, expand to pattern/expression
129 .take_while(|it| it.range() == leaf_node.range())
130 .find(|&it| ast::Expr::cast(it).is_some() || ast::Pat::cast(it).is_some())
131 .unwrap_or(leaf_node);
132 let parent_fn = node.ancestors().find_map(ast::FnDef::cast)?;
133 let function = hir::source_binder::function_from_source(db, frange.file_id, parent_fn)?;
134 let infer = function.infer(db);
135 let source_map = function.body_source_map(db);
136 if let Some(expr) = ast::Expr::cast(node).and_then(|e| source_map.node_expr(e)) {
137 Some(infer[expr].to_string())
138 } else if let Some(pat) = ast::Pat::cast(node).and_then(|p| source_map.node_pat(p)) {
139 Some(infer[pat].to_string())
145 fn rust_code_markup<CODE: AsRef<str>>(val: CODE) -> String {
146 rust_code_markup_with_doc::<_, &str>(val, None)
149 fn rust_code_markup_with_doc<CODE, DOC>(val: CODE, doc: Option<DOC>) -> String
154 if let Some(doc) = doc {
155 format!("```rust\n{}\n```\n\n{}", val.as_ref(), doc.as_ref())
157 format!("```rust\n{}\n```", val.as_ref())
161 // FIXME: this should not really use navigation target. Rather, approximately
162 // resolved symbol should return a `DefId`.
163 fn doc_text_for(db: &RootDatabase, nav: NavigationTarget) -> Option<String> {
164 match (nav.description(db), nav.docs(db)) {
165 (Some(desc), docs) => Some(rust_code_markup_with_doc(desc, docs)),
166 (None, Some(docs)) => Some(docs),
171 impl NavigationTarget {
172 fn node(&self, db: &RootDatabase) -> Option<TreeArc<SyntaxNode>> {
173 let source_file = db.parse(self.file_id());
174 let source_file = source_file.syntax();
175 let node = source_file
177 .find(|node| node.kind() == self.kind() && node.range() == self.full_range())?
182 fn docs(&self, db: &RootDatabase) -> Option<String> {
183 let node = self.node(db)?;
184 fn doc_comments<N: ast::DocCommentsOwner>(node: &N) -> Option<String> {
185 node.doc_comment_text()
189 .visit(doc_comments::<ast::FnDef>)
190 .visit(doc_comments::<ast::StructDef>)
191 .visit(doc_comments::<ast::EnumDef>)
192 .visit(doc_comments::<ast::TraitDef>)
193 .visit(doc_comments::<ast::Module>)
194 .visit(doc_comments::<ast::TypeAliasDef>)
195 .visit(doc_comments::<ast::ConstDef>)
196 .visit(doc_comments::<ast::StaticDef>)
197 .visit(doc_comments::<ast::NamedFieldDef>)
201 /// Get a description of this node.
203 /// e.g. `struct Name`, `enum Name`, `fn Name`
204 fn description(&self, db: &RootDatabase) -> Option<String> {
205 // TODO: After type inference is done, add type information to improve the output
206 let node = self.node(db)?;
208 fn visit_ascribed_node<T>(node: &T, prefix: &str) -> Option<String>
210 T: NameOwner + VisibilityOwner + TypeAscriptionOwner,
212 let mut string = visit_node(node, prefix)?;
214 if let Some(type_ref) = node.ascribed_type() {
215 string.push_str(": ");
216 type_ref.syntax().text().push_to(&mut string);
222 fn visit_node<T>(node: &T, label: &str) -> Option<String>
224 T: NameOwner + VisibilityOwner,
227 node.visibility().map(|v| format!("{} ", v.syntax().text())).unwrap_or_default();
228 string.push_str(label);
229 node.name()?.syntax().text().push_to(&mut string);
234 .visit(crate::completion::function_label)
235 .visit(|node: &ast::StructDef| visit_node(node, "struct "))
236 .visit(|node: &ast::EnumDef| visit_node(node, "enum "))
237 .visit(|node: &ast::TraitDef| visit_node(node, "trait "))
238 .visit(|node: &ast::Module| visit_node(node, "mod "))
239 .visit(|node: &ast::TypeAliasDef| visit_node(node, "type "))
240 .visit(|node: &ast::ConstDef| visit_ascribed_node(node, "const "))
241 .visit(|node: &ast::StaticDef| visit_ascribed_node(node, "static "))
242 .visit(|node: &ast::NamedFieldDef| visit_ascribed_node(node, ""))
249 use ra_syntax::TextRange;
250 use crate::mock_analysis::{single_file_with_position, single_file_with_range, analysis_and_position};
252 fn trim_markup(s: &str) -> &str {
253 s.trim_start_matches("```rust\n").trim_end_matches("\n```")
256 fn trim_markup_opt(s: Option<&str>) -> Option<&str> {
260 fn check_hover_result(fixture: &str, expected: &[&str]) {
261 let (analysis, position) = analysis_and_position(fixture);
262 let hover = analysis.hover(position).unwrap().unwrap();
264 for (markup, expected) in
265 hover.info.results().iter().zip(expected.iter().chain(std::iter::repeat(&"<missing>")))
267 assert_eq!(trim_markup(&markup), *expected);
270 assert_eq!(hover.info.len(), expected.len());
274 fn hover_shows_type_of_an_expression() {
275 let (analysis, position) = single_file_with_position(
277 pub fn foo() -> u32 { 1 }
280 let foo_test = foo()<|>;
284 let hover = analysis.hover(position).unwrap().unwrap();
285 assert_eq!(hover.range, TextRange::from_to(95.into(), 100.into()));
286 assert_eq!(trim_markup_opt(hover.info.first()), Some("u32"));
290 fn hover_shows_fn_signature() {
291 // Single file with result
295 pub fn foo() -> u32 { 1 }
298 let foo_test = fo<|>o();
301 &["pub fn foo() -> u32"],
308 pub fn foo() -> u32 { 1 }
311 pub fn foo() -> &str { "" }
314 pub fn foo(a: u32, b: u32) {}
322 let foo_test = fo<|>o();
325 &["pub fn foo() -> &str", "pub fn foo() -> u32", "pub fn foo(a: u32, b: u32)"],
330 fn hover_shows_fn_signature_with_type_params() {
334 pub fn foo<'a, T: AsRef<str>>(b: &'a T) -> &'a str { }
337 let foo_test = fo<|>o();
340 &["pub fn foo<'a, T: AsRef<str>>(b: &'a T) -> &'a str"],
345 fn hover_shows_fn_signature_on_fn_name() {
349 pub fn foo<|>(a: u32, b: u32) -> u32 {}
354 &["pub fn foo(a: u32, b: u32) -> u32"],
359 fn hover_shows_struct_field_info() {
360 // Hovering over the field when instantiating
377 // Hovering over the field in the definition
396 fn hover_const_static() {
401 const foo<|>: u32 = 0;
411 static foo<|>: u32 = 0;
414 &["static foo: u32"],
420 let (analysis, position) = single_file_with_position(
422 enum Option<T> { Some(T) }
430 let hover = analysis.hover(position).unwrap().unwrap();
431 // not the nicest way to show it currently
432 assert_eq!(trim_markup_opt(hover.info.first()), Some("Some<i32>(T) -> Option<T>"));
436 fn hover_for_local_variable() {
437 let (analysis, position) = single_file_with_position("fn func(foo: i32) { fo<|>o; }");
438 let hover = analysis.hover(position).unwrap().unwrap();
439 assert_eq!(trim_markup_opt(hover.info.first()), Some("i32"));
443 fn hover_for_local_variable_pat() {
444 let (analysis, position) = single_file_with_position("fn func(fo<|>o: i32) {}");
445 let hover = analysis.hover(position).unwrap().unwrap();
446 assert_eq!(trim_markup_opt(hover.info.first()), Some("i32"));
450 fn test_type_of_for_function() {
451 let (analysis, range) = single_file_with_range(
453 pub fn foo() -> u32 { 1 };
456 let foo_test = <|>foo()<|>;
461 let type_name = analysis.type_of(range).unwrap().unwrap();
462 assert_eq!("u32", &type_name);
465 // FIXME: improve type_of to make this work
467 fn test_type_of_for_expr_1() {
468 let (analysis, range) = single_file_with_range(
471 let foo = <|>1 + foo_test<|>;
476 let type_name = analysis.type_of(range).unwrap().unwrap();
477 assert_eq!("{unknown}", &type_name);
481 fn test_type_of_for_expr_2() {
482 let (analysis, range) = single_file_with_range(
486 let bar = <|>1 + foo<|>;
491 let type_name = analysis.type_of(range).unwrap().unwrap();
492 assert_eq!("usize", &type_name);
496 fn test_hover_infer_associated_method_result() {
497 let (analysis, position) = single_file_with_position(
499 struct Thing { x: u32 };
508 let foo_<|>test = Thing::new();
512 let hover = analysis.hover(position).unwrap().unwrap();
513 assert_eq!(trim_markup_opt(hover.info.first()), Some("Thing"));
517 fn test_hover_infer_associated_method_exact() {
518 let (analysis, position) = single_file_with_position(
520 struct Thing { x: u32 }
529 let foo_test = Thing::new<|>();
533 let hover = analysis.hover(position).unwrap().unwrap();
534 assert_eq!(trim_markup_opt(hover.info.first()), Some("fn new() -> Thing"));
535 assert_eq!(hover.info.is_exact(), true);
539 fn test_hover_infer_associated_const_in_pattern() {
540 let (analysis, position) = single_file_with_position(
556 let hover = analysis.hover(position).unwrap().unwrap();
557 assert_eq!(trim_markup_opt(hover.info.first()), Some("const C: u32"));
558 assert_eq!(hover.info.is_exact(), true);