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)]
20 fn foo<'a, T>() -> T {
25 ($($tt:tt)*) => {$($tt)*}
36 println!("Hello, {}!", 92);
38 let mut vec = Vec::new();
41 vec.push(Foo { x, y: 1 });
43 unsafe { vec.set_len(0); }
59 fn and<U>(self, other: Option<U>) -> Option<(T, U)> {
61 None => unimplemented!(),
69 let dst_file = project_dir().join("crates/ra_ide/src/snapshots/highlighting.html");
70 let actual_html = &analysis.highlight_as_html(file_id, false).unwrap();
71 let expected_html = &read_text(&dst_file);
72 fs::write(dst_file, &actual_html).unwrap();
73 assert_eq_text!(expected_html, actual_html);
77 fn test_rainbow_highlighting() {
78 let (analysis, file_id) = single_file(
82 let x = hello.to_string();
83 let y = hello.to_string();
85 let x = "other color please!";
86 let y = x.to_string();
90 let mut hello = "hello";
95 let dst_file = project_dir().join("crates/ra_ide/src/snapshots/rainbow_highlighting.html");
96 let actual_html = &analysis.highlight_as_html(file_id, true).unwrap();
97 let expected_html = &read_text(&dst_file);
98 fs::write(dst_file, &actual_html).unwrap();
99 assert_eq_text!(expected_html, actual_html);
103 fn accidentally_quadratic() {
104 let file = project_dir().join("crates/ra_syntax/test_data/accidentally_quadratic");
105 let src = fs::read_to_string(file).unwrap();
107 let mut mock = MockAnalysis::new();
108 let file_id = mock.add_file("/main.rs", &src);
109 let host = mock.analysis_host();
111 // let t = std::time::Instant::now();
112 let _ = host.analysis().highlight(file_id).unwrap();
113 // eprintln!("elapsed: {:?}", t.elapsed());
118 let (analysis, file_id) = single_file(
120 #[derive(Clone, Debug)]
128 let highlights = &analysis
129 .highlight_range(FileRange { file_id, range: TextRange::at(82.into(), 1.into()) })
132 assert_eq!(&highlights[0].highlight.to_string(), "field.declaration");
136 fn test_flattening() {
137 let (analysis, file_id) = single_file(
139 fn fixture(ra_fixture: &str) {}
145 println!("2 + 2 = {}", 4);
153 let dst_file = project_dir().join("crates/ra_ide/src/snapshots/highlight_injection.html");
154 let actual_html = &analysis.highlight_as_html(file_id, false).unwrap();
155 let expected_html = &read_text(&dst_file);
156 fs::write(dst_file, &actual_html).unwrap();
157 assert_eq_text!(expected_html, actual_html);
162 let (analysis, file_id) = single_file(
169 let _ = analysis.highlight(file_id).unwrap();
173 fn test_string_highlighting() {
174 // The format string detection is based on macro-expansion,
175 // thus, we have to copy the macro definition from `std`
176 let (analysis, file_id) = single_file(
178 macro_rules! println {
180 $crate::io::_print($crate::format_args_nl!($($arg)*));
183 #[rustc_builtin_macro]
184 macro_rules! format_args_nl {
185 ($fmt:expr) => {{ /* compiler built-in */ }};
186 ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
190 // from https://doc.rust-lang.org/std/fmt/index.html
191 println!("Hello"); // => "Hello"
192 println!("Hello, {}!", "world"); // => "Hello, world!"
193 println!("The number is {}", 1); // => "The number is 1"
194 println!("{:?}", (3, 4)); // => "(3, 4)"
195 println!("{value}", value=4); // => "4"
196 println!("{} {}", 1, 2); // => "1 2"
197 println!("{:04}", 42); // => "0042" with leading zerosV
198 println!("{1} {} {0} {}", 1, 2); // => "2 1 1 2"
199 println!("{argument}", argument = "test"); // => "test"
200 println!("{name} {}", 1, name = 2); // => "2 1"
201 println!("{a} {c} {b}", a="a", b='b', c=3); // => "a 3 b"
202 println!("Hello {:5}!", "x");
203 println!("Hello {:1$}!", "x", 5);
204 println!("Hello {1:0$}!", 5, "x");
205 println!("Hello {:width$}!", "x", width = 5);
206 println!("Hello {:<5}!", "x");
207 println!("Hello {:-<5}!", "x");
208 println!("Hello {:^5}!", "x");
209 println!("Hello {:>5}!", "x");
210 println!("Hello {:+}!", 5);
211 println!("{:#x}!", 27);
212 println!("Hello {:05}!", 5);
213 println!("Hello {:05}!", -5);
214 println!("{:#010x}!", 27);
215 println!("Hello {0} is {1:.5}", "x", 0.01);
216 println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
217 println!("Hello {0} is {2:.1$}", "x", 5, 0.01);
218 println!("Hello {} is {:.*}", "x", 5, 0.01);
219 println!("Hello {} is {2:.*}", "x", 5, 0.01);
220 println!("Hello {} is {number:.prec$}", "x", prec = 5, number = 0.01);
221 println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56);
222 println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56");
223 println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56");
224 println!("Hello {{}}");
225 println!("{{ Hello");
227 println!(r"Hello, {}!", "world");
229 println!("{\x41}", A = 92);
230 println!("{ничоси}", ничоси = 92);
235 let dst_file = project_dir().join("crates/ra_ide/src/snapshots/highlight_strings.html");
236 let actual_html = &analysis.highlight_as_html(file_id, false).unwrap();
237 let expected_html = &read_text(&dst_file);
238 fs::write(dst_file, &actual_html).unwrap();
239 assert_eq_text!(expected_html, actual_html);