Skip to content

Commit e1de391

Browse files
authored
Merge pull request #40724 from ahoppen/pr/sourcekit-completion-reference-to-swift-result
[CodeComplete] Make SourceKit::CodeCompletion::Completion store a reference to the underlying swift result instead of extending that type
2 parents cbf4eb2 + 520c41a commit e1de391

File tree

9 files changed

+202
-139
lines changed

9 files changed

+202
-139
lines changed

include/swift/IDE/CodeCompletion.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,8 +1003,7 @@ class ImportDepth {
10031003
class CodeCompletionContext {
10041004
friend class CodeCompletionResultBuilder;
10051005

1006-
/// A set of current completion results, not yet delivered to the
1007-
/// consumer.
1006+
/// A set of current completion results.
10081007
CodeCompletionResultSink CurrentResults;
10091008

10101009
public:
@@ -1072,13 +1071,10 @@ class CodeCompletionContext {
10721071
/// Allocate a string owned by the code completion context.
10731072
StringRef copyString(StringRef Str);
10741073

1075-
/// Return current code completion results.
1076-
MutableArrayRef<CodeCompletionResult *> takeResults();
1077-
10781074
/// Sort code completion results in an implementation-defined order
10791075
/// in place.
1080-
static void sortCompletionResults(
1081-
MutableArrayRef<CodeCompletionResult *> Results);
1076+
static std::vector<CodeCompletionResult *>
1077+
sortCompletionResults(ArrayRef<CodeCompletionResult *> Results);
10821078

10831079
CodeCompletionResultSink &getResultSink() {
10841080
return CurrentResults;

include/swift/IDE/CompletionInstance.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct CompletionInstanceResult {
5353

5454
/// The results returned from \c CompletionInstance::codeComplete.
5555
struct CodeCompleteResult {
56-
MutableArrayRef<CodeCompletionResult *> Results;
56+
CodeCompletionResultSink &ResultSink;
5757
SwiftCompletionInfo &Info;
5858
};
5959

lib/IDE/CodeCompletion.cpp

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,18 +1354,6 @@ void CodeCompletionResultBuilder::finishResult() {
13541354
Sink.Results.push_back(takeResult());
13551355
}
13561356

1357-
1358-
MutableArrayRef<CodeCompletionResult *> CodeCompletionContext::takeResults() {
1359-
// Copy pointers to the results.
1360-
const size_t Count = CurrentResults.Results.size();
1361-
CodeCompletionResult **Results =
1362-
CurrentResults.Allocator->Allocate<CodeCompletionResult *>(Count);
1363-
std::copy(CurrentResults.Results.begin(), CurrentResults.Results.end(),
1364-
Results);
1365-
CurrentResults.Results.clear();
1366-
return MutableArrayRef<CodeCompletionResult *>(Results, Count);
1367-
}
1368-
13691357
Optional<unsigned> CodeCompletionString::getFirstTextChunkIndex(
13701358
bool includeLeadingPunctuation) const {
13711359
for (auto i : indices(getChunks())) {
@@ -1451,25 +1439,29 @@ CodeCompletionString::getFirstTextChunk(bool includeLeadingPunctuation) const {
14511439
return StringRef();
14521440
}
14531441

1454-
void CodeCompletionContext::sortCompletionResults(
1455-
MutableArrayRef<CodeCompletionResult *> Results) {
1442+
std::vector<CodeCompletionResult *>
1443+
CodeCompletionContext::sortCompletionResults(
1444+
ArrayRef<CodeCompletionResult *> Results) {
1445+
std::vector<CodeCompletionResult *> SortedResults(Results.begin(),
1446+
Results.end());
14561447
struct ResultAndName {
14571448
CodeCompletionResult *result;
14581449
std::string name;
14591450
};
14601451

14611452
// Caching the name of each field is important to avoid unnecessary calls to
14621453
// CodeCompletionString::getName().
1463-
std::vector<ResultAndName> nameCache(Results.size());
1464-
for (unsigned i = 0, n = Results.size(); i < n; ++i) {
1465-
auto *result = Results[i];
1454+
std::vector<ResultAndName> nameCache(SortedResults.size());
1455+
for (unsigned i = 0, n = SortedResults.size(); i < n; ++i) {
1456+
auto *result = SortedResults[i];
14661457
nameCache[i].result = result;
14671458
llvm::raw_string_ostream OS(nameCache[i].name);
14681459
printCodeCompletionResultFilterName(*result, OS);
14691460
OS.flush();
14701461
}
14711462

1472-
// Sort nameCache, and then transform Results to return the pointers in order.
1463+
// Sort nameCache, and then transform SortedResults to return the pointers in
1464+
// order.
14731465
std::sort(nameCache.begin(), nameCache.end(),
14741466
[](const ResultAndName &LHS, const ResultAndName &RHS) {
14751467
int Result = StringRef(LHS.name).compare_insensitive(RHS.name);
@@ -1480,8 +1472,9 @@ void CodeCompletionContext::sortCompletionResults(
14801472
return Result < 0;
14811473
});
14821474

1483-
llvm::transform(nameCache, Results.begin(),
1475+
llvm::transform(nameCache, SortedResults.begin(),
14841476
[](const ResultAndName &entry) { return entry.result; });
1477+
return SortedResults;
14851478
}
14861479

14871480
namespace {

lib/IDE/CompletionInstance.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -709,9 +709,8 @@ void swift::ide::CompletionInstance::codeComplete(
709709
CancellationFlag->load(std::memory_order_relaxed)) {
710710
Callback(ResultType::cancelled());
711711
} else {
712-
MutableArrayRef<CodeCompletionResult *> Results = context.takeResults();
713712
assert(SwiftContext.swiftASTContext);
714-
Callback(ResultType::success({Results, SwiftContext}));
713+
Callback(ResultType::success({context.getResultSink(), SwiftContext}));
715714
}
716715
}
717716
};
@@ -736,7 +735,8 @@ void swift::ide::CompletionInstance::codeComplete(
736735
SwiftCompletionInfo Info{&CI.getASTContext(),
737736
&CI.getInvocation(),
738737
&CompletionContext};
739-
DeliverTransformed(ResultType::success({/*Results=*/{}, Info}));
738+
CodeCompletionResultSink ResultSink;
739+
DeliverTransformed(ResultType::success({ResultSink, Info}));
740740
return;
741741
}
742742

@@ -753,7 +753,8 @@ void swift::ide::CompletionInstance::codeComplete(
753753
SwiftCompletionInfo Info{&CI.getASTContext(),
754754
&CI.getInvocation(),
755755
&CompletionContext};
756-
DeliverTransformed(ResultType::success({/*Results=*/{}, Info}));
756+
CodeCompletionResultSink ResultSink;
757+
DeliverTransformed(ResultType::success({ResultSink, Info}));
757758
}
758759
},
759760
Callback);

lib/IDE/REPLCodeCompletion.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ class REPLCodeCompletionConsumer : public SimpleCachingCodeCompletionConsumer {
178178
: Completions(Completions) {}
179179

180180
void handleResults(CodeCompletionContext &context) override {
181-
MutableArrayRef<CodeCompletionResult *> Results = context.takeResults();
182-
CodeCompletionContext::sortCompletionResults(Results);
183-
for (auto Result : Results) {
181+
auto SortedResults = CodeCompletionContext::sortCompletionResults(
182+
context.getResultSink().Results);
183+
for (auto Result : SortedResults) {
184184
std::string InsertableString = toInsertableString(Result);
185185
if (StringRef(InsertableString).startswith(Completions.Prefix)) {
186186
llvm::SmallString<128> PrintedResult;

tools/SourceKit/lib/SwiftLang/CodeCompletion.h

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ namespace SourceKit {
2323
namespace CodeCompletion {
2424

2525
using swift::ide::CodeCompletionDeclKind;
26+
using swift::ide::CodeCompletionFlair;
2627
using swift::ide::CodeCompletionKeywordKind;
2728
using swift::ide::CodeCompletionLiteralKind;
28-
using swift::ide::SemanticContextKind;
29-
using swift::ide::CodeCompletionFlair;
29+
using swift::ide::CodeCompletionOperatorKind;
3030
using swift::ide::CodeCompletionString;
31+
using swift::ide::SemanticContextKind;
3132
using SwiftResult = swift::ide::CodeCompletionResult;
3233
using swift::ide::CompletionKind;
3334

@@ -75,7 +76,8 @@ struct NameStyle {
7576
///
7677
/// Extends a \c swift::ide::CodeCompletionResult with extra fields that are
7778
/// filled in by SourceKit. Generally stored in an \c CompletionSink.
78-
class Completion : public SwiftResult {
79+
class Completion {
80+
const SwiftResult &base;
7981
void *opaqueCustomKind = nullptr;
8082
Optional<uint8_t> moduleImportDepth;
8183
PopularityFactor popularityFactor;
@@ -89,9 +91,12 @@ class Completion : public SwiftResult {
8991

9092
/// Wraps \p base with an \c Completion. The \p name and \p description
9193
/// should outlive the result, generally by being stored in the same
92-
/// \c CompletionSink.
93-
Completion(SwiftResult base, StringRef name, StringRef description)
94-
: SwiftResult(base), name(name), description(description) {}
94+
/// \c CompletionSink or in a sink that was adopted by the sink that this
95+
/// \c Compleiton is being stored in.
96+
Completion(const SwiftResult &base, StringRef name, StringRef description)
97+
: base(base), name(name), description(description) {}
98+
99+
const SwiftResult &getSwiftResult() const { return base; }
95100

96101
bool hasCustomKind() const { return opaqueCustomKind; }
97102
void *getCustomKind() const { return opaqueCustomKind; }
@@ -102,6 +107,63 @@ class Completion : public SwiftResult {
102107
/// A popularity factory in the range [-1, 1]. The higher the value, the more
103108
/// 'popular' this result is. 0 indicates unknown.
104109
PopularityFactor getPopularityFactor() const { return popularityFactor; }
110+
111+
// MARK: Methods that forward to the SwiftResult
112+
113+
SwiftResult::ResultKind getKind() const { return getSwiftResult().getKind(); }
114+
115+
CodeCompletionDeclKind getAssociatedDeclKind() const {
116+
return getSwiftResult().getAssociatedDeclKind();
117+
}
118+
119+
CodeCompletionLiteralKind getLiteralKind() const {
120+
return getSwiftResult().getLiteralKind();
121+
}
122+
123+
CodeCompletionKeywordKind getKeywordKind() const {
124+
return getSwiftResult().getKeywordKind();
125+
}
126+
127+
bool isOperator() const { return getSwiftResult().isOperator(); }
128+
129+
CodeCompletionOperatorKind getOperatorKind() const {
130+
return getSwiftResult().getOperatorKind();
131+
}
132+
133+
bool isSystem() const { return getSwiftResult().isSystem(); }
134+
135+
SwiftResult::ExpectedTypeRelation getExpectedTypeRelation() const {
136+
return getSwiftResult().getExpectedTypeRelation();
137+
}
138+
139+
SemanticContextKind getSemanticContext() const {
140+
return getSwiftResult().getSemanticContext();
141+
}
142+
143+
CodeCompletionFlair getFlair() const { return getSwiftResult().getFlair(); }
144+
145+
bool isNotRecommended() const { return getSwiftResult().isNotRecommended(); }
146+
147+
unsigned getNumBytesToErase() const {
148+
return getSwiftResult().getNumBytesToErase();
149+
}
150+
151+
CodeCompletionString *getCompletionString() const {
152+
return getSwiftResult().getCompletionString();
153+
}
154+
155+
StringRef getModuleName() const { return getSwiftResult().getModuleName(); }
156+
157+
StringRef getBriefDocComment() const {
158+
return getSwiftResult().getBriefDocComment();
159+
}
160+
161+
ArrayRef<StringRef> getAssociatedUSRs() const {
162+
return getSwiftResult().getAssociatedUSRs();
163+
}
164+
165+
/// Allow "upcasting" the completion result to a SwiftResult.
166+
operator const SwiftResult &() const { return getSwiftResult(); }
105167
};
106168

107169
/// Storage sink for \c Completion objects.
@@ -123,9 +185,9 @@ struct CompletionSink {
123185

124186
class CompletionBuilder {
125187
CompletionSink &sink;
126-
SwiftResult &current;
188+
const SwiftResult &base;
127189
bool modified = false;
128-
Completion::ExpectedTypeRelation typeRelation;
190+
SwiftResult::ExpectedTypeRelation typeRelation;
129191
SemanticContextKind semanticContext;
130192
CodeCompletionFlair flair;
131193
CodeCompletionString *completionString;
@@ -135,7 +197,7 @@ class CompletionBuilder {
135197
PopularityFactor popularityFactor;
136198

137199
public:
138-
CompletionBuilder(CompletionSink &sink, SwiftResult &base);
200+
CompletionBuilder(CompletionSink &sink, const SwiftResult &base);
139201

140202
void setCustomKind(void *opaqueCustomKind) { customKind = opaqueCustomKind; }
141203

@@ -144,7 +206,7 @@ class CompletionBuilder {
144206
moduleImportDepth = value;
145207
}
146208

147-
void setExpectedTypeRelation(Completion::ExpectedTypeRelation Relation) {
209+
void setExpectedTypeRelation(SwiftResult::ExpectedTypeRelation Relation) {
148210
modified = true;
149211
typeRelation = Relation;
150212
}
@@ -233,8 +295,8 @@ struct FilterRules {
233295
llvm::StringMap<bool> hideByFilterName;
234296
llvm::StringMap<bool> hideByDescription;
235297

236-
bool hideCompletion(Completion *completion) const;
237-
bool hideCompletion(SwiftResult *completion, StringRef name,
298+
bool hideCompletion(const Completion &completion) const;
299+
bool hideCompletion(const SwiftResult &completion, StringRef name,
238300
StringRef description, void *customKind = nullptr) const;
239301
bool hideFilterName(StringRef name) const;
240302
};

0 commit comments

Comments
 (0)