@@ -56,7 +56,6 @@ SyntaxModelContext::SyntaxModelContext(SourceFile &SrcFile)
56
56
std::vector<SyntaxNode> Nodes;
57
57
SourceLoc AttrLoc;
58
58
SourceLoc UnaryMinusLoc;
59
- auto LiteralStartLoc = Optional<SourceLoc>();
60
59
for (unsigned I = 0 , E = Tokens.size (); I != E; ++I) {
61
60
auto &Tok = Tokens[I];
62
61
// Ignore empty string literals between interpolations, e.g. "\(1)\(2)"
@@ -85,16 +84,6 @@ SyntaxModelContext::SyntaxModelContext(SourceFile &SrcFile)
85
84
Loc = Tok.getLoc ();
86
85
Length = Tok.getLength ();
87
86
88
- if (LiteralStartLoc.hasValue () && Length.hasValue ()) {
89
- if (Tok.getKind () != tok::r_paren)
90
- continue ;
91
- Kind = SyntaxNodeKind::ObjectLiteral;
92
- Nodes.emplace_back (Kind, CharSourceRange (SM, LiteralStartLoc.getValue (),
93
- Tok.getRange ().getEnd ()));
94
- LiteralStartLoc = Optional<SourceLoc>();
95
- continue ;
96
- }
97
-
98
87
switch (Tok.getKind ()) {
99
88
#define KEYWORD (X ) case tok::kw_##X:
100
89
#include " swift/Syntax/TokenKinds.def"
@@ -105,8 +94,8 @@ SyntaxModelContext::SyntaxModelContext(SourceFile &SrcFile)
105
94
106
95
#define POUND_OBJECT_LITERAL (Name, Desc, Proto ) case tok::pound_##Name:
107
96
#include " swift/Syntax/TokenKinds.def"
108
- LiteralStartLoc = Loc ;
109
- continue ;
97
+ Kind = SyntaxNodeKind::Keyword ;
98
+ break ;
110
99
111
100
#define POUND_COND_DIRECTIVE_KEYWORD (Name ) case tok::pound_##Name:
112
101
#include " swift/Syntax/TokenKinds.def"
@@ -260,6 +249,9 @@ class ModelASTWalker : public ASTWalker {
260
249
Optional<SyntaxNode> parseFieldNode (StringRef Text, StringRef OrigText,
261
250
SourceLoc OrigLoc);
262
251
llvm::DenseSet<ASTNode> VisitedNodesInsideIfConfig;
252
+ // / When non-zero, we should avoid passing tokens as syntax nodes since a parent of several tokens
253
+ // / is considered as one, e.g. object literal expression.
254
+ uint8_t AvoidPassingSyntaxToken = 0 ;
263
255
264
256
public:
265
257
SyntaxModelWalker &Walker;
@@ -494,8 +486,9 @@ std::pair<bool, Expr *> ModelASTWalker::walkToExprPre(Expr *E) {
494
486
SN.NameRange = CharSourceRange (SM, NRStart, NREnd);
495
487
SN.BodyRange =
496
488
innerCharSourceRangeFromSourceRange (SM, ObjectE->getSourceRange ());
489
+ // Consider the object literal as a single syntax token for highlighting.
490
+ passNonTokenNode ({SyntaxNodeKind::ObjectLiteral, SN.Range });
497
491
pushStructureNode (SN, E);
498
-
499
492
} else if (auto *ArrayE = dyn_cast<ArrayExpr>(E)) {
500
493
SyntaxStructureNode SN;
501
494
SN.Kind = SyntaxStructureKind::ArrayExpression;
@@ -1152,8 +1145,10 @@ bool ModelASTWalker::passTokenNodesUntil(SourceLoc Loc,
1152
1145
}
1153
1146
break ;
1154
1147
}
1155
- if (!passNode (TokenNodes[I]))
1156
- return false ;
1148
+ if (!AvoidPassingSyntaxToken) {
1149
+ if (!passNode (TokenNodes[I]))
1150
+ return false ;
1151
+ }
1157
1152
}
1158
1153
1159
1154
TokenNodes = TokenNodes.slice (I);
@@ -1200,9 +1195,15 @@ bool ModelASTWalker::passNode(const SyntaxNode &Node) {
1200
1195
return Walker.walkToNodePost (Node);
1201
1196
}
1202
1197
1198
+ static bool shouldAvoidPssingSyntaxToken (const SyntaxStructureNode &Node) {
1199
+ return Node.Kind == SyntaxStructureKind::ObjectLiteralExpression;
1200
+ }
1201
+
1203
1202
bool ModelASTWalker::pushStructureNode (const SyntaxStructureNode &Node,
1204
1203
const ASTNodeType& ASTNode) {
1205
1204
SubStructureStack.emplace_back (Node, ASTNode);
1205
+ if (shouldAvoidPssingSyntaxToken (Node))
1206
+ AvoidPassingSyntaxToken ++;
1206
1207
1207
1208
if (!passTokenNodesUntil (Node.Range .getStart (), ExcludeNodeAtLocation))
1208
1209
return false ;
@@ -1215,6 +1216,12 @@ bool ModelASTWalker::pushStructureNode(const SyntaxStructureNode &Node,
1215
1216
bool ModelASTWalker::popStructureNode () {
1216
1217
assert (!SubStructureStack.empty ());
1217
1218
SyntaxStructureNode Node = SubStructureStack.back ().StructureNode ;
1219
+ SWIFT_DEFER {
1220
+ if (shouldAvoidPssingSyntaxToken (Node)) {
1221
+ assert (AvoidPassingSyntaxToken);
1222
+ AvoidPassingSyntaxToken --;
1223
+ }
1224
+ };
1218
1225
SubStructureStack.pop_back ();
1219
1226
1220
1227
// VarDecls are popped before we see their TypeRepr, so if we pass the token
0 commit comments