14
14
#define SWIFT_IDE_COMPLETIONINSTANCE_H
15
15
16
16
#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"
17
21
#include " llvm/ADT/Hashing.h"
18
22
#include " llvm/ADT/IntrusiveRefCntPtr.h"
19
23
#include " llvm/ADT/StringRef.h"
@@ -35,6 +39,40 @@ makeCodeCompletionMemoryBuffer(const llvm::MemoryBuffer *origBuf,
35
39
unsigned &Offset,
36
40
llvm::StringRef bufferIdentifier);
37
41
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
+
38
76
// / Manages \c CompilerInstance for completion like operations.
39
77
class CompletionInstance {
40
78
struct Options {
@@ -58,27 +96,49 @@ class CompletionInstance {
58
96
59
97
// / Calls \p Callback with cached \c CompilerInstance if it's usable for the
60
98
// / 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.
64
104
bool performCachedOperationIfPossible (
65
105
llvm::hash_code ArgsHash,
66
106
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
67
107
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
68
108
DiagnosticConsumer *DiagC,
69
- llvm::function_ref<void (CompilerInstance &, bool )> Callback);
109
+ llvm::function_ref<void (CancellableResult<CompletionInstanceResult>)>
110
+ Callback);
70
111
71
112
// / Calls \p Callback with new \c CompilerInstance for the completion
72
113
// / request. The \c CompilerInstace passed to the callback already performed
73
114
// / the first pass.
74
115
// / Returns \c false if it fails to setup the \c CompilerInstance.
75
- bool performNewOperation (
116
+ void performNewOperation (
76
117
llvm::Optional<llvm::hash_code> ArgsHash,
77
118
swift::CompilerInvocation &Invocation,
78
119
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
79
120
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);
82
142
83
143
public:
84
144
CompletionInstance () : CachedCIShouldBeInvalidated(false ) {}
@@ -90,22 +150,28 @@ class CompletionInstance {
90
150
// Update options with \c NewOpts. (Thread safe.)
91
151
void setOptions (Options NewOpts);
92
152
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 (
104
169
swift::CompilerInvocation &Invocation, llvm::ArrayRef<const char *> Args,
105
170
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem,
106
171
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);
109
175
};
110
176
111
177
} // namespace ide
0 commit comments