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