1
1
//! Syntax highlighting for escape sequences
2
2
use crate :: syntax_highlighting:: highlights:: Highlights ;
3
3
use crate :: { HlRange , HlTag } ;
4
- use syntax:: ast:: { Char , IsString } ;
4
+ use syntax:: ast:: { Byte , Char , IsString } ;
5
5
use syntax:: { AstToken , TextRange , TextSize } ;
6
6
7
7
pub ( super ) fn highlight_escape_string < T : IsString > (
@@ -10,14 +10,14 @@ pub(super) fn highlight_escape_string<T: IsString>(
10
10
start : TextSize ,
11
11
) {
12
12
string. escaped_char_ranges ( & mut |piece_range, char| {
13
- if char. is_err ( ) {
14
- return ;
15
- }
16
-
17
13
if string. text ( ) [ piece_range. start ( ) . into ( ) ..] . starts_with ( '\\' ) {
14
+ let highlight = match char {
15
+ Ok ( _) => HlTag :: EscapeSequence ,
16
+ Err ( _) => HlTag :: InvalidEscapeSequence ,
17
+ } ;
18
18
stack. add ( HlRange {
19
19
range : piece_range + start,
20
- highlight : HlTag :: EscapeSequence . into ( ) ,
20
+ highlight : highlight . into ( ) ,
21
21
binding_hash : None ,
22
22
} ) ;
23
23
}
@@ -26,6 +26,9 @@ pub(super) fn highlight_escape_string<T: IsString>(
26
26
27
27
pub ( super ) fn highlight_escape_char ( stack : & mut Highlights , char : & Char , start : TextSize ) {
28
28
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.
29
32
return ;
30
33
}
31
34
@@ -43,3 +46,24 @@ pub(super) fn highlight_escape_char(stack: &mut Highlights, char: &Char, start:
43
46
TextRange :: new ( start + TextSize :: from ( 1 ) , start + TextSize :: from ( text. len ( ) as u32 + 1 ) ) ;
44
47
stack. add ( HlRange { range, highlight : HlTag :: EscapeSequence . into ( ) , binding_hash : None } )
45
48
}
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