@@ -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) {
@@ -6497,9 +6515,16 @@ static void addConditionalCompilationFlags(ASTContext &Ctx,
6497
6515
}
6498
6516
}
6499
6517
6500
- static void postProcessResults (ArrayRef<CodeCompletionResult *> results,
6501
- CompletionKind Kind, DeclContext *DC) {
6502
- for (CodeCompletionResult *result : results) {
6518
+ // / Add flairs to the each item in \p results .
6519
+ // /
6520
+ // / If \p Sink is passed, the pointer of the each result may be replaced with a
6521
+ // / pointer to the new item allocated in \p Sink.
6522
+ // / If \p Sink is nullptr, the pointee of each result may be modified in place.
6523
+ static void postProcessResults (MutableArrayRef<CodeCompletionResult *> results,
6524
+ CompletionKind Kind, DeclContext *DC,
6525
+ CodeCompletionResultSink *Sink) {
6526
+ for (CodeCompletionResult *&result : results) {
6527
+ bool modified = false ;
6503
6528
auto flair = result->getFlair ();
6504
6529
6505
6530
// Starting a statement with a protocol name is not common. So protocol
@@ -6512,15 +6537,27 @@ static void postProcessResults(ArrayRef<CodeCompletionResult *> results,
6512
6537
Kind != CompletionKind::TypeDeclResultBeginning &&
6513
6538
Kind != CompletionKind::GenericRequirement) {
6514
6539
flair |= CodeCompletionFlairBit::RareTypeAtCurrentPosition;
6540
+ modified = true ;
6515
6541
}
6516
6542
6517
6543
// Starting a statement at top-level in non-script files is invalid.
6518
6544
if (Kind == CompletionKind::StmtOrExpr &&
6519
6545
result->getKind () == CodeCompletionResult::ResultKind::Declaration &&
6520
6546
isCodeCompletionAtTopLevelOfLibraryFile (DC)) {
6521
6547
flair |= CodeCompletionFlairBit::ExpressionAtNonScriptOrMainFileScope;
6548
+ modified = true ;
6549
+ }
6550
+
6551
+ if (!modified)
6552
+ continue ;
6553
+
6554
+ if (Sink) {
6555
+ // Replace the result with a new result with the flair.
6556
+ result = result->withFlair (flair, *Sink);
6557
+ } else {
6558
+ // 'Sink' == nullptr means the result is modifiable in place.
6559
+ result->setFlair (flair);
6522
6560
}
6523
- result->setFlair (flair);
6524
6561
}
6525
6562
}
6526
6563
@@ -6640,7 +6677,8 @@ static void deliverCompletionResults(CodeCompletionContext &CompletionContext,
6640
6677
CompletionContext.typeContextKind = Lookup.typeContextKind ();
6641
6678
6642
6679
postProcessResults (CompletionContext.getResultSink ().Results ,
6643
- CompletionContext.CodeCompletionKind , DC);
6680
+ CompletionContext.CodeCompletionKind , DC,
6681
+ /* Sink=*/ nullptr );
6644
6682
6645
6683
Consumer.handleResultsAndModules (CompletionContext, RequestedModules, DC);
6646
6684
}
@@ -7424,7 +7462,8 @@ void swift::ide::lookupCodeCompletionResultsFromModule(
7424
7462
CompletionLookup Lookup (targetSink, module ->getASTContext (), SF);
7425
7463
Lookup.lookupExternalModuleDecls (module , accessPath, needLeadingDot);
7426
7464
}
7427
- ArrayRef<CodeCompletionResult *>
7465
+
7466
+ MutableArrayRef<CodeCompletionResult *>
7428
7467
swift::ide::copyCodeCompletionResults (CodeCompletionResultSink &targetSink,
7429
7468
CodeCompletionResultSink &sourceSink,
7430
7469
bool onlyTypes,
@@ -7485,8 +7524,8 @@ swift::ide::copyCodeCompletionResults(CodeCompletionResultSink &targetSink,
7485
7524
sourceSink.Results .end ());
7486
7525
}
7487
7526
7488
- return llvm::makeArrayRef (targetSink.Results .data () + startSize,
7489
- targetSink.Results .size () - startSize);
7527
+ return llvm::makeMutableArrayRef (targetSink.Results .data () + startSize,
7528
+ targetSink.Results .size () - startSize);
7490
7529
}
7491
7530
7492
7531
void SimpleCachingCodeCompletionConsumer::handleResultsAndModules (
@@ -7515,9 +7554,11 @@ void SimpleCachingCodeCompletionConsumer::handleResultsAndModules(
7515
7554
context.Cache .set (R.Key , *V);
7516
7555
}
7517
7556
assert (V.hasValue ());
7518
- auto newItems = copyCodeCompletionResults (context.getResultSink (), (*V)->Sink ,
7519
- R.OnlyTypes , R.OnlyPrecedenceGroups );
7520
- postProcessResults (newItems, context.CodeCompletionKind , DC);
7557
+ auto newItems =
7558
+ copyCodeCompletionResults (context.getResultSink (), (*V)->Sink ,
7559
+ R.OnlyTypes , R.OnlyPrecedenceGroups );
7560
+ postProcessResults (newItems, context.CodeCompletionKind , DC,
7561
+ &context.getResultSink ());
7521
7562
}
7522
7563
7523
7564
handleResults (context.takeResults ());
0 commit comments