Skip to content

Commit 33ee4d6

Browse files
committed
[CodeCompletion] 'ContextFreeCodeCompletionResult' factory method
Convert 'ContextFreeCodeCompletionResult' constructor overloads to 'create()' factory methods. This is the consistent interface with 'CodeCompletionString'. NFC
1 parent 83be9ba commit 33ee4d6

File tree

4 files changed

+101
-63
lines changed

4 files changed

+101
-63
lines changed

include/swift/IDE/CodeCompletion.h

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -777,78 +777,54 @@ class ContextFreeCodeCompletionResult {
777777
"isOperator implies operator kind != None");
778778
}
779779

780-
/// Constructs a \c Pattern, \c Keyword or \c BuiltinOperator result.
780+
/// Constructs a \c Pattern or \c BuiltinOperator result.
781781
///
782782
/// \note The caller must ensure that the \p CompletionString and \c
783783
/// StringRefs outlive this result, typically by storing them in the same
784784
/// \c CodeCompletionResultSink as the result itself.
785-
ContextFreeCodeCompletionResult(
786-
CodeCompletionResultKind Kind, CodeCompletionString *CompletionString,
785+
static ContextFreeCodeCompletionResult *createPatternOrBuiltInOperatorResult(
786+
llvm::BumpPtrAllocator &Allocator, CodeCompletionResultKind Kind,
787+
CodeCompletionString *CompletionString,
787788
CodeCompletionOperatorKind KnownOperatorKind, StringRef BriefDocComment,
788789
CodeCompletionResultType ResultType,
789790
ContextFreeNotRecommendedReason NotRecommended,
790791
CodeCompletionDiagnosticSeverity DiagnosticSeverity,
791-
StringRef DiagnosticMessage)
792-
: ContextFreeCodeCompletionResult(
793-
Kind, /*AssociatedKind=*/0, KnownOperatorKind,
794-
/*IsSystem=*/false, CompletionString, /*ModuleName=*/"",
795-
BriefDocComment, /*AssociatedUSRs=*/{}, ResultType, NotRecommended,
796-
DiagnosticSeverity, DiagnosticMessage) {}
792+
StringRef DiagnosticMessage);
797793

798794
/// Constructs a \c Keyword result.
799795
///
800796
/// \note The caller must ensure that the \p CompletionString and
801-
/// \p BriefDocComment outlive this result, typically by storing them in the
802-
/// same \c CodeCompletionResultSink as the result itself.
803-
ContextFreeCodeCompletionResult(CodeCompletionKeywordKind Kind,
804-
CodeCompletionString *CompletionString,
805-
StringRef BriefDocComment,
806-
CodeCompletionResultType ResultType)
807-
: ContextFreeCodeCompletionResult(
808-
CodeCompletionResultKind::Keyword, static_cast<uint8_t>(Kind),
809-
CodeCompletionOperatorKind::None, /*IsSystem=*/false,
810-
CompletionString, /*ModuleName=*/"", BriefDocComment,
811-
/*AssociatedUSRs=*/{}, ResultType,
812-
ContextFreeNotRecommendedReason::None,
813-
CodeCompletionDiagnosticSeverity::None, /*DiagnosticMessage=*/"") {}
797+
/// \p BriefDocComment outlive this result, typically by storing them in
798+
/// the same \c CodeCompletionResultSink as the result itself.
799+
static ContextFreeCodeCompletionResult *createKeywordResult(
800+
llvm::BumpPtrAllocator &Allocator, CodeCompletionKeywordKind Kind,
801+
CodeCompletionString *CompletionString, StringRef BriefDocComment,
802+
CodeCompletionResultType ResultType);
814803

