@@ -1029,21 +1029,28 @@ impl RichChar for (usize, char) {
1029
1029
#[ derive( PartialEq , Eq , Debug , Clone , Copy ) ]
1030
1030
enum CharClassesStatus {
1031
1031
Normal ,
1032
+ /// Character is within a string
1032
1033
LitString ,
1033
1034
LitStringEscape ,
1035
+ /// Character is within a raw string
1034
1036
LitRawString ( u32 ) ,
1035
1037
RawStringPrefix ( u32 ) ,
1036
1038
RawStringSuffix ( u32 ) ,
1037
1039
LitChar ,
1038
1040
LitCharEscape ,
1039
- // The u32 is the nesting deepness of the comment
1041
+ /// Character inside a block comment, with the integer indicating the nesting deepness of the
1042
+ /// comment
1040
1043
BlockComment ( u32 ) ,
1041
- // Status when the '/' has been consumed, but not yet the '*', deepness is
1042
- // the new deepness (after the comment opening).
1044
+ /// Character inside a block-commented string, with the integer indicating the nesting deepness
1045
+ /// of the comment
1046
+ StringInBlockComment ( u32 ) ,
1047
+ /// Status when the '/' has been consumed, but not yet the '*', deepness is
1048
+ /// the new deepness (after the comment opening).
1043
1049
BlockCommentOpening ( u32 ) ,
1044
- // Status when the '*' has been consumed, but not yet the '/', deepness is
1045
- // the new deepness (after the comment closing).
1050
+ /// Status when the '*' has been consumed, but not yet the '/', deepness is
1051
+ /// the new deepness (after the comment closing).
1046
1052
BlockCommentClosing ( u32 ) ,
1053
+ /// Character is within a line comment
1047
1054
LineComment ,
1048
1055
}
1049
1056
@@ -1067,6 +1074,12 @@ pub enum FullCodeCharKind {
1067
1074
InComment ,
1068
1075
/// Last character of a comment, '\n' for a line comment, '/' for a block comment.
1069
1076
EndComment ,
1077
+ /// Start of a mutlitine string inside a comment
1078
+ StartStringCommented ,
1079
+ /// End of a mutlitine string inside a comment
1080
+ EndStringCommented ,
1081
+ /// Inside a commented string
1082
+ InStringCommented ,
1070
1083
/// Start of a mutlitine string
1071
1084
StartString ,
1072
1085
/// End of a mutlitine string
@@ -1080,7 +1093,21 @@ impl FullCodeCharKind {
1080
1093
match self {
1081
1094
FullCodeCharKind :: StartComment
1082
1095
| FullCodeCharKind :: InComment
1083
- | FullCodeCharKind :: EndComment => true ,
1096
+ | FullCodeCharKind :: EndComment
1097
+ | FullCodeCharKind :: StartStringCommented
1098
+ | FullCodeCharKind :: InStringCommented
1099
+ | FullCodeCharKind :: EndStringCommented => true ,
1100
+ _ => false ,
1101
+ }
1102
+ }
1103
+
1104
+ /// Returns true if the character is inside a comment
1105
+ pub fn inside_comment ( self ) -> bool {
1106
+ match self {
1107
+ FullCodeCharKind :: InComment
1108
+ | FullCodeCharKind :: StartStringCommented
1109
+ | FullCodeCharKind :: InStringCommented
1110
+ | FullCodeCharKind :: EndStringCommented => true ,
1084
1111
_ => false ,
1085
1112
}
1086
1113
}
@@ -1089,6 +1116,12 @@ impl FullCodeCharKind {
1089
1116
self == FullCodeCharKind :: InString || self == FullCodeCharKind :: StartString
1090
1117
}
1091
1118
1119
+ /// Returns true if the character is within a commented string
1120
+ pub fn is_commented_string ( self ) -> bool {
1121
+ self == FullCodeCharKind :: InStringCommented
1122
+ || self == FullCodeCharKind :: StartStringCommented
1123
+ }
1124
+
1092
1125
fn to_codecharkind ( self ) -> CodeCharKind {
1093
1126
if self . is_comment ( ) {
1094
1127
CodeCharKind :: Comment
@@ -1232,18 +1265,27 @@ where
1232
1265
} ,
1233
1266
_ => CharClassesStatus :: Normal ,
1234
1267
} ,
1268
+ CharClassesStatus :: StringInBlockComment ( deepness) => {
1269
+ char_kind = FullCodeCharKind :: InStringCommented ;
1270
+ if chr == '"' {
1271
+ CharClassesStatus :: BlockComment ( deepness)
1272
+ } else {
1273
+ CharClassesStatus :: StringInBlockComment ( deepness)
1274
+ }
1275
+ }
1235
1276
CharClassesStatus :: BlockComment ( deepness) => {
1236
1277
assert_ne ! ( deepness, 0 ) ;
1237
- self . status = match self . base . peek ( ) {
1278
+ char_kind = FullCodeCharKind :: InComment ;
1279
+ match self . base . peek ( ) {
1238
1280
Some ( next) if next. get_char ( ) == '/' && chr == '*' => {
1239
1281
CharClassesStatus :: BlockCommentClosing ( deepness - 1 )
1240
1282
}
1241
1283
Some ( next) if next. get_char ( ) == '*' && chr == '/' => {
1242
1284
CharClassesStatus :: BlockCommentOpening ( deepness + 1 )
1243
1285
}
1244
- _ = > CharClassesStatus :: BlockComment ( deepness) ,
1245
- } ;
1246
- return Some ( ( FullCodeCharKind :: InComment , item ) ) ;
1286
+ _ if chr == '"' = > CharClassesStatus :: StringInBlockComment ( deepness) ,
1287
+ _ => self . status ,
1288
+ }
1247
1289
}
1248
1290
CharClassesStatus :: BlockCommentOpening ( deepness) => {
1249
1291
assert_eq ! ( chr, '*' ) ;
@@ -1299,26 +1341,33 @@ impl<'a> Iterator for LineClasses<'a> {
1299
1341
1300
1342
let mut line = String :: new ( ) ;
1301
1343
1302
- let start_class = match self . base . peek ( ) {
1344
+ let start_kind = match self . base . peek ( ) {
1303
1345
Some ( ( kind, _) ) => * kind,
1304
1346
None => unreachable ! ( ) ,
1305
1347
} ;
1306
1348
1307
1349
while let Some ( ( kind, c) ) = self . base . next ( ) {
1350
+ // needed to set the kind of the ending character on the last line
1351
+ self . kind = kind;
1308
1352
if c == '\n' {
1309
- self . kind = match ( start_class , kind) {
1353
+ self . kind = match ( start_kind , kind) {
1310
1354
( FullCodeCharKind :: Normal , FullCodeCharKind :: InString ) => {
1311
1355
FullCodeCharKind :: StartString
1312
1356
}
1313
1357
( FullCodeCharKind :: InString , FullCodeCharKind :: Normal ) => {
1314
1358
FullCodeCharKind :: EndString
1315
1359
}
1360
+ ( FullCodeCharKind :: InComment , FullCodeCharKind :: InStringCommented ) => {
1361
+ FullCodeCharKind :: StartStringCommented
1362
+ }
1363
+ ( FullCodeCharKind :: InStringCommented , FullCodeCharKind :: InComment ) => {
1364
+ FullCodeCharKind :: EndStringCommented
1365
+ }
1316
1366
_ => kind,
1317
1367
} ;
1318
1368
break ;
1319
- } else {
1320
- line. push ( c) ;
1321
1369
}
1370
+ line. push ( c) ;
1322
1371
}
1323
1372
1324
1373
// Workaround for CRLF newline.
@@ -1364,7 +1413,12 @@ impl<'a> Iterator for UngroupedCommentCodeSlices<'a> {
1364
1413
}
1365
1414
FullCodeCharKind :: StartComment => {
1366
1415
// Consume the whole comment
1367
- while let Some ( ( FullCodeCharKind :: InComment , ( _, _) ) ) = self . iter . next ( ) { }
1416
+ loop {
1417
+ match self . iter . next ( ) {
1418
+ Some ( ( kind, ..) ) if kind. inside_comment ( ) => continue ,
1419
+ _ => break ,
1420
+ }
1421
+ }
1368
1422
}
1369
1423
_ => panic ! ( ) ,
1370
1424
}
0 commit comments