@@ -133,7 +133,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
133
133
removeRedundantFixIt: ( _ misplacedTokens: [ TokenSyntax ] ) -> FixItMessage ? = { _ in nil }
134
134
) {
135
135
guard let incorrectContainer = unexpected,
136
- let misplacedTokens = incorrectContainer. onlyTokens ( satisfying: unexpectedTokenCondition)
136
+ let misplacedTokens = incorrectContainer. onlyPresentTokens ( satisfying: unexpectedTokenCondition)
137
137
else {
138
138
// If there are no unexpected nodes or the unexpected contain multiple tokens, don't emit a diagnostic.
139
139
return
@@ -180,7 +180,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
180
180
message: ( TokenSyntax ) -> Message
181
181
) {
182
182
guard let unexpected = unexpected,
183
- let misplacedToken = unexpected. onlyToken ( where: predicate)
183
+ let misplacedToken = unexpected. onlyPresentToken ( where: predicate)
184
184
else {
185
185
// If there is no unexpected node or the unexpected doesn't have the
186
186
// expected token, don't emit a diagnostic.
@@ -262,7 +262,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
262
262
}
263
263
if specifier. presence == . present {
264
264
for case . some( let unexpected) in unexpectedNodes {
265
- for duplicateSpecifier in unexpected. tokens ( satisfying: isOfSameKind) {
265
+ for duplicateSpecifier in unexpected. presentTokens ( satisfying: isOfSameKind) {
266
266
addDiagnostic (
267
267
duplicateSpecifier,
268
268
DuplicateEffectSpecifiers ( correctSpecifier: specifier, unexpectedSpecifier: duplicateSpecifier) ,
@@ -298,29 +298,36 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
298
298
suppressRemainingDiagnostics = true
299
299
return . skipChildren
300
300
}
301
- if let tryKeyword = node. onlyToken ( where: { $0. tokenKind == . keyword( . try ) } ) ,
301
+ if let tryKeyword = node. onlyPresentToken ( where: { $0. tokenKind == . keyword( . try ) } ) ,
302
302
let nextToken = tryKeyword. nextToken ( viewMode: . sourceAccurate) ,
303
303
nextToken. tokenKind. isLexerClassifiedKeyword,
304
304
!( node. parent? . is ( TypeEffectSpecifiersSyntax . self) ?? false )
305
305
{
306
306
addDiagnostic ( node, TryCannotBeUsed ( nextToken: nextToken) )
307
- } else if let semicolons = node. onlyTokens ( satisfying: { $0. tokenKind == . semicolon } ) {
307
+ } else if let semicolons = node. onlyPresentTokens ( satisfying: { $0. tokenKind == . semicolon } ) {
308
308
addDiagnostic (
309
309
node,
310
310
. unexpectedSemicolon,
311
311
fixIts: [
312
312
FixIt ( message: RemoveNodesFixIt ( semicolons) , changes: semicolons. map { FixIt . MultiNodeChange. makeMissing ( $0) } )
313
313
]
314
314
)
315
- } else if node. first? . as ( TokenSyntax . self) ? . tokenKind. isIdentifier == true ,
315
+ } else if let firstToken = node. first? . as ( TokenSyntax . self) ,
316
+ firstToken. tokenKind. isIdentifier == true ,
317
+ firstToken. presence == . present,
316
318
let previousToken = node. previousToken ( viewMode: . sourceAccurate) ,
317
319
previousToken. tokenKind. isIdentifier,
318
320
previousToken. parent? . is ( DeclSyntax . self) == true || previousToken. parent? . is ( IdentifierPatternSyntax . self) == true
319
321
{
320
322
// If multiple identifiers are used for a declaration name, offer to join them together.
321
323
let tokens =
322
324
node
323
- . prefix ( while: { $0. as ( TokenSyntax . self) ? . tokenKind. isIdentifier == true } )
325
+ . prefix ( while: {
326
+ guard let token = $0. as ( TokenSyntax . self) else {
327
+ return false
328
+ }
329
+ return token. tokenKind. isIdentifier == true && token. presence == . present
330
+ } )
324
331
. map ( { $0. as ( TokenSyntax . self) ! } )
325
332
let joined = previousToken. text + tokens. map ( \. text) . joined ( )
326
333
var fixIts : [ FixIt ] = [
@@ -449,7 +456,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
449
456
return . skipChildren
450
457
}
451
458
if let unexpected = node. unexpectedBetweenPlatformAndVersion,
452
- unexpected. onlyToken ( where: { $0. tokenKind == . binaryOperator( " >= " ) } ) != nil
459
+ unexpected. onlyPresentToken ( where: { $0. tokenKind == . binaryOperator( " >= " ) } ) != nil
453
460
{
454
461
addDiagnostic (
455
462
unexpected,
@@ -552,7 +559,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
552
559
}
553
560
554
561
if let unexpected = node. unexpectedBetweenBodyAndTrailingComma,
555
- let token = unexpected. tokens ( satisfying: { $0. tokenKind == . binaryOperator( " && " ) } ) . first,
562
+ let token = unexpected. presentTokens ( satisfying: { $0. tokenKind == . binaryOperator( " && " ) } ) . first,
556
563
let trailingComma = node. trailingComma,
557
564
trailingComma. presence == . missing,
558
565
let previous = node. unexpectedBetweenBodyAndTrailingComma? . previousToken ( viewMode: . sourceAccurate)
@@ -583,7 +590,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
583
590
return . skipChildren
584
591
}
585
592
if let unexpected = node. unexpectedBetweenDeinitKeywordAndBody,
586
- let name = unexpected. filter ( { $0. as ( TokenSyntax . self ) ? . tokenKind. isIdentifier == true } ) . only? . as ( TokenSyntax . self)
593
+ let name = unexpected. presentTokens ( satisfying : { $0. tokenKind. isIdentifier == true } ) . only? . as ( TokenSyntax . self)
587
594
{
588
595
addDiagnostic (
589
596
name,
@@ -617,7 +624,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
617
624
// Detect C-style for loops based on two semicolons which could not be parsed between the 'for' keyword and the '{'
618
625
// This is mostly a proof-of-concept implementation to produce more complex diagnostics.
619
626
if let unexpectedCondition = node. body. unexpectedBeforeLeftBrace,
620
- unexpectedCondition. tokens ( withKind: . semicolon) . count == 2
627
+ unexpectedCondition. presentTokens ( withKind: . semicolon) . count == 2
621
628
{
622
629
// FIXME: This is aweful. We should have a way to either get all children between two cursors in a syntax node or highlight a range from one node to another.
623
630
addDiagnostic (
@@ -690,7 +697,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
690
697
message: { _ in . typeParameterPackEllipsis }
691
698
)
692
699
} else if let unexpected = node. unexpectedBetweenNameAndColon,
693
- let unexpectedEllipsis = unexpected. onlyToken ( where: { $0. tokenKind == . ellipsis } ) ,
700
+ let unexpectedEllipsis = unexpected. onlyPresentToken ( where: { $0. tokenKind == . ellipsis } ) ,
694
701
let each = node. each
695
702
{
696
703
addDiagnostic (
@@ -929,7 +936,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
929
936
if shouldSkip ( node) {
930
937
return . skipChildren
931
938
}
932
- if let token = node. unexpectedBetweenModuleLabelAndColon? . onlyToken ( where: { $0. tokenKind. isIdentifier } ) ,
939
+ if let token = node. unexpectedBetweenModuleLabelAndColon? . onlyPresentToken ( where: { $0. tokenKind. isIdentifier } ) ,
933
940
node. moduleLabel. presence == . missing
934
941
{
935
942
addDiagnostic (
@@ -976,9 +983,9 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
976
983
return
977
984
}
978
985
let message : DiagnosticMessage ?
979
- if let identifier = unexpected. onlyToken ( where: { $0. tokenKind. isIdentifier } ) {
986
+ if let identifier = unexpected. onlyPresentToken ( where: { $0. tokenKind. isIdentifier } ) {
980
987
message = IdentifierNotAllowedInOperatorName ( identifier: identifier)
981
- } else if let tokens = unexpected. onlyTokens ( satisfying: { _ in true } ) {
988
+ } else if let tokens = unexpected. onlyPresentTokens ( satisfying: { _ in true } ) {
982
989
message = TokensNotAllowedInOperatorName ( tokens: tokens)
983
990
} else {
984
991
message = nil
@@ -1068,7 +1075,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
1068
1075
if shouldSkip ( node) {
1069
1076
return . skipChildren
1070
1077
}
1071
- if let singleQuote = node. unexpectedBetweenOpenDelimiterAndOpenQuote? . onlyToken ( where: { $0. tokenKind == . singleQuote } ) {
1078
+ if let singleQuote = node. unexpectedBetweenOpenDelimiterAndOpenQuote? . onlyPresentToken ( where: { $0. tokenKind == . singleQuote } ) {
1072
1079
let fixIt = FixIt (
1073
1080
message: ReplaceTokensFixIt ( replaceTokens: [ singleQuote] , replacement: node. openQuote) ,
1074
1081
changes: [
@@ -1094,7 +1101,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
1094
1101
addDiagnostic ( diagnostic, handledNodes: handledNodes)
1095
1102
}
1096
1103
if case . stringSegment( let segment) = node. segments. last {
1097
- if let invalidContent = segment. unexpectedBeforeContent? . onlyToken ( where: { $0. trailingTrivia. contains ( where: { $0. isBackslash } ) } ) {
1104
+ if let invalidContent = segment. unexpectedBeforeContent? . onlyPresentToken ( where: { $0. trailingTrivia. contains ( where: { $0. isBackslash } ) } ) {
1098
1105
let fixIt = FixIt (
1099
1106
message: . removeBackslash,
1100
1107
changes: [
@@ -1119,7 +1126,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
1119
1126
return . skipChildren
1120
1127
}
1121
1128
if let unexpected = node. unexpectedBetweenSubscriptKeywordAndGenericParameterClause,
1122
- let nameTokens = unexpected. onlyTokens ( satisfying: { !$0. tokenKind. isLexerClassifiedKeyword } )
1129
+ let nameTokens = unexpected. onlyPresentTokens ( satisfying: { !$0. tokenKind. isLexerClassifiedKeyword } )
1123
1130
{
1124
1131
addDiagnostic (
1125
1132
unexpected,
@@ -1131,7 +1138,7 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
1131
1138
)
1132
1139
}
1133
1140
if let unexpected = node. indices. unexpectedBeforeLeftParen,
1134
- let nameTokens = unexpected. onlyTokens ( satisfying: { !$0. tokenKind. isLexerClassifiedKeyword } )
1141
+ let nameTokens = unexpected. onlyPresentTokens ( satisfying: { !$0. tokenKind. isLexerClassifiedKeyword } )
1135
1142
{
1136
1143
addDiagnostic (
1137
1144
unexpected,
@@ -1255,7 +1262,8 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
1255
1262
return . skipChildren
1256
1263
}
1257
1264
1258
- if let token = node. unexpectedBetweenMessageLabelAndColon? . onlyToken ( where: { $0. tokenKind. isIdentifier } ) ,
1265
+ if let token = node. unexpectedBetweenMessageLabelAndColon? . onlyPresentToken ( where: { $0. tokenKind. isIdentifier } ) ,
1266
+ token. presence == . present,
1259
1267
node. messageLabel. presence == . missing
1260
1268
{
1261
1269
addDiagnostic (
0 commit comments