diff options
author | Igor Aleksanov <[email protected]> | 2020-08-14 05:34:07 +0100 |
---|---|---|
committer | Igor Aleksanov <[email protected]> | 2020-08-14 05:34:07 +0100 |
commit | c26c911ec1e6c2ad1dcb7d155a6a1d528839ad1a (patch) | |
tree | 7cff36c38234be0afb65273146d8247083a5cfeb /crates/ide/src/display/short_label.rs | |
parent | 3c018bf84de5c693b5ee1c6bec0fed3b201c2060 (diff) | |
parent | f1f73649a686dc6e6449afc35e0fa6fed00e225d (diff) |
Merge branch 'master' into add-disable-diagnostics
Diffstat (limited to 'crates/ide/src/display/short_label.rs')
-rw-r--r-- | crates/ide/src/display/short_label.rs | 111 |
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 | |||
3 | use stdx::format_to; | ||
4 | use syntax::ast::{self, AstNode, NameOwner, VisibilityOwner}; | ||
5 | |||
6 | pub(crate) trait ShortLabel { | ||
7 | fn short_label(&self) -> Option<String>; | ||
8 | } | ||
9 | |||
10 | impl ShortLabel for ast::Fn { | ||
11 | fn short_label(&self) -> Option<String> { | ||
12 | Some(crate::display::function_declaration(self)) | ||
13 | } | ||
14 | } | ||
15 | |||
16 | impl ShortLabel for ast::Struct { | ||
17 | fn short_label(&self) -> Option<String> { | ||
18 | short_label_from_node(self, "struct ") | ||
19 | } | ||
20 | } | ||
21 | |||
22 | impl ShortLabel for ast::Union { | ||
23 | fn short_label(&self) -> Option<String> { | ||
24 | short_label_from_node(self, "union ") | ||
25 | } | ||
26 | } | ||
27 | |||
28 | impl ShortLabel for ast::Enum { | ||
29 | fn short_label(&self) -> Option<String> { | ||
30 | short_label_from_node(self, "enum ") | ||
31 | } | ||
32 | } | ||
33 | |||
34 | impl 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 | |||
44 | impl ShortLabel for ast::Module { | ||
45 | fn short_label(&self) -> Option<String> { | ||
46 | short_label_from_node(self, "mod ") | ||
47 | } | ||
48 | } | ||
49 | |||
50 | impl ShortLabel for ast::SourceFile { | ||
51 | fn short_label(&self) -> Option<String> { | ||
52 | None | ||
53 | } | ||
54 | } | ||
55 | |||
56 | impl ShortLabel for ast::TypeAlias { | ||
57 | fn short_label(&self) -> Option<String> { | ||
58 | short_label_from_node(self, "type ") | ||
59 | } | ||
60 | } | ||
61 | |||
62 | impl 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 | |||
72 | impl ShortLabel for ast::Static { | ||
73 | fn short_label(&self) -> Option<String> { | ||
74 | short_label_from_ty(self, self.ty(), "static ") | ||
75 | } | ||
76 | } | ||
77 | |||
78 | impl ShortLabel for ast::RecordField { | ||
79 | fn short_label(&self) -> Option<String> { | ||
80 | short_label_from_ty(self, self.ty(), "") | ||
81 | } | ||
82 | } | ||
83 | |||
84 | impl ShortLabel for ast::Variant { | ||
85 | fn short_label(&self) -> Option<String> { | ||
86 | Some(self.name()?.text().to_string()) | ||
87 | } | ||
88 | } | ||
89 | |||
90 | fn short_label_from_ty<T>(node: &T, ty: Option<ast::Type>, prefix: &str) -> Option<String> | ||
91 | where | ||
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 | |||
103 | fn short_label_from_node<T>(node: &T, label: &str) -> Option<String> | ||
104 | where | ||
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 | } | ||