1 use ide_db::SymbolKind;
3 ast::{self, HasAttrs, HasGenericParams, HasName},
4 match_ast, AstNode, AstToken, NodeOrToken, SourceFile, SyntaxNode, SyntaxToken, TextRange,
8 #[derive(Debug, Clone)]
9 pub struct StructureNode {
10 pub parent: Option<usize>,
12 pub navigation_range: TextRange,
13 pub node_range: TextRange,
14 pub kind: StructureNodeKind,
15 pub detail: Option<String>,
19 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
20 pub enum StructureNodeKind {
21 SymbolKind(SymbolKind),
25 // Feature: File Structure
27 // Provides a tree of the symbols defined in the file. Can be used to
29 // * fuzzy search symbol in a file (super useful)
30 // * draw breadcrumbs to describe the context around the cursor
31 // * draw outline of the file
34 // | Editor | Shortcut
36 // | VS Code | kbd:[Ctrl+Shift+O]
39 // image::https://user-images.githubusercontent.com/48062697/113020654-b42fc800-917a-11eb-8388-e7dc4d92b02e.gif[]
41 pub(crate) fn file_structure(file: &SourceFile) -> Vec<StructureNode> {
42 let mut res = Vec::new();
43 let mut stack = Vec::new();
45 for event in file.syntax().preorder_with_tokens() {
47 WalkEvent::Enter(NodeOrToken::Node(node)) => {
48 if let Some(mut symbol) = structure_node(&node) {
49 symbol.parent = stack.last().copied();
50 stack.push(res.len());
54 WalkEvent::Leave(NodeOrToken::Node(node)) => {
55 if structure_node(&node).is_some() {
59 WalkEvent::Enter(NodeOrToken::Token(token)) => {
60 if let Some(mut symbol) = structure_token(token) {
61 symbol.parent = stack.last().copied();
62 stack.push(res.len());
66 WalkEvent::Leave(NodeOrToken::Token(token)) => {
67 if structure_token(token).is_some() {
76 fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
77 fn decl<N: HasName + HasAttrs>(node: N, kind: StructureNodeKind) -> Option<StructureNode> {
78 decl_with_detail(&node, None, kind)
81 fn decl_with_type_ref<N: HasName + HasAttrs>(
83 type_ref: Option<ast::Type>,
84 kind: StructureNodeKind,
85 ) -> Option<StructureNode> {
86 let detail = type_ref.map(|type_ref| {
87 let mut detail = String::new();
88 collapse_ws(type_ref.syntax(), &mut detail);
91 decl_with_detail(node, detail, kind)
94 fn decl_with_detail<N: HasName + HasAttrs>(
96 detail: Option<String>,
97 kind: StructureNodeKind,
98 ) -> Option<StructureNode> {
99 let name = node.name()?;
103 label: name.text().to_string(),
104 navigation_range: name.syntax().text_range(),
105 node_range: node.syntax().text_range(),
108 deprecated: node.attrs().filter_map(|x| x.simple_name()).any(|x| x == "deprecated"),
112 fn collapse_ws(node: &SyntaxNode, output: &mut String) {
113 let mut can_insert_ws = false;
114 node.text().for_each_chunk(|chunk| {
115 for line in chunk.lines() {
116 let line = line.trim();
120 can_insert_ws = false;
123 output.push_str(line);
124 can_insert_ws = true;
133 let mut detail = String::from("fn");
134 if let Some(type_param_list) = it.generic_param_list() {
135 collapse_ws(type_param_list.syntax(), &mut detail);
137 if let Some(param_list) = it.param_list() {
138 collapse_ws(param_list.syntax(), &mut detail);
140 if let Some(ret_type) = it.ret_type() {
141 detail.push_str(" ");
142 collapse_ws(ret_type.syntax(), &mut detail);
145 decl_with_detail(&it, Some(detail), StructureNodeKind::SymbolKind(SymbolKind::Function))
147 ast::Struct(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Struct)),
148 ast::Union(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Union)),
149 ast::Enum(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Enum)),
150 ast::Variant(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Variant)),
151 ast::Trait(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Trait)),
152 ast::Module(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Module)),
153 ast::TypeAlias(it) => decl_with_type_ref(&it, it.ty(), StructureNodeKind::SymbolKind(SymbolKind::TypeAlias)),
154 ast::RecordField(it) => decl_with_type_ref(&it, it.ty(), StructureNodeKind::SymbolKind(SymbolKind::Field)),
155 ast::Const(it) => decl_with_type_ref(&it, it.ty(), StructureNodeKind::SymbolKind(SymbolKind::Const)),
156 ast::Static(it) => decl_with_type_ref(&it, it.ty(), StructureNodeKind::SymbolKind(SymbolKind::Static)),
158 let target_type = it.self_ty()?;
159 let target_trait = it.trait_();
160 let label = match target_trait {
161 None => format!("impl {}", target_type.syntax().text()),
163 format!("impl {} for {}", t.syntax().text(), target_type.syntax().text(),)
167 let node = StructureNode {
170 navigation_range: target_type.syntax().text_range(),
171 node_range: it.syntax().text_range(),
172 kind: StructureNodeKind::SymbolKind(SymbolKind::Impl),
178 ast::Macro(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Macro)),
184 fn structure_token(token: SyntaxToken) -> Option<StructureNode> {
185 if let Some(comment) = ast::Comment::cast(token) {
186 let text = comment.text().trim();
188 if let Some(region_name) = text.strip_prefix("// region:").map(str::trim) {
189 return Some(StructureNode {
191 label: region_name.to_string(),
192 navigation_range: comment.syntax().text_range(),
193 node_range: comment.syntax().text_range(),
194 kind: StructureNodeKind::Region,
206 use expect_test::{expect, Expect};
210 fn check(ra_fixture: &str, expect: Expect) {
211 let file = SourceFile::parse(ra_fixture).ok().unwrap();
212 let structure = file_structure(&file);
213 expect.assert_debug_eq(&structure)
217 fn test_file_structure() {
226 fn bar2<T>(t: T) -> T {}
241 impl fmt::Debug for E {}
260 #[deprecated(note = "for awhile")]
261 fn very_obsolete() {}
263 // region: Some region name
278 navigation_range: 8..11,
291 navigation_range: 18..19,
304 navigation_range: 32..33,
317 navigation_range: 43..47,
332 navigation_range: 60..64,
347 navigation_range: 89..93,
353 "fn<A, B>(a: A, b: B) -> Vec< u32 >",
360 navigation_range: 165..166,
361 node_range: 160..180,
373 navigation_range: 169..170,
374 node_range: 169..170,
386 navigation_range: 172..173,
387 node_range: 172..178,
397 navigation_range: 186..187,
398 node_range: 181..193,
410 navigation_range: 201..202,
411 node_range: 194..213,
423 navigation_range: 220..221,
424 node_range: 214..232,
436 navigation_range: 239..240,
437 node_range: 234..243,
446 label: "impl fmt::Debug for E",
447 navigation_range: 265..266,
448 node_range: 245..269,
458 navigation_range: 284..286,
459 node_range: 271..303,
469 navigation_range: 334..339,
470 node_range: 305..356,
480 navigation_range: 387..392,
481 node_range: 358..409,
491 navigation_range: 428..436,
492 node_range: 411..441,
503 label: "very_obsolete",
504 navigation_range: 481..494,
505 node_range: 443..499,
516 label: "Some region name",
517 navigation_range: 501..528,
518 node_range: 501..528,
526 navigation_range: 568..569,
527 node_range: 543..606,
539 navigation_range: 543..563,
540 node_range: 543..563,
550 navigation_range: 575..576,
551 node_range: 572..581,
565 navigation_range: 598..599,
566 node_range: 582..604,