3 use test_utils::{assert_eq_text, project_dir, read_text};
6 mock_analysis::{single_file, MockAnalysis},
11 fn test_highlighting() {
14 #[derive(Clone, Debug)]
25 fn bar(&self) -> i32 {
30 static mut STATIC_MUT: i32 = 0;
32 fn foo<'a, T>() -> T {
37 ($($tt:tt)*) => {$($tt)*}
48 println!("Hello, {}!", 92);
50 let mut vec = Vec::new();
53 vec.push(Foo { x, y: 1 });
68 let Foo { x: z, y } = Foo { x: z, y };
80 fn and<U>(self, other: Option<U>) -> Option<(T, U)> {
82 None => unimplemented!(),
89 "crates/ra_ide/src/snapshots/highlighting.html",
95 fn test_rainbow_highlighting() {
100 let x = hello.to_string();
101 let y = hello.to_string();
103 let x = "other color please!";
104 let y = x.to_string();
108 let mut hello = "hello";
112 "crates/ra_ide/src/snapshots/rainbow_highlighting.html",
118 fn accidentally_quadratic() {
119 let file = project_dir().join("crates/ra_syntax/test_data/accidentally_quadratic");
120 let src = fs::read_to_string(file).unwrap();
122 let mut mock = MockAnalysis::new();
123 let file_id = mock.add_file("/main.rs", &src);
124 let host = mock.analysis_host();
126 // let t = std::time::Instant::now();
127 let _ = host.analysis().highlight(file_id).unwrap();
128 // eprintln!("elapsed: {:?}", t.elapsed());
133 let (analysis, file_id) = single_file(
135 #[derive(Clone, Debug)]
143 let highlights = &analysis
144 .highlight_range(FileRange { file_id, range: TextRange::at(82.into(), 1.into()) })
147 assert_eq!(&highlights[0].highlight.to_string(), "field.declaration");
151 fn test_flattening() {
154 fn fixture(ra_fixture: &str) {}
160 println!("2 + 2 = {}", 4);
166 "crates/ra_ide/src/snapshots/highlight_injection.html",
173 let (analysis, file_id) = single_file(
180 let _ = analysis.highlight(file_id).unwrap();
184 fn test_string_highlighting() {
185 // The format string detection is based on macro-expansion,
186 // thus, we have to copy the macro definition from `std`
189 macro_rules! println {
191 $crate::io::_print($crate::format_args_nl!($($arg)*));
194 #[rustc_builtin_macro]
195 macro_rules! format_args_nl {
196 ($fmt:expr) => {{ /* compiler built-in */ }};
197 ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
201 // from https://doc.rust-lang.org/std/fmt/index.html
202 println!("Hello"); // => "Hello"
203 println!("Hello, {}!", "world"); // => "Hello, world!"
204 println!("The number is {}", 1); // => "The number is 1"
205 println!("{:?}", (3, 4)); // => "(3, 4)"
206 println!("{value}", value=4); // => "4"
207 println!("{} {}", 1, 2); // => "1 2"
208 println!("{:04}", 42); // => "0042" with leading zerosV
209 println!("{1} {} {0} {}", 1, 2); // => "2 1 1 2"
210 println!("{argument}", argument = "test"); // => "test"
211 println!("{name} {}", 1, name = 2); // => "2 1"
212 println!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b"
213 println!("{{{}}}", 2); // => "{2}"
214 println!("Hello {:5}!", "x");
215 println!("Hello {:1$}!", "x", 5);
216 println!("Hello {1:0$}!", 5, "x");
217 println!("Hello {:width$}!", "x", width = 5);
218 println!("Hello {:<5}!", "x");
219 println!("Hello {:-<5}!", "x");
220 println!("Hello {:^5}!", "x");
221 println!("Hello {:>5}!", "x");
222 println!("Hello {:+}!", 5);
223 println!("{:#x}!", 27);
224 println!("Hello {:05}!", 5);
225 println!("Hello {:05}!", -5);
226 println!("{:#010x}!", 27);
227 println!("Hello {0} is {1:.5}", "x", 0.01);
228 println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
229 println!("Hello {0} is {2:.1$}", "x", 5, 0.01);
230 println!("Hello {} is {:.*}", "x", 5, 0.01);
231 println!("Hello {} is {2:.*}", "x", 5, 0.01);
232 println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01);
233 println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56);
234 println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56");
235 println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56");
236 println!("Hello {{}}");
237 println!("{{ Hello");
239 println!(r"Hello, {}!", "world");
241 println!("{\x41}", A = 92);
242 println!("{ничоси}", ничоси = 92);
245 "crates/ra_ide/src/snapshots/highlight_strings.html",
251 fn test_unsafe_highlighting() {
254 unsafe fn unsafe_fn() {}
259 unsafe fn unsafe_method(&self) {}
263 let x = &5 as *const usize;
266 HasUnsafeFn.unsafe_method();
273 "crates/ra_ide/src/snapshots/highlight_unsafe.html",
279 fn test_highlight_doctest() {
283 /// Constructs a new `Foo`.
288 /// # #![allow(unused_mut)]
289 /// let mut foo: Foo = Foo::new();
291 pub const fn new() -> Foo {
295 /// `bar` method on `Foo`.
300 /// let foo = Foo::new();
302 /// // calls bar on foo
303 /// assert!(foo.bar());
308 /// let multi_line_string = "Foo
315 /// let foobar = Foo::new().bar();
317 pub fn foo(&self) -> bool {
323 "crates/ra_ide/src/snapshots/highlight_doctest.html",
328 /// Highlights the code given by the `ra_fixture` argument, renders the
329 /// result as HTML, and compares it with the HTML file given as `snapshot`.
330 /// Note that the `snapshot` file is overwritten by the rendered HTML.
331 fn check_highlighting(ra_fixture: &str, snapshot: &str, rainbow: bool) {
332 let (analysis, file_id) = single_file(ra_fixture);
333 let dst_file = project_dir().join(snapshot);
334 let actual_html = &analysis.highlight_as_html(file_id, rainbow).unwrap();
335 let expected_html = &read_text(&dst_file);
336 fs::write(dst_file, &actual_html).unwrap();
337 assert_eq_text!(expected_html, actual_html);