3 use expect_test::{expect_file, ExpectFile};
4 use test_utils::project_dir;
6 use crate::{fixture, FileRange, TextRange};
9 fn test_highlighting() {
12 use inner::{self as inner_mod};
15 #[rustc_builtin_macro]
18 // Needed for function consuming vs normal
26 pub trait FnOnce<Args> {}
29 pub trait FnMut<Args>: FnOnce<Args> {}
32 pub trait Fn<Args>: FnMut<Args> {}
46 fn bar(&self) -> i32 {
52 fn baz(mut self, f: Foo) -> i32 {
60 fn quop(&self) -> i32 {
71 fn baz(self, f: FooCopy) -> u32 {
79 fn quop(&self) -> u32 {
84 static mut STATIC_MUT: i32 = 0;
86 fn foo<'a, T>() -> T {
94 fn const_param<const FOO: usize>() -> usize {
99 fn baz<F: Fn() -> ()>(f: F) {
103 fn foobar() -> impl Copy {}
109 macro_rules! def_fn {
110 ($($tt:tt)*) => {$($tt)*}
125 macro_rules! keyword_frag {
126 ($type:ty) => ($type)
131 println!("Hello, {}!", 92);
133 let mut vec = Vec::new();
136 vec.push(Foo { x, y: 1 });
153 let Foo { x: z, y } = Foo { x: z, y };
157 let mut foo = Foo { x, y: x };
158 let foo2 = Foo { x, y: x };
163 let mut copy = FooCopy { x };
189 fn and<U>(self, other: Option<U>) -> Option<(T, U)> {
191 None => unimplemented!(),
198 expect_file!["./test_data/highlighting.html"],
204 fn test_rainbow_highlighting() {
209 let x = hello.to_string();
210 let y = hello.to_string();
212 let x = "other color please!";
213 let y = x.to_string();
217 let mut hello = "hello";
221 expect_file!["./test_data/rainbow_highlighting.html"],
227 fn accidentally_quadratic() {
228 let file = project_dir().join("crates/syntax/test_data/accidentally_quadratic");
229 let src = fs::read_to_string(file).unwrap();
231 let (analysis, file_id) = fixture::file(&src);
233 // let t = std::time::Instant::now();
234 let _ = analysis.highlight(file_id).unwrap();
235 // eprintln!("elapsed: {:?}", t.elapsed());
240 let (analysis, file_id) = fixture::file(
242 #[derive(Clone, Debug)]
251 let highlights = &analysis
252 .highlight_range(FileRange { file_id, range: TextRange::at(45.into(), 1.into()) })
255 assert_eq!(&highlights[0].highlight.to_string(), "field.declaration");
259 fn test_flattening() {
262 fn fixture(ra_fixture: &str) {}
268 println!("2 + 2 = {}", 4);
274 expect_file!["./test_data/highlight_injection.html"],
281 let (analysis, file_id) = fixture::file(
288 let _ = analysis.highlight(file_id).unwrap();
292 fn test_string_highlighting() {
293 // The format string detection is based on macro-expansion,
294 // thus, we have to copy the macro definition from `std`
297 macro_rules! println {
299 $crate::io::_print($crate::format_args_nl!($($arg)*));
302 #[rustc_builtin_macro]
303 macro_rules! format_args_nl {
304 ($fmt:expr) => {{ /* compiler built-in */ }};
305 ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
309 // from https://doc.rust-lang.org/std/fmt/index.html
310 println!("Hello"); // => "Hello"
311 println!("Hello, {}!", "world"); // => "Hello, world!"
312 println!("The number is {}", 1); // => "The number is 1"
313 println!("{:?}", (3, 4)); // => "(3, 4)"
314 println!("{value}", value=4); // => "4"
315 println!("{} {}", 1, 2); // => "1 2"
316 println!("{:04}", 42); // => "0042" with leading zerosV
317 println!("{1} {} {0} {}", 1, 2); // => "2 1 1 2"
318 println!("{argument}", argument = "test"); // => "test"
319 println!("{name} {}", 1, name = 2); // => "2 1"
320 println!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b"
321 println!("{{{}}}", 2); // => "{2}"
322 println!("Hello {:5}!", "x");
323 println!("Hello {:1$}!", "x", 5);
324 println!("Hello {1:0$}!", 5, "x");
325 println!("Hello {:width$}!", "x", width = 5);
326 println!("Hello {:<5}!", "x");
327 println!("Hello {:-<5}!", "x");
328 println!("Hello {:^5}!", "x");
329 println!("Hello {:>5}!", "x");
330 println!("Hello {:+}!", 5);
331 println!("{:#x}!", 27);
332 println!("Hello {:05}!", 5);
333 println!("Hello {:05}!", -5);
334 println!("{:#010x}!", 27);
335 println!("Hello {0} is {1:.5}", "x", 0.01);
336 println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
337 println!("Hello {0} is {2:.1$}", "x", 5, 0.01);
338 println!("Hello {} is {:.*}", "x", 5, 0.01);
339 println!("Hello {} is {2:.*}", "x", 5, 0.01);
340 println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01);
341 println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56);
342 println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56");
343 println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56");
344 println!("Hello {{}}");
345 println!("{{ Hello");
347 println!(r"Hello, {}!", "world");
350 println!("Hello\nWorld");
351 println!("\u{48}\x65\x6C\x6C\x6F World");
353 println!("{\x41}", A = 92);
354 println!("{ничоси}", ничоси = 92);
356 println!("{:x?} {} ", thingy, n2);
359 expect_file!["./test_data/highlight_strings.html"],
365 fn test_unsafe_highlighting() {
368 unsafe fn unsafe_fn() {}
378 unsafe fn unsafe_method(&self) {}
381 struct TypeForStaticMut {
385 static mut global_mut: TypeForStaticMut = TypeForStaticMut { a: 0 };
393 fn calls_autoref(&self);
396 impl DoTheAutoref for u16 {
397 fn calls_autoref(&self) {}
401 let x = &5 as *const _ as *const usize;
402 let u = Union { b: 0 };
404 // unsafe fn and method calls
408 Union { b: 0 } => (),
411 HasUnsafeFn.unsafe_method();
416 // unsafe access to a static mut
417 let a = global_mut.a;
419 // unsafe ref of packed fields
420 let packed = Packed { a: 0 };
422 let ref a = packed.a;
423 let Packed { ref a } = packed;
424 let Packed { a: ref _a } = packed;
426 // unsafe auto ref of packed field
427 packed.a.calls_autoref();
432 expect_file!["./test_data/highlight_unsafe.html"],
438 fn test_highlight_doctest() {
442 /// let _ = "early doctests should not go boom";
449 pub const bar: bool = true;
451 /// Constructs a new `Foo`.
456 /// # #![allow(unused_mut)]
457 /// let mut foo: Foo = Foo::new();
459 pub const fn new() -> Foo {
463 /// `bar` method on `Foo`.
470 /// let foo = Foo::new();
472 /// // calls bar on foo
473 /// assert!(foo.bar());
475 /// let bar = foo.bar || Foo::bar;
480 /// let multi_line_string = "Foo
487 /// let foobar = Foo::new().bar();
493 pub fn foo(&self) -> bool {
508 expect_file!["./test_data/highlight_doctest.html"],
514 fn test_extern_crate() {
517 //- /main.rs crate:main deps:std,alloc
519 extern crate alloc as abc;
520 //- /std/lib.rs crate:std
522 //- /alloc/lib.rs crate:alloc
525 expect_file!["./test_data/highlight_extern_crate.html"],
531 fn test_associated_function() {
539 pub fn is_static() {}
540 pub fn is_not_static(&self) {}
545 fn t_is_not_static(&self) {}
549 pub fn is_static() {}
550 pub fn is_not_static(&self) {}
553 expect_file!["./test_data/highlight_assoc_functions.html"],
558 /// Highlights the code given by the `ra_fixture` argument, renders the
559 /// result as HTML, and compares it with the HTML file given as `snapshot`.
560 /// Note that the `snapshot` file is overwritten by the rendered HTML.
561 fn check_highlighting(ra_fixture: &str, expect: ExpectFile, rainbow: bool) {
562 let (analysis, file_id) = fixture::file(ra_fixture);
563 let actual_html = &analysis.highlight_as_html(file_id, rainbow).unwrap();
564 expect.assert_eq(actual_html)