ide/syntax_highlighting/
escape.rsuse crate::syntax_highlighting::highlights::Highlights;
use crate::{HlRange, HlTag};
use syntax::ast::{Byte, Char, IsString};
use syntax::{AstToken, TextRange, TextSize};
pub(super) fn highlight_escape_string<T: IsString>(
stack: &mut Highlights,
string: &T,
start: TextSize,
) {
let text = string.text();
string.escaped_char_ranges(&mut |piece_range, char| {
if text[piece_range.start().into()..].starts_with('\\') {
let highlight = match char {
Ok(_) => HlTag::EscapeSequence,
Err(_) => HlTag::InvalidEscapeSequence,
};
stack.add(HlRange {
range: piece_range + start,
highlight: highlight.into(),
binding_hash: None,
});
}
});
}
pub(super) fn highlight_escape_char(stack: &mut Highlights, char: &Char, start: TextSize) {
if char.value().is_err() {
return;
}
let text = char.text();
let Some(text) = text
.strip_prefix('\'')
.and_then(|it| it.strip_suffix('\''))
.filter(|it| it.starts_with('\\'))
else {
return;
};
let range = TextRange::at(start + TextSize::from(1), TextSize::from(text.len() as u32));
stack.add(HlRange { range, highlight: HlTag::EscapeSequence.into(), binding_hash: None })
}
pub(super) fn highlight_escape_byte(stack: &mut Highlights, byte: &Byte, start: TextSize) {
if byte.value().is_err() {
return;
}
let text = byte.text();
let Some(text) = text
.strip_prefix("b'")
.and_then(|it| it.strip_suffix('\''))
.filter(|it| it.starts_with('\\'))
else {
return;
};
let range = TextRange::at(start + TextSize::from(2), TextSize::from(text.len() as u32));
stack.add(HlRange { range, highlight: HlTag::EscapeSequence.into(), binding_hash: None })
}