aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src
diff options
context:
space:
mode:
authorSergey Parilin <[email protected]>2019-03-22 12:54:26 +0000
committerSergey Parilin <[email protected]>2019-03-25 08:41:21 +0000
commit36cb58f76d702d87f445ee7eefe26a0bc1ee3811 (patch)
treea13de9e2de33ec86524d360acaa0793540363c2f /crates/ra_ide_api/src
parentafe96b75eae99fe2dcd7fb7c99e15507290b3d5f (diff)
structure moved to ra_ide_api
ra_ide_api_light removed completely
Diffstat (limited to 'crates/ra_ide_api/src')
-rw-r--r--crates/ra_ide_api/src/lib.rs11
-rw-r--r--crates/ra_ide_api/src/snapshots/tests__file_structure.snap182
-rw-r--r--crates/ra_ide_api/src/structure.rs190
3 files changed, 377 insertions, 6 deletions
diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs
index 8aa3eb088..9063f78a9 100644
--- a/crates/ra_ide_api/src/lib.rs
+++ b/crates/ra_ide_api/src/lib.rs
@@ -6,9 +6,6 @@
6//! database, and the `ra_hir` crate, where majority of the analysis happens. 6//! database, and the `ra_hir` crate, where majority of the analysis happens.
7//! However, IDE specific bits of the analysis (most notably completion) happen 7//! However, IDE specific bits of the analysis (most notably completion) happen
8//! in this crate. 8//! in this crate.
9//!
10//! The sibling `ra_ide_api_light` handles those bits of IDE functionality
11//! which are restricted to a single file and need only syntax.
12 9
13// For proving that RootDatabase is RefUnwindSafe. 10// For proving that RootDatabase is RefUnwindSafe.
14#![recursion_limit = "128"] 11#![recursion_limit = "128"]
@@ -33,10 +30,11 @@ mod impls;
33mod assists; 30mod assists;
34mod diagnostics; 31mod diagnostics;
35mod syntax_tree; 32mod syntax_tree;
36mod line_index;
37mod folding_ranges; 33mod folding_ranges;
34mod line_index;
38mod line_index_utils; 35mod line_index_utils;
39mod join_lines; 36mod join_lines;
37mod structure;
40mod typing; 38mod typing;
41mod matching_brace; 39mod matching_brace;
42 40
@@ -72,9 +70,10 @@ pub use crate::{
72 line_index_utils::translate_offset_with_edit, 70 line_index_utils::translate_offset_with_edit,
73 folding_ranges::{Fold, FoldKind}, 71 folding_ranges::{Fold, FoldKind},
74 syntax_highlighting::HighlightedRange, 72 syntax_highlighting::HighlightedRange,
73 structure::{StructureNode, file_structure},
75 diagnostics::Severity, 74 diagnostics::Severity,
76}; 75};
77pub use ra_ide_api_light::StructureNode; 76
78pub use ra_db::{ 77pub use ra_db::{
79 Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId, 78 Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId,
80 Edition 79 Edition
@@ -388,7 +387,7 @@ impl Analysis {
388 /// file outline. 387 /// file outline.
389 pub fn file_structure(&self, file_id: FileId) -> Vec<StructureNode> { 388 pub fn file_structure(&self, file_id: FileId) -> Vec<StructureNode> {
390 let file = self.db.parse(file_id); 389 let file = self.db.parse(file_id);
391 ra_ide_api_light::file_structure(&file) 390 structure::file_structure(&file)
392 } 391 }
393 392
394 /// Returns the set of folding ranges. 393 /// Returns the set of folding ranges.
diff --git a/crates/ra_ide_api/src/snapshots/tests__file_structure.snap b/crates/ra_ide_api/src/snapshots/tests__file_structure.snap
new file mode 100644
index 000000000..2efa8e22c
--- /dev/null
+++ b/crates/ra_ide_api/src/snapshots/tests__file_structure.snap
@@ -0,0 +1,182 @@
1---
2created: "2019-02-05T22:03:50.763530100Z"
3creator: [email protected]
4source: crates/ra_ide_api/src/structure.rs
5expression: structure
6---
7[
8 StructureNode {
9 parent: None,
10 label: "Foo",
11 navigation_range: [8; 11),
12 node_range: [1; 26),
13 kind: STRUCT_DEF,
14 detail: None,
15 deprecated: false
16 },
17 StructureNode {
18 parent: Some(
19 0
20 ),
21 label: "x",
22 navigation_range: [18; 19),
23 node_range: [18; 24),
24 kind: NAMED_FIELD_DEF,
25 detail: Some(
26 "i32"
27 ),
28 deprecated: false
29 },
30 StructureNode {
31 parent: None,
32 label: "m",
33 navigation_range: [32; 33),
34 node_range: [28; 158),
35 kind: MODULE,
36 detail: None,
37 deprecated: false
38 },
39 StructureNode {
40 parent: Some(
41 2
42 ),
43 label: "bar1",
44 navigation_range: [43; 47),
45 node_range: [40; 52),
46 kind: FN_DEF,
47 detail: Some(
48 "fn()"
49 ),
50 deprecated: false
51 },
52 StructureNode {
53 parent: Some(
54 2
55 ),
56 label: "bar2",
57 navigation_range: [60; 64),
58 node_range: [57; 81),
59 kind: FN_DEF,
60 detail: Some(
61 "fn<T>(t: T) -> T"
62 ),
63 deprecated: false
64 },
65 StructureNode {
66 parent: Some(
67 2
68 ),
69 label: "bar3",
70 navigation_range: [89; 93),
71 node_range: [86; 156),
72 kind: FN_DEF,
73 detail: Some(
74 "fn<A, B>(a: A, b: B) -> Vec< u32 >"
75 ),
76 deprecated: false
77 },
78 StructureNode {
79 parent: None,
80 label: "E",
81 navigation_range: [165; 166),
82 node_range: [160; 180),
83 kind: ENUM_DEF,
84 detail: None,
85 deprecated: false
86 },
87 StructureNode {
88 parent: Some(
89 6
90 ),
91 label: "X",
92 navigation_range: [169; 170),
93 node_range: [169; 170),
94 kind: ENUM_VARIANT,
95 detail: None,
96 deprecated: false
97 },
98 StructureNode {
99 parent: Some(
100 6
101 ),
102 label: "Y",
103 navigation_range: [172; 173),
104 node_range: [172; 178),
105 kind: ENUM_VARIANT,
106 detail: None,
107 deprecated: false
108 },
109 StructureNode {
110 parent: None,
111 label: "T",
112 navigation_range: [186; 187),
113 node_range: [181; 193),
114 kind: TYPE_ALIAS_DEF,
115 detail: Some(
116 "()"
117 ),
118 deprecated: false
119 },
120 StructureNode {
121 parent: None,
122 label: "S",
123 navigation_range: [201; 202),
124 node_range: [194; 213),
125 kind: STATIC_DEF,
126 detail: Some(
127 "i32"
128 ),
129 deprecated: false
130 },
131 StructureNode {
132 parent: None,
133 label: "C",
134 navigation_range: [220; 221),
135 node_range: [214; 232),
136 kind: CONST_DEF,
137 detail: Some(
138 "i32"
139 ),
140 deprecated: false
141 },
142 StructureNode {
143 parent: None,
144 label: "impl E",
145 navigation_range: [239; 240),
146 node_range: [234; 243),
147 kind: IMPL_BLOCK,
148 detail: None,
149 deprecated: false
150 },
151 StructureNode {
152 parent: None,
153 label: "impl fmt::Debug for E",
154 navigation_range: [265; 266),
155 node_range: [245; 269),
156 kind: IMPL_BLOCK,
157 detail: None,
158 deprecated: false
159 },
160 StructureNode {
161 parent: None,
162 label: "obsolete",
163 navigation_range: [288; 296),
164 node_range: [271; 301),
165 kind: FN_DEF,
166 detail: Some(
167 "fn()"
168 ),
169 deprecated: true
170 },
171 StructureNode {
172 parent: None,
173 label: "very_obsolete",
174 navigation_range: [341; 354),
175 node_range: [303; 359),
176 kind: FN_DEF,
177 detail: Some(
178 "fn()"
179 ),
180 deprecated: true
181 }
182]
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 @@
1use crate::TextRange;
2
3use 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)]
10pub 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
20pub 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
43fn 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)]
149mod 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#"
157struct Foo {
158 x: i32
159}
160
161mod 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
171enum E { X, Y(i32) }
172type T = ();
173static S: i32 = 92;
174const C: i32 = 92;
175
176impl E {}
177
178impl fmt::Debug for E {}
179
180#[deprecated]
181fn obsolete() {}
182
183#[deprecated(note = "for awhile")]
184fn very_obsolete() {}
185"#,
186 );
187 let structure = file_structure(&file);
188 assert_debug_snapshot_matches!("file_structure", structure);
189 }
190}