diff options
Diffstat (limited to 'crates/ra_ide_api/src/structure.rs')
-rw-r--r-- | crates/ra_ide_api/src/structure.rs | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/structure.rs b/crates/ra_ide_api/src/structure.rs new file mode 100644 index 000000000..ec2c9bbc6 --- /dev/null +++ b/crates/ra_ide_api/src/structure.rs | |||
@@ -0,0 +1,190 @@ | |||
1 | use crate::TextRange; | ||
2 | |||
3 | use ra_syntax::{ | ||
4 | algo::visit::{visitor, Visitor}, | ||
5 | ast::{self, AttrsOwner, NameOwner, TypeParamsOwner, TypeAscriptionOwner}, | ||
6 | AstNode, SourceFile, SyntaxKind, SyntaxNode, WalkEvent, | ||
7 | }; | ||
8 | |||
9 | #[derive(Debug, Clone)] | ||
10 | pub struct StructureNode { | ||
11 | pub parent: Option<usize>, | ||
12 | pub label: String, | ||
13 | pub navigation_range: TextRange, | ||
14 | pub node_range: TextRange, | ||
15 | pub kind: SyntaxKind, | ||
16 | pub detail: Option<String>, | ||
17 | pub deprecated: bool, | ||
18 | } | ||
19 | |||
20 | pub fn file_structure(file: &SourceFile) -> Vec<StructureNode> { | ||
21 | let mut res = Vec::new(); | ||
22 | let mut stack = Vec::new(); | ||
23 | |||
24 | for event in file.syntax().preorder() { | ||
25 | match event { | ||
26 | WalkEvent::Enter(node) => { | ||
27 | if let Some(mut symbol) = structure_node(node) { | ||
28 | symbol.parent = stack.last().map(|&n| n); | ||
29 | stack.push(res.len()); | ||
30 | res.push(symbol); | ||
31 | } | ||
32 | } | ||
33 | WalkEvent::Leave(node) => { | ||
34 | if structure_node(node).is_some() { | ||
35 | stack.pop().unwrap(); | ||
36 | } | ||
37 | } | ||
38 | } | ||
39 | } | ||
40 | res | ||
41 | } | ||
42 | |||
43 | fn structure_node(node: &SyntaxNode) -> Option<StructureNode> { | ||
44 | fn decl<N: NameOwner + AttrsOwner>(node: &N) -> Option<StructureNode> { | ||
45 | decl_with_detail(node, None) | ||
46 | } | ||
47 | |||
48 | fn decl_with_ascription<N: NameOwner + AttrsOwner + TypeAscriptionOwner>( | ||
49 | node: &N, | ||
50 | ) -> Option<StructureNode> { | ||
51 | decl_with_type_ref(node, node.ascribed_type()) | ||
52 | } | ||
53 | |||
54 | fn decl_with_type_ref<N: NameOwner + AttrsOwner>( | ||
55 | node: &N, | ||
56 | type_ref: Option<&ast::TypeRef>, | ||
57 | ) -> Option<StructureNode> { | ||
58 | let detail = type_ref.map(|type_ref| { | ||
59 | let mut detail = String::new(); | ||
60 | collapse_ws(type_ref.syntax(), &mut detail); | ||
61 | detail | ||
62 | }); | ||
63 | decl_with_detail(node, detail) | ||
64 | } | ||
65 | |||
66 | fn decl_with_detail<N: NameOwner + AttrsOwner>( | ||
67 | node: &N, | ||
68 | detail: Option<String>, | ||
69 | ) -> Option<StructureNode> { | ||
70 | let name = node.name()?; | ||
71 | |||
72 | Some(StructureNode { | ||
73 | parent: None, | ||
74 | label: name.text().to_string(), | ||
75 | navigation_range: name.syntax().range(), | ||
76 | node_range: node.syntax().range(), | ||
77 | kind: node.syntax().kind(), | ||
78 | detail, | ||
79 | deprecated: node.attrs().filter_map(|x| x.as_named()).any(|x| x == "deprecated"), | ||
80 | }) | ||
81 | } | ||
82 | |||
83 | fn collapse_ws(node: &SyntaxNode, output: &mut String) { | ||
84 | let mut can_insert_ws = false; | ||
85 | for line in node.text().chunks().flat_map(|chunk| chunk.lines()) { | ||
86 | let line = line.trim(); | ||
87 | if line.is_empty() { | ||
88 | if can_insert_ws { | ||
89 | output.push_str(" "); | ||
90 | can_insert_ws = false; | ||
91 | } | ||
92 | } else { | ||
93 | output.push_str(line); | ||
94 | can_insert_ws = true; | ||
95 | } | ||
96 | } | ||
97 | } | ||
98 | |||
99 | visitor() | ||
100 | .visit(|fn_def: &ast::FnDef| { | ||
101 | let mut detail = String::from("fn"); | ||
102 | if let Some(type_param_list) = fn_def.type_param_list() { | ||
103 | collapse_ws(type_param_list.syntax(), &mut detail); | ||
104 | } | ||
105 | if let Some(param_list) = fn_def.param_list() { | ||
106 | collapse_ws(param_list.syntax(), &mut detail); | ||
107 | } | ||
108 | if let Some(ret_type) = fn_def.ret_type() { | ||
109 | detail.push_str(" "); | ||
110 | collapse_ws(ret_type.syntax(), &mut detail); | ||
111 | } | ||
112 | |||
113 | decl_with_detail(fn_def, Some(detail)) | ||
114 | }) | ||
115 | .visit(decl::<ast::StructDef>) | ||
116 | .visit(decl::<ast::EnumDef>) | ||
117 | .visit(decl::<ast::EnumVariant>) | ||
118 | .visit(decl::<ast::TraitDef>) | ||
119 | .visit(decl::<ast::Module>) | ||
120 | .visit(|td: &ast::TypeAliasDef| decl_with_type_ref(td, td.type_ref())) | ||
121 | .visit(decl_with_ascription::<ast::NamedFieldDef>) | ||
122 | .visit(decl_with_ascription::<ast::ConstDef>) | ||
123 | .visit(decl_with_ascription::<ast::StaticDef>) | ||
124 | .visit(|im: &ast::ImplBlock| { | ||
125 | let target_type = im.target_type()?; | ||
126 | let target_trait = im.target_trait(); | ||
127 | let label = match target_trait { | ||
128 | None => format!("impl {}", target_type.syntax().text()), | ||
129 | Some(t) => { | ||
130 | format!("impl {} for {}", t.syntax().text(), target_type.syntax().text(),) | ||
131 | } | ||
132 | }; | ||
133 | |||
134 | let node = StructureNode { | ||
135 | parent: None, | ||
136 | label, | ||
137 | navigation_range: target_type.syntax().range(), | ||
138 | node_range: im.syntax().range(), | ||
139 | kind: im.syntax().kind(), | ||
140 | detail: None, | ||
141 | deprecated: false, | ||
142 | }; | ||
143 | Some(node) | ||
144 | }) | ||
145 | .accept(node)? | ||
146 | } | ||
147 | |||
148 | #[cfg(test)] | ||
149 | mod tests { | ||
150 | use super::*; | ||
151 | use insta::assert_debug_snapshot_matches; | ||
152 | |||
153 | #[test] | ||
154 | fn test_file_structure() { | ||
155 | let file = SourceFile::parse( | ||
156 | r#" | ||
157 | struct Foo { | ||
158 | x: i32 | ||
159 | } | ||
160 | |||
161 | mod m { | ||
162 | fn bar1() {} | ||
163 | fn bar2<T>(t: T) -> T {} | ||
164 | fn bar3<A, | ||
165 | B>(a: A, | ||
166 | b: B) -> Vec< | ||
167 | u32 | ||
168 | > {} | ||
169 | } | ||
170 | |||
171 | enum E { X, Y(i32) } | ||
172 | type T = (); | ||
173 | static S: i32 = 92; | ||
174 | const C: i32 = 92; | ||
175 | |||
176 | impl E {} | ||
177 | |||
178 | impl fmt::Debug for E {} | ||
179 | |||
180 | #[deprecated] | ||
181 | fn obsolete() {} | ||
182 | |||
183 | #[deprecated(note = "for awhile")] | ||
184 | fn very_obsolete() {} | ||
185 | "#, | ||
186 | ); | ||
187 | let structure = file_structure(&file); | ||
188 | assert_debug_snapshot_matches!("file_structure", structure); | ||
189 | } | ||
190 | } | ||