diff options
Diffstat (limited to 'crates/ra_ide/src/syntax_highlighting.rs')
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting.rs | 136 |
1 files changed, 2 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 | ||
3 | mod tags; | 3 | mod tags; |
4 | mod html; | 4 | mod html; |
5 | #[cfg(test)] | ||
6 | mod tests; | ||
5 | 7 | ||
6 | use hir::{Name, Semantics}; | 8 | use hir::{Name, Semantics}; |
7 | use ra_ide_db::{ | 9 | use 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)] | ||
284 | mod 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)] | ||
299 | struct Foo { | ||
300 | pub x: i32, | ||
301 | pub y: i32, | ||
302 | } | ||
303 | |||
304 | fn foo<T>() -> T { | ||
305 | unimplemented!(); | ||
306 | foo::<i32>(); | ||
307 | } | ||
308 | |||
309 | macro_rules! def_fn { | ||
310 | ($($tt:tt)*) => {$($tt)*} | ||
311 | } | ||
312 | |||
313 | def_fn!{ | ||
314 | fn bar() -> u32 { | ||
315 | 100 | ||
316 | } | ||
317 | } | ||
318 | |||
319 | // comment | ||
320 | fn 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 | |||
337 | enum E<X> { | ||
338 | V(X) | ||
339 | } | ||
340 | |||
341 | impl<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#" | ||
358 | fn 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 | |||
367 | fn 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 | } | ||