Skip to content

Commit 48179ad

Browse files
committed
[CodeCompletion] Split CodeCompletionConsumer into its own file
1 parent faa8fd3 commit 48179ad

File tree

7 files changed

+206
-153
lines changed

7 files changed

+206
-153
lines changed

include/swift/IDE/CodeCompletion.h

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/Basic/OptionSet.h"
2121
#include "swift/Basic/StringExtras.h"
2222
#include "swift/Frontend/Frontend.h"
23+
#include "swift/IDE/CodeCompletionConsumer.h"
2324
#include "swift/IDE/CodeCompletionContext.h"
2425
#include "swift/IDE/CodeCompletionResult.h"
2526
#include "swift/IDE/CodeCompletionResultSink.h"
@@ -103,30 +104,10 @@ struct SwiftCompletionInfo {
103104
CodeCompletionContext *completionContext = nullptr;
104105
};
105106

106-
/// An abstract base class for consumers of code completion results.
107-
/// \see \c SimpleCachingCodeCompletionConsumer.
108-
class CodeCompletionConsumer {
109-
public:
110-
virtual ~CodeCompletionConsumer() {}
111-
virtual void
112-
handleResultsAndModules(CodeCompletionContext &context,
113-
ArrayRef<RequestedCachedModule> requestedModules,
114-
DeclContext *DC) = 0;
115-
};
116-
117-
/// A simplified code completion consumer interface that clients can use to get
118-
/// CodeCompletionResults with automatic caching of top-level completions from
119-
/// imported modules.
120-
struct SimpleCachingCodeCompletionConsumer : public CodeCompletionConsumer {
107+
void postProcessResults(MutableArrayRef<CodeCompletionResult *> results,
108+
CompletionKind Kind, DeclContext *DC,
109+
CodeCompletionResultSink *Sink);
121110

122-
// Implement the CodeCompletionConsumer interface.
123-
void handleResultsAndModules(CodeCompletionContext &context,
124-
ArrayRef<RequestedCachedModule> requestedModules,
125-
DeclContext *DCForModules) override;
126-
127-
/// Clients should override this method to receive \p Results.
128-
virtual void handleResults(CodeCompletionContext &context) = 0;
129-
};
130111

131112
/// Create a factory for code completion callbacks.
132113
CodeCompletionCallbacksFactory *
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===--- CodeCompletionConsumer.h -----------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_IDE_CODECOMPLETIONCONSUMER
14+
#define SWIFT_IDE_CODECOMPLETIONCONSUMER
15+
16+
#include "swift/IDE/CodeCompletionContext.h"
17+
#include "swift/Parse/CodeCompletionCallbacks.h"
18+
19+
namespace swift {
20+
namespace ide {
21+
22+
struct RequestedCachedModule;
23+
24+
/// An abstract base class for consumers of code completion results.
25+
/// \see \c SimpleCachingCodeCompletionConsumer.
26+
class CodeCompletionConsumer {
27+
public:
28+
virtual ~CodeCompletionConsumer() {}
29+
virtual void
30+
handleResultsAndModules(CodeCompletionContext &context,
31+
ArrayRef<RequestedCachedModule> requestedModules,
32+
DeclContext *DC) = 0;
33+
};
34+
35+
/// A simplified code completion consumer interface that clients can use to get
36+
/// CodeCompletionResults with automatic caching of top-level completions from
37+
/// imported modules.
38+
struct SimpleCachingCodeCompletionConsumer : public CodeCompletionConsumer {
39+
40+
// Implement the CodeCompletionConsumer interface.
41+
void handleResultsAndModules(CodeCompletionContext &context,
42+
ArrayRef<RequestedCachedModule> requestedModules,
43+
DeclContext *DCForModules) override;
44+
45+
/// Clients should override this method to receive \p Results.
46+
virtual void handleResults(CodeCompletionContext &context) = 0;
47+
};
48+
49+
} // end namespace ide
50+
} // end namespace swift
51+
52+
#endif // SWIFT_IDE_CODECOMPLETIONCONSUMER

include/swift/IDE/REPLCodeCompletion.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
#include "swift/Basic/LLVM.h"
2121
#include "swift/IDE/CodeCompletion.h"
2222
#include "swift/IDE/CodeCompletionCache.h"
23+
#include "swift/IDE/CodeCompletionConsumer.h"
2324
#include "swift/Parse/CodeCompletionCallbacks.h"
2425
#include "llvm/ADT/ArrayRef.h"
2526
#include "llvm/ADT/StringRef.h"
2627
#include <memory>
27-
#include <vector>
2828
#include <string>
2929
#include <utility>
30+
#include <vector>
3031

