aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/display/short_label.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-06-09 20:45:35 +0100
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-06-09 20:45:35 +0100
commitcbafae6fa8e1292302d1ea0c04871fe24d174954 (patch)
tree047b949be1ee4908d2e429bfffc545a4f859215f /crates/ra_ide_api/src/display/short_label.rs
parent9c92c05ca614fcded456153b1bc6717d17f0dafb (diff)
parent60938ff73ed80461982f52cb2b044572e7323a0c (diff)
Merge #1388
1388: Remove NavigationTarget::node and fill docs and description in during construction r=matklad a=edwin0cheng Related dissused: https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/MBE.20discussion/near/167105559 Co-authored-by: Edwin Cheng <[email protected]>
Diffstat (limited to 'crates/ra_ide_api/src/display/short_label.rs')
-rw-r--r--crates/ra_ide_api/src/display/short_label.rs91
1 files changed, 91 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..dc8245c34
--- /dev/null
+++ b/crates/ra_ide_api/src/display/short_label.rs
@@ -0,0 +1,91 @@
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 buf = short_label_from_node(node, prefix)?;
74
75 if let Some(type_ref) = node.ascribed_type() {
76 buf.push_str(": ");
77 type_ref.syntax().text().push_to(&mut buf);
78 }
79
80 Some(buf)
81}
82
83fn short_label_from_node<T>(node: &T, label: &str) -> Option<String>
84where
85 T: NameOwner + VisibilityOwner,
86{
87 let mut buf = node.visibility().map(|v| format!("{} ", v.syntax().text())).unwrap_or_default();
88 buf.push_str(label);
89 buf.push_str(node.name()?.text().as_str());
90 Some(buf)
91}