diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index 454a250f3d..5821bb1202 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs @@ -21,8 +21,11 @@ use syntax::{ use crate::{ syntax_highlighting::{ - escape::highlight_escape_string, format::highlight_format_string, highlights::Highlights, - macro_::MacroHighlighter, tags::Highlight, + escape::{highlight_escape_char, highlight_escape_string}, + format::highlight_format_string, + highlights::Highlights, + macro_::MacroHighlighter, + tags::Highlight, }, FileId, HlMod, HlOperator, HlPunct, HlTag, }; @@ -427,6 +430,14 @@ fn traverse( if let Some(byte_string) = ast::ByteString::cast(token) { highlight_escape_string(hl, &byte_string, range.start()); } + } else if ast::Char::can_cast(token.kind()) + && ast::Char::can_cast(descended_token.kind()) + { + let Some(char) = ast::Char::cast(token) else { + continue; + }; + + highlight_escape_char(hl, &char, range.start()) } } diff --git a/crates/ide/src/syntax_highlighting/escape.rs b/crates/ide/src/syntax_highlighting/escape.rs index 6a1236c793..211e358809 100644 --- a/crates/ide/src/syntax_highlighting/escape.rs +++ b/crates/ide/src/syntax_highlighting/escape.rs @@ -1,8 +1,8 @@ //! Syntax highlighting for escape sequences use crate::syntax_highlighting::highlights::Highlights; use crate::{HlRange, HlTag}; -use syntax::ast::IsString; -use syntax::TextSize; +use syntax::ast::{Char, IsString}; +use syntax::{AstToken, TextRange, TextSize}; pub(super) fn highlight_escape_string( stack: &mut Highlights, @@ -23,3 +23,23 @@ pub(super) fn highlight_escape_string( } }); } + +pub(super) fn highlight_escape_char(stack: &mut Highlights, char: &Char, start: TextSize) { + if char.value().is_none() { + return; + } + + let text = char.text(); + if !text.starts_with('\'') || !text.ends_with('\'') { + return; + } + + let text = &text[1..text.len() - 1]; + if !text.starts_with('\\') { + return; + } + + let range = + TextRange::new(start + TextSize::from(1), start + TextSize::from(text.len() as u32 + 1)); + stack.add(HlRange { range, highlight: HlTag::EscapeSequence.into(), binding_hash: None }) +} diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html index a626cda3fe..d34f5cffbf 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html @@ -93,6 +93,16 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd } fn main() { + let a = '\n'; + let a = '\t'; + let a = '\e'; // invalid escape + let a = 'e'; + let a = ' '; + let a = '\u{48}'; + let a = '\u{4823}'; + let a = '\x65'; + let a = '\x00'; + println!("Hello {{Hello}}"); // from https://doc.rust-lang.org/std/fmt/index.html println!("Hello"); // => "Hello" diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index 5cc3bad04b..12205d47e5 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -439,6 +439,16 @@ macro_rules! toho { } fn main() { + let a = '\n'; + let a = '\t'; + let a = '\e'; // invalid escape + let a = 'e'; + let a = ' '; + let a = '\u{48}'; + let a = '\u{4823}'; + let a = '\x65'; + let a = '\x00'; + println!("Hello {{Hello}}"); // from https://doc.rust-lang.org/std/fmt/index.html println!("Hello"); // => "Hello"