aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src
diff options
context:
space:
mode:
authorLeander Tentrup <[email protected]>2020-04-22 14:28:35 +0100
committerLeander Tentrup <[email protected]>2020-04-22 14:28:35 +0100
commit445052f6d426043b543033f3fa4594fc1a09d7fa (patch)
tree3638c65c16b78f142101a87274edd94d647b6745 /crates/ra_ide/src
parentb2829a52161bc414f3b361c06b66633a234bba16 (diff)
Adapt format specifier highlighting to support escaped squences and unicode identifiers
Diffstat (limited to 'crates/ra_ide/src')
-rw-r--r--crates/ra_ide/src/snapshots/highlight_strings.html5
-rw-r--r--crates/ra_ide/src/syntax_highlighting.rs63
-rw-r--r--crates/ra_ide/src/syntax_highlighting/tests.rs5
3 files changed, 51 insertions, 22 deletions
diff --git a/crates/ra_ide/src/snapshots/highlight_strings.html b/crates/ra_ide/src/snapshots/highlight_strings.html
index d70627da0..433f2e0c5 100644
--- a/crates/ra_ide/src/snapshots/highlight_strings.html
+++ b/crates/ra_ide/src/snapshots/highlight_strings.html
@@ -74,4 +74,9 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
74 <span class="macro">println!</span>(<span class="string_literal">"</span><span class="attribute">{</span><span class="attribute">}</span><span class="string_literal">, `</span><span class="attribute">{</span><span class="variable">name</span><span class="attribute">:</span><span class="attribute">&gt;</span><span class="numeric_literal">8</span><span class="attribute">.</span><span class="attribute">*</span><span class="attribute">}</span><span class="string_literal">` has 3 right-aligned characters"</span>, <span class="string_literal">"Hello"</span>, <span class="numeric_literal">3</span>, name=<span class="string_literal">"1234.56"</span>); 74 <span class="macro">println!</span>(<span class="string_literal">"</span><span class="attribute">{</span><span class="attribute">}</span><span class="string_literal">, `</span><span class="attribute">{</span><span class="variable">name</span><span class="attribute">:</span><span class="attribute">&gt;</span><span class="numeric_literal">8</span><span class="attribute">.</span><span class="attribute">*</span><span class="attribute">}</span><span class="string_literal">` has 3 right-aligned characters"</span>, <span class="string_literal">"Hello"</span>, <span class="numeric_literal">3</span>, name=<span class="string_literal">"1234.56"</span>);
75 <span class="macro">println!</span>(<span class="string_literal">"Hello {{}}"</span>); 75 <span class="macro">println!</span>(<span class="string_literal">"Hello {{}}"</span>);
76 <span class="macro">println!</span>(<span class="string_literal">"{{ Hello"</span>); 76 <span class="macro">println!</span>(<span class="string_literal">"{{ Hello"</span>);
77
78 <span class="macro">println!</span>(<span class="string_literal">r"Hello, </span><span class="attribute">{</span><span class="attribute">}</span><span class="string_literal">!"</span>, <span class="string_literal">"world"</span>);
79
80 <span class="macro">println!</span>(<span class="string_literal">"</span><span class="attribute">{</span><span class="variable">\x41</span><span class="attribute">}</span><span class="string_literal">"</span>, A = <span class="numeric_literal">92</span>);
81 <span class="macro">println!</span>(<span class="string_literal">"</span><span class="attribute">{</span><span class="variable">ничоси</span><span class="attribute">}</span><span class="string_literal">"</span>, ничоси = <span class="numeric_literal">92</span>);
77}</code></pre> \ No newline at end of file 82}</code></pre> \ No newline at end of file
diff --git a/crates/ra_ide/src/syntax_highlighting.rs b/crates/ra_ide/src/syntax_highlighting.rs
index 8ee3a78c6..b5fd3390f 100644
--- a/crates/ra_ide/src/syntax_highlighting.rs
+++ b/crates/ra_ide/src/syntax_highlighting.rs
@@ -245,28 +245,29 @@ pub(crate) fn highlight(
245 stack.push(); 245 stack.push();
246 if is_format_string { 246 if is_format_string {
247 string.lex_format_specifier(|piece_range, kind| { 247 string.lex_format_specifier(|piece_range, kind| {
248 let highlight = match kind { 248 if let Some(highlight) = highlight_format_specifier(kind) {
249 FormatSpecifier::Open 249 stack.add(HighlightedRange {
250 | FormatSpecifier::Close 250 range: piece_range + range.start(),
251 | FormatSpecifier::Colon 251 highlight: highlight.into(),
252 | FormatSpecifier::Fill 252 binding_hash: None,
253 | FormatSpecifier::Align 253 });
254 | FormatSpecifier::Sign 254 }
255 | FormatSpecifier::NumberSign 255 });
256 | FormatSpecifier::DollarSign 256 }
257 | FormatSpecifier::Dot 257 stack.pop();
258 | FormatSpecifier::Asterisk 258 } else if let Some(string) =
259 | FormatSpecifier::QuestionMark => HighlightTag::Attribute, 259 element_to_highlight.as_token().cloned().and_then(ast::RawString::cast)
260 FormatSpecifier::Integer | FormatSpecifier::Zero => { 260 {
261 HighlightTag::NumericLiteral 261 stack.push();
262 } 262 if is_format_string {
263 FormatSpecifier::Identifier => HighlightTag::Local, 263 string.lex_format_specifier(|piece_range, kind| {
264 }; 264 if let Some(highlight) = highlight_format_specifier(kind) {
265 stack.add(HighlightedRange { 265 stack.add(HighlightedRange {
266 range: piece_range + range.start(), 266 range: piece_range + range.start(),
267 highlight: highlight.into(), 267 highlight: highlight.into(),
268 binding_hash: None, 268 binding_hash: None,
269 }); 269 });
270 }
270 }); 271 });
271 } 272 }
272 stack.pop(); 273 stack.pop();
@@ -277,6 +278,24 @@ pub(crate) fn highlight(
277 stack.flattened() 278 stack.flattened()
278} 279}
279 280
281fn highlight_format_specifier(kind: FormatSpecifier) -> Option<HighlightTag> {
282 Some(match kind {
283 FormatSpecifier::Open
284 | FormatSpecifier::Close
285 | FormatSpecifier::Colon
286 | FormatSpecifier::Fill
287 | FormatSpecifier::Align
288 | FormatSpecifier::Sign
289 | FormatSpecifier::NumberSign
290 | FormatSpecifier::DollarSign
291 | FormatSpecifier::Dot
292 | FormatSpecifier::Asterisk
293 | FormatSpecifier::QuestionMark => HighlightTag::Attribute,
294 FormatSpecifier::Integer | FormatSpecifier::Zero => HighlightTag::NumericLiteral,
295 FormatSpecifier::Identifier => HighlightTag::Local,
296 })
297}
298
280fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> { 299fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> {
281 let path = macro_call.path()?; 300 let path = macro_call.path()?;
282 let name_ref = path.segment()?.name_ref()?; 301 let name_ref = path.segment()?.name_ref()?;
diff --git a/crates/ra_ide/src/syntax_highlighting/tests.rs b/crates/ra_ide/src/syntax_highlighting/tests.rs
index f198767ce..a9aae957f 100644
--- a/crates/ra_ide/src/syntax_highlighting/tests.rs
+++ b/crates/ra_ide/src/syntax_highlighting/tests.rs
@@ -223,6 +223,11 @@ fn main() {
223 println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56"); 223 println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56");
224 println!("Hello {{}}"); 224 println!("Hello {{}}");
225 println!("{{ Hello"); 225 println!("{{ Hello");
226
227 println!(r"Hello, {}!", "world");
228
229 println!("{\x41}", A = 92);
230 println!("{ничоси}", ничоси = 92);
226}"# 231}"#
227 .trim(), 232 .trim(),
228 ); 233 );