3 use test_utils::{assert_eq_text, project_dir, read_text};
6 mock_analysis::{single_file, MockAnalysis},
11 fn test_highlighting() {
12 let (analysis, file_id) = single_file(
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 });
78 fn and<U>(self, other: Option<U>) -> Option<(T, U)> {
80 None => unimplemented!(),
88 let dst_file = project_dir().join("crates/ra_ide/src/snapshots/highlighting.html");
89 let actual_html = &analysis.highlight_as_html(file_id, false).unwrap();
90 let expected_html = &read_text(&dst_file);
91 fs::write(dst_file, &actual_html).unwrap();
92 assert_eq_text!(expected_html, actual_html);
96 fn test_rainbow_highlighting() {
97 let (analysis, file_id) = single_file(
101 let x = hello.to_string();
102 let y = hello.to_string();
104 let x = "other color please!";
105 let y = x.to_string();
109 let mut hello = "hello";
114 let dst_file = project_dir().join("crates/ra_ide/src/snapshots/rainbow_highlighting.html");
115 let actual_html = &analysis.highlight_as_html(file_id, true).unwrap();
116 let expected_html = &read_text(&dst_file);
117 fs::write(dst_file, &actual_html).unwrap();
118 assert_eq_text!(expected_html, actual_html);
122 fn accidentally_quadratic() {
123 let file = project_dir().join("crates/ra_syntax/test_data/accidentally_quadratic");
124 let src = fs::read_to_string(file).unwrap();
126 let mut mock = MockAnalysis::new();
127 let file_id = mock.add_file("/main.rs", &src);
128 let host = mock.analysis_host();
130 // let t = std::time::Instant::now();
131 let _ = host.analysis().highlight(file_id).unwrap();
132 // eprintln!("elapsed: {:?}", t.elapsed());
137 let (analysis, file_id) = single_file(
139 #[derive(Clone, Debug)]
147 let highlights = &analysis
148 .highlight_range(FileRange { file_id, range: TextRange::at(82.into(), 1.into()) })
151 assert_eq!(&highlights[0].highlight.to_string(), "field.declaration");
155 fn test_flattening() {
156 let (analysis, file_id) = single_file(
158 fn fixture(ra_fixture: &str) {}
164 println!("2 + 2 = {}", 4);
172 let dst_file = project_dir().join("crates/ra_ide/src/snapshots/highlight_injection.html");
173 let actual_html = &analysis.highlight_as_html(file_id, false).unwrap();
174 let expected_html = &read_text(&dst_file);
175 fs::write(dst_file, &actual_html).unwrap();
176 assert_eq_text!(expected_html, actual_html);
181 let (analysis, file_id) = single_file(
188 let _ = analysis.highlight(file_id).unwrap();
192 fn test_string_highlighting() {
193 // The format string detection is based on macro-expansion,
194 // thus, we have to copy the macro definition from `std`
195 let (analysis, file_id) = single_file(
197 macro_rules! println {
199 $crate::io::_print($crate::format_args_nl!($($arg)*));
202 #[rustc_builtin_macro]
203 macro_rules! format_args_nl {
204 ($fmt:expr) => {{ /* compiler built-in */ }};
205 ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
209 // from https://doc.rust-lang.org/std/fmt/index.html
210 println!("Hello"); // => "Hello"
211 println!("Hello, {}!", "world"); // => "Hello, world!"
212 println!("The number is {}", 1); // => "The number is 1"
213 println!("{:?}", (3, 4)); // => "(3, 4)"
214 println!("{value}", value=4); // => "4"
215 println!("{} {}", 1, 2); // => "1 2"
216 println!("{:04}", 42); // => "0042" with leading zerosV
217 println!("{1} {} {0} {}", 1, 2); // => "2 1 1 2"
218 println!("{argument}", argument = "test"); // => "test"
219 println!("{name} {}", 1, name = 2); // => "2 1"
220 println!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b"
221 println!("{{{}}}", 2); // => "{2}"
222 println!("Hello {:5}!", "x");
223 println!("Hello {:1$}!", "x", 5);
224 println!("Hello {1:0$}!", 5, "x");
225 println!("Hello {:width$}!", "x", width = 5);
226 println!("Hello {:<5}!", "x");
227 println!("Hello {:-<5}!", "x");
228 println!("Hello {:^5}!", "x");
229 println!("Hello {:>5}!", "x");
230 println!("Hello {:+}!", 5);
231 println!("{:#x}!", 27);
232 println!("Hello {:05}!", 5);
233 println!("Hello {:05}!", -5);
234 println!("{:#010x}!", 27);
235 println!("Hello {0} is {1:.5}", "x", 0.01);
236 println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
237 println!("Hello {0} is {2:.1$}", "x", 5, 0.01);
238 println!("Hello {} is {:.*}", "x", 5, 0.01);
239 println!("Hello {} is {2:.*}", "x", 5, 0.01);
240 println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01);
241 println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56);
242 println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56");
243 println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56");
244 println!("Hello {{}}");
245 println!("{{ Hello");
247 println!(r"Hello, {}!", "world");
249 println!("{\x41}", A = 92);
250 println!("{ничоси}", ничоси = 92);
255 let dst_file = project_dir().join("crates/ra_ide/src/snapshots/highlight_strings.html");
256 let actual_html = &analysis.highlight_as_html(file_id, false).unwrap();
257 let expected_html = &read_text(&dst_file);
258 fs::write(dst_file, &actual_html).unwrap();
259 assert_eq_text!(expected_html, actual_html);
263 fn test_unsafe_highlighting() {
264 let (analysis, file_id) = single_file(
266 unsafe fn unsafe_fn() {}
271 unsafe fn unsafe_method(&self) {}
275 let x = &5 as *const usize;
278 HasUnsafeFn.unsafe_method();
286 let dst_file = project_dir().join("crates/ra_ide/src/snapshots/highlight_unsafe.html");
287 let actual_html = &analysis.highlight_as_html(file_id, false).unwrap();
288 let expected_html = &read_text(&dst_file);
289 fs::write(dst_file, &actual_html).unwrap();
290 assert_eq_text!(expected_html, actual_html);