aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/syntax_tree.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/syntax_tree.rs')
-rw-r--r--crates/ide/src/syntax_tree.rs52
1 files changed, 26 insertions, 26 deletions
diff --git a/crates/ide/src/syntax_tree.rs b/crates/ide/src/syntax_tree.rs
index 6dd05c05d..4c63d3023 100644
--- a/crates/ide/src/syntax_tree.rs
+++ b/crates/ide/src/syntax_tree.rs
@@ -1,7 +1,7 @@
1use ide_db::base_db::{FileId, SourceDatabase}; 1use ide_db::base_db::{FileId, SourceDatabase};
2use ide_db::RootDatabase; 2use ide_db::RootDatabase;
3use syntax::{ 3use syntax::{
4 algo, AstNode, NodeOrToken, SourceFile, SyntaxKind::STRING, SyntaxToken, TextRange, TextSize, 4 AstNode, NodeOrToken, SourceFile, SyntaxKind::STRING, SyntaxToken, TextRange, TextSize,
5}; 5};
6 6
7// Feature: Show Syntax Tree 7// Feature: Show Syntax Tree
@@ -21,7 +21,7 @@ pub(crate) fn syntax_tree(
21) -> String { 21) -> String {
22 let parse = db.parse(file_id); 22 let parse = db.parse(file_id);
23 if let Some(text_range) = text_range { 23 if let Some(text_range) = text_range {
24 let node = match algo::find_covering_element(parse.tree().syntax(), text_range) { 24 let node = match parse.tree().syntax().covering_element(text_range) {
25 NodeOrToken::Node(node) => node, 25 NodeOrToken::Node(node) => node,
26 NodeOrToken::Token(token) => { 26 NodeOrToken::Token(token) => {
27 if let Some(tree) = syntax_tree_for_string(&token, text_range) { 27 if let Some(tree) = syntax_tree_for_string(&token, text_range) {
@@ -85,7 +85,7 @@ fn syntax_tree_for_token(node: &SyntaxToken, text_range: TextRange) -> Option<St
85 .trim_end_matches('"') 85 .trim_end_matches('"')
86 .trim() 86 .trim()
87 // Remove custom markers 87 // Remove custom markers
88 .replace("<|>", ""); 88 .replace("$0", "");
89 89
90 let parsed = SourceFile::parse(&text); 90 let parsed = SourceFile::parse(&text);
91 91
@@ -111,7 +111,6 @@ mod tests {
111 let syn = analysis.syntax_tree(file_id, None).unwrap(); 111 let syn = analysis.syntax_tree(file_id, None).unwrap();
112 112
113 assert_eq_text!( 113 assert_eq_text!(
114 syn.trim(),
115 r#" 114 r#"
116[email protected] 115[email protected]
117 [email protected] 116 [email protected]
@@ -127,7 +126,8 @@ [email protected]
127 [email protected] "{" 126 [email protected] "{"
128 [email protected] "}" 127 [email protected] "}"
129"# 128"#
130 .trim() 129 .trim(),
130 syn.trim()
131 ); 131 );
132 132
133 let (analysis, file_id) = fixture::file( 133 let (analysis, file_id) = fixture::file(
@@ -143,7 +143,6 @@ fn test() {
143 let syn = analysis.syntax_tree(file_id, None).unwrap(); 143 let syn = analysis.syntax_tree(file_id, None).unwrap();
144 144
145 assert_eq_text!( 145 assert_eq_text!(
146 syn.trim(),
147 r#" 146 r#"
148[email protected] 147[email protected]
149 [email protected] 148 [email protected]
@@ -176,17 +175,17 @@ [email protected]
176 [email protected] "\n" 175 [email protected] "\n"
177 [email protected] "}" 176 [email protected] "}"
178"# 177"#
179 .trim() 178 .trim(),
179 syn.trim()
180 ); 180 );
181 } 181 }
182 182
183 #[test] 183 #[test]
184 fn test_syntax_tree_with_range() { 184 fn test_syntax_tree_with_range() {
185 let (analysis, range) = fixture::range(r#"<|>fn foo() {}<|>"#.trim()); 185 let (analysis, range) = fixture::range(r#"$0fn foo() {}$0"#.trim());
186 let syn = analysis.syntax_tree(range.file_id, Some(range.range)).unwrap(); 186 let syn = analysis.syntax_tree(range.file_id, Some(range.range)).unwrap();
187 187
188 assert_eq_text!( 188 assert_eq_text!(
189 syn.trim(),
190 r#" 189 r#"
191[email protected] 190[email protected]
192 [email protected] "fn" 191 [email protected] "fn"
@@ -201,22 +200,22 @@ [email protected]
201 [email protected] "{" 200 [email protected] "{"
202 [email protected] "}" 201 [email protected] "}"
203"# 202"#
204 .trim() 203 .trim(),
204 syn.trim()
205 ); 205 );
206 206
207 let (analysis, range) = fixture::range( 207 let (analysis, range) = fixture::range(
208 r#"fn test() { 208 r#"fn test() {
209 <|>assert!(" 209 $0assert!("
210 fn foo() { 210 fn foo() {
211 } 211 }
212 ", "");<|> 212 ", "");$0
213}"# 213}"#
214 .trim(), 214 .trim(),
215 ); 215 );
216 let syn = analysis.syntax_tree(range.file_id, Some(range.range)).unwrap(); 216 let syn = analysis.syntax_tree(range.file_id, Some(range.range)).unwrap();
217 217
218 assert_eq_text!( 218 assert_eq_text!(
219 syn.trim(),
220 r#" 219 r#"
221[email protected] 220[email protected]
222 [email protected] 221 [email protected]
@@ -234,7 +233,8 @@ [email protected]
234 [email protected] ")" 233 [email protected] ")"
235 [email protected] ";" 234 [email protected] ";"
236"# 235"#
237 .trim() 236 .trim(),
237 syn.trim()
238 ); 238 );
239 } 239 }
240 240
@@ -243,8 +243,8 @@ [email protected]
243 let (analysis, range) = fixture::range( 243 let (analysis, range) = fixture::range(
244 r#"fn test() { 244 r#"fn test() {
245 assert!(" 245 assert!("
246<|>fn foo() { 246$0fn foo() {
247}<|> 247}$0
248fn bar() { 248fn bar() {
249} 249}
250 ", ""); 250 ", "");
@@ -253,7 +253,6 @@ fn bar() {
253 ); 253 );
254 let syn = analysis.syntax_tree(range.file_id, Some(range.range)).unwrap(); 254 let syn = analysis.syntax_tree(range.file_id, Some(range.range)).unwrap();
255 assert_eq_text!( 255 assert_eq_text!(
256 syn.trim(),
257 r#" 256 r#"
258[email protected] 257[email protected]
259 [email protected] 258 [email protected]
@@ -270,15 +269,16 @@ [email protected]
270 [email protected] "\n" 269 [email protected] "\n"
271 [email protected] "}" 270 [email protected] "}"
272"# 271"#
273 .trim() 272 .trim(),
273 syn.trim()
274 ); 274 );
275 275
276 // With a raw string 276 // With a raw string
277 let (analysis, range) = fixture::range( 277 let (analysis, range) = fixture::range(
278 r###"fn test() { 278 r###"fn test() {
279 assert!(r#" 279 assert!(r#"
280<|>fn foo() { 280$0fn foo() {
281}<|> 281}$0
282fn bar() { 282fn bar() {
283} 283}
284 "#, ""); 284 "#, "");
@@ -287,7 +287,6 @@ fn bar() {
287 ); 287 );
288 let syn = analysis.syntax_tree(range.file_id, Some(range.range)).unwrap(); 288 let syn = analysis.syntax_tree(range.file_id, Some(range.range)).unwrap();
289 assert_eq_text!( 289 assert_eq_text!(
290 syn.trim(),
291 r#" 290 r#"
292[email protected] 291[email protected]
293 [email protected] 292 [email protected]
@@ -304,23 +303,23 @@ [email protected]
304 [email protected] "\n" 303 [email protected] "\n"
305 [email protected] "}" 304 [email protected] "}"
306"# 305"#
307 .trim() 306 .trim(),
307 syn.trim()
308 ); 308 );
309 309
310 // With a raw string 310 // With a raw string
311 let (analysis, range) = fixture::range( 311 let (analysis, range) = fixture::range(
312 r###"fn test() { 312 r###"fn test() {
313 assert!(r<|>#" 313 assert!(r$0#"
314fn foo() { 314fn foo() {
315} 315}
316fn bar() { 316fn bar() {
317}"<|>#, ""); 317}"$0#, "");
318}"### 318}"###
319 .trim(), 319 .trim(),
320 ); 320 );
321 let syn = analysis.syntax_tree(range.file_id, Some(range.range)).unwrap(); 321 let syn = analysis.syntax_tree(range.file_id, Some(range.range)).unwrap();
322 assert_eq_text!( 322 assert_eq_text!(
323 syn.trim(),
324 r#" 323 r#"
325[email protected] 324[email protected]
326 [email protected] 325 [email protected]
@@ -351,7 +350,8 @@ [email protected]
351 [email protected] "\n" 350 [email protected] "\n"
352 [email protected] "}" 351 [email protected] "}"
353"# 352"#
354 .trim() 353 .trim(),
354 syn.trim()
355 ); 355 );
356 } 356 }
357} 357}