aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_editor/src/symbols.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_editor/src/symbols.rs')
-rw-r--r--crates/ra_editor/src/symbols.rs40
1 files changed, 35 insertions, 5 deletions
diff --git a/crates/ra_editor/src/symbols.rs b/crates/ra_editor/src/symbols.rs
index 4e602d0e3..6d3b0514a 100644
--- a/crates/ra_editor/src/symbols.rs
+++ b/crates/ra_editor/src/symbols.rs
@@ -2,8 +2,8 @@ use crate::TextRange;
2 2
3use ra_syntax::{ 3use ra_syntax::{
4 algo::visit::{visitor, Visitor}, 4 algo::visit::{visitor, Visitor},
5 ast::{self, NameOwner}, 5 ast::{self, DocCommentsOwner, NameOwner},
6 AstNode, File, SmolStr, SyntaxKind, SyntaxNodeRef, WalkEvent, 6 AstNode, SourceFileNode, SmolStr, SyntaxKind, SyntaxNodeRef, WalkEvent,
7}; 7};
8 8
9#[derive(Debug, Clone)] 9#[derive(Debug, Clone)]
@@ -22,7 +22,37 @@ pub struct FileSymbol {
22 pub kind: SyntaxKind, 22 pub kind: SyntaxKind,
23} 23}
24 24
25pub fn file_symbols(file: &File) -> Vec<FileSymbol> { 25impl FileSymbol {
26 pub fn docs(&self, file: &SourceFileNode) -> Option<String> {
27 file.syntax()
28 .descendants()
29 .filter(|node| node.kind() == self.kind && node.range() == self.node_range)
30 .filter_map(|node: SyntaxNodeRef| {
31 fn doc_comments<'a, N: DocCommentsOwner<'a>>(node: N) -> Option<String> {
32 let comments = node.doc_comment_text();
33 if comments.is_empty() {
34 None
35 } else {
36 Some(comments)
37 }
38 }
39
40 visitor()
41 .visit(doc_comments::<ast::FnDef>)
42 .visit(doc_comments::<ast::StructDef>)
43 .visit(doc_comments::<ast::EnumDef>)
44 .visit(doc_comments::<ast::TraitDef>)
45 .visit(doc_comments::<ast::Module>)
46 .visit(doc_comments::<ast::TypeDef>)
47 .visit(doc_comments::<ast::ConstDef>)
48 .visit(doc_comments::<ast::StaticDef>)
49 .accept(node)?
50 })
51 .nth(0)
52 }
53}
54
55pub fn file_symbols(file: &SourceFileNode) -> Vec<FileSymbol> {
26 file.syntax().descendants().filter_map(to_symbol).collect() 56 file.syntax().descendants().filter_map(to_symbol).collect()
27} 57}
28 58
@@ -47,7 +77,7 @@ fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> {
47 .accept(node)? 77 .accept(node)?
48} 78}
49 79
50pub fn file_structure(file: &File) -> Vec<StructureNode> { 80pub fn file_structure(file: &SourceFileNode) -> Vec<StructureNode> {
51 let mut res = Vec::new(); 81 let mut res = Vec::new();
52 let mut stack = Vec::new(); 82 let mut stack = Vec::new();
53 83
@@ -123,7 +153,7 @@ mod tests {
123 153
124 #[test] 154 #[test]
125 fn test_file_structure() { 155 fn test_file_structure() {
126 let file = File::parse( 156 let file = SourceFileNode::parse(
127 r#" 157 r#"
128struct Foo { 158struct Foo {
129 x: i32 159 x: i32