11//! Syntax highlighting for escape sequences
22use crate :: syntax_highlighting:: highlights:: Highlights ;
33use crate :: { HlRange , HlTag } ;
4- use syntax:: ast:: { Char , IsString } ;
4+ use syntax:: ast:: { Byte , Char , IsString } ;
55use syntax:: { AstToken , TextRange , TextSize } ;
66
77pub ( super ) fn highlight_escape_string < T : IsString > (
@@ -10,14 +10,14 @@ pub(super) fn highlight_escape_string<T: IsString>(
1010 start : TextSize ,
1111) {
1212 string. escaped_char_ranges ( & mut |piece_range, char| {
13- if char. is_err ( ) {
14- return ;
15- }
16-
1713 if string. text ( ) [ piece_range. start ( ) . into ( ) ..] . starts_with ( '\\' ) {
14+ let highlight = match char {
15+ Ok ( _) => HlTag :: EscapeSequence ,
16+ Err ( _) => HlTag :: InvalidEscapeSequence ,
17+ } ;
1818 stack. add ( HlRange {
1919 range : piece_range + start,
20- highlight : HlTag :: EscapeSequence . into ( ) ,
20+ highlight : highlight . into ( ) ,
2121 binding_hash : None ,
2222 } ) ;
2323 }
@@ -26,6 +26,9 @@ pub(super) fn highlight_escape_string<T: IsString>(
2626
2727pub ( super ) fn highlight_escape_char ( stack : & mut Highlights , char : & Char , start : TextSize ) {
2828 if char. value ( ) . is_none ( ) {
29+ // We do not emit invalid escapes highlighting here. The lexer would likely be in a bad
30+ // state and this token contains junks, since `'` is not a reliable delimiter (consider
31+ // lifetimes). Nonetheless, parser errors should already be emitted.
2932 return ;
3033 }
3134
@@ -43,3 +46,24 @@ pub(super) fn highlight_escape_char(stack: &mut Highlights, char: &Char, start:
4346 TextRange :: new ( start + TextSize :: from ( 1 ) , start + TextSize :: from ( text. len ( ) as u32 + 1 ) ) ;
4447 stack. add ( HlRange { range, highlight : HlTag :: EscapeSequence . into ( ) , binding_hash : None } )
4548}
49+
50+ pub ( super ) fn highlight_escape_byte ( stack : & mut Highlights , byte : & Byte , start : TextSize ) {
51+ if byte. value ( ) . is_none ( ) {
52+ // See `highlight_escape_char` for why no error highlighting here.
53+ return ;
54+ }
55+
56+ let text = byte. text ( ) ;
57+ if !text. starts_with ( "b'" ) || !text. ends_with ( '\'' ) {
58+ return ;
59+ }
60+
61+ let text = & text[ 2 ..text. len ( ) - 1 ] ;
62+ if !text. starts_with ( '\\' ) {
63+ return ;
64+ }
65+
66+ let range =
67+ TextRange :: new ( start + TextSize :: from ( 2 ) , start + TextSize :: from ( text. len ( ) as u32 + 2 ) ) ;
68+ stack. add ( HlRange { range, highlight : HlTag :: EscapeSequence . into ( ) , binding_hash : None } )
69+ }
0 commit comments