1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
use ra_syntax::{
ast::{self, NameOwner, VisibilityOwner, TypeAscriptionOwner, AstNode},
};
pub(crate) trait Description {
fn description(&self) -> Option<String>;
}
impl Description for ast::FnDef {
fn description(&self) -> Option<String> {
Some(crate::display::function_label(self))
}
}
impl Description for ast::StructDef {
fn description(&self) -> Option<String> {
visit_node(self, "struct ")
}
}
impl Description for ast::EnumDef {
fn description(&self) -> Option<String> {
visit_node(self, "enum ")
}
}
impl Description for ast::TraitDef {
fn description(&self) -> Option<String> {
visit_node(self, "trait ")
}
}
impl Description for ast::Module {
fn description(&self) -> Option<String> {
visit_node(self, "mod ")
}
}
impl Description for ast::TypeAliasDef {
fn description(&self) -> Option<String> {
visit_node(self, "type ")
}
}
impl Description for ast::ConstDef {
fn description(&self) -> Option<String> {
visit_ascribed_node(self, "const ")
}
}
impl Description for ast::StaticDef {
fn description(&self) -> Option<String> {
visit_ascribed_node(self, "static ")
}
}
impl Description for ast::NamedFieldDef {
fn description(&self) -> Option<String> {
visit_ascribed_node(self, "")
}
}
impl Description for ast::EnumVariant {
fn description(&self) -> Option<String> {
Some(self.name()?.text().to_string())
}
}
fn visit_ascribed_node<T>(node: &T, prefix: &str) -> Option<String>
where
T: NameOwner + VisibilityOwner + TypeAscriptionOwner,
{
let mut string = visit_node(node, prefix)?;
if let Some(type_ref) = node.ascribed_type() {
string.push_str(": ");
type_ref.syntax().text().push_to(&mut string);
}
Some(string)
}
fn visit_node<T>(node: &T, label: &str) -> Option<String>
where
T: NameOwner + VisibilityOwner,
{
let mut string =
node.visibility().map(|v| format!("{} ", v.syntax().text())).unwrap_or_default();
string.push_str(label);
string.push_str(node.name()?.text().as_str());
Some(string)
}
|