From 4628d94e749f05a12ca51548a2c9afa2cc44ea84 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 16 Mar 2021 15:44:31 +0100 Subject: Remove ShortLabel --- crates/ide/src/display.rs | 3 +- crates/ide/src/display/navigation_target.rs | 30 +++---- crates/ide/src/display/short_label.rs | 128 ---------------------------- 3 files changed, 16 insertions(+), 145 deletions(-) delete mode 100644 crates/ide/src/display/short_label.rs diff --git a/crates/ide/src/display.rs b/crates/ide/src/display.rs index 1f7b665c0..71e97d6b4 100644 --- a/crates/ide/src/display.rs +++ b/crates/ide/src/display.rs @@ -2,8 +2,7 @@ //! into types that may be used to render in a UI. pub(crate) mod navigation_target; -mod short_label; pub(crate) use navigation_target::{ToNav, TryToNav}; -pub(crate) use syntax::display::{function_declaration, macro_label}; +pub(crate) use syntax::display::macro_label; diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs index 69c3751a1..c086de163 100644 --- a/crates/ide/src/display/navigation_target.rs +++ b/crates/ide/src/display/navigation_target.rs @@ -5,9 +5,10 @@ use std::fmt; use either::Either; use hir::{ AssocItem, Documentation, FieldSource, HasAttrs, HasSource, HirDisplay, InFile, ModuleSource, + Semantics, }; use ide_db::{ - base_db::{FileId, FileRange, SourceDatabase}, + base_db::{FileId, FileRange}, symbol_index::FileSymbolKind, SymbolKind, }; @@ -19,8 +20,6 @@ use syntax::{ use crate::FileSymbol; -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. /// @@ -502,21 +501,22 @@ impl TryToNav for hir::ConstParam { /// /// e.g. `struct Name`, `enum Name`, `fn Name` pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option { - let parse = db.parse(symbol.file_id); - let node = symbol.ptr.to_node(parse.tree().syntax()); + let sema = Semantics::new(db); + let parse = sema.parse(symbol.file_id); + let node = symbol.ptr.to_node(parse.syntax()); match_ast! { match node { - ast::Fn(it) => it.short_label(), - ast::Struct(it) => it.short_label(), - ast::Enum(it) => it.short_label(), - ast::Trait(it) => it.short_label(), - ast::Module(it) => it.short_label(), - ast::TypeAlias(it) => it.short_label(), - ast::Const(it) => it.short_label(), - ast::Static(it) => it.short_label(), - ast::RecordField(it) => it.short_label(), - ast::Variant(it) => it.short_label(), + ast::Fn(it) => sema.to_def(&it).map(|it| it.display(db).to_string()), + ast::Struct(it) => sema.to_def(&it).map(|it| it.display(db).to_string()), + ast::Enum(it) => sema.to_def(&it).map(|it| it.display(db).to_string()), + ast::Trait(it) => sema.to_def(&it).map(|it| it.display(db).to_string()), + ast::Module(it) => sema.to_def(&it).map(|it| it.display(db).to_string()), + ast::TypeAlias(it) => sema.to_def(&it).map(|it| it.display(db).to_string()), + ast::Const(it) => sema.to_def(&it).map(|it| it.display(db).to_string()), + ast::Static(it) => sema.to_def(&it).map(|it| it.display(db).to_string()), + ast::RecordField(it) => sema.to_def(&it).map(|it| it.display(db).to_string()), + ast::Variant(it) => sema.to_def(&it).map(|it| it.display(db).to_string()), _ => None, } } diff --git a/crates/ide/src/display/short_label.rs b/crates/ide/src/display/short_label.rs deleted file mode 100644 index 2df9266b4..000000000 --- a/crates/ide/src/display/short_label.rs +++ /dev/null @@ -1,128 +0,0 @@ -//! FIXME: write short doc here - -use stdx::format_to; -use syntax::ast::{self, AstNode, NameOwner, VisibilityOwner}; - -pub(crate) trait ShortLabel { - fn short_label(&self) -> Option; -} - -impl ShortLabel for ast::Fn { - fn short_label(&self) -> Option { - Some(crate::display::function_declaration(self)) - } -} - -impl ShortLabel for ast::Struct { - fn short_label(&self) -> Option { - short_label_from_node(self, "struct ") - } -} - -impl ShortLabel for ast::Union { - fn short_label(&self) -> Option { - short_label_from_node(self, "union ") - } -} - -impl ShortLabel for ast::Enum { - fn short_label(&self) -> Option { - short_label_from_node(self, "enum ") - } -} - -impl ShortLabel for ast::Trait { - fn short_label(&self) -> Option { - if self.unsafe_token().is_some() { - short_label_from_node(self, "unsafe trait ") - } else { - 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::SourceFile { - fn short_label(&self) -> Option { - None - } -} - -impl ShortLabel for ast::BlockExpr { - fn short_label(&self) -> Option { - None - } -} - -impl ShortLabel for ast::TypeAlias { - fn short_label(&self) -> Option { - let mut buf = short_label_from_node(self, "type ")?; - if let Some(type_ref) = self.ty() { - format_to!(buf, " = {}", type_ref.syntax()); - } - Some(buf) - } -} - -impl ShortLabel for ast::Const { - fn short_label(&self) -> Option { - short_label_from_ty(self, self.ty(), "const ") - } -} - -impl ShortLabel for ast::Static { - fn short_label(&self) -> Option { - short_label_from_ty(self, self.ty(), "static ") - } -} - -impl ShortLabel for ast::RecordField { - fn short_label(&self) -> Option { - short_label_from_ty(self, self.ty(), "") - } -} - -impl ShortLabel for ast::Variant { - fn short_label(&self) -> Option { - Some(self.name()?.text().to_string()) - } -} - -impl ShortLabel for ast::ConstParam { - fn short_label(&self) -> Option { - let mut buf = "const ".to_owned(); - buf.push_str(self.name()?.text()); - if let Some(type_ref) = self.ty() { - format_to!(buf, ": {}", type_ref.syntax()); - } - Some(buf) - } -} - -fn short_label_from_ty(node: &T, ty: Option, prefix: &str) -> Option -where - T: NameOwner + VisibilityOwner, -{ - let mut buf = short_label_from_node(node, prefix)?; - - if let Some(type_ref) = ty { - format_to!(buf, ": {}", type_ref.syntax()); - } - - Some(buf) -} - -fn short_label_from_node(node: &T, label: &str) -> Option -where - T: NameOwner + VisibilityOwner, -{ - let mut buf = node.visibility().map(|v| format!("{} ", v.syntax())).unwrap_or_default(); - buf.push_str(label); - buf.push_str(node.name()?.text()); - Some(buf) -} -- cgit v1.2.3