Rename Description to ShortLabel

This commit is contained in:
Edwin Cheng 2019-06-10 03:28:53 +08:00
parent 358ad0efca
commit 41cb3fd758
5 changed files with 133 additions and 133 deletions

View file

@ -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()

View file

@ -1,92 +0,0 @@
use ra_syntax::{
ast::{self, NameOwner, VisibilityOwner, TypeAscriptionOwner, AstNode},
};
pub(crate) trait Description {
fn description(&self) -> Option<String>;
}
impl Description for ast::FnDef {
fn description(&self) -> Option<String> {
Some(crate::display::function_label(self))
}
}
impl Description for ast::StructDef {
fn description(&self) -> Option<String> {
description_from_node(self, "struct ")
}
}
impl Description for ast::EnumDef {
fn description(&self) -> Option<String> {
description_from_node(self, "enum ")
}
}
impl Description for ast::TraitDef {
fn description(&self) -> Option<String> {
description_from_node(self, "trait ")
}
}
impl Description for ast::Module {
fn description(&self) -> Option<String> {
description_from_node(self, "mod ")
}
}
impl Description for ast::TypeAliasDef {
fn description(&self) -> Option<String> {
description_from_node(self, "type ")
}
}
impl Description for ast::ConstDef {
fn description(&self) -> Option<String> {
description_from_ascribed_node(self, "const ")
}
}
impl Description for ast::StaticDef {
fn description(&self) -> Option<String> {
description_from_ascribed_node(self, "static ")
}
}
impl Description for ast::NamedFieldDef {
fn description(&self) -> Option<String> {
description_from_ascribed_node(self, "")
}
}
impl Description for ast::EnumVariant {
fn description(&self) -> Option<String> {
Some(self.name()?.text().to_string())
}
}
fn description_from_ascribed_node<T>(node: &T, prefix: &str) -> Option<String>
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<T>(node: &T, label: &str) -> Option<String>
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)
}

View file

@ -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<Str
// FIXME: After type inference is done, add type information to improve the output
visitor()
.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())
.visit(|node: &ast::FnDef| node.short_label())
.visit(|node: &ast::StructDef| node.short_label())
.visit(|node: &ast::EnumDef| node.short_label())
.visit(|node: &ast::TraitDef| node.short_label())
.visit(|node: &ast::Module| node.short_label())
.visit(|node: &ast::TypeAliasDef| node.short_label())
.visit(|node: &ast::ConstDef| node.short_label())
.visit(|node: &ast::StaticDef| node.short_label())
.visit(|node: &ast::NamedFieldDef| node.short_label())
.visit(|node: &ast::EnumVariant| node.short_label())
.accept(&node)?
}

View file

@ -0,0 +1,92 @@
use ra_syntax::{
ast::{self, NameOwner, VisibilityOwner, TypeAscriptionOwner, AstNode},
};
pub(crate) trait ShortLabel {
fn short_label(&self) -> Option<String>;
}
impl ShortLabel for ast::FnDef {
fn short_label(&self) -> Option<String> {
Some(crate::display::function_label(self))
}
}
impl ShortLabel for ast::StructDef {
fn short_label(&self) -> Option<String> {
short_label_from_node(self, "struct ")
}
}
impl ShortLabel for ast::EnumDef {
fn short_label(&self) -> Option<String> {
short_label_from_node(self, "enum ")
}
}
impl ShortLabel for ast::TraitDef {
fn short_label(&self) -> Option<String> {
short_label_from_node(self, "trait ")
}
}
impl ShortLabel for ast::Module {
fn short_label(&self) -> Option<String> {
short_label_from_node(self, "mod ")
}
}
impl ShortLabel for ast::TypeAliasDef {
fn short_label(&self) -> Option<String> {
short_label_from_node(self, "type ")
}
}
impl ShortLabel for ast::ConstDef {
fn short_label(&self) -> Option<String> {
short_label_from_ascribed_node(self, "const ")
}
}
impl ShortLabel for ast::StaticDef {
fn short_label(&self) -> Option<String> {
short_label_from_ascribed_node(self, "static ")
}
}
impl ShortLabel for ast::NamedFieldDef {
fn short_label(&self) -> Option<String> {
short_label_from_ascribed_node(self, "")
}
}
impl ShortLabel for ast::EnumVariant {
fn short_label(&self) -> Option<String> {
Some(self.name()?.text().to_string())
}
}
fn short_label_from_ascribed_node<T>(node: &T, prefix: &str) -> Option<String>
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<T>(node: &T, label: &str) -> Option<String>
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)
}

View file

@ -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<NavigationTarget> {
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)