815804
/// Constructs a \c Literal result.
816805
///
817806
/// \note The caller must ensure that the \p CompletionString outlives this
818807
/// result, typically by storing them in the same \c CodeCompletionResultSink
819808
/// as the result itself.
820-
ContextFreeCodeCompletionResult(CodeCompletionLiteralKind LiteralKind,
821-
CodeCompletionString *CompletionString,
822-
CodeCompletionResultType ResultType)
823-
: ContextFreeCodeCompletionResult(
824-
CodeCompletionResultKind::Literal,
825-
static_cast<uint8_t>(LiteralKind), CodeCompletionOperatorKind::None,
826-
/*IsSystem=*/false, CompletionString, /*ModuleName=*/"",
827-
/*BriefDocComment=*/"",
828-
/*AssociatedUSRs=*/{}, ResultType,
829-
ContextFreeNotRecommendedReason::None,
830-
CodeCompletionDiagnosticSeverity::None, /*DiagnosticMessage=*/"") {}
809+
static ContextFreeCodeCompletionResult *
810+
createLiteralResult(llvm::BumpPtrAllocator &Allocator,
811+
CodeCompletionLiteralKind LiteralKind,
812+
CodeCompletionString *CompletionString,
813+
CodeCompletionResultType ResultType);
831814

832815
/// Constructs a \c Declaration result.
833816
///
834817
/// \note The caller must ensure that the \p CompletionString and all
835818
/// \c StringRefs outlive this result, typically by storing them in the same
836819
/// \c CodeCompletionResultSink as the result itself.
837-
ContextFreeCodeCompletionResult(
838-
CodeCompletionString *CompletionString, const Decl *AssociatedDecl,
839-
StringRef ModuleName, StringRef BriefDocComment,
840-
ArrayRef<StringRef> AssociatedUSRs, CodeCompletionResultType ResultType,
820+
static ContextFreeCodeCompletionResult *createDeclResult(
821+
llvm::BumpPtrAllocator &Allocator, CodeCompletionString *CompletionString,
822+
const Decl *AssociatedDecl, StringRef ModuleName,
823+
StringRef BriefDocComment, ArrayRef<StringRef> AssociatedUSRs,
824+
CodeCompletionResultType ResultType,
841825
ContextFreeNotRecommendedReason NotRecommended,
842826
CodeCompletionDiagnosticSeverity DiagnosticSeverity,
843-
StringRef DiagnosticMessage)
844-
: ContextFreeCodeCompletionResult(
845-
CodeCompletionResultKind::Declaration,
846-
static_cast<uint8_t>(getCodeCompletionDeclKind(AssociatedDecl)),
847-
CodeCompletionOperatorKind::None, getDeclIsSystem(AssociatedDecl),
848-
CompletionString, ModuleName, BriefDocComment, AssociatedUSRs,
849-
ResultType, NotRecommended, DiagnosticSeverity, DiagnosticMessage) {
850-
assert(AssociatedDecl && "should have a decl");
851-
}
827+
StringRef DiagnosticMessage);
852828

853829
CodeCompletionResultKind getKind() const { return Kind; }
854830

lib/IDE/CodeCompletion.cpp

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,67 @@ void CodeCompletionString::dump() const {
323323
}
324324
}
325325

