aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_ide/src/syntax_highlighting.rs136
-rw-r--r--crates/ra_ide/src/syntax_highlighting/tests.rs127
2 files changed, 129 insertions, 134 deletions
diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs
index d31c09abb..796f0e545 100644
--- a/crates/ra_ide/src/syntax_highlighting.rs
+++ b/crates/ra_ide/src/syntax_highlighting.rs
@@ -2,6 +2,8 @@
2 2
3mod tags; 3mod tags;
4mod html; 4mod html;
5#[cfg(test)]
6mod tests;
5 7
6use hir::{Name, Semantics}; 8use hir::{Name, Semantics};
7use ra_ide_db::{ 9use ra_ide_db::{
@@ -279,137 +281,3 @@ fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
279 _ => default, 281 _ => default,
280 } 282 }
281} 283}
282
283#[cfg(test)]
284mod tests {
285 use std::fs;
286
287 use test_utils::{assert_eq_text, project_dir, read_text};
288
289 use crate::{
290 mock_analysis::{single_file, MockAnalysis},
291 FileRange, TextRange,
292 };
293
294 #[test]
295 fn test_highlighting() {
296 let (analysis, file_id) = single_file(
297 r#"
298#[derive(Clone, Debug)]
299struct Foo {
300 pub x: i32,
301 pub y: i32,
302}
303
304fn foo<T>() -> T {
305 unimplemented!();
306 foo::<i32>();
307}
308
309macro_rules! def_fn {
310 ($($tt:tt)*) => {$($tt)*}
311}
312
313def_fn!{
314 fn bar() -> u32 {
315 100
316 }
317}
318
319// comment
320fn main() {
321 println!("Hello, {}!", 92);
322
323 let mut vec = Vec::new();
324 if true {
325 let x = 92;
326 vec.push(Foo { x, y: 1 });
327 }
328 unsafe { vec.set_len(0); }
329
330 let mut x = 42;
331 let y = &mut x;
332 let z = &y;
333
334 y;
335}
336
337enum E<X> {
338 V(X)
339}
340
341impl<X> E<X> {
342 fn new<T>() -> E<T> {}
343}
344"#
345 .trim(),
346 );
347 let dst_file = project_dir().join("crates/ra_ide/src/snapshots/highlighting.html");
348 let actual_html = &analysis.highlight_as_html(file_id, false).unwrap();
349 let expected_html = &read_text(&dst_file);
350 fs::write(dst_file, &actual_html).unwrap();
351 assert_eq_text!(expected_html, actual_html);
352 }
353
354 #[test]
355 fn test_rainbow_highlighting() {
356 let (analysis, file_id) = single_file(
357 r#"
358fn main() {
359 let hello = "hello";
360 let x = hello.to_string();
361 let y = hello.to_string();
362
363 let x = "other color please!";
364 let y = x.to_string();
365}
366
367fn bar() {
368 let mut hello = "hello";
369}
370"#
371 .trim(),
372 );
373 let dst_file = project_dir().join("crates/ra_ide/src/snapshots/rainbow_highlighting.html");
374 let actual_html = &analysis.highlight_as_html(file_id, true).unwrap();
375 let expected_html = &read_text(&dst_file);
376 fs::write(dst_file, &actual_html).unwrap();
377 assert_eq_text!(expected_html, actual_html);
378 }
379
380 #[test]
381 fn accidentally_quadratic() {
382 let file = project_dir().join("crates/ra_syntax/test_data/accidentally_quadratic");
383 let src = fs::read_to_string(file).unwrap();
384
385 let mut mock = MockAnalysis::new();
386 let file_id = mock.add_file("/main.rs", &src);
387 let host = mock.analysis_host();
388
389 // let t = std::time::Instant::now();
390 let _ = host.analysis().highlight(file_id).unwrap();
391 // eprintln!("elapsed: {:?}", t.elapsed());
392 }
393
394 #[test]
395 fn test_ranges() {
396 let (analysis, file_id) = single_file(
397 r#"
398 #[derive(Clone, Debug)]
399 struct Foo {
400 pub x: i32,
401 pub y: i32,
402 }"#,
403 );
404
405 // The "x"
406 let highlights = &analysis
407 .highlight_range(FileRange {
408 file_id,
409 range: TextRange::offset_len(82.into(), 1.into()),
410 })
411 .unwrap();
412
413 assert_eq!(&highlights[0].highlight.to_string(), "field");
414 }
415}
diff --git a/crates/ra_ide/src/syntax_highlighting/tests.rs b/crates/ra_ide/src/syntax_highlighting/tests.rs
new file mode 100644
index 000000000..ff23d4ac5
--- /dev/null
+++ b/crates/ra_ide/src/syntax_highlighting/tests.rs
@@ -0,0 +1,127 @@
1use std::fs;
2
3use test_utils::{assert_eq_text, project_dir, read_text};
4
5use crate::{
6 mock_analysis::{single_file, MockAnalysis},
7 FileRange, TextRange,
8};
9
10#[test]
11fn test_highlighting() {
12 let (analysis, file_id) = single_file(
13 r#"
14#[derive(Clone, Debug)]
15struct Foo {
16 pub x: i32,
17 pub y: i32,
18}
19
20fn foo<T>() -> T {
21 unimplemented!();
22 foo::<i32>();
23}
24
25macro_rules! def_fn {
26 ($($tt:tt)*) => {$($tt)*}
27}
28
29def_fn!{
30 fn bar() -> u32 {
31 100
32 }
33}
34
35// comment
36fn main() {
37 println!("Hello, {}!", 92);
38
39 let mut vec = Vec::new();
40 if true {
41 let x = 92;
42 vec.push(Foo { x, y: 1 });
43 }
44 unsafe { vec.set_len(0); }
45
46 let mut x = 42;
47 let y = &mut x;
48 let z = &y;
49
50 y;
51}
52
53enum E<X> {
54 V(X)
55}
56
57impl<X> E<X> {
58 fn new<T>() -> E<T> {}
59}
60"#
61 .trim(),
62 );
63 let dst_file = project_dir().join("crates/ra_ide/src/snapshots/highlighting.html");
64 let actual_html = &analysis.highlight_as_html(file_id, false).unwrap();
65 let expected_html = &read_text(&dst_file);
66 fs::write(dst_file, &actual_html).unwrap();
67 assert_eq_text!(expected_html, actual_html);
68}
69
70#[test]
71fn test_rainbow_highlighting() {
72 let (analysis, file_id) = single_file(
73 r#"
74fn main() {
75 let hello = "hello";
76 let x = hello.to_string();
77 let y = hello.to_string();
78
79 let x = "other color please!";
80 let y = x.to_string();
81}
82
83fn bar() {
84 let mut hello = "hello";
85}
86"#
87 .trim(),
88 );
89 let dst_file = project_dir().join("crates/ra_ide/src/snapshots/rainbow_highlighting.html");
90 let actual_html = &analysis.highlight_as_html(file_id, true).unwrap();
91 let expected_html = &read_text(&dst_file);
92 fs::write(dst_file, &actual_html).unwrap();
93 assert_eq_text!(expected_html, actual_html);
94}
95
96#[test]
97fn accidentally_quadratic() {
98 let file = project_dir().join("crates/ra_syntax/test_data/accidentally_quadratic");
99 let src = fs::read_to_string(file).unwrap();
100
101 let mut mock = MockAnalysis::new();
102 let file_id = mock.add_file("/main.rs", &src);
103 let host = mock.analysis_host();
104
105 // let t = std::time::Instant::now();
106 let _ = host.analysis().highlight(file_id).unwrap();
107 // eprintln!("elapsed: {:?}", t.elapsed());
108}
109
110#[test]
111fn test_ranges() {
112 let (analysis, file_id) = single_file(
113 r#"
114 #[derive(Clone, Debug)]
115 struct Foo {
116 pub x: i32,
117 pub y: i32,
118 }"#,
119 );
120
121 // The "x"
122 let highlights = &analysis
123 .highlight_range(FileRange { file_id, range: TextRange::offset_len(82.into(), 1.into()) })
124 .unwrap();
125
126 assert_eq!(&highlights[0].highlight.to_string(), "field");
127}