@@ -801,6 +801,24 @@ void CodeCompletionResult::dump() const {
801
801
llvm::errs () << " \n " ;
802
802
}
803
803
804
+ CodeCompletionResult *
805
+ CodeCompletionResult::withFlair (CodeCompletionFlair newFlair,
806
+ CodeCompletionResultSink &Sink) {
807
+ if (Kind == ResultKind::Declaration) {
808
+ return new (*Sink.Allocator ) CodeCompletionResult (
809
+ getSemanticContext (), newFlair, getNumBytesToErase (),
810
+ getCompletionString (), getAssociatedDeclKind (), isSystem (),
811
+ getModuleName (), getNotRecommendedReason (), getBriefDocComment (),
812
+ getAssociatedUSRs (), getDeclKeywords (), getExpectedTypeRelation (),
813
+ isOperator () ? getOperatorKind () : CodeCompletionOperatorKind::None);
814
+ } else {
815
+ return new (*Sink.Allocator ) CodeCompletionResult (
816
+ getKind (), getSemanticContext (), newFlair, getNumBytesToErase (),
817
+ getCompletionString (), getExpectedTypeRelation (),
818
+ isOperator () ? getOperatorKind () : CodeCompletionOperatorKind::None);
819
+ }
820
+ }
821
+
804
822
void CodeCompletionResultBuilder::withNestedGroup (
805
823
CodeCompletionString::Chunk::ChunkKind Kind,
806
824
llvm::function_ref<void ()> body) {
@@ -6517,9 +6535,16 @@ static void addConditionalCompilationFlags(ASTContext &Ctx,
6517
6535
}
6518
6536
}
6519
6537
6520
- static void postProcessResults (ArrayRef<CodeCompletionResult *> results,
6521
- CompletionKind Kind, DeclContext *DC) {
6522
- for (CodeCompletionResult *result : results) {
6538
+ // / Add flairs to the each item in \p results .
6539
+ // /
6540
+ // / If \p Sink is passed, the pointer of the each result may be replaced with a
6541
+ // / pointer to the new item allocated in \p Sink.
6542
+ // / If \p Sink is nullptr, the pointee of each result may be modified in place.
6543
+ static void postProcessResults (MutableArrayRef<CodeCompletionResult *> results,
6544
+ CompletionKind Kind, DeclContext *DC,
6545
+ CodeCompletionResultSink *Sink) {
6546
+ for (CodeCompletionResult *&result : results) {
6547
+ bool modified = false ;
6523
6548
auto flair = result->getFlair ();
6524
6549
6525
6550
// Starting a statement with a protocol name is not common. So protocol
@@ -6532,15 +6557,27 @@ static void postProcessResults(ArrayRef<CodeCompletionResult *> results,
6532
6557
Kind != CompletionKind::TypeDeclResultBeginning &&
6533
6558
Kind != CompletionKind::GenericRequirement) {
6534
6559
flair |= CodeCompletionFlairBit::RareTypeAtCurrentPosition;
6560
+ modified = true ;
6535
6561
}
6536
6562
6537
6563
// Starting a statement at top-level in non-script files is invalid.
6538
6564
if (Kind == CompletionKind::StmtOrExpr &&
6539
6565
result->getKind () == CodeCompletionResult::ResultKind::Declaration &&
6540
6566
isCodeCompletionAtTopLevelOfLibraryFile (DC)) {
6541
6567
flair |= CodeCompletionFlairBit::ExpressionAtNonScriptOrMainFileScope;
6568
+ modified = true ;
6569
+ }
6570
+
6571
+ if (!modified)
6572
+ continue ;
6573
+
6574
+ if (Sink) {
6575
+ // Replace the result with a new result with the flair.
6576
+ result = result->withFlair (flair, *Sink);
6577
+ } else {
6578
+ // 'Sink' == nullptr means the result is modifiable in place.
6579
+ result->setFlair (flair);
6542
6580
}
6543
- result->setFlair (flair);
6544
6581
}
6545
6582
}
6546
6583
@@ -6660,7 +6697,8 @@ static void deliverCompletionResults(CodeCompletionContext &CompletionContext,
6660
6697
CompletionContext.typeContextKind = Lookup.typeContextKind ();
6661
6698
6662
6699
postProcessResults (CompletionContext.getResultSink ().Results ,
6663
- CompletionContext.CodeCompletionKind , DC);
6700
+ CompletionContext.CodeCompletionKind , DC,
6701
+ /* Sink=*/ nullptr );
6664
6702
6665
6703
Consumer.handleResultsAndModules (CompletionContext, RequestedModules, DC);
6666
6704
}
@@ -7446,7 +7484,8 @@ void swift::ide::lookupCodeCompletionResultsFromModule(
7446
7484
CompletionLookup Lookup (targetSink, module ->getASTContext (), SF);
7447
7485
Lookup.lookupExternalModuleDecls (module , accessPath, needLeadingDot);
7448
7486
}
7449
- ArrayRef<CodeCompletionResult *>
7487
+
7488
+ MutableArrayRef<CodeCompletionResult *>
7450
7489
swift::ide::copyCodeCompletionResults (CodeCompletionResultSink &targetSink,
7451
7490
CodeCompletionResultSink &sourceSink,
7452
7491
bool onlyTypes,
@@ -7507,8 +7546,8 @@ swift::ide::copyCodeCompletionResults(CodeCompletionResultSink &targetSink,
7507
7546
sourceSink.Results .end ());
7508
7547
}
7509
7548
7510
- return llvm::makeArrayRef (targetSink.Results .data () + startSize,
7511
- targetSink.Results .size () - startSize);
7549
+ return llvm::makeMutableArrayRef (targetSink.Results .data () + startSize,
7550
+ targetSink.Results .size () - startSize);
7512
7551
}
7513
7552
7514
7553
void SimpleCachingCodeCompletionConsumer::handleResultsAndModules (
@@ -7537,9 +7576,11 @@ void SimpleCachingCodeCompletionConsumer::handleResultsAndModules(
7537
7576
context.Cache .set (R.Key , *V);
7538
7577
}
7539
7578
assert (V.hasValue ());
7540
- auto newItems = copyCodeCompletionResults (context.getResultSink (), (*V)->Sink ,
7541
- R.OnlyTypes , R.OnlyPrecedenceGroups );
7542
- postProcessResults (newItems, context.CodeCompletionKind , DC);
7579
+ auto newItems =
7580
+ copyCodeCompletionResults (context.getResultSink (), (*V)->Sink ,
7581
+ R.OnlyTypes , R.OnlyPrecedenceGroups );
7582
+ postProcessResults (newItems, context.CodeCompletionKind , DC,
7583
+ &context.getResultSink ());
7543
7584
}
7544
7585
7545
7586
handleResults (context.takeResults ());
0 commit comments