diff options
Diffstat (limited to 'crates/ra_ide_api/src/syntax_tree.rs')
-rw-r--r-- | crates/ra_ide_api/src/syntax_tree.rs | 257 |
1 files changed, 257 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/syntax_tree.rs b/crates/ra_ide_api/src/syntax_tree.rs index bbe9222b4..276f8a8c8 100644 --- a/crates/ra_ide_api/src/syntax_tree.rs +++ b/crates/ra_ide_api/src/syntax_tree.rs | |||
@@ -85,3 +85,260 @@ fn syntax_tree_for_token<T: AstToken>(node: &T, text_range: TextRange) -> Option | |||
85 | 85 | ||
86 | None | 86 | None |
87 | } | 87 | } |
88 | |||
89 | #[cfg(test)] | ||
90 | mod tests { | ||
91 | use crate::mock_analysis::{single_file, single_file_with_range}; | ||
92 | |||
93 | #[test] | ||
94 | fn test_syntax_tree_without_range() { | ||
95 | // Basic syntax | ||
96 | let (analysis, file_id) = single_file(r#"fn foo() {}"#); | ||
97 | let syn = analysis.syntax_tree(file_id, None); | ||
98 | |||
99 | assert_eq!( | ||
100 | syn.trim(), | ||
101 | r#" | ||
102 | SOURCE_FILE@[0; 11) | ||
103 | FN_DEF@[0; 11) | ||
104 | FN_KW@[0; 2) | ||
105 | WHITESPACE@[2; 3) | ||
106 | NAME@[3; 6) | ||
107 | IDENT@[3; 6) "foo" | ||
108 | PARAM_LIST@[6; 8) | ||
109 | L_PAREN@[6; 7) | ||
110 | R_PAREN@[7; 8) | ||
111 | WHITESPACE@[8; 9) | ||
112 | BLOCK@[9; 11) | ||
113 | L_CURLY@[9; 10) | ||
114 | R_CURLY@[10; 11) | ||
115 | "# | ||
116 | .trim() | ||
117 | ); | ||
118 | |||
119 | let (analysis, file_id) = single_file( | ||
120 | r#" | ||
121 | fn test() { | ||
122 | assert!(" | ||
123 | fn foo() { | ||
124 | } | ||
125 | ", ""); | ||
126 | }"# | ||
127 | .trim(), | ||
128 | ); | ||
129 | let syn = analysis.syntax_tree(file_id, None); | ||
130 | |||
131 | assert_eq!( | ||
132 | syn.trim(), | ||
133 | r#" | ||
134 | SOURCE_FILE@[0; 60) | ||
135 | FN_DEF@[0; 60) | ||
136 | FN_KW@[0; 2) | ||
137 | WHITESPACE@[2; 3) | ||
138 | NAME@[3; 7) | ||
139 | IDENT@[3; 7) "test" | ||
140 | PARAM_LIST@[7; 9) | ||
141 | L_PAREN@[7; 8) | ||
142 | R_PAREN@[8; 9) | ||
143 | WHITESPACE@[9; 10) | ||
144 | BLOCK@[10; 60) | ||
145 | L_CURLY@[10; 11) | ||
146 | WHITESPACE@[11; 16) | ||
147 | EXPR_STMT@[16; 58) | ||
148 | MACRO_CALL@[16; 57) | ||
149 | PATH@[16; 22) | ||
150 | PATH_SEGMENT@[16; 22) | ||
151 | NAME_REF@[16; 22) | ||
152 | IDENT@[16; 22) "assert" | ||
153 | EXCL@[22; 23) | ||
154 | TOKEN_TREE@[23; 57) | ||
155 | L_PAREN@[23; 24) | ||
156 | STRING@[24; 52) | ||
157 | COMMA@[52; 53) | ||
158 | WHITESPACE@[53; 54) | ||
159 | STRING@[54; 56) | ||
160 | R_PAREN@[56; 57) | ||
161 | SEMI@[57; 58) | ||
162 | WHITESPACE@[58; 59) | ||
163 | R_CURLY@[59; 60) | ||
164 | "# | ||
165 | .trim() | ||
166 | ); | ||
167 | } | ||
168 | |||
169 | #[test] | ||
170 | fn test_syntax_tree_with_range() { | ||
171 | let (analysis, range) = single_file_with_range(r#"<|>fn foo() {}<|>"#.trim()); | ||
172 | let syn = analysis.syntax_tree(range.file_id, Some(range.range)); | ||
173 | |||
174 | assert_eq!( | ||
175 | syn.trim(), | ||
176 | r#" | ||
177 | FN_DEF@[0; 11) | ||
178 | FN_KW@[0; 2) | ||
179 | WHITESPACE@[2; 3) | ||
180 | NAME@[3; 6) | ||
181 | IDENT@[3; 6) "foo" | ||
182 | PARAM_LIST@[6; 8) | ||
183 | L_PAREN@[6; 7) | ||
184 | R_PAREN@[7; 8) | ||
185 | WHITESPACE@[8; 9) | ||
186 | BLOCK@[9; 11) | ||
187 | L_CURLY@[9; 10) | ||
188 | R_CURLY@[10; 11) | ||
189 | "# | ||
190 | .trim() | ||
191 | ); | ||
192 | |||
193 | let (analysis, range) = single_file_with_range( | ||
194 | r#"fn test() { | ||
195 | <|>assert!(" | ||
196 | fn foo() { | ||
197 | } | ||
198 | ", "");<|> | ||
199 | }"# | ||
200 | .trim(), | ||
201 | ); | ||
202 | let syn = analysis.syntax_tree(range.file_id, Some(range.range)); | ||
203 | |||
204 | assert_eq!( | ||
205 | syn.trim(), | ||
206 | r#" | ||
207 | EXPR_STMT@[16; 58) | ||
208 | MACRO_CALL@[16; 57) | ||
209 | PATH@[16; 22) | ||
210 | PATH_SEGMENT@[16; 22) | ||
211 | NAME_REF@[16; 22) | ||
212 | IDENT@[16; 22) "assert" | ||
213 | EXCL@[22; 23) | ||
214 | TOKEN_TREE@[23; 57) | ||
215 | L_PAREN@[23; 24) | ||
216 | STRING@[24; 52) | ||
217 | COMMA@[52; 53) | ||
218 | WHITESPACE@[53; 54) | ||
219 | STRING@[54; 56) | ||
220 | R_PAREN@[56; 57) | ||
221 | SEMI@[57; 58) | ||
222 | "# | ||
223 | .trim() | ||
224 | ); | ||
225 | } | ||
226 | |||
227 | #[test] | ||
228 | fn test_syntax_tree_inside_string() { | ||
229 | let (analysis, range) = single_file_with_range( | ||
230 | r#"fn test() { | ||
231 | assert!(" | ||
232 | <|>fn foo() { | ||
233 | }<|> | ||
234 | fn bar() { | ||
235 | } | ||
236 | ", ""); | ||
237 | }"# | ||
238 | .trim(), | ||
239 | ); | ||
240 | let syn = analysis.syntax_tree(range.file_id, Some(range.range)); | ||
241 | assert_eq!( | ||
242 | syn.trim(), | ||
243 | r#" | ||
244 | SOURCE_FILE@[0; 12) | ||
245 | FN_DEF@[0; 12) | ||
246 | FN_KW@[0; 2) | ||
247 | WHITESPACE@[2; 3) | ||
248 | NAME@[3; 6) | ||
249 | IDENT@[3; 6) "foo" | ||
250 | PARAM_LIST@[6; 8) | ||
251 | L_PAREN@[6; 7) | ||
252 | R_PAREN@[7; 8) | ||
253 | WHITESPACE@[8; 9) | ||
254 | BLOCK@[9; 12) | ||
255 | L_CURLY@[9; 10) | ||
256 | WHITESPACE@[10; 11) | ||
257 | R_CURLY@[11; 12) | ||
258 | "# | ||
259 | .trim() | ||
260 | ); | ||
261 | |||
262 | // With a raw string | ||
263 | let (analysis, range) = single_file_with_range( | ||
264 | r###"fn test() { | ||
265 | assert!(r#" | ||
266 | <|>fn foo() { | ||
267 | }<|> | ||
268 | fn bar() { | ||
269 | } | ||
270 | "#, ""); | ||
271 | }"### | ||
272 | .trim(), | ||
273 | ); | ||
274 | let syn = analysis.syntax_tree(range.file_id, Some(range.range)); | ||
275 | assert_eq!( | ||
276 | syn.trim(), | ||
277 | r#" | ||
278 | SOURCE_FILE@[0; 12) | ||
279 | FN_DEF@[0; 12) | ||
280 | FN_KW@[0; 2) | ||
281 | WHITESPACE@[2; 3) | ||
282 | NAME@[3; 6) | ||
283 | IDENT@[3; 6) "foo" | ||
284 | PARAM_LIST@[6; 8) | ||
285 | L_PAREN@[6; 7) | ||
286 | R_PAREN@[7; 8) | ||
287 | WHITESPACE@[8; 9) | ||
288 | BLOCK@[9; 12) | ||
289 | L_CURLY@[9; 10) | ||
290 | WHITESPACE@[10; 11) | ||
291 | R_CURLY@[11; 12) | ||
292 | "# | ||
293 | .trim() | ||
294 | ); | ||
295 | |||
296 | // With a raw string | ||
297 | let (analysis, range) = single_file_with_range( | ||
298 | r###"fn test() { | ||
299 | assert!(r<|>#" | ||
300 | fn foo() { | ||
301 | } | ||
302 | fn bar() { | ||
303 | }"<|>#, ""); | ||
304 | }"### | ||
305 | .trim(), | ||
306 | ); | ||
307 | let syn = analysis.syntax_tree(range.file_id, Some(range.range)); | ||
308 | assert_eq!( | ||
309 | syn.trim(), | ||
310 | r#" | ||
311 | SOURCE_FILE@[0; 25) | ||
312 | FN_DEF@[0; 12) | ||
313 | FN_KW@[0; 2) | ||
314 | WHITESPACE@[2; 3) | ||
315 | NAME@[3; 6) | ||
316 | IDENT@[3; 6) "foo" | ||
317 | PARAM_LIST@[6; 8) | ||
318 | L_PAREN@[6; 7) | ||
319 | R_PAREN@[7; 8) | ||
320 | WHITESPACE@[8; 9) | ||
321 | BLOCK@[9; 12) | ||
322 | L_CURLY@[9; 10) | ||
323 | WHITESPACE@[10; 11) | ||
324 | R_CURLY@[11; 12) | ||
325 | WHITESPACE@[12; 13) | ||
326 | FN_DEF@[13; 25) | ||
327 | FN_KW@[13; 15) | ||
328 | WHITESPACE@[15; 16) | ||
329 | NAME@[16; 19) | ||
330 | IDENT@[16; 19) "bar" | ||
331 | PARAM_LIST@[19; 21) | ||
332 | L_PAREN@[19; 20) | ||
333 | R_PAREN@[20; 21) | ||
334 | WHITESPACE@[21; 22) | ||
335 | BLOCK@[22; 25) | ||
336 | L_CURLY@[22; 23) | ||
337 | WHITESPACE@[23; 24) | ||
338 | R_CURLY@[24; 25) | ||
339 | |||
340 | "# | ||
341 | .trim() | ||
342 | ); | ||
343 | } | ||
344 | } | ||