326+
ContextFreeCodeCompletionResult *
327+
ContextFreeCodeCompletionResult::createPatternOrBuiltInOperatorResult(
328+
llvm::BumpPtrAllocator &Allocator, CodeCompletionResultKind Kind,
329+
CodeCompletionString *CompletionString,
330+
CodeCompletionOperatorKind KnownOperatorKind, StringRef BriefDocComment,
331+
CodeCompletionResultType ResultType,
332+
ContextFreeNotRecommendedReason NotRecommended,
333+
CodeCompletionDiagnosticSeverity DiagnosticSeverity,
334+
StringRef DiagnosticMessage) {
335+
return new (Allocator) ContextFreeCodeCompletionResult(
336+
Kind, /*AssociatedKind=*/0, KnownOperatorKind,
337+
/*IsSystem=*/false, CompletionString, /*ModuleName=*/"", BriefDocComment,
338+
/*AssociatedUSRs=*/{}, ResultType, NotRecommended, DiagnosticSeverity,
339+
DiagnosticMessage);
340+
}
341+
342+
ContextFreeCodeCompletionResult *
343+
ContextFreeCodeCompletionResult::createKeywordResult(
344+
llvm::BumpPtrAllocator &Allocator, CodeCompletionKeywordKind Kind,
345+
CodeCompletionString *CompletionString, StringRef BriefDocComment,
346+
CodeCompletionResultType ResultType) {
347+
return new (Allocator) ContextFreeCodeCompletionResult(
348+
CodeCompletionResultKind::Keyword, static_cast<uint8_t>(Kind),
349+
CodeCompletionOperatorKind::None, /*IsSystem=*/false, CompletionString,
350+
351+
/*ModuleName=*/"", BriefDocComment,
352+
/*AssociatedUSRs=*/{}, ResultType, ContextFreeNotRecommendedReason::None,
353+
CodeCompletionDiagnosticSeverity::None, /*DiagnosticMessage=*/"");
354+
}
355+
356+
ContextFreeCodeCompletionResult *
357+
ContextFreeCodeCompletionResult::createLiteralResult(
358+
llvm::BumpPtrAllocator &Allocator, CodeCompletionLiteralKind LiteralKind,
359+
CodeCompletionString *CompletionString,
360+
CodeCompletionResultType ResultType) {
361+
return new (Allocator) ContextFreeCodeCompletionResult(
362+
CodeCompletionResultKind::Literal, static_cast<uint8_t>(LiteralKind),
363+
CodeCompletionOperatorKind::None,
364+
/*IsSystem=*/false, CompletionString, /*ModuleName=*/"",
365+
/*BriefDocComment=*/"",
366+
/*AssociatedUSRs=*/{}, ResultType, ContextFreeNotRecommendedReason::None,
367+
CodeCompletionDiagnosticSeverity::None, /*DiagnosticMessage=*/"");
368+
}
369+
370+
ContextFreeCodeCompletionResult *
371+
ContextFreeCodeCompletionResult::createDeclResult(
372+
llvm::BumpPtrAllocator &Allocator, CodeCompletionString *CompletionString,
373+
const Decl *AssociatedDecl, StringRef ModuleName, StringRef BriefDocComment,
374+
ArrayRef<StringRef> AssociatedUSRs, CodeCompletionResultType ResultType,
375+
ContextFreeNotRecommendedReason NotRecommended,
376+
CodeCompletionDiagnosticSeverity DiagnosticSeverity,
377+
StringRef DiagnosticMessage) {
378+
assert(AssociatedDecl && "should have a decl");
379+
return new (Allocator) ContextFreeCodeCompletionResult(
380+
CodeCompletionResultKind::Declaration,
381+
static_cast<uint8_t>(getCodeCompletionDeclKind(AssociatedDecl)),
382+
CodeCompletionOperatorKind::None, getDeclIsSystem(AssociatedDecl),
383+
CompletionString, ModuleName, BriefDocComment, AssociatedUSRs, ResultType,
384+
NotRecommended, DiagnosticSeverity, DiagnosticMessage);
385+
}
386+
326387
CodeCompletionDeclKind
327388
ContextFreeCodeCompletionResult::getCodeCompletionDeclKind(const Decl *D) {
328389
switch (D->getKind()) {
@@ -1276,8 +1337,8 @@ CodeCompletionResult *CodeCompletionResultBuilder::takeResult() {
12761337
}
12771338
}
12781339

1279-
ContextFreeResult = new (*Sink.Allocator) ContextFreeCodeCompletionResult(
1280-
CCS, AssociatedDecl, ModuleName,
1340+
ContextFreeResult = ContextFreeCodeCompletionResult::createDeclResult(
1341+
*Sink.Allocator, CCS, AssociatedDecl, ModuleName,
12811342
copyString(*Sink.Allocator, BriefDocComment),
12821343
copyAssociatedUSRs(*Sink.Allocator, AssociatedDecl), ResultType,
12831344
ContextFreeNotRecReason, ContextFreeDiagnosticSeverity,
@@ -1286,22 +1347,23 @@ CodeCompletionResult *CodeCompletionResultBuilder::takeResult() {
12861347
}
12871348

12881349
case CodeCompletionResultKind::Keyword:
1289-
ContextFreeResult = new (*Sink.Allocator) ContextFreeCodeCompletionResult(
1290-
KeywordKind, CCS, copyString(*Sink.Allocator, BriefDocComment),
1291-
ResultType);
1350+
ContextFreeResult = ContextFreeCodeCompletionResult::createKeywordResult(
1351+
*Sink.Allocator, KeywordKind, CCS,
1352+
copyString(*Sink.Allocator, BriefDocComment), ResultType);
12921353
break;
12931354
case CodeCompletionResultKind::BuiltinOperator:
12941355
case CodeCompletionResultKind::Pattern:
1295-
ContextFreeResult = new (*Sink.Allocator) ContextFreeCodeCompletionResult(
1296-
Kind, CCS, CodeCompletionOperatorKind::None,
1297-
copyString(*Sink.Allocator, BriefDocComment), ResultType,
1298-
ContextFreeNotRecReason,
1299-
ContextFreeDiagnosticSeverity, ContextFreeDiagnosticMessage);
1356+
ContextFreeResult =
1357+
ContextFreeCodeCompletionResult::createPatternOrBuiltInOperatorResult(
1358+
*Sink.Allocator, Kind, CCS, CodeCompletionOperatorKind::None,
1359+
copyString(*Sink.Allocator, BriefDocComment), ResultType,
1360+
ContextFreeNotRecReason, ContextFreeDiagnosticSeverity,
1361+
ContextFreeDiagnosticMessage);
13001362
break;
13011363
case CodeCompletionResultKind::Literal:
13021364
assert(LiteralKind.hasValue());
1303-
ContextFreeResult = new (*Sink.Allocator)
1304-
ContextFreeCodeCompletionResult(*LiteralKind, CCS, ResultType);
1365+
ContextFreeResult = ContextFreeCodeCompletionResult::createLiteralResult(
1366+
*Sink.Allocator, *LiteralKind, CCS, ResultType);
13051367
break;
13061368
}
13071369

tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ bool SourceKit::CodeCompletion::addCustomCompletions(
131131
auto *completionString =
132132
CodeCompletionString::create(sink.allocator, chunk);
133133
auto *contextFreeResult =
134-
new (sink.allocator) ContextFreeCodeCompletionResult(
135-
CodeCompletionResultKind::Pattern, completionString,
134+
ContextFreeCodeCompletionResult::createPatternOrBuiltInOperatorResult(
135+
sink.allocator, CodeCompletionResultKind::Pattern, completionString,
136136
CodeCompletionOperatorKind::None, /*BriefDocComment=*/"",
137137
CodeCompletionResultType::unknown(),
138138
ContextFreeNotRecommendedReason::None,

tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -920,9 +920,9 @@ static void transformAndForwardResults(
920920
auto *completionString =
921921
CodeCompletionString::create(innerSink.allocator, chunks);
922922
ContextFreeCodeCompletionResult *contextFreeResult =
923-
new (innerSink.allocator) ContextFreeCodeCompletionResult(
924-
CodeCompletionResultKind::BuiltinOperator, completionString,
925-
CodeCompletionOperatorKind::None,
923+
ContextFreeCodeCompletionResult::createPatternOrBuiltInOperatorResult(
924+
innerSink.allocator, CodeCompletionResultKind::BuiltinOperator,
925+
completionString, CodeCompletionOperatorKind::None,
926926
/*BriefDocComment=*/"", CodeCompletionResultType::notApplicable(),
927927
ContextFreeNotRecommendedReason::None,
928928
CodeCompletionDiagnosticSeverity::None,

0 commit comments

Comments
 (0)