1 use std::time::Instant;
3 use expect_test::{expect_file, ExpectFile};
4 use ide_db::SymbolKind;
5 use test_utils::{bench, bench_fixture, skip_slow_tests, AssertLinear};
7 use crate::{fixture, FileRange, HlTag, TextRange};
10 fn test_highlighting() {
13 use inner::{self as inner_mod};
16 #[rustc_builtin_macro]
19 // Needed for function consuming vs normal
27 pub trait FnOnce<Args> {}
30 pub trait FnMut<Args>: FnOnce<Args> {}
33 pub trait Fn<Args>: FnMut<Args> {}
47 fn bar(&self) -> i32 {
53 fn baz(mut self, f: Foo) -> i32 {
61 fn quop(&self) -> i32 {
72 fn baz(self, f: FooCopy) -> u32 {
80 fn quop(&self) -> u32 {
89 static mut STATIC_MUT: i32 = 0;
91 fn foo<'a, T>() -> T {
99 fn const_param<const FOO: usize>() -> usize {
104 fn baz<F: Fn() -> ()>(f: F) {
108 fn foobar() -> impl Copy {}
114 macro_rules! def_fn {
115 ($($tt:tt)*) => {$($tt)*}
130 macro_rules! keyword_frag {
131 ($type:ty) => ($type)
134 macro with_args($i:ident) {
146 println!("Hello, {}!", 92);
148 let mut vec = Vec::new();
151 vec.push(Foo { x, y: 1 });
168 let Foo { x: z, y } = Foo { x: z, y };
172 let mut foo = Foo { x, y: x };
173 let foo2 = Foo { x, y: x };
178 let mut copy = FooCopy { x };
204 fn and<U>(self, other: Option<U>) -> Option<(T, U)> {
206 None => unimplemented!(),
213 expect_file!["./test_data/highlighting.html"],
219 fn test_rainbow_highlighting() {
224 let x = hello.to_string();
225 let y = hello.to_string();
227 let x = "other color please!";
228 let y = x.to_string();
232 let mut hello = "hello";
236 expect_file!["./test_data/rainbow_highlighting.html"],
242 fn benchmark_syntax_highlighting_long_struct() {
243 if skip_slow_tests() {
247 let fixture = bench_fixture::big_struct();
248 let (analysis, file_id) = fixture::file(&fixture);
251 let _pt = bench("syntax highlighting long struct");
256 .filter(|it| it.highlight.tag == HlTag::Symbol(SymbolKind::Struct))
259 assert_eq!(hash, 2001);
263 fn syntax_highlighting_not_quadratic() {
264 if skip_slow_tests() {
268 let mut al = AssertLinear::default();
269 while al.next_round() {
273 let fixture = bench_fixture::big_struct_n(n);
274 let (analysis, file_id) = fixture::file(&fixture);
276 let time = Instant::now();
282 .filter(|it| it.highlight.tag == HlTag::Symbol(SymbolKind::Struct))
284 assert!(hash > n as usize);
286 let elapsed = time.elapsed();
287 al.sample(n as f64, elapsed.as_millis() as f64);
293 fn benchmark_syntax_highlighting_parser() {
294 if skip_slow_tests() {
298 let fixture = bench_fixture::glorious_old_parser();
299 let (analysis, file_id) = fixture::file(&fixture);
302 let _pt = bench("syntax highlighting parser");
307 .filter(|it| it.highlight.tag == HlTag::Symbol(SymbolKind::Function))
310 assert_eq!(hash, 1632);
315 let (analysis, file_id) = fixture::file(
317 #[derive(Clone, Debug)]
326 let highlights = &analysis
327 .highlight_range(FileRange { file_id, range: TextRange::at(45.into(), 1.into()) })
330 assert_eq!(&highlights[0].highlight.to_string(), "field.declaration");
334 fn test_flattening() {
337 fn fixture(ra_fixture: &str) {}
343 println!("2 + 2 = {}", 4);
349 expect_file!["./test_data/highlight_injection.html"],
356 let (analysis, file_id) = fixture::file(
363 let _ = analysis.highlight(file_id).unwrap();
367 fn test_string_highlighting() {
368 // The format string detection is based on macro-expansion,
369 // thus, we have to copy the macro definition from `std`
372 macro_rules! println {
374 $crate::io::_print($crate::format_args_nl!($($arg)*));
377 #[rustc_builtin_macro]
378 macro_rules! format_args_nl {
379 ($fmt:expr) => {{ /* compiler built-in */ }};
380 ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
384 // from https://doc.rust-lang.org/std/fmt/index.html
385 println!("Hello"); // => "Hello"
386 println!("Hello, {}!", "world"); // => "Hello, world!"
387 println!("The number is {}", 1); // => "The number is 1"
388 println!("{:?}", (3, 4)); // => "(3, 4)"
389 println!("{value}", value=4); // => "4"
390 println!("{} {}", 1, 2); // => "1 2"
391 println!("{:04}", 42); // => "0042" with leading zerosV
392 println!("{1} {} {0} {}", 1, 2); // => "2 1 1 2"
393 println!("{argument}", argument = "test"); // => "test"
394 println!("{name} {}", 1, name = 2); // => "2 1"
395 println!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b"
396 println!("{{{}}}", 2); // => "{2}"
397 println!("Hello {:5}!", "x");
398 println!("Hello {:1$}!", "x", 5);
399 println!("Hello {1:0$}!", 5, "x");
400 println!("Hello {:width$}!", "x", width = 5);
401 println!("Hello {:<5}!", "x");
402 println!("Hello {:-<5}!", "x");
403 println!("Hello {:^5}!", "x");
404 println!("Hello {:>5}!", "x");
405 println!("Hello {:+}!", 5);
406 println!("{:#x}!", 27);
407 println!("Hello {:05}!", 5);
408 println!("Hello {:05}!", -5);
409 println!("{:#010x}!", 27);
410 println!("Hello {0} is {1:.5}", "x", 0.01);
411 println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
412 println!("Hello {0} is {2:.1$}", "x", 5, 0.01);
413 println!("Hello {} is {:.*}", "x", 5, 0.01);
414 println!("Hello {} is {2:.*}", "x", 5, 0.01);
415 println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01);
416 println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56);
417 println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56");
418 println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56");
419 println!("Hello {{}}");
420 println!("{{ Hello");
422 println!(r"Hello, {}!", "world");
425 println!("Hello\nWorld");
426 println!("\u{48}\x65\x6C\x6C\x6F World");
428 println!("{\x41}", A = 92);
429 println!("{ничоси}", ничоси = 92);
431 println!("{:x?} {} ", thingy, n2);
434 expect_file!["./test_data/highlight_strings.html"],
440 fn test_unsafe_highlighting() {
443 unsafe fn unsafe_fn() {}
453 unsafe fn unsafe_method(&self) {}
456 struct TypeForStaticMut {
460 static mut global_mut: TypeForStaticMut = TypeForStaticMut { a: 0 };
468 fn calls_autoref(&self);
471 impl DoTheAutoref for u16 {
472 fn calls_autoref(&self) {}
476 let x = &5 as *const _ as *const usize;
477 let u = Union { b: 0 };
479 // unsafe fn and method calls
483 Union { b: 0 } => (),
486 HasUnsafeFn.unsafe_method();
491 // unsafe access to a static mut
492 let a = global_mut.a;
494 // unsafe ref of packed fields
495 let packed = Packed { a: 0 };
497 let ref a = packed.a;
498 let Packed { ref a } = packed;
499 let Packed { a: ref _a } = packed;
501 // unsafe auto ref of packed field
502 packed.a.calls_autoref();
507 expect_file!["./test_data/highlight_unsafe.html"],
513 fn test_highlight_doc_comment() {
517 /// let _ = "early doctests should not go boom";
529 pub const bar: bool = true;
531 /// Constructs a new `Foo`.
536 /// # #![allow(unused_mut)]
537 /// let mut foo: Foo = Foo::new();
539 pub const fn new() -> Foo {
543 /// `bar` method on `Foo`.
550 /// let foo = Foo::new();
552 /// // calls bar on foo
553 /// assert!(foo.bar());
555 /// let bar = foo.bar || Foo::bar;
560 /// let multi_line_string = "Foo
567 /// let foobar = Foo::new().bar();
573 pub fn foo(&self) -> bool {
578 /// [`Foo`](Foo) is a struct
579 /// This function is > [`all_the_links`](all_the_links) <
580 /// [`noop`](noop) is a macro below
581 /// [`Item`] is a struct in the module [`module`]
583 /// [`Item`]: module::Item
584 /// [mix_and_match]: ThisShouldntResolve
585 pub fn all_the_links() {}
601 /// let _ = example(&[1, 2, 3]);
606 #[cfg_attr(not(feature = "false"), doc = "loop {}")]
610 #[cfg_attr(feature = "alloc", doc = "```rust")]
611 #[cfg_attr(not(feature = "alloc"), doc = "```ignore")]
612 /// let _ = example(&alloc::vec![1, 2, 3]);
614 pub fn mix_and_match() {}
617 It is beyond me why you'd use these when you got ///
619 let _ = example(&[1, 2, 3]);
621 [`block_comments2`] tests these with indentation
623 pub fn block_comments() {}
626 Really, I don't get it
628 let _ = example(&[1, 2, 3]);
630 [`block_comments`] tests these without indentation
632 pub fn block_comments2() {}
635 expect_file!["./test_data/highlight_doctest.html"],
641 fn test_extern_crate() {
644 //- /main.rs crate:main deps:std,alloc
646 extern crate alloc as abc;
647 //- /std/lib.rs crate:std
649 //- /alloc/lib.rs crate:alloc
652 expect_file!["./test_data/highlight_extern_crate.html"],
658 fn test_associated_function() {
666 pub fn is_static() {}
667 pub fn is_not_static(&self) {}
672 fn t_is_not_static(&self) {}
676 pub fn is_static() {}
677 pub fn is_not_static(&self) {}
680 expect_file!["./test_data/highlight_assoc_functions.html"],
686 fn test_injection() {
689 fn f(ra_fixture: &str) {}
699 expect_file!["./test_data/injection.html"],
704 /// Highlights the code given by the `ra_fixture` argument, renders the
705 /// result as HTML, and compares it with the HTML file given as `snapshot`.
706 /// Note that the `snapshot` file is overwritten by the rendered HTML.
707 fn check_highlighting(ra_fixture: &str, expect: ExpectFile, rainbow: bool) {
708 let (analysis, file_id) = fixture::file(ra_fixture);
709 let actual_html = &analysis.highlight_as_html(file_id, rainbow).unwrap();
710 expect.assert_eq(actual_html)