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!(),
212 async fn learn_and_sing() {
213 let song = learn_song().await;
214 sing_song(song).await;
217 async fn async_main() {
218 let f1 = learn_and_sing();
220 futures::join!(f1, f2);
224 expect_file!["./test_data/highlighting.html"],
230 fn test_rainbow_highlighting() {
235 let x = hello.to_string();
236 let y = hello.to_string();
238 let x = "other color please!";
239 let y = x.to_string();
243 let mut hello = "hello";
247 expect_file!["./test_data/rainbow_highlighting.html"],
253 fn benchmark_syntax_highlighting_long_struct() {
254 if skip_slow_tests() {
258 let fixture = bench_fixture::big_struct();
259 let (analysis, file_id) = fixture::file(&fixture);
262 let _pt = bench("syntax highlighting long struct");
267 .filter(|it| it.highlight.tag == HlTag::Symbol(SymbolKind::Struct))
270 assert_eq!(hash, 2001);
274 fn syntax_highlighting_not_quadratic() {
275 if skip_slow_tests() {
279 let mut al = AssertLinear::default();
280 while al.next_round() {
284 let fixture = bench_fixture::big_struct_n(n);
285 let (analysis, file_id) = fixture::file(&fixture);
287 let time = Instant::now();
293 .filter(|it| it.highlight.tag == HlTag::Symbol(SymbolKind::Struct))
295 assert!(hash > n as usize);
297 let elapsed = time.elapsed();
298 al.sample(n as f64, elapsed.as_millis() as f64);
304 fn benchmark_syntax_highlighting_parser() {
305 if skip_slow_tests() {
309 let fixture = bench_fixture::glorious_old_parser();
310 let (analysis, file_id) = fixture::file(&fixture);
313 let _pt = bench("syntax highlighting parser");
318 .filter(|it| it.highlight.tag == HlTag::Symbol(SymbolKind::Function))
321 assert_eq!(hash, 1632);
326 let (analysis, file_id) = fixture::file(
328 #[derive(Clone, Debug)]
337 let highlights = &analysis
338 .highlight_range(FileRange { file_id, range: TextRange::at(45.into(), 1.into()) })
341 assert_eq!(&highlights[0].highlight.to_string(), "field.declaration");
345 fn test_flattening() {
348 fn fixture(ra_fixture: &str) {}
354 println!("2 + 2 = {}", 4);
360 expect_file!["./test_data/highlight_injection.html"],
367 let (analysis, file_id) = fixture::file(
374 let _ = analysis.highlight(file_id).unwrap();
378 fn test_string_highlighting() {
379 // The format string detection is based on macro-expansion,
380 // thus, we have to copy the macro definition from `std`
383 macro_rules! println {
385 $crate::io::_print($crate::format_args_nl!($($arg)*));
388 #[rustc_builtin_macro]
389 macro_rules! format_args_nl {
390 ($fmt:expr) => {{ /* compiler built-in */ }};
391 ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
395 // from https://doc.rust-lang.org/std/fmt/index.html
396 println!("Hello"); // => "Hello"
397 println!("Hello, {}!", "world"); // => "Hello, world!"
398 println!("The number is {}", 1); // => "The number is 1"
399 println!("{:?}", (3, 4)); // => "(3, 4)"
400 println!("{value}", value=4); // => "4"
401 println!("{} {}", 1, 2); // => "1 2"
402 println!("{:04}", 42); // => "0042" with leading zerosV
403 println!("{1} {} {0} {}", 1, 2); // => "2 1 1 2"
404 println!("{argument}", argument = "test"); // => "test"
405 println!("{name} {}", 1, name = 2); // => "2 1"
406 println!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b"
407 println!("{{{}}}", 2); // => "{2}"
408 println!("Hello {:5}!", "x");
409 println!("Hello {:1$}!", "x", 5);
410 println!("Hello {1:0$}!", 5, "x");
411 println!("Hello {:width$}!", "x", width = 5);
412 println!("Hello {:<5}!", "x");
413 println!("Hello {:-<5}!", "x");
414 println!("Hello {:^5}!", "x");
415 println!("Hello {:>5}!", "x");
416 println!("Hello {:+}!", 5);
417 println!("{:#x}!", 27);
418 println!("Hello {:05}!", 5);
419 println!("Hello {:05}!", -5);
420 println!("{:#010x}!", 27);
421 println!("Hello {0} is {1:.5}", "x", 0.01);
422 println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
423 println!("Hello {0} is {2:.1$}", "x", 5, 0.01);
424 println!("Hello {} is {:.*}", "x", 5, 0.01);
425 println!("Hello {} is {2:.*}", "x", 5, 0.01);
426 println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01);
427 println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56);
428 println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56");
429 println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56");
430 println!("Hello {{}}");
431 println!("{{ Hello");
433 println!(r"Hello, {}!", "world");
436 println!("Hello\nWorld");
437 println!("\u{48}\x65\x6C\x6C\x6F World");
439 println!("{\x41}", A = 92);
440 println!("{ничоси}", ничоси = 92);
442 println!("{:x?} {} ", thingy, n2);
445 expect_file!["./test_data/highlight_strings.html"],
451 fn test_unsafe_highlighting() {
454 unsafe fn unsafe_fn() {}
464 unsafe fn unsafe_method(&self) {}
467 struct TypeForStaticMut {
471 static mut global_mut: TypeForStaticMut = TypeForStaticMut { a: 0 };
479 fn calls_autoref(&self);
482 impl DoTheAutoref for u16 {
483 fn calls_autoref(&self) {}
487 let x = &5 as *const _ as *const usize;
488 let u = Union { b: 0 };
490 // unsafe fn and method calls
494 Union { b: 0 } => (),
497 HasUnsafeFn.unsafe_method();
502 // unsafe access to a static mut
503 let a = global_mut.a;
505 // unsafe ref of packed fields
506 let packed = Packed { a: 0 };
508 let ref a = packed.a;
509 let Packed { ref a } = packed;
510 let Packed { a: ref _a } = packed;
512 // unsafe auto ref of packed field
513 packed.a.calls_autoref();
518 expect_file!["./test_data/highlight_unsafe.html"],
524 fn test_highlight_doc_comment() {
528 /// let _ = "early doctests should not go boom";
540 pub const bar: bool = true;
542 /// Constructs a new `Foo`.
547 /// # #![allow(unused_mut)]
548 /// let mut foo: Foo = Foo::new();
550 pub const fn new() -> Foo {
554 /// `bar` method on `Foo`.
561 /// let foo = Foo::new();
563 /// // calls bar on foo
564 /// assert!(foo.bar());
566 /// let bar = foo.bar || Foo::bar;
571 /// let multi_line_string = "Foo
578 /// let foobar = Foo::new().bar();
584 pub fn foo(&self) -> bool {
589 /// [`Foo`](Foo) is a struct
590 /// This function is > [`all_the_links`](all_the_links) <
591 /// [`noop`](noop) is a macro below
592 /// [`Item`] is a struct in the module [`module`]
594 /// [`Item`]: module::Item
595 /// [mix_and_match]: ThisShouldntResolve
596 pub fn all_the_links() {}
612 /// let _ = example(&[1, 2, 3]);
617 #[cfg_attr(not(feature = "false"), doc = "loop {}")]
621 #[cfg_attr(feature = "alloc", doc = "```rust")]
622 #[cfg_attr(not(feature = "alloc"), doc = "```ignore")]
623 /// let _ = example(&alloc::vec![1, 2, 3]);
625 pub fn mix_and_match() {}
628 It is beyond me why you'd use these when you got ///
630 let _ = example(&[1, 2, 3]);
632 [`block_comments2`] tests these with indentation
634 pub fn block_comments() {}
637 Really, I don't get it
639 let _ = example(&[1, 2, 3]);
641 [`block_comments`] tests these without indentation
643 pub fn block_comments2() {}
646 expect_file!["./test_data/highlight_doctest.html"],
652 fn test_extern_crate() {
655 //- /main.rs crate:main deps:std,alloc
657 extern crate alloc as abc;
658 //- /std/lib.rs crate:std
660 //- /alloc/lib.rs crate:alloc
663 expect_file!["./test_data/highlight_extern_crate.html"],
669 fn test_associated_function() {
677 pub fn is_static() {}
678 pub fn is_not_static(&self) {}
683 fn t_is_not_static(&self) {}
687 pub fn is_static() {}
688 pub fn is_not_static(&self) {}
691 expect_file!["./test_data/highlight_assoc_functions.html"],
697 fn test_injection() {
700 fn f(ra_fixture: &str) {}
710 expect_file!["./test_data/injection.html"],
715 /// Highlights the code given by the `ra_fixture` argument, renders the
716 /// result as HTML, and compares it with the HTML file given as `snapshot`.
717 /// Note that the `snapshot` file is overwritten by the rendered HTML.
718 fn check_highlighting(ra_fixture: &str, expect: ExpectFile, rainbow: bool) {
719 let (analysis, file_id) = fixture::file(ra_fixture);
720 let actual_html = &analysis.highlight_as_html(file_id, rainbow).unwrap();
721 expect.assert_eq(actual_html)