aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/syntax_highlighting
diff options
context:
space:
mode:
authorLeander Tentrup <[email protected]>2020-04-17 08:37:18 +0100
committerLeander Tentrup <[email protected]>2020-04-20 10:19:15 +0100
commitac798e1f7cfbc6d27c87bb28e3f1d5b6801796aa (patch)
tree958ccd22ae5e75047b8189f69cf5878eeb9611ea /crates/ra_ide/src/syntax_highlighting
parent29a846464b63259f5152d61a5520bffcc2cb8703 (diff)
Implement syntax highlighting for format strings
Detailed changes: 1) Implement a lexer for string literals that divides the string in format specifier `{}` including the format specifier modifier. 2) Adapt syntax highlighting to add ranges for the detected sequences. 3) Add a test case for the format string syntax highlighting.
Diffstat (limited to 'crates/ra_ide/src/syntax_highlighting')
-rw-r--r--crates/ra_ide/src/syntax_highlighting/tests.rs65
1 files changed, 65 insertions, 0 deletions
diff --git a/crates/ra_ide/src/syntax_highlighting/tests.rs b/crates/ra_ide/src/syntax_highlighting/tests.rs
index 73611e23a..f198767ce 100644
--- a/crates/ra_ide/src/syntax_highlighting/tests.rs
+++ b/crates/ra_ide/src/syntax_highlighting/tests.rs
@@ -168,3 +168,68 @@ macro_rules! test {}
168 ); 168 );
169 let _ = analysis.highlight(file_id).unwrap(); 169 let _ = analysis.highlight(file_id).unwrap();
170} 170}
171
172#[test]
173fn 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(
177 r#"
178macro_rules! println {
179 ($($arg:tt)*) => ({
180 $crate::io::_print($crate::format_args_nl!($($arg)*));
181 })
182}
183#[rustc_builtin_macro]
184macro_rules! format_args_nl {
185 ($fmt:expr) => {{ /* compiler built-in */ }};
186 ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
187}
188
189fn main() {
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");
226}"#
227 .trim(),
228 );
229
230 let dst_file = project_dir().join("crates/ra_ide/src/snapshots/highlight_strings.html");
231 let actual_html = &analysis.highlight_as_html(file_id, false).unwrap();
232 let expected_html = &read_text(&dst_file);
233 fs::write(dst_file, &actual_html).unwrap();
234 assert_eq_text!(expected_html, actual_html);
235}