ide/syntax_highlighting/
escape.rs

1//! Syntax highlighting for escape sequences
2use crate::syntax_highlighting::highlights::Highlights;
3use crate::{HlRange, HlTag};
4use syntax::ast::{Byte, Char, IsString};
5use syntax::{AstToken, TextRange, TextSize};
6
7pub(super) fn highlight_escape_string<T: IsString>(stack: &mut Highlights, string: &T) {
8    let text = string.text();
9    let start = string.syntax().text_range().start();
10    string.escaped_char_ranges(&mut |piece_range, char| {
11        if text[piece_range.start().into()..].starts_with('\\') {
12            let highlight = match char {
13                Ok(_) => HlTag::EscapeSequence,
14                Err(_) => HlTag::InvalidEscapeSequence,
15            };
16            stack.add(HlRange {
17                range: piece_range + start,
18                highlight: highlight.into(),
19                binding_hash: None,
20            });
21        }
22    });
23}
24
25pub(super) fn highlight_escape_char(stack: &mut Highlights, char: &Char) {
26    if char.value().is_err() {
27        // We do not emit invalid escapes highlighting here. The lexer would likely be in a bad
28        // state and this token contains junk, since `'` is not a reliable delimiter (consider
29        // lifetimes). Nonetheless, parser errors should already be emitted.
30        return;
31    }
32
33    let text = char.text();
34    let Some(text) = text
35        .strip_prefix('\'')
36        .and_then(|it| it.strip_suffix('\''))
37        .filter(|it| it.starts_with('\\'))
38    else {
39        return;
40    };
41
42    let range = TextRange::at(
43        char.syntax().text_range().start() + TextSize::from(1),
44        TextSize::from(text.len() as u32),
45    );
46    stack.add(HlRange { range, highlight: HlTag::EscapeSequence.into(), binding_hash: None })
47}
48
49pub(super) fn highlight_escape_byte(stack: &mut Highlights, byte: &Byte) {
50    if byte.value().is_err() {
51        // See `highlight_escape_char` for why no error highlighting here.
52        return;
53    }
54
55    let text = byte.text();
56    let Some(text) = text
57        .strip_prefix("b'")
58        .and_then(|it| it.strip_suffix('\''))
59        .filter(|it| it.starts_with('\\'))
60    else {
61        return;
62    };
63
64    let range = TextRange::at(
65        byte.syntax().text_range().start() + TextSize::from(2),
66        TextSize::from(text.len() as u32),
67    );
68    stack.add(HlRange { range, highlight: HlTag::EscapeSequence.into(), binding_hash: None })
69}