@@ -204,19 +204,40 @@ doCodeCompletion(SourceFile &SF, StringRef EnteredCode, unsigned *BufferID,
204
204
205
205
Ctx.SourceMgr .setCodeCompletionPoint (*BufferID, CodeCompletionOffset);
206
206
207
- // Parse, typecheck and temporarily insert the incomplete code into the AST.
208
- const unsigned OriginalDeclCount = SF.getTopLevelDecls ().size ();
207
+ // Create a new module and file for the code completion buffer, similar to how
208
+ // we handle new lines of REPL input.
209
+ auto *newModule =
210
+ ModuleDecl::create (Ctx.getIdentifier (" REPL_Code_Completion" ), Ctx);
211
+ auto &newSF =
212
+ *new (Ctx) SourceFile (*newModule, SourceFileKind::REPL, *BufferID,
213
+ SourceFile::ImplicitModuleImportKind::None);
214
+ newModule->addFile (newSF);
215
+
216
+ // Import the last module.
217
+ auto *lastModule = SF.getParentModule ();
218
+ ModuleDecl::ImportedModule importOfLastModule{/* AccessPath*/ {}, lastModule};
219
+ newSF.addImports (SourceFile::ImportedModuleDesc (importOfLastModule,
220
+ SourceFile::ImportOptions ()));
221
+
222
+ // Carry over the private imports from the last module.
223
+ SmallVector<ModuleDecl::ImportedModule, 8 > imports;
224
+ lastModule->getImportedModules (imports,
225
+ ModuleDecl::ImportFilterKind::Private);
226
+ if (!imports.empty ()) {
227
+ SmallVector<SourceFile::ImportedModuleDesc, 8 > importsWithOptions;
228
+ for (auto &import : imports) {
229
+ importsWithOptions.emplace_back (
230
+ SourceFile::ImportedModuleDesc (import , SourceFile::ImportOptions ()));
231
+ }
232
+ newSF.addImports (importsWithOptions);
233
+ }
209
234
210
235
PersistentParserState PersistentState;
211
- parseIntoSourceFile (SF , *BufferID, &PersistentState);
212
- performTypeChecking (SF, OriginalDeclCount );
236
+ parseIntoSourceFile (newSF , *BufferID, &PersistentState);
237
+ performTypeChecking (newSF );
213
238
214
239
performCodeCompletionSecondPass (PersistentState, *CompletionCallbacksFactory);
215
240
216
- // Now we are done with code completion. Remove the declarations we
217
- // temporarily inserted.
218
- SF.truncateTopLevelDecls (OriginalDeclCount);
219
-
220
241
// Reset the error state because it's only relevant to the code that we just
221
242
// processed, which now gets thrown away.
222
243
Ctx.Diags .resetHadAnyError ();
0 commit comments