aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/display/short_label.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api/src/display/short_label.rs')
-rw-r--r--crates/ra_ide_api/src/display/short_label.rs92
1 files changed, 92 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/display/short_label.rs b/crates/ra_ide_api/src/display/short_label.rs
new file mode 100644
index 000000000..6acb2ab92
--- /dev/null
+++ b/crates/ra_ide_api/src/display/short_label.rs
@@ -0,0 +1,92 @@
1use ra_syntax::{
2 ast::{self, NameOwner, VisibilityOwner, TypeAscriptionOwner, AstNode},
3};
4
5pub(crate) trait ShortLabel {
6 fn short_label(&self) -> Option<String>;
7}
8
9impl ShortLabel for ast::FnDef {
10 fn short_label(&self) -> Option<String> {
11 Some(crate::display::function_label(self))
12 }
13}
14
15impl ShortLabel for ast::StructDef {
16 fn short_label(&self) -> Option<String> {
17 short_label_from_node(self, "struct ")
18 }
19}
20
21impl ShortLabel for ast::EnumDef {
22 fn short_label(&self) -> Option<String> {
23 short_label_from_node(self, "enum ")
24 }
25}
26
27impl ShortLabel for ast::TraitDef {
28 fn short_label(&self) -> Option<String> {
29 short_label_from_node(self, "trait ")
30 }
31}
32
33impl ShortLabel for ast::Module {
34 fn short_label(&self) -> Option<String> {
35 short_label_from_node(self, "mod ")
36 }
37}
38
39impl ShortLabel for ast::TypeAliasDef {
40 fn short_label(&self) -> Option<String> {
41 short_label_from_node(self, "type ")
42 }
43}
44
45impl ShortLabel for ast::ConstDef {
46 fn short_label(&self) -> Option<String> {
47 short_label_from_ascribed_node(self, "const ")
48 }
49}
50
51impl ShortLabel for ast::StaticDef {
52 fn short_label(&self) -> Option<String> {
53 short_label_from_ascribed_node(self, "static ")
54 }
55}
56
57impl ShortLabel for ast::NamedFieldDef {
58 fn short_label(&self) -> Option<String> {
59 short_label_from_ascribed_node(self, "")
60 }
61}
62
63impl ShortLabel for ast::EnumVariant {
64 fn short_label(&self) -> Option<String> {
65 Some(self.name()?.text().to_string())
66 }
67}
68
69fn short_label_from_ascribed_node<T>(node: &T, prefix: &str) -> Option<String>
70where
71 T: NameOwner + VisibilityOwner + TypeAscriptionOwner,
72{
73 let mut string = short_label_from_node(node, prefix)?;
74
75 if let Some(type_ref) = node.ascribed_type() {
76 string.push_str(": ");
77 type_ref.syntax().text().push_to(&mut string);
78 }
79
80 Some(string)
81}
82
83fn short_label_from_node<T>(node: &T, label: &str) -> Option<String>
84where
85 T: NameOwner + VisibilityOwner,
86{
87 let mut string =
88 node.visibility().map(|v| format!("{} ", v.syntax().text())).unwrap_or_default();
89 string.push_str(label);
90 string.push_str(node.name()?.text().as_str());
91 Some(string)
92}