aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/display/short_label.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/display/short_label.rs')
-rw-r--r--crates/ide/src/display/short_label.rs111
1 files changed, 111 insertions, 0 deletions
diff --git a/crates/ide/src/display/short_label.rs b/crates/ide/src/display/short_label.rs
new file mode 100644
index 000000000..ea49d9f97
--- /dev/null
+++ b/crates/ide/src/display/short_label.rs
@@ -0,0 +1,111 @@
1//! FIXME: write short doc here
2
3use stdx::format_to;
4use syntax::ast::{self, AstNode, NameOwner, VisibilityOwner};
5
6pub(crate) trait ShortLabel {
7 fn short_label(&self) -> Option<String>;
8}
9
10impl ShortLabel for ast::Fn {
11 fn short_label(&self) -> Option<String> {
12 Some(crate::display::function_declaration(self))
13 }
14}
15
16impl ShortLabel for ast::Struct {
17 fn short_label(&self) -> Option<String> {
18 short_label_from_node(self, "struct ")
19 }
20}
21
22impl ShortLabel for ast::Union {
23 fn short_label(&self) -> Option<String> {
24 short_label_from_node(self, "union ")
25 }
26}
27
28impl ShortLabel for ast::Enum {
29 fn short_label(&self) -> Option<String> {
30 short_label_from_node(self, "enum ")
31 }
32}
33
34impl ShortLabel for ast::Trait {
35 fn short_label(&self) -> Option<String> {
36 if self.unsafe_token().is_some() {
37 short_label_from_node(self, "unsafe trait ")
38 } else {
39 short_label_from_node(self, "trait ")
40 }
41 }
42}
43
44impl ShortLabel for ast::Module {
45 fn short_label(&self) -> Option<String> {
46 short_label_from_node(self, "mod ")
47 }
48}
49
50impl ShortLabel for ast::SourceFile {
51 fn short_label(&self) -> Option<String> {
52 None
53 }
54}
55
56impl ShortLabel for ast::TypeAlias {
57 fn short_label(&self) -> Option<String> {
58 short_label_from_node(self, "type ")
59 }
60}
61
62impl ShortLabel for ast::Const {
63 fn short_label(&self) -> Option<String> {
64 let mut new_buf = short_label_from_ty(self, self.ty(), "const ")?;
65 if let Some(expr) = self.body() {
66 format_to!(new_buf, " = {}", expr.syntax());
67 }
68 Some(new_buf)
69 }
70}
71
72impl ShortLabel for ast::Static {
73 fn short_label(&self) -> Option<String> {
74 short_label_from_ty(self, self.ty(), "static ")
75 }
76}
77
78impl ShortLabel for ast::RecordField {
79 fn short_label(&self) -> Option<String> {
80 short_label_from_ty(self, self.ty(), "")
81 }
82}
83
84impl ShortLabel for ast::Variant {
85 fn short_label(&self) -> Option<String> {
86 Some(self.name()?.text().to_string())
87 }
88}
89
90fn short_label_from_ty<T>(node: &T, ty: Option<ast::Type>, prefix: &str) -> Option<String>
91where
92 T: NameOwner + VisibilityOwner,
93{
94 let mut buf = short_label_from_node(node, prefix)?;
95
96 if let Some(type_ref) = ty {
97 format_to!(buf, ": {}", type_ref.syntax());
98 }
99
100 Some(buf)
101}
102
103fn short_label_from_node<T>(node: &T, label: &str) -> Option<String>
104where
105 T: NameOwner + VisibilityOwner,
106{
107 let mut buf = node.visibility().map(|v| format!("{} ", v.syntax())).unwrap_or_default();
108 buf.push_str(label);
109 buf.push_str(node.name()?.text().as_str());
110 Some(buf)
111}