@@ -1848,14 +1848,14 @@ static bool canDeclContextHandleAsync(const DeclContext *DC) {
1848
1848
// / #false#
1849
1849
// / }
1850
1850
// / }
1851
- static bool isCodeCompletionAtTopLevel (DeclContext *DC) {
1851
+ static bool isCodeCompletionAtTopLevel (const DeclContext *DC) {
1852
1852
if (DC->isModuleScopeContext ())
1853
1853
return true ;
1854
1854
1855
1855
// CC token at top-level is parsed as an expression. If the only element
1856
1856
// body of the TopLevelCodeDecl is a CodeCompletionExpr without a base
1857
1857
// expression, the user might be writing a top-level declaration.
1858
- if (TopLevelCodeDecl *TLCD = dyn_cast<TopLevelCodeDecl>(DC)) {
1858
+ if (const TopLevelCodeDecl *TLCD = dyn_cast<const TopLevelCodeDecl>(DC)) {
1859
1859
auto body = TLCD->getBody ();
1860
1860
if (!body || body->empty ())
1861
1861
return true ;
@@ -1897,6 +1897,13 @@ static bool isCompletionDeclContextLocalContext(DeclContext *DC) {
1897
1897
return true ;
1898
1898
}
1899
1899
1900
+ // / Return \c true if the completion happens at top-level of a library file.
1901
+ static bool isCodeCompletionAtTopLevelOfLibraryFile (const DeclContext *DC) {
1902
+ if (DC->getParentSourceFile ()->isScriptMode ())
1903
+ return false ;
1904
+ return isCodeCompletionAtTopLevel (DC);
1905
+ }
1906
+
1900
1907
// / Build completions by doing visible decl lookup from a context.
1901
1908
class CompletionLookup final : public swift::VisibleDeclConsumer {
1902
1909
CodeCompletionResultSink &Sink;
@@ -3606,11 +3613,13 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3606
3613
3607
3614
void addKeyword (StringRef Name, StringRef TypeAnnotation,
3608
3615
CodeCompletionKeywordKind KeyKind
3609
- = CodeCompletionKeywordKind::None) {
3616
+ = CodeCompletionKeywordKind::None,
3617
+ CodeCompletionFlair flair = {}) {
3610
3618
CodeCompletionResultBuilder Builder (
3611
3619
Sink,
3612
3620
CodeCompletionResult::ResultKind::Keyword,
3613
3621
SemanticContextKind::None, expectedTypeContext);
3622
+ Builder.addFlair (flair);
3614
3623
addLeadingDot (Builder);
3615
3624
Builder.addKeyword (Name);
3616
3625
Builder.setKeywordKind (KeyKind);
@@ -4360,6 +4369,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
4360
4369
4361
4370
// / Add '#file', '#line', et at.
4362
4371
void addPoundLiteralCompletions (bool needPound) {
4372
+ CodeCompletionFlair flair;
4373
+ if (isCodeCompletionAtTopLevelOfLibraryFile (CurrDeclContext))
4374
+ flair |= CodeCompletionFlairBit::ExpressionAtNonScriptOrMainFileScope;
4375
+
4363
4376
auto addFromProto = [&](MagicIdentifierLiteralExpr::Kind magicKind,
4364
4377
Optional<CodeCompletionLiteralKind> literalKind) {
4365
4378
CodeCompletionKeywordKind kwKind;
@@ -4384,13 +4397,14 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
4384
4397
4385
4398
if (!literalKind) {
4386
4399
// Pointer type
4387
- addKeyword (name, " UnsafeRawPointer" , kwKind);
4400
+ addKeyword (name, " UnsafeRawPointer" , kwKind, flair );
4388
4401
return ;
4389
4402
}
4390
4403
4391
4404
CodeCompletionResultBuilder builder (
4392
4405
Sink, CodeCompletionResult::ResultKind::Keyword,
4393
4406
SemanticContextKind::None, {});
4407
+ builder.addFlair (flair);
4394
4408
builder.setLiteralKind (literalKind.getValue ());
4395
4409
builder.setKeywordKind (kwKind);
4396
4410
builder.addBaseName (name);
@@ -4412,6 +4426,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
4412
4426
void addValueLiteralCompletions () {
4413
4427
auto &context = CurrDeclContext->getASTContext ();
4414
4428
4429
+ CodeCompletionFlair flair;
4430
+ if (isCodeCompletionAtTopLevelOfLibraryFile (CurrDeclContext))
4431
+ flair |= CodeCompletionFlairBit::ExpressionAtNonScriptOrMainFileScope;
4432
+
4415
4433
auto addFromProto = [&](
4416
4434
CodeCompletionLiteralKind kind,
4417
4435
llvm::function_ref<void (CodeCompletionResultBuilder &)> consumer,
@@ -4420,6 +4438,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
4420
4438
CodeCompletionResultBuilder builder (Sink, CodeCompletionResult::Literal,
4421
4439
SemanticContextKind::None, {});
4422
4440
builder.setLiteralKind (kind);
4441
+ builder.addFlair (flair);
4423
4442
4424
4443
consumer (builder);
4425
4444
addTypeRelationFromProtocol (builder, kind);
@@ -4492,6 +4511,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
4492
4511
CodeCompletionResultBuilder builder (Sink, CodeCompletionResult::Literal,
4493
4512
SemanticContextKind::None, {});
4494
4513
builder.setLiteralKind (LK::Tuple);
4514
+ builder.addFlair (flair);
4495
4515
4496
4516
builder.addLeftParen ();
4497
4517
builder.addSimpleNamedParameter (" values" );
@@ -6038,8 +6058,7 @@ static void addDeclKeywords(CodeCompletionResultSink &Sink, DeclContext *DC,
6038
6058
6039
6059
auto getFlair = [&](CodeCompletionKeywordKind Kind,
6040
6060
Optional<DeclAttrKind> DAK) -> CodeCompletionFlair {
6041
- if (isCodeCompletionAtTopLevel (DC) &&
6042
- !DC->getParentSourceFile ()->isScriptMode ()) {
6061
+ if (isCodeCompletionAtTopLevelOfLibraryFile (DC)) {
6043
6062
// Type decls are common in library file top-level.
6044
6063
if (isTypeDeclIntroducer (Kind, DAK))
6045
6064
return CodeCompletionFlairBit::CommonKeywordAtCurrentPosition;
@@ -6147,11 +6166,20 @@ static void addDeclKeywords(CodeCompletionResultSink &Sink, DeclContext *DC,
6147
6166
#undef CONTEXTUAL_CASE
6148
6167
}
6149
6168
6150
- static void addStmtKeywords (CodeCompletionResultSink &Sink, bool MaybeFuncBody) {
6169
+ static void addStmtKeywords (CodeCompletionResultSink &Sink, DeclContext *DC,
6170
+ bool MaybeFuncBody) {
6171
+ CodeCompletionFlair flair;
6172
+ // Starting a statement at top-level in non-script files is invalid.
6173
+ if (isCodeCompletionAtTopLevelOfLibraryFile (DC)) {
6174
+ flair |= CodeCompletionFlairBit::ExpressionAtNonScriptOrMainFileScope;
6175
+ }
6176
+
6151
6177
auto AddStmtKeyword = [&](StringRef Name, CodeCompletionKeywordKind Kind) {
6152
6178
if (!MaybeFuncBody && Kind == CodeCompletionKeywordKind::kw_return)
6153
6179
return ;
6154
- addKeyword (Sink, Name, Kind);
6180
+ addKeyword (Sink, Name, Kind, " " ,
6181
+ CodeCompletionResult::ExpectedTypeRelation::NotApplicable,
6182
+ flair);
6155
6183
};
6156
6184
#define STMT_KEYWORD (kw ) AddStmtKeyword(#kw, CodeCompletionKeywordKind::kw_##kw);
6157
6185
#include " swift/Syntax/TokenKinds.def"
@@ -6177,12 +6205,22 @@ static void addObserverKeywords(CodeCompletionResultSink &Sink) {
6177
6205
addKeyword (Sink, " didSet" , CodeCompletionKeywordKind::None);
6178
6206
}
6179
6207
6180
- static void addExprKeywords (CodeCompletionResultSink &Sink) {
6208
+ static void addExprKeywords (CodeCompletionResultSink &Sink, DeclContext *DC) {
6209
+ // Expression is invalid at top-level of non-script files.
6210
+ CodeCompletionFlair flair;
6211
+ if (isCodeCompletionAtTopLevelOfLibraryFile (DC)) {
6212
+ flair |= CodeCompletionFlairBit::ExpressionAtNonScriptOrMainFileScope;
6213
+ }
6214
+
6181
6215
// Expr keywords.
6182
- addKeyword (Sink, " try" , CodeCompletionKeywordKind::kw_try);
6183
- addKeyword (Sink, " try!" , CodeCompletionKeywordKind::kw_try);
6184
- addKeyword (Sink, " try?" , CodeCompletionKeywordKind::kw_try);
6185
- addKeyword (Sink, " await" , CodeCompletionKeywordKind::None);
6216
+ addKeyword (Sink, " try" , CodeCompletionKeywordKind::kw_try, " " ,
6217
+ CodeCompletionResult::ExpectedTypeRelation::NotApplicable, flair);
6218
+ addKeyword (Sink, " try!" , CodeCompletionKeywordKind::kw_try, " " ,
6219
+ CodeCompletionResult::ExpectedTypeRelation::NotApplicable, flair);
6220
+ addKeyword (Sink, " try?" , CodeCompletionKeywordKind::kw_try, " " ,
6221
+ CodeCompletionResult::ExpectedTypeRelation::NotApplicable, flair);
6222
+ addKeyword (Sink, " await" , CodeCompletionKeywordKind::None, " " ,
6223
+ CodeCompletionResult::ExpectedTypeRelation::NotApplicable, flair);
6186
6224
}
6187
6225
6188
6226
static void addOpaqueTypeKeyword (CodeCompletionResultSink &Sink) {
@@ -6250,15 +6288,15 @@ void CodeCompletionCallbacksImpl::addKeywords(CodeCompletionResultSink &Sink,
6250
6288
case CompletionKind::StmtOrExpr:
6251
6289
addDeclKeywords (Sink, CurDeclContext,
6252
6290
Context.LangOpts .EnableExperimentalConcurrency );
6253
- addStmtKeywords (Sink, MaybeFuncBody);
6291
+ addStmtKeywords (Sink, CurDeclContext, MaybeFuncBody);
6254
6292
LLVM_FALLTHROUGH;
6255
6293
case CompletionKind::ReturnStmtExpr:
6256
6294
case CompletionKind::YieldStmtExpr:
6257
6295
case CompletionKind::PostfixExprBeginning:
6258
6296
case CompletionKind::ForEachSequence:
6259
6297
addSuperKeyword (Sink);
6260
6298
addLetVarKeywords (Sink);
6261
- addExprKeywords (Sink);
6299
+ addExprKeywords (Sink, CurDeclContext );
6262
6300
addAnyTypeKeyword (Sink, CurDeclContext->getASTContext ().TheAnyType );
6263
6301
break ;
6264
6302
@@ -6461,19 +6499,6 @@ static void addConditionalCompilationFlags(ASTContext &Ctx,
6461
6499
6462
6500
static void postProcessResults (ArrayRef<CodeCompletionResult *> results,
6463
6501
CompletionKind Kind, DeclContext *DC) {
6464
- auto isExprKeyword = [](const CodeCompletionResult *result) {
6465
- if (result->getKind () != CodeCompletionResult::ResultKind::Keyword)
6466
- return false ;
6467
- switch (result->getKeywordKind ()) {
6468
- #define POUND_DIRECTIVE_KEYWORD (kw )
6469
- #define POUND_CONFIG (kw )
6470
- #define POUND_KEYWORD (kw ) case CodeCompletionKeywordKind::pound_##kw:
6471
- #include " swift/Syntax/TokenKinds.def"
6472
- return true ;
6473
- default :
6474
- return false ;
6475
- }
6476
- };
6477
6502
for (CodeCompletionResult *result : results) {
6478
6503
auto flair = result->getFlair ();
6479
6504
@@ -6491,11 +6516,8 @@ static void postProcessResults(ArrayRef<CodeCompletionResult *> results,
6491
6516
6492
6517
// Starting a statement at top-level in non-script files is invalid.
6493
6518
if (Kind == CompletionKind::StmtOrExpr &&
6494
- isCodeCompletionAtTopLevel (DC) &&
6495
- !DC->getParentSourceFile ()->isScriptMode () &&
6496
- (result->getKind () == CodeCompletionResult::ResultKind::Declaration ||
6497
- result->getKind () == CodeCompletionResult::ResultKind::Literal ||
6498
- isExprKeyword (result))) {
6519
+ result->getKind () == CodeCompletionResult::ResultKind::Declaration &&
6520
+ isCodeCompletionAtTopLevelOfLibraryFile (DC)) {
6499
6521
flair |= CodeCompletionFlairBit::ExpressionAtNonScriptOrMainFileScope;
6500
6522
}
6501
6523
result->setFlair (flair);
@@ -6986,7 +7008,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
6986
7008
6987
7009
// Add any keywords that can be used in an argument expr position.
6988
7010
addSuperKeyword (CompletionContext.getResultSink ());
6989
- addExprKeywords (CompletionContext.getResultSink ());
7011
+ addExprKeywords (CompletionContext.getResultSink (), CurDeclContext );
6990
7012
6991
7013
DoPostfixExprBeginning ();
6992
7014
}
@@ -7132,7 +7154,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
7132
7154
7133
7155
// Add any keywords that can be used in an argument expr position.
7134
7156
addSuperKeyword (CompletionContext.getResultSink ());
7135
- addExprKeywords (CompletionContext.getResultSink ());
7157
+ addExprKeywords (CompletionContext.getResultSink (), CurDeclContext );
7136
7158
7137
7159
DoPostfixExprBeginning ();
7138
7160
}
@@ -7186,10 +7208,10 @@ void CodeCompletionCallbacksImpl::doneParsing() {
7186
7208
// Global completion (CompletionKind::PostfixExprBeginning).
7187
7209
addDeclKeywords (Sink, CurDeclContext,
7188
7210
Context.LangOpts .EnableExperimentalConcurrency );
7189
- addStmtKeywords (Sink, MaybeFuncBody);
7211
+ addStmtKeywords (Sink, CurDeclContext, MaybeFuncBody);
7190
7212
addSuperKeyword (Sink);
7191
7213
addLetVarKeywords (Sink);
7192
- addExprKeywords (Sink);
7214
+ addExprKeywords (Sink, CurDeclContext );
7193
7215
addAnyTypeKeyword (Sink, Context.TheAnyType );
7194
7216
DoPostfixExprBeginning ();
7195
7217
}
0 commit comments