@@ -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;
@@ -3611,11 +3618,13 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3611
3618
3612
3619
void addKeyword (StringRef Name, StringRef TypeAnnotation,
3613
3620
CodeCompletionKeywordKind KeyKind
3614
- = CodeCompletionKeywordKind::None) {
3621
+ = CodeCompletionKeywordKind::None,
3622
+ CodeCompletionFlair flair = {}) {
3615
3623
CodeCompletionResultBuilder Builder (
3616
3624
Sink,
3617
3625
CodeCompletionResult::ResultKind::Keyword,
3618
3626
SemanticContextKind::None, expectedTypeContext);
3627
+ Builder.addFlair (flair);
3619
3628
addLeadingDot (Builder);
3620
3629
Builder.addKeyword (Name);
3621
3630
Builder.setKeywordKind (KeyKind);
@@ -4365,6 +4374,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
4365
4374
4366
4375
// / Add '#file', '#line', et at.
4367
4376
void addPoundLiteralCompletions (bool needPound) {
4377
+ CodeCompletionFlair flair;
4378
+ if (isCodeCompletionAtTopLevelOfLibraryFile (CurrDeclContext))
4379
+ flair |= CodeCompletionFlairBit::ExpressionAtNonScriptOrMainFileScope;
4380
+
4368
4381
auto addFromProto = [&](MagicIdentifierLiteralExpr::Kind magicKind,
4369
4382
Optional<CodeCompletionLiteralKind> literalKind) {
4370
4383
CodeCompletionKeywordKind kwKind;
@@ -4389,13 +4402,14 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
4389
4402
4390
4403
if (!literalKind) {
4391
4404
// Pointer type
4392
- addKeyword (name, " UnsafeRawPointer" , kwKind);
4405
+ addKeyword (name, " UnsafeRawPointer" , kwKind, flair );
4393
4406
return ;
4394
4407
}
4395
4408
4396
4409
CodeCompletionResultBuilder builder (
4397
4410
Sink, CodeCompletionResult::ResultKind::Keyword,
4398
4411
SemanticContextKind::None, {});
4412
+ builder.addFlair (flair);
4399
4413
builder.setLiteralKind (literalKind.getValue ());
4400
4414
builder.setKeywordKind (kwKind);
4401
4415
builder.addBaseName (name);
@@ -4417,6 +4431,10 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
4417
4431
void addValueLiteralCompletions () {
4418
4432
auto &context = CurrDeclContext->getASTContext ();
4419
4433
4434
+ CodeCompletionFlair flair;
4435
+ if (isCodeCompletionAtTopLevelOfLibraryFile (CurrDeclContext))
4436
+ flair |= CodeCompletionFlairBit::ExpressionAtNonScriptOrMainFileScope;
4437
+
4420
4438
auto addFromProto = [&](
4421
4439
CodeCompletionLiteralKind kind,
4422
4440
llvm::function_ref<void (CodeCompletionResultBuilder &)> consumer,
@@ -4425,6 +4443,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
4425
4443
CodeCompletionResultBuilder builder (Sink, CodeCompletionResult::Literal,
4426
4444
SemanticContextKind::None, {});
4427
4445
builder.setLiteralKind (kind);
4446
+ builder.addFlair (flair);
4428
4447
4429
4448
consumer (builder);
4430
4449
addTypeRelationFromProtocol (builder, kind);
@@ -4497,6 +4516,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
4497
4516
CodeCompletionResultBuilder builder (Sink, CodeCompletionResult::Literal,
4498
4517
SemanticContextKind::None, {});
4499
4518
builder.setLiteralKind (LK::Tuple);
4519
+ builder.addFlair (flair);
4500
4520
4501
4521
builder.addLeftParen ();
4502
4522
builder.addSimpleNamedParameter (" values" );
@@ -6051,8 +6071,7 @@ static void addDeclKeywords(CodeCompletionResultSink &Sink, DeclContext *DC,
6051
6071
6052
6072
auto getFlair = [&](CodeCompletionKeywordKind Kind,
6053
6073
Optional<DeclAttrKind> DAK) -> CodeCompletionFlair {
6054
- if (isCodeCompletionAtTopLevel (DC) &&
6055
- !DC->getParentSourceFile ()->isScriptMode ()) {
6074
+ if (isCodeCompletionAtTopLevelOfLibraryFile (DC)) {
6056
6075
// Type decls are common in library file top-level.
6057
6076
if (isTypeDeclIntroducer (Kind, DAK))
6058
6077
return CodeCompletionFlairBit::CommonKeywordAtCurrentPosition;
@@ -6165,11 +6184,20 @@ static void addDeclKeywords(CodeCompletionResultSink &Sink, DeclContext *DC,
6165
6184
#undef CONTEXTUAL_CASE
6166
6185
}
6167
6186
6168
- static void addStmtKeywords (CodeCompletionResultSink &Sink, bool MaybeFuncBody) {
6187
+ static void addStmtKeywords (CodeCompletionResultSink &Sink, DeclContext *DC,
6188
+ bool MaybeFuncBody) {
6189
+ CodeCompletionFlair flair;
6190
+ // Starting a statement at top-level in non-script files is invalid.
6191
+ if (isCodeCompletionAtTopLevelOfLibraryFile (DC)) {
6192
+ flair |= CodeCompletionFlairBit::ExpressionAtNonScriptOrMainFileScope;
6193
+ }
6194
+
6169
6195
auto AddStmtKeyword = [&](StringRef Name, CodeCompletionKeywordKind Kind) {
6170
6196
if (!MaybeFuncBody && Kind == CodeCompletionKeywordKind::kw_return)
6171
6197
return ;
6172
- addKeyword (Sink, Name, Kind);
6198
+ addKeyword (Sink, Name, Kind, " " ,
6199
+ CodeCompletionResult::ExpectedTypeRelation::NotApplicable,
6200
+ flair);
6173
6201
};
6174
6202
#define STMT_KEYWORD (kw ) AddStmtKeyword(#kw, CodeCompletionKeywordKind::kw_##kw);
6175
6203
#include " swift/Syntax/TokenKinds.def"
@@ -6195,12 +6223,22 @@ static void addObserverKeywords(CodeCompletionResultSink &Sink) {
6195
6223
addKeyword (Sink, " didSet" , CodeCompletionKeywordKind::None);
6196
6224
}
6197
6225
6198
- static void addExprKeywords (CodeCompletionResultSink &Sink) {
6226
+ static void addExprKeywords (CodeCompletionResultSink &Sink, DeclContext *DC) {
6227
+ // Expression is invalid at top-level of non-script files.
6228
+ CodeCompletionFlair flair;
6229
+ if (isCodeCompletionAtTopLevelOfLibraryFile (DC)) {
6230
+ flair |= CodeCompletionFlairBit::ExpressionAtNonScriptOrMainFileScope;
6231
+ }
6232
+
6199
6233
// Expr keywords.
6200
- addKeyword (Sink, " try" , CodeCompletionKeywordKind::kw_try);
6201
- addKeyword (Sink, " try!" , CodeCompletionKeywordKind::kw_try);
6202
- addKeyword (Sink, " try?" , CodeCompletionKeywordKind::kw_try);
6203
- addKeyword (Sink, " await" , CodeCompletionKeywordKind::None);
6234
+ addKeyword (Sink, " try" , CodeCompletionKeywordKind::kw_try, " " ,
6235
+ CodeCompletionResult::ExpectedTypeRelation::NotApplicable, flair);
6236
+ addKeyword (Sink, " try!" , CodeCompletionKeywordKind::kw_try, " " ,
6237
+ CodeCompletionResult::ExpectedTypeRelation::NotApplicable, flair);
6238
+ addKeyword (Sink, " try?" , CodeCompletionKeywordKind::kw_try, " " ,
6239
+ CodeCompletionResult::ExpectedTypeRelation::NotApplicable, flair);
6240
+ addKeyword (Sink, " await" , CodeCompletionKeywordKind::None, " " ,
6241
+ CodeCompletionResult::ExpectedTypeRelation::NotApplicable, flair);
6204
6242
}
6205
6243
6206
6244
static void addOpaqueTypeKeyword (CodeCompletionResultSink &Sink) {
@@ -6269,15 +6307,15 @@ void CodeCompletionCallbacksImpl::addKeywords(CodeCompletionResultSink &Sink,
6269
6307
addDeclKeywords (Sink, CurDeclContext,
6270
6308
Context.LangOpts .EnableExperimentalConcurrency ,
6271
6309
Context.LangOpts .EnableExperimentalDistributed );
6272
- addStmtKeywords (Sink, MaybeFuncBody);
6310
+ addStmtKeywords (Sink, CurDeclContext, MaybeFuncBody);
6273
6311
LLVM_FALLTHROUGH;
6274
6312
case CompletionKind::ReturnStmtExpr:
6275
6313
case CompletionKind::YieldStmtExpr:
6276
6314
case CompletionKind::PostfixExprBeginning:
6277
6315
case CompletionKind::ForEachSequence:
6278
6316
addSuperKeyword (Sink);
6279
6317
addLetVarKeywords (Sink);
6280
- addExprKeywords (Sink);
6318
+ addExprKeywords (Sink, CurDeclContext );
6281
6319
addAnyTypeKeyword (Sink, CurDeclContext->getASTContext ().TheAnyType );
6282
6320
break ;
6283
6321
@@ -6481,19 +6519,6 @@ static void addConditionalCompilationFlags(ASTContext &Ctx,
6481
6519
6482
6520
static void postProcessResults (ArrayRef<CodeCompletionResult *> results,
6483
6521
CompletionKind Kind, DeclContext *DC) {
6484
- auto isExprKeyword = [](const CodeCompletionResult *result) {
6485
- if (result->getKind () != CodeCompletionResult::ResultKind::Keyword)
6486
- return false ;
6487
- switch (result->getKeywordKind ()) {
6488
- #define POUND_DIRECTIVE_KEYWORD (kw )
6489
- #define POUND_CONFIG (kw )
6490
- #define POUND_KEYWORD (kw ) case CodeCompletionKeywordKind::pound_##kw:
6491
- #include " swift/Syntax/TokenKinds.def"
6492
- return true ;
6493
- default :
6494
- return false ;
6495
- }
6496
- };
6497
6522
for (CodeCompletionResult *result : results) {
6498
6523
auto flair = result->getFlair ();
6499
6524
@@ -6511,11 +6536,8 @@ static void postProcessResults(ArrayRef<CodeCompletionResult *> results,
6511
6536
6512
6537
// Starting a statement at top-level in non-script files is invalid.
6513
6538
if (Kind == CompletionKind::StmtOrExpr &&
6514
- isCodeCompletionAtTopLevel (DC) &&
6515
- !DC->getParentSourceFile ()->isScriptMode () &&
6516
- (result->getKind () == CodeCompletionResult::ResultKind::Declaration ||
6517
- result->getKind () == CodeCompletionResult::ResultKind::Literal ||
6518
- isExprKeyword (result))) {
6539
+ result->getKind () == CodeCompletionResult::ResultKind::Declaration &&
6540
+ isCodeCompletionAtTopLevelOfLibraryFile (DC)) {
6519
6541
flair |= CodeCompletionFlairBit::ExpressionAtNonScriptOrMainFileScope;
6520
6542
}
6521
6543
result->setFlair (flair);
@@ -7006,7 +7028,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
7006
7028
7007
7029
// Add any keywords that can be used in an argument expr position.
7008
7030
addSuperKeyword (CompletionContext.getResultSink ());
7009
- addExprKeywords (CompletionContext.getResultSink ());
7031
+ addExprKeywords (CompletionContext.getResultSink (), CurDeclContext );
7010
7032
7011
7033
DoPostfixExprBeginning ();
7012
7034
}
@@ -7152,7 +7174,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
7152
7174
7153
7175
// Add any keywords that can be used in an argument expr position.
7154
7176
addSuperKeyword (CompletionContext.getResultSink ());
7155
- addExprKeywords (CompletionContext.getResultSink ());
7177
+ addExprKeywords (CompletionContext.getResultSink (), CurDeclContext );
7156
7178
7157
7179
DoPostfixExprBeginning ();
7158
7180
}
@@ -7208,10 +7230,10 @@ void CodeCompletionCallbacksImpl::doneParsing() {
7208
7230
addDeclKeywords (Sink, CurDeclContext,
7209
7231
Context.LangOpts .EnableExperimentalConcurrency ,
7210
7232
Context.LangOpts .EnableExperimentalDistributed );
7211
- addStmtKeywords (Sink, MaybeFuncBody);
7233
+ addStmtKeywords (Sink, CurDeclContext, MaybeFuncBody);
7212
7234
addSuperKeyword (Sink);
7213
7235
addLetVarKeywords (Sink);
7214
- addExprKeywords (Sink);
7236
+ addExprKeywords (Sink, CurDeclContext );
7215
7237
addAnyTypeKeyword (Sink, Context.TheAnyType );
7216
7238
DoPostfixExprBeginning ();
7217
7239
}
0 commit comments