rust/crates/ide/src/display/short_label.rs

123 lines
2.9 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
use stdx::format_to;
2020-08-12 11:26:51 -05:00
use syntax::ast::{self, AstNode, NameOwner, VisibilityOwner};
2019-06-09 14:28:53 -05:00
pub(crate) trait ShortLabel {
fn short_label(&self) -> Option<String>;
}
2020-07-30 07:51:08 -05:00
impl ShortLabel for ast::Fn {
2019-06-09 14:28:53 -05:00
fn short_label(&self) -> Option<String> {
2020-07-16 14:33:11 -05:00
Some(crate::display::function_declaration(self))
2019-06-09 14:28:53 -05:00
}
}
2020-07-30 10:50:40 -05:00
impl ShortLabel for ast::Struct {
2019-06-09 14:28:53 -05:00
fn short_label(&self) -> Option<String> {
short_label_from_node(self, "struct ")
}
}
2020-07-30 10:36:46 -05:00
impl ShortLabel for ast::Union {
2019-11-25 08:30:50 -06:00
fn short_label(&self) -> Option<String> {
short_label_from_node(self, "union ")
}
}
2020-07-30 10:52:53 -05:00
impl ShortLabel for ast::Enum {
2019-06-09 14:28:53 -05:00
fn short_label(&self) -> Option<String> {
short_label_from_node(self, "enum ")
}
}
2020-07-30 11:17:28 -05:00
impl ShortLabel for ast::Trait {
2019-06-09 14:28:53 -05:00
fn short_label(&self) -> Option<String> {
2020-05-01 10:49:41 -05:00
if self.unsafe_token().is_some() {
short_label_from_node(self, "unsafe trait ")
} else {
short_label_from_node(self, "trait ")
}
2019-06-09 14:28:53 -05:00
}
}
impl ShortLabel for ast::Module {
fn short_label(&self) -> Option<String> {
short_label_from_node(self, "mod ")
}
}
impl ShortLabel for ast::SourceFile {
fn short_label(&self) -> Option<String> {
None
}
}
2020-07-30 08:25:46 -05:00
impl ShortLabel for ast::TypeAlias {
2019-06-09 14:28:53 -05:00
fn short_label(&self) -> Option<String> {
short_label_from_node(self, "type ")
}
}
2020-07-30 11:02:20 -05:00
impl ShortLabel for ast::Const {
2019-06-09 14:28:53 -05:00
fn short_label(&self) -> Option<String> {
let mut new_buf = short_label_from_ty(self, self.ty(), "const ")?;
if let Some(expr) = self.body() {
format_to!(new_buf, " = {}", expr.syntax());
}
Some(new_buf)
2019-06-09 14:28:53 -05:00
}
}
2020-07-30 11:02:20 -05:00
impl ShortLabel for ast::Static {
2019-06-09 14:28:53 -05:00
fn short_label(&self) -> Option<String> {
2020-07-30 13:51:43 -05:00
short_label_from_ty(self, self.ty(), "static ")
2019-06-09 14:28:53 -05:00
}
}
2020-07-30 09:49:13 -05:00
impl ShortLabel for ast::RecordField {
2019-06-09 14:28:53 -05:00
fn short_label(&self) -> Option<String> {
2020-07-30 13:51:43 -05:00
short_label_from_ty(self, self.ty(), "")
2019-06-09 14:28:53 -05:00
}
}
2020-07-30 10:56:53 -05:00
impl ShortLabel for ast::Variant {
2019-06-09 14:28:53 -05:00
fn short_label(&self) -> Option<String> {
Some(self.name()?.text().to_string())
}
}
2021-01-04 07:18:31 -06:00
impl ShortLabel for ast::ConstParam {
fn short_label(&self) -> Option<String> {
let mut buf = "const ".to_owned();
buf.push_str(self.name()?.text().as_str());
if let Some(type_ref) = self.ty() {
format_to!(buf, ": {}", type_ref.syntax());
}
Some(buf)
}
}
fn short_label_from_ty<T>(node: &T, ty: Option<ast::Type>, prefix: &str) -> Option<String>
2019-06-09 14:28:53 -05:00
where
2020-07-30 13:51:43 -05:00
T: NameOwner + VisibilityOwner,
2019-06-09 14:28:53 -05:00
{
2019-06-09 14:30:03 -05:00
let mut buf = short_label_from_node(node, prefix)?;
2019-06-09 14:28:53 -05:00
2020-07-30 13:51:43 -05:00
if let Some(type_ref) = ty {
format_to!(buf, ": {}", type_ref.syntax());
2019-06-09 14:28:53 -05:00
}
2019-06-09 14:30:03 -05:00
Some(buf)
2019-06-09 14:28:53 -05:00
}
fn short_label_from_node<T>(node: &T, label: &str) -> Option<String>
where
T: NameOwner + VisibilityOwner,
{
2019-07-20 08:52:11 -05:00
let mut buf = node.visibility().map(|v| format!("{} ", v.syntax())).unwrap_or_default();
2019-06-09 14:30:03 -05:00
buf.push_str(label);
buf.push_str(node.name()?.text().as_str());
Some(buf)
2019-06-09 14:28:53 -05:00
}