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(-) 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 From 2acf1e16fc89c51725a1bd6aafe8dfe4b9d19b1f Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 9 Jun 2019 03:27:01 +0800 Subject: Construct doc individually --- crates/ra_ide_api/src/display/navigation_target.rs | 140 ++++++++++++++++----- crates/ra_ide_api/src/goto_definition.rs | 46 +++++-- 2 files changed, 141 insertions(+), 45 deletions(-) diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index f8a7bd3ad..b2a925e2a 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -2,7 +2,7 @@ use ra_db::{FileId, SourceDatabase}; use ra_syntax::{ SyntaxNode, AstNode, SmolStr, TextRange, AstPtr, SyntaxKind::{self, NAME}, - ast::{self, NameOwner, VisibilityOwner, TypeAscriptionOwner}, + ast::{self, NameOwner, VisibilityOwner, TypeAscriptionOwner, DocCommentsOwner}, algo::visit::{visitor, Visitor}, }; use hir::{ModuleSource, FieldSource, ImplItem}; @@ -63,7 +63,7 @@ impl NavigationTarget { } pub(crate) fn from_bind_pat(file_id: FileId, pat: &ast::BindPat) -> NavigationTarget { - NavigationTarget::from_named(file_id, pat) + NavigationTarget::from_named(file_id, pat, None) } pub(crate) fn from_symbol(db: &RootDatabase, symbol: FileSymbol) -> NavigationTarget { @@ -78,7 +78,7 @@ impl NavigationTarget { focus_range: symbol.name_range, container_name: symbol.container_name.clone(), description: description_inner(&node), - docs: docs_inner(&node), + docs: docs_from_symbol(db, &symbol), } } @@ -102,7 +102,7 @@ impl NavigationTarget { kind: NAME, container_name: None, description: description_inner(&node), - docs: docs_inner(&node), + docs: None, //< No documentation for Pattern } } @@ -113,7 +113,8 @@ impl NavigationTarget { ) -> 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(); + let ast = par.to_node(file.syntax()); + let node = ast.syntax().to_owned(); NavigationTarget { file_id, @@ -123,7 +124,7 @@ impl NavigationTarget { kind: NAME, container_name: None, description: description_inner(&node), - docs: docs_inner(&node), + docs: None, //< No document node for SelfParam } } @@ -133,11 +134,15 @@ impl NavigationTarget { let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); match source { ModuleSource::SourceFile(node) => { - NavigationTarget::from_syntax(file_id, name, None, node.syntax()) - } - ModuleSource::Module(node) => { - NavigationTarget::from_syntax(file_id, name, None, node.syntax()) + NavigationTarget::from_syntax(file_id, name, None, node.syntax(), None) } + ModuleSource::Module(node) => NavigationTarget::from_syntax( + file_id, + name, + None, + node.syntax(), + node.doc_comment_text(), + ), } } @@ -145,23 +150,31 @@ impl NavigationTarget { let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); if let Some((file_id, source)) = module.declaration_source(db) { let file_id = file_id.as_original_file(); - return NavigationTarget::from_syntax(file_id, name, None, source.syntax()); + return NavigationTarget::from_syntax( + file_id, + name, + None, + source.syntax(), + source.doc_comment_text(), + ); } NavigationTarget::from_module(db, module) } pub(crate) fn from_function(db: &RootDatabase, func: hir::Function) -> NavigationTarget { let (file_id, fn_def) = func.source(db); - NavigationTarget::from_named(file_id.original_file(db), &*fn_def) + NavigationTarget::from_named(file_id.original_file(db), &*fn_def, fn_def.doc_comment_text()) } pub(crate) fn from_field(db: &RootDatabase, field: hir::StructField) -> NavigationTarget { let (file_id, field) = field.source(db); let file_id = file_id.original_file(db); match field { - FieldSource::Named(it) => NavigationTarget::from_named(file_id, &*it), + FieldSource::Named(it) => { + NavigationTarget::from_named(file_id, &*it, it.doc_comment_text()) + } FieldSource::Pos(it) => { - NavigationTarget::from_syntax(file_id, "".into(), None, it.syntax()) + NavigationTarget::from_syntax(file_id, "".into(), None, it.syntax(), None) } } } @@ -170,15 +183,27 @@ impl NavigationTarget { match adt_def { hir::AdtDef::Struct(s) => { let (file_id, node) = s.source(db); - NavigationTarget::from_named(file_id.original_file(db), &*node) + NavigationTarget::from_named( + file_id.original_file(db), + &*node, + node.doc_comment_text(), + ) } hir::AdtDef::Union(s) => { let (file_id, node) = s.source(db); - NavigationTarget::from_named(file_id.original_file(db), &*node) + NavigationTarget::from_named( + file_id.original_file(db), + &*node, + node.doc_comment_text(), + ) } hir::AdtDef::Enum(s) => { let (file_id, node) = s.source(db); - NavigationTarget::from_named(file_id.original_file(db), &*node) + NavigationTarget::from_named( + file_id.original_file(db), + &*node, + node.doc_comment_text(), + ) } } } @@ -192,35 +217,67 @@ impl NavigationTarget { hir::ModuleDef::Function(func) => NavigationTarget::from_function(db, func), hir::ModuleDef::Struct(s) => { let (file_id, node) = s.source(db); - NavigationTarget::from_named(file_id.original_file(db), &*node) + NavigationTarget::from_named( + file_id.original_file(db), + &*node, + node.doc_comment_text(), + ) } hir::ModuleDef::Union(s) => { let (file_id, node) = s.source(db); - NavigationTarget::from_named(file_id.original_file(db), &*node) + NavigationTarget::from_named( + file_id.original_file(db), + &*node, + node.doc_comment_text(), + ) } hir::ModuleDef::Const(s) => { let (file_id, node) = s.source(db); - NavigationTarget::from_named(file_id.original_file(db), &*node) + NavigationTarget::from_named( + file_id.original_file(db), + &*node, + node.doc_comment_text(), + ) } hir::ModuleDef::Static(s) => { let (file_id, node) = s.source(db); - NavigationTarget::from_named(file_id.original_file(db), &*node) + NavigationTarget::from_named( + file_id.original_file(db), + &*node, + node.doc_comment_text(), + ) } hir::ModuleDef::Enum(e) => { let (file_id, node) = e.source(db); - NavigationTarget::from_named(file_id.original_file(db), &*node) + NavigationTarget::from_named( + file_id.original_file(db), + &*node, + node.doc_comment_text(), + ) } hir::ModuleDef::EnumVariant(var) => { let (file_id, node) = var.source(db); - NavigationTarget::from_named(file_id.original_file(db), &*node) + NavigationTarget::from_named( + file_id.original_file(db), + &*node, + node.doc_comment_text(), + ) } hir::ModuleDef::Trait(e) => { let (file_id, node) = e.source(db); - NavigationTarget::from_named(file_id.original_file(db), &*node) + NavigationTarget::from_named( + file_id.original_file(db), + &*node, + node.doc_comment_text(), + ) } hir::ModuleDef::TypeAlias(e) => { let (file_id, node) = e.source(db); - NavigationTarget::from_named(file_id.original_file(db), &*node) + NavigationTarget::from_named( + file_id.original_file(db), + &*node, + node.doc_comment_text(), + ) } hir::ModuleDef::BuiltinType(..) => { return None; @@ -239,6 +296,7 @@ impl NavigationTarget { "impl".into(), None, node.syntax(), + None, ) } @@ -247,11 +305,19 @@ impl NavigationTarget { ImplItem::Method(f) => NavigationTarget::from_function(db, f), ImplItem::Const(c) => { let (file_id, node) = c.source(db); - NavigationTarget::from_named(file_id.original_file(db), &*node) + NavigationTarget::from_named( + file_id.original_file(db), + &*node, + node.doc_comment_text(), + ) } ImplItem::TypeAlias(a) => { let (file_id, node) = a.source(db); - NavigationTarget::from_named(file_id.original_file(db), &*node) + NavigationTarget::from_named( + file_id.original_file(db), + &*node, + node.doc_comment_text(), + ) } } } @@ -259,7 +325,7 @@ impl NavigationTarget { pub(crate) fn from_macro_def(db: &RootDatabase, macro_call: hir::MacroDef) -> NavigationTarget { let (file_id, node) = macro_call.source(db); log::debug!("nav target {}", node.syntax().debug_dump()); - NavigationTarget::from_named(file_id.original_file(db), &*node) + NavigationTarget::from_named(file_id.original_file(db), &*node, node.doc_comment_text()) } #[cfg(test)] @@ -287,11 +353,15 @@ impl NavigationTarget { } /// Allows `NavigationTarget` to be created from a `NameOwner` - pub(crate) fn from_named(file_id: FileId, node: &impl ast::NameOwner) -> NavigationTarget { + pub(crate) fn from_named( + file_id: FileId, + node: &impl ast::NameOwner, + docs: Option, + ) -> NavigationTarget { //FIXME: use `_` instead of empty string let name = node.name().map(|it| it.text().clone()).unwrap_or_default(); let focus_range = node.name().map(|it| it.syntax().range()); - NavigationTarget::from_syntax(file_id, name, focus_range, node.syntax()) + NavigationTarget::from_syntax(file_id, name, focus_range, node.syntax(), docs) } fn from_syntax( @@ -299,6 +369,7 @@ impl NavigationTarget { name: SmolStr, focus_range: Option, node: &SyntaxNode, + docs: Option, ) -> NavigationTarget { NavigationTarget { file_id, @@ -309,12 +380,15 @@ impl NavigationTarget { // ptr: Some(LocalSyntaxPtr::new(node)), container_name: None, description: description_inner(node), - docs: docs_inner(node), + docs, } } } -fn docs_inner(node: &SyntaxNode) -> Option { +fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option { + let file = db.parse(symbol.file_id).tree; + let node = symbol.ptr.to_node(file.syntax()).to_owned(); + fn doc_comments(node: &N) -> Option { node.doc_comment_text() } @@ -334,7 +408,7 @@ fn docs_inner(node: &SyntaxNode) -> Option { .accept(&node)? } -/// Get a description of this node. +/// Get a description of a node. /// /// e.g. `struct Name`, `enum Name`, `fn Name` fn description_inner(node: &SyntaxNode) -> Option { diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index 31b6679ae..fbd881bfe 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -1,6 +1,6 @@ use ra_db::{FileId, SourceDatabase}; use ra_syntax::{ - AstNode, ast, + AstNode, ast::{self, DocCommentsOwner}, algo::{ find_node_at_offset, visit::{visitor, Visitor}, @@ -114,17 +114,39 @@ pub(crate) fn name_definition( fn named_target(file_id: FileId, node: &SyntaxNode) -> Option { visitor() - .visit(|node: &ast::StructDef| NavigationTarget::from_named(file_id, node)) - .visit(|node: &ast::EnumDef| NavigationTarget::from_named(file_id, node)) - .visit(|node: &ast::EnumVariant| NavigationTarget::from_named(file_id, node)) - .visit(|node: &ast::FnDef| NavigationTarget::from_named(file_id, node)) - .visit(|node: &ast::TypeAliasDef| NavigationTarget::from_named(file_id, node)) - .visit(|node: &ast::ConstDef| NavigationTarget::from_named(file_id, node)) - .visit(|node: &ast::StaticDef| NavigationTarget::from_named(file_id, node)) - .visit(|node: &ast::TraitDef| NavigationTarget::from_named(file_id, node)) - .visit(|node: &ast::NamedFieldDef| NavigationTarget::from_named(file_id, node)) - .visit(|node: &ast::Module| NavigationTarget::from_named(file_id, node)) - .visit(|node: &ast::MacroCall| NavigationTarget::from_named(file_id, node)) + .visit(|node: &ast::StructDef| { + NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + }) + .visit(|node: &ast::EnumDef| { + NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + }) + .visit(|node: &ast::EnumVariant| { + NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + }) + .visit(|node: &ast::FnDef| { + NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + }) + .visit(|node: &ast::TypeAliasDef| { + NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + }) + .visit(|node: &ast::ConstDef| { + NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + }) + .visit(|node: &ast::StaticDef| { + NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + }) + .visit(|node: &ast::TraitDef| { + NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + }) + .visit(|node: &ast::NamedFieldDef| { + NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + }) + .visit(|node: &ast::Module| { + NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + }) + .visit(|node: &ast::MacroCall| { + NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + }) .accept(node) } -- cgit v1.2.3 From aacc8941342d9b04fe65164018c07c994ff981b7 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 9 Jun 2019 23:59:59 +0800 Subject: Add display::Description --- crates/ra_ide_api/src/display.rs | 3 + crates/ra_ide_api/src/display/description.rs | 92 +++++++++++++++++ crates/ra_ide_api/src/display/navigation_target.rs | 110 ++++++++++----------- crates/ra_ide_api/src/goto_definition.rs | 25 ++--- 4 files changed, 163 insertions(+), 67 deletions(-) create mode 100644 crates/ra_ide_api/src/display/description.rs diff --git a/crates/ra_ide_api/src/display.rs b/crates/ra_ide_api/src/display.rs index 0eef11464..1d0e8ba12 100644 --- a/crates/ra_ide_api/src/display.rs +++ b/crates/ra_ide_api/src/display.rs @@ -4,6 +4,7 @@ mod function_signature; mod navigation_target; mod structure; +mod description; use ra_syntax::{ast::{self, AstNode, TypeParamsOwner}, SyntaxKind::{ATTR, COMMENT}}; @@ -11,6 +12,8 @@ pub use navigation_target::NavigationTarget; pub use structure::{StructureNode, file_structure}; pub use function_signature::FunctionSignature; +pub(crate) use description::Description; + pub(crate) fn function_label(node: &ast::FnDef) -> String { FunctionSignature::from(node).to_string() } diff --git a/crates/ra_ide_api/src/display/description.rs b/crates/ra_ide_api/src/display/description.rs new file mode 100644 index 000000000..fd0f564de --- /dev/null +++ b/crates/ra_ide_api/src/display/description.rs @@ -0,0 +1,92 @@ +use ra_syntax::{ + ast::{self, NameOwner, VisibilityOwner, TypeAscriptionOwner, AstNode}, +}; + +pub(crate) trait Description { + fn description(&self) -> Option; +} + +impl Description for ast::FnDef { + fn description(&self) -> Option { + Some(crate::display::function_label(self)) + } +} + +impl Description for ast::StructDef { + fn description(&self) -> Option { + visit_node(self, "struct ") + } +} + +impl Description for ast::EnumDef { + fn description(&self) -> Option { + visit_node(self, "enum ") + } +} + +impl Description for ast::TraitDef { + fn description(&self) -> Option { + visit_node(self, "trait ") + } +} + +impl Description for ast::Module { + fn description(&self) -> Option { + visit_node(self, "mod ") + } +} + +impl Description for ast::TypeAliasDef { + fn description(&self) -> Option { + visit_node(self, "type ") + } +} + +impl Description for ast::ConstDef { + fn description(&self) -> Option { + visit_ascribed_node(self, "const ") + } +} + +impl Description for ast::StaticDef { + fn description(&self) -> Option { + visit_ascribed_node(self, "static ") + } +} + +impl Description for ast::NamedFieldDef { + fn description(&self) -> Option { + visit_ascribed_node(self, "") + } +} + +impl Description for ast::EnumVariant { + fn description(&self) -> Option { + Some(self.name()?.text().to_string()) + } +} + +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, +{ + 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) +} diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index b2a925e2a..f60a07f1d 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -2,12 +2,13 @@ use ra_db::{FileId, SourceDatabase}; use ra_syntax::{ SyntaxNode, AstNode, SmolStr, TextRange, AstPtr, SyntaxKind::{self, NAME}, - ast::{self, NameOwner, VisibilityOwner, TypeAscriptionOwner, DocCommentsOwner}, + ast::{self, DocCommentsOwner}, algo::visit::{visitor, Visitor}, }; use hir::{ModuleSource, FieldSource, ImplItem}; use crate::{FileSymbol, db::RootDatabase}; +use super::description::Description; /// `NavigationTarget` represents and element in the editor's UI which you can /// click on to navigate to a particular piece of code. @@ -63,7 +64,7 @@ impl NavigationTarget { } pub(crate) fn from_bind_pat(file_id: FileId, pat: &ast::BindPat) -> NavigationTarget { - NavigationTarget::from_named(file_id, pat, None) + NavigationTarget::from_named(file_id, pat, None, None) } pub(crate) fn from_symbol(db: &RootDatabase, symbol: FileSymbol) -> NavigationTarget { @@ -77,7 +78,7 @@ impl NavigationTarget { full_range: symbol.ptr.range(), focus_range: symbol.name_range, container_name: symbol.container_name.clone(), - description: description_inner(&node), + description: description_from_symbol(&node), docs: docs_from_symbol(db, &symbol), } } @@ -92,7 +93,6 @@ 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, @@ -101,20 +101,16 @@ impl NavigationTarget { focus_range: None, kind: NAME, container_name: None, - description: description_inner(&node), - docs: None, //< No documentation for Pattern + description: None, //< No documentation for Description + docs: None, //< No documentation for Pattern } } 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 ast = par.to_node(file.syntax()); - let node = ast.syntax().to_owned(); NavigationTarget { file_id, @@ -123,8 +119,8 @@ impl NavigationTarget { focus_range: None, kind: NAME, container_name: None, - description: description_inner(&node), - docs: None, //< No document node for SelfParam + description: None, //< No document node for SelfParam + docs: None, //< No document node for SelfParam } } @@ -134,7 +130,7 @@ impl NavigationTarget { let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default(); match source { ModuleSource::SourceFile(node) => { - NavigationTarget::from_syntax(file_id, name, None, node.syntax(), None) + NavigationTarget::from_syntax(file_id, name, None, node.syntax(), None, None) } ModuleSource::Module(node) => NavigationTarget::from_syntax( file_id, @@ -142,6 +138,7 @@ impl NavigationTarget { None, node.syntax(), node.doc_comment_text(), + node.description(), ), } } @@ -156,6 +153,7 @@ impl NavigationTarget { None, source.syntax(), source.doc_comment_text(), + source.description(), ); } NavigationTarget::from_module(db, module) @@ -163,7 +161,12 @@ impl NavigationTarget { pub(crate) fn from_function(db: &RootDatabase, func: hir::Function) -> NavigationTarget { let (file_id, fn_def) = func.source(db); - NavigationTarget::from_named(file_id.original_file(db), &*fn_def, fn_def.doc_comment_text()) + NavigationTarget::from_named( + file_id.original_file(db), + &*fn_def, + fn_def.doc_comment_text(), + fn_def.description(), + ) } pub(crate) fn from_field(db: &RootDatabase, field: hir::StructField) -> NavigationTarget { @@ -171,10 +174,10 @@ impl NavigationTarget { let file_id = file_id.original_file(db); match field { FieldSource::Named(it) => { - NavigationTarget::from_named(file_id, &*it, it.doc_comment_text()) + NavigationTarget::from_named(file_id, &*it, it.doc_comment_text(), it.description()) } FieldSource::Pos(it) => { - NavigationTarget::from_syntax(file_id, "".into(), None, it.syntax(), None) + NavigationTarget::from_syntax(file_id, "".into(), None, it.syntax(), None, None) } } } @@ -187,6 +190,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), + node.description(), ) } hir::AdtDef::Union(s) => { @@ -195,6 +199,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), + node.description(), ) } hir::AdtDef::Enum(s) => { @@ -203,6 +208,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), + node.description(), ) } } @@ -221,6 +227,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), + node.description(), ) } hir::ModuleDef::Union(s) => { @@ -229,6 +236,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), + node.description(), ) } hir::ModuleDef::Const(s) => { @@ -237,6 +245,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), + node.description(), ) } hir::ModuleDef::Static(s) => { @@ -245,6 +254,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), + node.description(), ) } hir::ModuleDef::Enum(e) => { @@ -253,6 +263,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), + node.description(), ) } hir::ModuleDef::EnumVariant(var) => { @@ -261,6 +272,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), + node.description(), ) } hir::ModuleDef::Trait(e) => { @@ -269,6 +281,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), + node.description(), ) } hir::ModuleDef::TypeAlias(e) => { @@ -277,6 +290,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), + node.description(), ) } hir::ModuleDef::BuiltinType(..) => { @@ -297,6 +311,7 @@ impl NavigationTarget { None, node.syntax(), None, + None, ) } @@ -309,6 +324,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), + node.description(), ) } ImplItem::TypeAlias(a) => { @@ -317,6 +333,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), + node.description(), ) } } @@ -325,7 +342,12 @@ impl NavigationTarget { pub(crate) fn from_macro_def(db: &RootDatabase, macro_call: hir::MacroDef) -> NavigationTarget { let (file_id, node) = macro_call.source(db); log::debug!("nav target {}", node.syntax().debug_dump()); - NavigationTarget::from_named(file_id.original_file(db), &*node, node.doc_comment_text()) + NavigationTarget::from_named( + file_id.original_file(db), + &*node, + node.doc_comment_text(), + None, + ) } #[cfg(test)] @@ -357,11 +379,12 @@ impl NavigationTarget { file_id: FileId, node: &impl ast::NameOwner, docs: Option, + description: Option, ) -> NavigationTarget { //FIXME: use `_` instead of empty string let name = node.name().map(|it| it.text().clone()).unwrap_or_default(); let focus_range = node.name().map(|it| it.syntax().range()); - NavigationTarget::from_syntax(file_id, name, focus_range, node.syntax(), docs) + NavigationTarget::from_syntax(file_id, name, focus_range, node.syntax(), docs, description) } fn from_syntax( @@ -370,6 +393,7 @@ impl NavigationTarget { focus_range: Option, node: &SyntaxNode, docs: Option, + description: Option, ) -> NavigationTarget { NavigationTarget { file_id, @@ -379,7 +403,7 @@ impl NavigationTarget { focus_range, // ptr: Some(LocalSyntaxPtr::new(node)), container_name: None, - description: description_inner(node), + description, docs, } } @@ -408,46 +432,22 @@ fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option { .accept(&node)? } -/// Get a description of a node. +/// Get a description of a symbol. /// /// e.g. `struct Name`, `enum Name`, `fn Name` -fn description_inner(node: &SyntaxNode) -> Option { +fn description_from_symbol(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); - } - - Some(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) - } 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())) + .visit(|node: &ast::FnDef| node.description()) + .visit(|node: &ast::StructDef| node.description()) + .visit(|node: &ast::EnumDef| node.description()) + .visit(|node: &ast::TraitDef| node.description()) + .visit(|node: &ast::Module| node.description()) + .visit(|node: &ast::TypeAliasDef| node.description()) + .visit(|node: &ast::ConstDef| node.description()) + .visit(|node: &ast::StaticDef| node.description()) + .visit(|node: &ast::NamedFieldDef| node.description()) + .visit(|node: &ast::EnumVariant| node.description()) .accept(&node)? } diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index fbd881bfe..359fc2da1 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -13,6 +13,7 @@ use crate::{ db::RootDatabase, RangeInfo, name_ref_kind::{NameRefKind::*, classify_name_ref}, + display::Description, }; pub(crate) fn goto_definition( @@ -72,7 +73,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(db, file_id, par)), + Some(SelfParam(par)) => return Exact(NavigationTarget::from_self_param(file_id, par)), Some(GenericParam(_)) => { // FIXME: go to the generic param def } @@ -115,37 +116,37 @@ pub(crate) fn name_definition( fn named_target(file_id: FileId, node: &SyntaxNode) -> Option { visitor() .visit(|node: &ast::StructDef| { - NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) }) .visit(|node: &ast::EnumDef| { - NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) }) .visit(|node: &ast::EnumVariant| { - NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) }) .visit(|node: &ast::FnDef| { - NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) }) .visit(|node: &ast::TypeAliasDef| { - NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) }) .visit(|node: &ast::ConstDef| { - NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) }) .visit(|node: &ast::StaticDef| { - NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) }) .visit(|node: &ast::TraitDef| { - NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) }) .visit(|node: &ast::NamedFieldDef| { - NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) }) .visit(|node: &ast::Module| { - NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) }) .visit(|node: &ast::MacroCall| { - NavigationTarget::from_named(file_id, node, node.doc_comment_text()) + NavigationTarget::from_named(file_id, node, node.doc_comment_text(), None) }) .accept(node) } -- cgit v1.2.3 From e9848b2e6ce96a52252c8797c6f467e8e6e951a4 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 10 Jun 2019 00:03:38 +0800 Subject: Change docs and description to getter method --- crates/ra_ide_api/src/display.rs | 2 +- crates/ra_ide_api/src/display/navigation_target.rs | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/crates/ra_ide_api/src/display.rs b/crates/ra_ide_api/src/display.rs index 1d0e8ba12..313415610 100644 --- a/crates/ra_ide_api/src/display.rs +++ b/crates/ra_ide_api/src/display.rs @@ -76,7 +76,7 @@ where // 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) { + 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 f60a07f1d..c0fa9439e 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -23,9 +23,8 @@ pub struct NavigationTarget { full_range: TextRange, focus_range: Option, container_name: Option, - - pub(crate) description: Option, - pub(crate) docs: Option, + description: Option, + docs: Option, } impl NavigationTarget { @@ -55,6 +54,14 @@ impl NavigationTarget { self.full_range } + pub fn docs(&self) -> Option { + self.docs.clone() + } + + pub fn description(&self) -> Option { + self.description.clone() + } + /// A "most interesting" range withing the `full_range`. /// /// Typically, `full_range` is the whole syntax node, -- cgit v1.2.3 From 4db0f0ccaf6d44be6369b2da3a0cf58db6a7246c Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 10 Jun 2019 00:09:19 +0800 Subject: Rename description funcs --- crates/ra_ide_api/src/display/description.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/crates/ra_ide_api/src/display/description.rs b/crates/ra_ide_api/src/display/description.rs index fd0f564de..4f6b0a858 100644 --- a/crates/ra_ide_api/src/display/description.rs +++ b/crates/ra_ide_api/src/display/description.rs @@ -14,49 +14,49 @@ impl Description for ast::FnDef { impl Description for ast::StructDef { fn description(&self) -> Option { - visit_node(self, "struct ") + description_from_node(self, "struct ") } } impl Description for ast::EnumDef { fn description(&self) -> Option { - visit_node(self, "enum ") + description_from_node(self, "enum ") } } impl Description for ast::TraitDef { fn description(&self) -> Option { - visit_node(self, "trait ") + description_from_node(self, "trait ") } } impl Description for ast::Module { fn description(&self) -> Option { - visit_node(self, "mod ") + description_from_node(self, "mod ") } } impl Description for ast::TypeAliasDef { fn description(&self) -> Option { - visit_node(self, "type ") + description_from_node(self, "type ") } } impl Description for ast::ConstDef { fn description(&self) -> Option { - visit_ascribed_node(self, "const ") + description_from_ascribed_node(self, "const ") } } impl Description for ast::StaticDef { fn description(&self) -> Option { - visit_ascribed_node(self, "static ") + description_from_ascribed_node(self, "static ") } } impl Description for ast::NamedFieldDef { fn description(&self) -> Option { - visit_ascribed_node(self, "") + description_from_ascribed_node(self, "") } } @@ -66,11 +66,11 @@ impl Description for ast::EnumVariant { } } -fn visit_ascribed_node(node: &T, prefix: &str) -> Option +fn description_from_ascribed_node(node: &T, prefix: &str) -> Option where T: NameOwner + VisibilityOwner + TypeAscriptionOwner, { - let mut string = visit_node(node, prefix)?; + let mut string = description_from_node(node, prefix)?; if let Some(type_ref) = node.ascribed_type() { string.push_str(": "); @@ -80,7 +80,7 @@ where Some(string) } -fn visit_node(node: &T, label: &str) -> Option +fn description_from_node(node: &T, label: &str) -> Option where T: NameOwner + VisibilityOwner, { -- cgit v1.2.3 From 358ad0efca0fe76b040b47d199e166feb6128ac0 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 10 Jun 2019 00:20:49 +0800 Subject: Make description_from_symbol depends on symbol --- crates/ra_ide_api/src/display/navigation_target.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index c0fa9439e..f441ae943 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -75,9 +75,6 @@ impl 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(), @@ -85,7 +82,7 @@ impl NavigationTarget { full_range: symbol.ptr.range(), focus_range: symbol.name_range, container_name: symbol.container_name.clone(), - description: description_from_symbol(&node), + description: description_from_symbol(db, &symbol), docs: docs_from_symbol(db, &symbol), } } @@ -442,7 +439,10 @@ fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option { /// Get a description of a symbol. /// /// e.g. `struct Name`, `enum Name`, `fn Name` -fn description_from_symbol(node: &SyntaxNode) -> Option { +fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option { + let file = db.parse(symbol.file_id).tree; + let node = symbol.ptr.to_node(file.syntax()).to_owned(); + // FIXME: After type inference is done, add type information to improve the output visitor() -- cgit v1.2.3 From 41cb3fd7587f418ad18b6a3672211e0bfa304cfb Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 10 Jun 2019 03:28:53 +0800 Subject: Rename Description to ShortLabel --- crates/ra_ide_api/src/display.rs | 4 +- crates/ra_ide_api/src/display/description.rs | 92 ---------------------- crates/ra_ide_api/src/display/navigation_target.rs | 56 ++++++------- crates/ra_ide_api/src/display/short_label.rs | 92 ++++++++++++++++++++++ crates/ra_ide_api/src/goto_definition.rs | 22 +++--- 5 files changed, 133 insertions(+), 133 deletions(-) delete mode 100644 crates/ra_ide_api/src/display/description.rs create mode 100644 crates/ra_ide_api/src/display/short_label.rs diff --git a/crates/ra_ide_api/src/display.rs b/crates/ra_ide_api/src/display.rs index 313415610..7a8734a75 100644 --- a/crates/ra_ide_api/src/display.rs +++ b/crates/ra_ide_api/src/display.rs @@ -4,7 +4,7 @@ mod function_signature; mod navigation_target; mod structure; -mod description; +mod short_label; use ra_syntax::{ast::{self, AstNode, TypeParamsOwner}, SyntaxKind::{ATTR, COMMENT}}; @@ -12,7 +12,7 @@ pub use navigation_target::NavigationTarget; pub use structure::{StructureNode, file_structure}; pub use function_signature::FunctionSignature; -pub(crate) use description::Description; +pub(crate) use short_label::ShortLabel; pub(crate) fn function_label(node: &ast::FnDef) -> String { FunctionSignature::from(node).to_string() diff --git a/crates/ra_ide_api/src/display/description.rs b/crates/ra_ide_api/src/display/description.rs deleted file mode 100644 index 4f6b0a858..000000000 --- a/crates/ra_ide_api/src/display/description.rs +++ /dev/null @@ -1,92 +0,0 @@ -use ra_syntax::{ - ast::{self, NameOwner, VisibilityOwner, TypeAscriptionOwner, AstNode}, -}; - -pub(crate) trait Description { - fn description(&self) -> Option; -} - -impl Description for ast::FnDef { - fn description(&self) -> Option { - Some(crate::display::function_label(self)) - } -} - -impl Description for ast::StructDef { - fn description(&self) -> Option { - description_from_node(self, "struct ") - } -} - -impl Description for ast::EnumDef { - fn description(&self) -> Option { - description_from_node(self, "enum ") - } -} - -impl Description for ast::TraitDef { - fn description(&self) -> Option { - description_from_node(self, "trait ") - } -} - -impl Description for ast::Module { - fn description(&self) -> Option { - description_from_node(self, "mod ") - } -} - -impl Description for ast::TypeAliasDef { - fn description(&self) -> Option { - description_from_node(self, "type ") - } -} - -impl Description for ast::ConstDef { - fn description(&self) -> Option { - description_from_ascribed_node(self, "const ") - } -} - -impl Description for ast::StaticDef { - fn description(&self) -> Option { - description_from_ascribed_node(self, "static ") - } -} - -impl Description for ast::NamedFieldDef { - fn description(&self) -> Option { - description_from_ascribed_node(self, "") - } -} - -impl Description for ast::EnumVariant { - fn description(&self) -> Option { - Some(self.name()?.text().to_string()) - } -} - -fn description_from_ascribed_node(node: &T, prefix: &str) -> Option -where - T: NameOwner + VisibilityOwner + TypeAscriptionOwner, -{ - let mut string = description_from_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 description_from_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) -} diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index f441ae943..9b17d6adc 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -8,7 +8,7 @@ use ra_syntax::{ use hir::{ModuleSource, FieldSource, ImplItem}; use crate::{FileSymbol, db::RootDatabase}; -use super::description::Description; +use super::short_label::ShortLabel; /// `NavigationTarget` represents and element in the editor's UI which you can /// click on to navigate to a particular piece of code. @@ -142,7 +142,7 @@ impl NavigationTarget { None, node.syntax(), node.doc_comment_text(), - node.description(), + node.short_label(), ), } } @@ -157,7 +157,7 @@ impl NavigationTarget { None, source.syntax(), source.doc_comment_text(), - source.description(), + source.short_label(), ); } NavigationTarget::from_module(db, module) @@ -169,7 +169,7 @@ impl NavigationTarget { file_id.original_file(db), &*fn_def, fn_def.doc_comment_text(), - fn_def.description(), + fn_def.short_label(), ) } @@ -178,7 +178,7 @@ impl NavigationTarget { let file_id = file_id.original_file(db); match field { FieldSource::Named(it) => { - NavigationTarget::from_named(file_id, &*it, it.doc_comment_text(), it.description()) + NavigationTarget::from_named(file_id, &*it, it.doc_comment_text(), it.short_label()) } FieldSource::Pos(it) => { NavigationTarget::from_syntax(file_id, "".into(), None, it.syntax(), None, None) @@ -194,7 +194,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), - node.description(), + node.short_label(), ) } hir::AdtDef::Union(s) => { @@ -203,7 +203,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), - node.description(), + node.short_label(), ) } hir::AdtDef::Enum(s) => { @@ -212,7 +212,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), - node.description(), + node.short_label(), ) } } @@ -231,7 +231,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), - node.description(), + node.short_label(), ) } hir::ModuleDef::Union(s) => { @@ -240,7 +240,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), - node.description(), + node.short_label(), ) } hir::ModuleDef::Const(s) => { @@ -249,7 +249,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), - node.description(), + node.short_label(), ) } hir::ModuleDef::Static(s) => { @@ -258,7 +258,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), - node.description(), + node.short_label(), ) } hir::ModuleDef::Enum(e) => { @@ -267,7 +267,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), - node.description(), + node.short_label(), ) } hir::ModuleDef::EnumVariant(var) => { @@ -276,7 +276,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), - node.description(), + node.short_label(), ) } hir::ModuleDef::Trait(e) => { @@ -285,7 +285,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), - node.description(), + node.short_label(), ) } hir::ModuleDef::TypeAlias(e) => { @@ -294,7 +294,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), - node.description(), + node.short_label(), ) } hir::ModuleDef::BuiltinType(..) => { @@ -328,7 +328,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), - node.description(), + node.short_label(), ) } ImplItem::TypeAlias(a) => { @@ -337,7 +337,7 @@ impl NavigationTarget { file_id.original_file(db), &*node, node.doc_comment_text(), - node.description(), + node.short_label(), ) } } @@ -446,15 +446,15 @@ fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option Option; +} + +impl ShortLabel for ast::FnDef { + fn short_label(&self) -> Option { + Some(crate::display::function_label(self)) + } +} + +impl ShortLabel for ast::StructDef { + fn short_label(&self) -> Option { + short_label_from_node(self, "struct ") + } +} + +impl ShortLabel for ast::EnumDef { + fn short_label(&self) -> Option { + short_label_from_node(self, "enum ") + } +} + +impl ShortLabel for ast::TraitDef { + fn short_label(&self) -> Option { + short_label_from_node(self, "trait ") + } +} + +impl ShortLabel for ast::Module { + fn short_label(&self) -> Option { + short_label_from_node(self, "mod ") + } +} + +impl ShortLabel for ast::TypeAliasDef { + fn short_label(&self) -> Option { + short_label_from_node(self, "type ") + } +} + +impl ShortLabel for ast::ConstDef { + fn short_label(&self) -> Option { + short_label_from_ascribed_node(self, "const ") + } +} + +impl ShortLabel for ast::StaticDef { + fn short_label(&self) -> Option { + short_label_from_ascribed_node(self, "static ") + } +} + +impl ShortLabel for ast::NamedFieldDef { + fn short_label(&self) -> Option { + short_label_from_ascribed_node(self, "") + } +} + +impl ShortLabel for ast::EnumVariant { + fn short_label(&self) -> Option { + Some(self.name()?.text().to_string()) + } +} + +fn short_label_from_ascribed_node(node: &T, prefix: &str) -> Option +where + T: NameOwner + VisibilityOwner + TypeAscriptionOwner, +{ + let mut string = short_label_from_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 short_label_from_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) +} diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index 359fc2da1..325a5a4f3 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -13,7 +13,7 @@ use crate::{ db::RootDatabase, RangeInfo, name_ref_kind::{NameRefKind::*, classify_name_ref}, - display::Description, + display::ShortLabel, }; pub(crate) fn goto_definition( @@ -116,34 +116,34 @@ pub(crate) fn name_definition( fn named_target(file_id: FileId, node: &SyntaxNode) -> Option { visitor() .visit(|node: &ast::StructDef| { - NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) + NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.short_label()) }) .visit(|node: &ast::EnumDef| { - NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) + NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.short_label()) }) .visit(|node: &ast::EnumVariant| { - NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) + NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.short_label()) }) .visit(|node: &ast::FnDef| { - NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) + NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.short_label()) }) .visit(|node: &ast::TypeAliasDef| { - NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) + NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.short_label()) }) .visit(|node: &ast::ConstDef| { - NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) + NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.short_label()) }) .visit(|node: &ast::StaticDef| { - NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) + NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.short_label()) }) .visit(|node: &ast::TraitDef| { - NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) + NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.short_label()) }) .visit(|node: &ast::NamedFieldDef| { - NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) + NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.short_label()) }) .visit(|node: &ast::Module| { - NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.description()) + NavigationTarget::from_named(file_id, node, node.doc_comment_text(), node.short_label()) }) .visit(|node: &ast::MacroCall| { NavigationTarget::from_named(file_id, node, node.doc_comment_text(), None) -- cgit v1.2.3 From fc70275fedc4edd7c53003d93df8826046591951 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 10 Jun 2019 03:30:03 +0800 Subject: Rename "string" to "buf" --- crates/ra_ide_api/src/display/short_label.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/ra_ide_api/src/display/short_label.rs b/crates/ra_ide_api/src/display/short_label.rs index 6acb2ab92..ab67cc90c 100644 --- a/crates/ra_ide_api/src/display/short_label.rs +++ b/crates/ra_ide_api/src/display/short_label.rs @@ -70,23 +70,23 @@ fn short_label_from_ascribed_node(node: &T, prefix: &str) -> Option where T: NameOwner + VisibilityOwner + TypeAscriptionOwner, { - let mut string = short_label_from_node(node, prefix)?; + let mut buf = short_label_from_node(node, prefix)?; if let Some(type_ref) = node.ascribed_type() { - string.push_str(": "); - type_ref.syntax().text().push_to(&mut string); + buf.push_str(": "); + type_ref.syntax().text().push_to(&mut buf); } - Some(string) + Some(buf) } fn short_label_from_node(node: &T, label: &str) -> Option where T: NameOwner + VisibilityOwner, { - let mut string = + let mut buf = 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) + buf.push_str(label); + buf.push_str(node.name()?.text().as_str()); + Some(buf) } -- cgit v1.2.3 From e91bf0bba4d0611af81b17430a975656e96006c6 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 10 Jun 2019 03:37:34 +0800 Subject: Use &str instread of Option --- crates/ra_ide_api/src/display.rs | 2 +- crates/ra_ide_api/src/display/navigation_target.rs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/ra_ide_api/src/display.rs b/crates/ra_ide_api/src/display.rs index 7a8734a75..f11af0a0b 100644 --- a/crates/ra_ide_api/src/display.rs +++ b/crates/ra_ide_api/src/display.rs @@ -78,7 +78,7 @@ where 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, Some(docs)) => Some(docs.to_string()), _ => None, } } diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index 9b17d6adc..80726efbb 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -54,12 +54,12 @@ impl NavigationTarget { self.full_range } - pub fn docs(&self) -> Option { - self.docs.clone() + pub fn docs(&self) -> Option<&str> { + self.docs.as_ref().map(String::as_str) } - pub fn description(&self) -> Option { - self.description.clone() + pub fn description(&self) -> Option<&str> { + self.description.as_ref().map(String::as_str) } /// A "most interesting" range withing the `full_range`. -- cgit v1.2.3 From 9ded8d2eabfbe49fe59788d3d9e05e07f10d52ec Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 10 Jun 2019 03:39:42 +0800 Subject: Formating --- crates/ra_ide_api/src/display/short_label.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/ra_ide_api/src/display/short_label.rs b/crates/ra_ide_api/src/display/short_label.rs index ab67cc90c..dc8245c34 100644 --- a/crates/ra_ide_api/src/display/short_label.rs +++ b/crates/ra_ide_api/src/display/short_label.rs @@ -84,8 +84,7 @@ fn short_label_from_node(node: &T, label: &str) -> Option where T: NameOwner + VisibilityOwner, { - let mut buf = - node.visibility().map(|v| format!("{} ", v.syntax().text())).unwrap_or_default(); + let mut buf = node.visibility().map(|v| format!("{} ", v.syntax().text())).unwrap_or_default(); buf.push_str(label); buf.push_str(node.name()?.text().as_str()); Some(buf) -- cgit v1.2.3 From 60938ff73ed80461982f52cb2b044572e7323a0c Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 10 Jun 2019 03:41:21 +0800 Subject: Remove incorrect FIXME --- crates/ra_ide_api/src/display/navigation_target.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index 80726efbb..983ebe788 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -443,8 +443,6 @@ fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option