3132
namespace swift {
3233

lib/IDE/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ add_swift_host_library(swiftIDE STATIC
33
CodeCompletion.cpp
44
CodeCompletionCache.cpp
55
CodeCompletionContext.cpp
6+
CodeCompletionConsumer.cpp
67
CodeCompletionDiagnostics.cpp
78
CodeCompletionResultBuilder.cpp
89
CodeCompletionResultPrinter.cpp

lib/IDE/CodeCompletion.cpp

Lines changed: 4 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "swift/ClangImporter/ClangModule.h"
3434
#include "swift/Frontend/FrontendOptions.h"
3535
#include "swift/IDE/CodeCompletionCache.h"
36+
#include "swift/IDE/CodeCompletionConsumer.h"
3637
#include "swift/IDE/CodeCompletionResultPrinter.h"
3738
#include "swift/IDE/CodeCompletionStringPrinter.h"
3839
#include "swift/IDE/CompletionLookup.h"
@@ -1156,9 +1157,9 @@ static void addConditionalCompilationFlags(ASTContext &Ctx,
11561157
/// If \p Sink is passed, the pointer of the each result may be replaced with a
11571158
/// pointer to the new item allocated in \p Sink.
11581159
/// If \p Sink is nullptr, the pointee of each result may be modified in place.
1159-
static void postProcessResults(MutableArrayRef<CodeCompletionResult *> results,
1160-
CompletionKind Kind, DeclContext *DC,
1161-
CodeCompletionResultSink *Sink) {
1160+
void swift::ide::postProcessResults(
1161+
MutableArrayRef<CodeCompletionResult *> results, CompletionKind Kind,
1162+
DeclContext *DC, CodeCompletionResultSink *Sink) {
11621163
for (CodeCompletionResult *&result : results) {
11631164
bool modified = false;
11641165
auto flair = result->getFlair();
@@ -2065,132 +2066,6 @@ void swift::ide::lookupCodeCompletionResultsFromModule(
20652066
Lookup.lookupExternalModuleDecls(module, accessPath, needLeadingDot);
20662067
}
20672068

2068-
static MutableArrayRef<CodeCompletionResult *>
2069-
copyCodeCompletionResults(CodeCompletionResultSink &targetSink,
2070-
CodeCompletionCache::Value &source, bool onlyTypes,
2071-
bool onlyPrecedenceGroups) {
2072-
2073-
// We will be adding foreign results (from another sink) into TargetSink.
2074-
// TargetSink should have an owning pointer to the allocator that keeps the
2075-
// results alive.
2076-
targetSink.ForeignAllocators.push_back(source.Allocator);
2077-
auto startSize = targetSink.Results.size();
2078-
2079-
std::function<bool(const ContextFreeCodeCompletionResult *)>
2080-
shouldIncludeResult;
2081-
if (onlyTypes) {
2082-
shouldIncludeResult = [](const ContextFreeCodeCompletionResult *R) -> bool {
2083-
if (R->getKind() != CodeCompletionResultKind::Declaration)
2084-
return false;
2085-
switch (R->getAssociatedDeclKind()) {
2086-
case CodeCompletionDeclKind::Module:
2087-
case CodeCompletionDeclKind::Class:
2088-
case CodeCompletionDeclKind::Actor:
2089-
case CodeCompletionDeclKind::Struct:
2090-
case CodeCompletionDeclKind::Enum:
2091-
case CodeCompletionDeclKind::Protocol:
2092-
case CodeCompletionDeclKind::TypeAlias:
2093-
case CodeCompletionDeclKind::AssociatedType:
2094-
case CodeCompletionDeclKind::GenericTypeParam:
2095-
return true;
2096-
case CodeCompletionDeclKind::PrecedenceGroup:
2097-
case CodeCompletionDeclKind::EnumElement:
2098-
case CodeCompletionDeclKind::Constructor:
2099-
case CodeCompletionDeclKind::Destructor:
2100-
case CodeCompletionDeclKind::Subscript:
2101-
case CodeCompletionDeclKind::StaticMethod:
2102-
case CodeCompletionDeclKind::InstanceMethod:
2103-
case CodeCompletionDeclKind::PrefixOperatorFunction:
2104-
case CodeCompletionDeclKind::PostfixOperatorFunction:
2105-
case CodeCompletionDeclKind::InfixOperatorFunction:
2106-
case CodeCompletionDeclKind::FreeFunction:
2107-
case CodeCompletionDeclKind::StaticVar:
2108-
case CodeCompletionDeclKind::InstanceVar:
2109-
case CodeCompletionDeclKind::LocalVar:
2110-
case CodeCompletionDeclKind::GlobalVar:
2111-
return false;
2112-
}
2113-
2114-
llvm_unreachable("Unhandled CodeCompletionDeclKind in switch.");
2115-
};
2116-
} else if (onlyPrecedenceGroups) {
2117-
shouldIncludeResult = [](const ContextFreeCodeCompletionResult *R) -> bool {
2118-
return R->getAssociatedDeclKind() ==
2119-
CodeCompletionDeclKind::PrecedenceGroup;
2120-
};
2121-
} else {
2122-
shouldIncludeResult = [](const ContextFreeCodeCompletionResult *R) -> bool {
2123-
return true;
2124-
};
2125-
}
2126-
for (auto contextFreeResult : source.Results) {
2127-
if (!shouldIncludeResult(contextFreeResult)) {
2128-
continue;
2129-
}
2130-
auto contextualResult = new (*targetSink.Allocator) CodeCompletionResult(
2131-
*contextFreeResult, SemanticContextKind::OtherModule,
2132-
CodeCompletionFlair(),
2133-
/*numBytesToErase=*/0, /*TypeContext=*/nullptr, /*DC=*/nullptr,
2134-
ContextualNotRecommendedReason::None,
2135-
CodeCompletionDiagnosticSeverity::None, /*DiagnosticMessage=*/"");
2136-
targetSink.Results.push_back(contextualResult);
2137-
}
2138-
2139-
return llvm::makeMutableArrayRef(targetSink.Results.data() + startSize,
2140-
targetSink.Results.size() - startSize);
2141-
}
2142-
2143-
void SimpleCachingCodeCompletionConsumer::handleResultsAndModules(
2144-
CodeCompletionContext &context,
2145-
ArrayRef<RequestedCachedModule> requestedModules,
2146-
DeclContext *DC) {
2147-
2148-
// Use the current SourceFile as the DeclContext so that we can use it to
2149-
// perform qualified lookup, and to get the correct visibility for
2150-
// @testable imports. Also it cannot use 'DC' since it would apply decl
2151-
// context changes to cached results.
2152-
const SourceFile *SF = DC->getParentSourceFile();
2153-
2154-
for (auto &R : requestedModules) {
2155-
// FIXME(thread-safety): lock the whole AST context. We might load a
2156-
// module.
2157-
llvm::Optional<CodeCompletionCache::ValueRefCntPtr> V =
2158-
context.Cache.get(R.Key);
2159-
if (!V.hasValue()) {
2160-
// No cached results found. Fill the cache.
2161-
V = context.Cache.createValue();
2162-
// Temporary sink in which we gather the result. The cache value retains
2163-
// the sink's allocator.
2164-
CodeCompletionResultSink Sink;
2165-
Sink.annotateResult = context.getAnnotateResult();
2166-
Sink.addInitsToTopLevel = context.getAddInitsToTopLevel();
2167-
Sink.enableCallPatternHeuristics = context.getCallPatternHeuristics();
2168-
Sink.includeObjectLiterals = context.includeObjectLiterals();
2169-
Sink.addCallWithNoDefaultArgs = context.addCallWithNoDefaultArgs();
2170-
lookupCodeCompletionResultsFromModule(Sink, R.TheModule, R.Key.AccessPath,
2171-
R.Key.ResultsHaveLeadingDot, SF);
2172-
(*V)->Allocator = Sink.Allocator;
2173-
auto &CachedResults = (*V)->Results;
2174-
CachedResults.reserve(Sink.Results.size());
2175-
// Instead of copying the context free results out of the sink's allocator
2176-
// retain the sink's entire allocator (which also includes the contextual
2177-
// properities) and simply store pointers to the context free results that
2178-
// back the contextual results.
2179-
for (auto Result : Sink.Results) {
2180-
CachedResults.push_back(Result->getContextFreeResultPtr());
2181-
}
2182-
context.Cache.set(R.Key, *V);
2183-
}
2184-
assert(V.hasValue());
2185-
auto newItems = copyCodeCompletionResults(
2186-
context.getResultSink(), **V, R.OnlyTypes, R.OnlyPrecedenceGroups);
2187-
postProcessResults(newItems, context.CodeCompletionKind, DC,
2188-
&context.getResultSink());
2189-
}
2190-
2191-
handleResults(context);
2192-
}
2193-
21942069
//===----------------------------------------------------------------------===//
21952070
// ImportDepth
21962071
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)