@@ -743,6 +743,8 @@ void CodeCompletionResult::printPrefix(raw_ostream &OS) const {
743
743
PRINT_FLAIR (ArgumentLabels, " ArgLabels" );
744
744
PRINT_FLAIR (CommonKeywordAtCurrentPosition, " CommonKeyword" )
745
745
PRINT_FLAIR (RareKeywordAtCurrentPosition, " RareKeyword" )
746
+ PRINT_FLAIR (RareTypeAtCurrentPosition, " RareType" )
747
+ PRINT_FLAIR (ExpressionAtNonScriptOrMainFileScope, " ExprAtFileScope" )
746
748
Prefix.append (" ]" );
747
749
}
748
750
if (NotRecommended)
@@ -6457,10 +6459,54 @@ static void addConditionalCompilationFlags(ASTContext &Ctx,
6457
6459
}
6458
6460
}
6459
6461
6462
+ static void postProcessResults (ArrayRef<CodeCompletionResult *> results,
6463
+ 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
+ for (CodeCompletionResult *result : results) {
6478
+ auto flair = result->getFlair ();
6479
+
6480
+ // Starting a statement with a protocol name is not common. So protocol
6481
+ // names at non-type name position are "rare".
6482
+ if (result->getKind () == CodeCompletionResult::ResultKind::Declaration &&
6483
+ result->getAssociatedDeclKind () == CodeCompletionDeclKind::Protocol &&
6484
+ Kind != CompletionKind::TypeSimpleBeginning &&
6485
+ Kind != CompletionKind::TypeIdentifierWithoutDot &&
6486
+ Kind != CompletionKind::TypeIdentifierWithDot &&
6487
+ Kind != CompletionKind::TypeDeclResultBeginning &&
6488
+ Kind != CompletionKind::GenericRequirement) {
6489
+ flair |= CodeCompletionFlairBit::RareTypeAtCurrentPosition;
6490
+ }
6491
+
6492
+ // Starting a statement at top-level in non-script files is invalid.
6493
+ 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))) {
6499
+ flair |= CodeCompletionFlairBit::ExpressionAtNonScriptOrMainFileScope;
6500
+ }
6501
+ result->setFlair (flair);
6502
+ }
6503
+ }
6504
+
6460
6505
static void deliverCompletionResults (CodeCompletionContext &CompletionContext,
6461
6506
CompletionLookup &Lookup,
6462
- SourceFile &SF ,
6507
+ DeclContext *DC ,
6463
6508
CodeCompletionConsumer &Consumer) {
6509
+ auto &SF = *DC->getParentSourceFile ();
6464
6510
llvm::SmallPtrSet<Identifier, 8 > seenModuleNames;
6465
6511
std::vector<RequestedCachedModule> RequestedModules;
6466
6512
@@ -6571,12 +6617,10 @@ static void deliverCompletionResults(CodeCompletionContext &CompletionContext,
6571
6617
Lookup.RequestedCachedResults .clear ();
6572
6618
CompletionContext.typeContextKind = Lookup.typeContextKind ();
6573
6619
6574
- // Use the current SourceFile as the DeclContext so that we can use it to
6575
- // perform qualified lookup, and to get the correct visibility for
6576
- // @testable imports.
6577
- DeclContext *DCForModules = &SF;
6578
- Consumer.handleResultsAndModules (CompletionContext, RequestedModules,
6579
- DCForModules);
6620
+ postProcessResults (CompletionContext.getResultSink ().Results ,
6621
+ CompletionContext.CodeCompletionKind , DC);
6622
+
6623
+ Consumer.handleResultsAndModules (CompletionContext, RequestedModules, DC);
6580
6624
}
6581
6625
6582
6626
void deliverUnresolvedMemberResults (
@@ -6614,8 +6658,8 @@ void deliverUnresolvedMemberResults(
6614
6658
}
6615
6659
Lookup.getUnresolvedMemberCompletions (Result.ExpectedTy );
6616
6660
}
6617
- SourceFile *SF = DC-> getParentSourceFile ();
6618
- deliverCompletionResults (CompletionCtx, Lookup, *SF , Consumer);
6661
+
6662
+ deliverCompletionResults (CompletionCtx, Lookup, DC , Consumer);
6619
6663
}
6620
6664
6621
6665
void deliverDotExprResults (
@@ -6655,8 +6699,7 @@ void deliverDotExprResults(
6655
6699
Lookup.getValueExprCompletions (Result.BaseTy , Result.BaseDecl );
6656
6700
}
6657
6701
6658
- SourceFile *SF = DC->getParentSourceFile ();
6659
- deliverCompletionResults (CompletionCtx, Lookup, *SF, Consumer);
6702
+ deliverCompletionResults (CompletionCtx, Lookup, DC, Consumer);
6660
6703
}
6661
6704
6662
6705
bool CodeCompletionCallbacksImpl::trySolverCompletion (bool MaybeFuncBody) {
@@ -7284,7 +7327,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
7284
7327
break ;
7285
7328
}
7286
7329
7287
- deliverCompletionResults (CompletionContext, Lookup, P. SF , Consumer);
7330
+ deliverCompletionResults (CompletionContext, Lookup, CurDeclContext , Consumer);
7288
7331
}
7289
7332
7290
7333
void PrintingCodeCompletionConsumer::handleResults (
@@ -7360,16 +7403,17 @@ void swift::ide::lookupCodeCompletionResultsFromModule(
7360
7403
CompletionLookup Lookup (targetSink, module ->getASTContext (), SF);
7361
7404
Lookup.lookupExternalModuleDecls (module , accessPath, needLeadingDot);
7362
7405
}
7363
-
7364
- void swift::ide::copyCodeCompletionResults (CodeCompletionResultSink &targetSink,
7365
- CodeCompletionResultSink &sourceSink,
7366
- bool onlyTypes,
7367
- bool onlyPrecedenceGroups) {
7406
+ ArrayRef<CodeCompletionResult *>
7407
+ swift::ide::copyCodeCompletionResults (CodeCompletionResultSink &targetSink,
7408
+ CodeCompletionResultSink &sourceSink,
7409
+ bool onlyTypes,
7410
+ bool onlyPrecedenceGroups) {
7368
7411
7369
7412
// We will be adding foreign results (from another sink) into TargetSink.
7370
7413
// TargetSink should have an owning pointer to the allocator that keeps the
7371
7414
// results alive.
7372
7415
targetSink.ForeignAllocators .push_back (sourceSink.Allocator );
7416
+ auto startSize = targetSink.Results .size ();
7373
7417
7374
7418
if (onlyTypes) {
7375
7419
std::copy_if (sourceSink.Results .begin (), sourceSink.Results .end (),
@@ -7419,12 +7463,21 @@ void swift::ide::copyCodeCompletionResults(CodeCompletionResultSink &targetSink,
7419
7463
sourceSink.Results .begin (),
7420
7464
sourceSink.Results .end ());
7421
7465
}
7466
+
7467
+ return llvm::makeArrayRef (targetSink.Results .data () + startSize,
7468
+ targetSink.Results .size () - startSize);
7422
7469
}
7423
7470
7424
7471
void SimpleCachingCodeCompletionConsumer::handleResultsAndModules (
7425
7472
CodeCompletionContext &context,
7426
7473
ArrayRef<RequestedCachedModule> requestedModules,
7427
- DeclContext *DCForModules) {
7474
+ DeclContext *DC) {
7475
+
7476
+ // Use the current SourceFile as the DeclContext so that we can use it to
7477
+ // perform qualified lookup, and to get the correct visibility for
7478
+ // @testable imports.
7479
+ DeclContext *DCForModules = DC->getParentSourceFile ();
7480
+
7428
7481
for (auto &R : requestedModules) {
7429
7482
// FIXME(thread-safety): lock the whole AST context. We might load a
7430
7483
// module.
@@ -7440,8 +7493,9 @@ void SimpleCachingCodeCompletionConsumer::handleResultsAndModules(
7440
7493
context.Cache .set (R.Key , *V);
7441
7494
}
7442
7495
assert (V.hasValue ());
7443
- copyCodeCompletionResults (context.getResultSink (), (*V)->Sink ,
7444
- R.OnlyTypes , R.OnlyPrecedenceGroups );
7496
+ auto newItems = copyCodeCompletionResults (context.getResultSink (), (*V)->Sink ,
7497
+ R.OnlyTypes , R.OnlyPrecedenceGroups );
7498
+ postProcessResults (newItems, context.CodeCompletionKind , DC);
7445
7499
}
7446
7500
7447
7501
handleResults (context.takeResults ());
0 commit comments