1414#define SWIFT_IDE_COMPLETIONINSTANCE_H
1515
1616#include " swift/Frontend/Frontend.h"
17+ #include " swift/IDE/CancellableResult.h"
18+ #include " swift/IDE/CodeCompletion.h"
19+ #include " swift/IDE/ConformingMethodList.h"
20+ #include " swift/IDE/TypeContextInfo.h"
1721#include " llvm/ADT/Hashing.h"
1822#include " llvm/ADT/IntrusiveRefCntPtr.h"
1923#include " llvm/ADT/StringRef.h"
@@ -35,6 +39,40 @@ makeCodeCompletionMemoryBuffer(const llvm::MemoryBuffer *origBuf,
3539 unsigned &Offset,
3640 llvm::StringRef bufferIdentifier);
3741
42+ // / The result returned via the callback from the perform*Operation methods.
43+ struct CompletionInstanceResult {
44+ // / The compiler instance that is prepared for the second pass.
45+ CompilerInstance &CI;
46+ // / Whether an AST was reused.
47+ bool DidReuseAST;
48+ // / Whether the CompletionInstance found a code completion token in the source
49+ // / file. If this is \c false, the user will most likely want to return empty
50+ // / results.
51+ bool DidFindCodeCompletionToken;
52+ };
53+
54+ // / The results returned from \c CompletionInstance::codeComplete.
55+ struct CodeCompleteResult {
56+ MutableArrayRef<CodeCompletionResult *> Results;
57+ SwiftCompletionInfo &Info;
58+ };
59+
60+ // / The results returned from \c CompletionInstance::typeContextInfo.
61+ struct TypeContextInfoResult {
62+ // / The actual results. If empty, no results were found.
63+ ArrayRef<TypeContextInfoItem> Results;
64+ // / Whether an AST was reused to produce the results.
65+ bool DidReuseAST;
66+ };
67+
68+ // / The results returned from \c CompletionInstance::conformingMethodList.
69+ struct ConformingMethodListResults {
70+ // / The actual results. If \c nullptr, no results were found.
71+ const ConformingMethodListResult *Result;
72+ // / Whether an AST was reused for the completion.
73+ bool DidReuseAST;
74+ };
75+
3876// / Manages \c CompilerInstance for completion like operations.
3977class CompletionInstance {
4078 struct Options {
@@ -58,27 +96,49 @@ class CompletionInstance {
5896
5997 // / Calls \p Callback with cached \c CompilerInstance if it's usable for the
6098 // / specified completion request.
61- // / Returns \c if the callback was called. Returns \c false if the compiler
62- // / argument has changed, primary file is not the same, the \c Offset is not
63- // / in function bodies, or the interface hash of the file has changed.
99+ // / Returns \c true if performing the cached operation was possible. Returns
100+ // / \c false if the compiler argument has changed, primary file is not the
101+ // / same, the \c Offset is not in function bodies, or the interface hash of
102+ // / the file has changed.
103+ // / \p Callback will be called if and only if this function returns \c true.
64104 bool performCachedOperationIfPossible (
65105 llvm::hash_code ArgsHash,
66106 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
67107 llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
68108 DiagnosticConsumer *DiagC,
69- llvm::function_ref<void (CompilerInstance &, bool )> Callback);
109+ llvm::function_ref<void (CancellableResult<CompletionInstanceResult>)>
110+ Callback);
70111
71112 // / Calls \p Callback with new \c CompilerInstance for the completion
72113 // / request. The \c CompilerInstace passed to the callback already performed
73114 // / the first pass.
74115 // / Returns \c false if it fails to setup the \c CompilerInstance.
75- bool performNewOperation (
116+ void performNewOperation (
76117 llvm::Optional<llvm::hash_code> ArgsHash,
77118 swift::CompilerInvocation &Invocation,
78119 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
79120 llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
80- std::string &Error, DiagnosticConsumer *DiagC,
81- llvm::function_ref<void (CompilerInstance &, bool )> Callback);
121+ DiagnosticConsumer *DiagC,
122+ llvm::function_ref<void (CancellableResult<CompletionInstanceResult>)>
123+ Callback);
124+
125+ // / Calls \p Callback with a \c CompilerInstance which is prepared for the
126+ // / second pass. \p Callback is resposible to perform the second pass on it.
127+ // / The \c CompilerInstance may be reused from the previous completions,
128+ // / and may be cached for the next completion.
129+ // / In case of failure or cancellation, the callback receives the
130+ // / corresponding failed or cancelled result.
131+ // /
132+ // / NOTE: \p Args is only used for checking the equaity of the invocation.
133+ // / Since this function assumes that it is already normalized, exact the same
134+ // / arguments including their order is considered as the same invocation.
135+ void performOperation (
136+ swift::CompilerInvocation &Invocation, llvm::ArrayRef<const char *> Args,
137+ llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
138+ llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
139+ DiagnosticConsumer *DiagC,
140+ llvm::function_ref<void (CancellableResult<CompletionInstanceResult>)>
141+ Callback);
82142
83143public:
84144 CompletionInstance () : CachedCIShouldBeInvalidated(false ) {}
@@ -90,22 +150,28 @@ class CompletionInstance {
90150 // Update options with \c NewOpts. (Thread safe.)
91151 void setOptions (Options NewOpts);
92152
93- // / Calls \p Callback with a \c CompilerInstance which is prepared for the
94- // / second pass. \p Callback is resposible to perform the second pass on it.
95- // / The \c CompilerInstance may be reused from the previous completions,
96- // / and may be cached for the next completion.
97- // / Return \c true if \p is successfully called, \c it fails. In failure
98- // / cases \p Error is populated with an error message.
99- // /
100- // / NOTE: \p Args is only used for checking the equaity of the invocation.
101- // / Since this function assumes that it is already normalized, exact the same
102- // / arguments including their order is considered as the same invocation.
103- bool performOperation (
153+ void codeComplete (
154+ swift::CompilerInvocation &Invocation, llvm::ArrayRef<const char *> Args,
155+ llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
156+ llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
157+ DiagnosticConsumer *DiagC, ide::CodeCompletionContext &CompletionContext,
158+ llvm::function_ref<void (CancellableResult<CodeCompleteResult>)> Callback);
159+
160+ void typeContextInfo (
161+ swift::CompilerInvocation &Invocation, llvm::ArrayRef<const char *> Args,
162+ llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
163+ llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
164+ DiagnosticConsumer *DiagC,
165+ llvm::function_ref<void (CancellableResult<TypeContextInfoResult>)>
166+ Callback);
167+
168+ void conformingMethodList (
104169 swift::CompilerInvocation &Invocation, llvm::ArrayRef<const char *> Args,
105170 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
106171 llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
107- std::string &Error, DiagnosticConsumer *DiagC,
108- llvm::function_ref<void (CompilerInstance &, bool )> Callback);
172+ DiagnosticConsumer *DiagC, ArrayRef<const char *> ExpectedTypeNames,
173+ llvm::function_ref<void (CancellableResult<ConformingMethodListResults>)>
174+ Callback);
109175};
110176
111177} // namespace ide
0 commit comments