@@ -1047,21 +1047,28 @@ impl RichChar for (usize, char) {
1047
1047
#[ derive( PartialEq , Eq , Debug , Clone , Copy ) ]
1048
1048
enum CharClassesStatus {
1049
1049
Normal ,
1050
+ /// Character is within a string
1050
1051
LitString ,
1051
1052
LitStringEscape ,
1053
+ /// Character is within a raw string
1052
1054
LitRawString ( u32 ) ,
1053
1055
RawStringPrefix ( u32 ) ,
1054
1056
RawStringSuffix ( u32 ) ,
1055
1057
LitChar ,
1056
1058
LitCharEscape ,
1057
- // The u32 is the nesting deepness of the comment
1059
+ /// Character inside a block comment, with the integer indicating the nesting deepness of the
1060
+ /// comment
1058
1061
BlockComment ( u32 ) ,
1059
- // Status when the '/' has been consumed, but not yet the '*', deepness is
1060
- // the new deepness (after the comment opening).
1062
+ /// Character inside a block-commented string, with the integer indicating the nesting deepness
1063
+ /// of the comment
1064
+ StringInBlockComment ( u32 ) ,
1065
+ /// Status when the '/' has been consumed, but not yet the '*', deepness is
1066
+ /// the new deepness (after the comment opening).
1061
1067
BlockCommentOpening ( u32 ) ,
1062
- // Status when the '*' has been consumed, but not yet the '/', deepness is
1063
- // the new deepness (after the comment closing).
1068
+ /// Status when the '*' has been consumed, but not yet the '/', deepness is
1069
+ /// the new deepness (after the comment closing).
1064
1070
BlockCommentClosing ( u32 ) ,
1071
+ /// Character is within a line comment
1065
1072
LineComment ,
1066
1073
}
1067
1074
@@ -1085,6 +1092,12 @@ pub enum FullCodeCharKind {
1085
1092
InComment ,
1086
1093
/// Last character of a comment, '\n' for a line comment, '/' for a block comment.
1087
1094
EndComment ,
1095
+ /// Start of a mutlitine string inside a comment
1096
+ StartStringCommented ,
1097
+ /// End of a mutlitine string inside a comment
1098
+ EndStringCommented ,
1099
+ /// Inside a commented string
1100
+ InStringCommented ,
1088
1101
/// Start of a mutlitine string
1089
1102
StartString ,
1090
1103
/// End of a mutlitine string
@@ -1098,7 +1111,21 @@ impl FullCodeCharKind {
1098
1111
match self {
1099
1112
FullCodeCharKind :: StartComment
1100
1113
| FullCodeCharKind :: InComment
1101
- | FullCodeCharKind :: EndComment => true ,
1114
+ | FullCodeCharKind :: EndComment
1115
+ | FullCodeCharKind :: StartStringCommented
1116
+ | FullCodeCharKind :: InStringCommented
1117
+ | FullCodeCharKind :: EndStringCommented => true ,
1118
+ _ => false ,
1119
+ }
1120
+ }
1121
+
1122
+ /// Returns true if the character is inside a comment
1123
+ pub fn inside_comment ( self ) -> bool {
1124
+ match self {
1125
+ FullCodeCharKind :: InComment
1126
+ | FullCodeCharKind :: StartStringCommented
1127
+ | FullCodeCharKind :: InStringCommented
1128
+ | FullCodeCharKind :: EndStringCommented => true ,
1102
1129
_ => false ,
1103
1130
}
1104
1131
}
@@ -1107,6 +1134,12 @@ impl FullCodeCharKind {
1107
1134
self == FullCodeCharKind :: InString || self == FullCodeCharKind :: StartString
1108
1135
}
1109
1136
1137
+ /// Returns true if the character is within a commented string
1138
+ pub fn is_commented_string ( self ) -> bool {
1139
+ self == FullCodeCharKind :: InStringCommented
1140
+ || self == FullCodeCharKind :: StartStringCommented
1141
+ }
1142
+
1110
1143
fn to_codecharkind ( self ) -> CodeCharKind {
1111
1144
if self . is_comment ( ) {
1112
1145
CodeCharKind :: Comment
@@ -1250,18 +1283,27 @@ where
1250
1283
} ,
1251
1284
_ => CharClassesStatus :: Normal ,
1252
1285
} ,
1286
+ CharClassesStatus :: StringInBlockComment ( deepness) => {
1287
+ char_kind = FullCodeCharKind :: InStringCommented ;
1288
+ if chr == '"' {
1289
+ CharClassesStatus :: BlockComment ( deepness)
1290
+ } else {
1291
+ CharClassesStatus :: StringInBlockComment ( deepness)
1292
+ }
1293
+ }
1253
1294
CharClassesStatus :: BlockComment ( deepness) => {
1254
1295
assert_ne ! ( deepness, 0 ) ;
1255
- self . status = match self . base . peek ( ) {
1296
+ char_kind = FullCodeCharKind :: InComment ;
1297
+ match self . base . peek ( ) {
1256
1298
Some ( next) if next. get_char ( ) == '/' && chr == '*' => {
1257
1299
CharClassesStatus :: BlockCommentClosing ( deepness - 1 )
1258
1300
}
1259
1301
Some ( next) if next. get_char ( ) == '*' && chr == '/' => {
1260
1302
CharClassesStatus :: BlockCommentOpening ( deepness + 1 )
1261
1303
}
1262
- _ = > CharClassesStatus :: BlockComment ( deepness) ,
1263
- } ;
1264
- return Some ( ( FullCodeCharKind :: InComment , item ) ) ;
1304
+ _ if chr == '"' = > CharClassesStatus :: StringInBlockComment ( deepness) ,
1305
+ _ => self . status ,
1306
+ }
1265
1307
}
1266
1308
CharClassesStatus :: BlockCommentOpening ( deepness) => {
1267
1309
assert_eq ! ( chr, '*' ) ;
@@ -1317,26 +1359,33 @@ impl<'a> Iterator for LineClasses<'a> {
1317
1359
1318
1360
let mut line = String :: new ( ) ;
1319
1361
1320
- let start_class = match self . base . peek ( ) {
1362
+ let start_kind = match self . base . peek ( ) {
1321
1363
Some ( ( kind, _) ) => * kind,
1322
1364
None => unreachable ! ( ) ,
1323
1365
} ;
1324
1366
1325
1367
while let Some ( ( kind, c) ) = self . base . next ( ) {
1368
+ // needed to set the kind of the ending character on the last line
1369
+ self . kind = kind;
1326
1370
if c == '\n' {
1327
- self . kind = match ( start_class , kind) {
1371
+ self . kind = match ( start_kind , kind) {
1328
1372
( FullCodeCharKind :: Normal , FullCodeCharKind :: InString ) => {
1329
1373
FullCodeCharKind :: StartString
1330
1374
}
1331
1375
( FullCodeCharKind :: InString , FullCodeCharKind :: Normal ) => {
1332
1376
FullCodeCharKind :: EndString
1333
1377
}
1378
+ ( FullCodeCharKind :: InComment , FullCodeCharKind :: InStringCommented ) => {
1379
+ FullCodeCharKind :: StartStringCommented
1380
+ }
1381
+ ( FullCodeCharKind :: InStringCommented , FullCodeCharKind :: InComment ) => {
1382
+ FullCodeCharKind :: EndStringCommented
1383
+ }
1334
1384
_ => kind,
1335
1385
} ;
1336
1386
break ;
1337
- } else {
1338
- line. push ( c) ;
1339
1387
}
1388
+ line. push ( c) ;
1340
1389
}
1341
1390
1342
1391
// Workaround for CRLF newline.
@@ -1382,7 +1431,12 @@ impl<'a> Iterator for UngroupedCommentCodeSlices<'a> {
1382
1431
}
1383
1432
FullCodeCharKind :: StartComment => {
1384
1433
// Consume the whole comment
1385
- while let Some ( ( FullCodeCharKind :: InComment , ( _, _) ) ) = self . iter . next ( ) { }
1434
+ loop {
1435
+ match self . iter . next ( ) {
1436
+ Some ( ( kind, ..) ) if kind. inside_comment ( ) => continue ,
1437
+ _ => break ,
1438
+ }
1439
+ }
1386
1440
}
1387
1441
_ => panic ! ( ) ,
1388
1442
}
0 commit comments