Skip to content

Commit 72cadec

Browse files
committed
[CodeCompletion] Split result delivery into a result colleciton and consumer phase
This will allow us to run two different completion kinds and deliver results from both of them. Also: Compute a unified type context for global lookup. Previously, we always used the expected type context of the last lookup. But really, we should be considering all possible types from all constraint system solutions when computing code completion results from the cache.
1 parent 2d171bd commit 72cadec

22 files changed

+261
-242
lines changed

include/swift/IDE/AfterPoundExprCompletion.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ class AfterPoundExprCompletion : public TypeCheckCompletionCallback {
4747
: CompletionExpr(CompletionExpr), DC(DC), ParentStmtKind(ParentStmtKind) {
4848
}
4949

50-
void deliverResults(ide::CodeCompletionContext &CompletionCtx,
51-
CodeCompletionConsumer &Consumer);
50+
void collectResults(ide::CodeCompletionContext &CompletionCtx);
5251
};
5352

5453
} // end namespace ide

include/swift/IDE/ArgumentCompletion.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,8 @@ class ArgumentTypeCheckCompletionCallback : public TypeCheckCompletionCallback {
9393
/// function signature instead of suggesting individual labels. Used when
9494
/// completing after the opening '(' of a function call \param Loc The
9595
/// location of the code completion token
96-
void deliverResults(bool IncludeSignature, SourceLoc Loc, DeclContext *DC,
97-
CodeCompletionContext &CompletionCtx,
98-
CodeCompletionConsumer &Consumer);
96+
void collectResults(bool IncludeSignature, SourceLoc Loc, DeclContext *DC,
97+
CodeCompletionContext &CompletionCtx);
9998
};
10099

101100
} // end namespace ide

include/swift/IDE/CodeCompletion.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ void postProcessCompletionResults(
6565
MutableArrayRef<CodeCompletionResult *> results, CompletionKind Kind,
6666
const DeclContext *DC, CodeCompletionResultSink *Sink);
6767

68-
void deliverCompletionResults(CodeCompletionContext &CompletionContext,
68+
void collectCompletionResults(CodeCompletionContext &CompletionContext,
6969
CompletionLookup &Lookup, DeclContext *DC,
70-
CodeCompletionConsumer &Consumer);
70+
const ExpectedTypeContext &TypeContext,
71+
bool CanCurrDeclContextHandleAsync);
7172

7273
/// Create a factory for code completion callbacks.
7374
IDEInspectionCallbacksFactory *

include/swift/IDE/CodeCompletionConsumer.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,9 @@ namespace ide {
2222
struct RequestedCachedModule;
2323

2424
/// An abstract base class for consumers of code completion results.
25-
/// \see \c SimpleCachingCodeCompletionConsumer.
2625
class CodeCompletionConsumer {
2726
public:
2827
virtual ~CodeCompletionConsumer() {}
29-
virtual void
30-
handleResultsAndModules(CodeCompletionContext &context,
31-
ArrayRef<RequestedCachedModule> requestedModules,
32-
const ExpectedTypeContext *TypeContext,
33-
const DeclContext *DC,
34-
bool CanCurrDeclContextHandleAsync) = 0;
35-
};
36-
37-
/// A simplified code completion consumer interface that clients can use to get
38-
/// CodeCompletionResults with automatic caching of top-level completions from
39-
/// imported modules.
40-
struct SimpleCachingCodeCompletionConsumer : public CodeCompletionConsumer {
41-
42-
// Implement the CodeCompletionConsumer interface.
43-
void handleResultsAndModules(CodeCompletionContext &context,
44-
ArrayRef<RequestedCachedModule> requestedModules,
45-
const ExpectedTypeContext *TypeContext,
46-
const DeclContext *DCForModules,
47-
bool CanCurrDeclContextHandleAsync) override;
48-
4928
/// Clients should override this method to receive \p Results.
5029
virtual void handleResults(CodeCompletionContext &context) = 0;
5130
};

include/swift/IDE/CodeCompletionContext.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace swift {
2020
namespace ide {
2121

2222
class CodeCompletionCache;
23+
struct RequestedCachedModule;
2324

2425
class CodeCompletionContext {
2526
friend class CodeCompletionResultBuilder;
@@ -103,6 +104,13 @@ class CodeCompletionContext {
103104
sortCompletionResults(ArrayRef<CodeCompletionResult *> Results);
104105

105106
CodeCompletionResultSink &getResultSink() { return CurrentResults; }
107+
108+
/// Add code completion results from the given requested modules to this
109+
/// context.
110+
void addResultsFromModules(ArrayRef<RequestedCachedModule> RequestedModules,
111+
const ExpectedTypeContext &TypeContext,
112+
const DeclContext *DC,
113+
bool CanCurrDeclContextHandleAsync);
106114
};
107115

108116
} // end namespace ide

include/swift/IDE/CodeCompletionResultType.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,26 @@ class ExpectedTypeContext {
7474
}
7575
}
7676

77+
/// Form a union of this expected type context with \p Other.
78+
///
79+
/// Any possible type from either type context will be considered a possible
80+
/// type in the merged type context.
81+
void merge(const ExpectedTypeContext &Other) {
82+
PossibleTypes.append(Other.PossibleTypes);
83+
84+
// We can't merge ideal types. Setting to a null type is the best thing we
85+
// can do if they differ.
86+
if (IdealType.isNull() != Other.IdealType.isNull()) {
87+
IdealType = Type();
88+
} else if (IdealType && Other.IdealType &&
89+
!IdealType->isEqual(Other.IdealType)) {
90+
IdealType = Type();
91+
}
92+
IsImplicitSingleExpressionReturn |= Other.IsImplicitSingleExpressionReturn;
93+
PreferNonVoid &= Other.PreferNonVoid;
94+
ExpectedCustomAttributeKinds |= Other.ExpectedCustomAttributeKinds;
95+
}
96+
7797
Type getIdealType() const { return IdealType; }
7898

7999
void setIdealType(Type IdealType) { this->IdealType = IdealType; }

include/swift/IDE/ExprCompletion.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,8 @@ class ExprTypeCheckCompletionCallback : public TypeCheckCompletionCallback {
9090
AddUnresolvedMemberCompletions(AddUnresolvedMemberCompletions) {}
9191

9292
/// \param CCLoc The location of the code completion token.
93-
void deliverResults(SourceLoc CCLoc,
94-
ide::CodeCompletionContext &CompletionCtx,
95-
CodeCompletionConsumer &Consumer);
93+
void collectResults(SourceLoc CCLoc,
94+
ide::CodeCompletionContext &CompletionCtx);
9695
};
9796

9897
} // end namespace ide

include/swift/IDE/KeyPathCompletion.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ class KeyPathTypeCheckCompletionCallback : public TypeCheckCompletionCallback {
3737
public:
3838
KeyPathTypeCheckCompletionCallback(KeyPathExpr *KeyPath) : KeyPath(KeyPath) {}
3939

40-
void deliverResults(DeclContext *DC, SourceLoc DotLoc,
41-
ide::CodeCompletionContext &CompletionCtx,
42-
CodeCompletionConsumer &Consumer);
40+
void collectResults(DeclContext *DC, SourceLoc DotLoc,
41+
ide::CodeCompletionContext &CompletionCtx);
4342
};
4443

4544
} // end namespace ide

include/swift/IDE/PostfixCompletion.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,9 @@ class PostfixCompletionCallback : public TypeCheckCompletionCallback {
103103
/// \p DotLoc is invalid
104104
/// \param HasLeadingSpace Whether there is a space separating the exiting
105105
/// expression and the code completion token.
106-
void deliverResults(SourceLoc DotLoc, bool IsInSelector,
106+
void collectResults(SourceLoc DotLoc, bool IsInSelector,
107107
bool IncludeOperators, bool HasLeadingSpace,
108-
CodeCompletionContext &CompletionCtx,
109-
CodeCompletionConsumer &Consumer);
108+
CodeCompletionContext &CompletionCtx);
110109
};
111110

112111
} // end namespace ide

include/swift/IDE/UnresolvedMemberCompletion.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,8 @@ class UnresolvedMemberTypeCheckCompletionCallback
5959
CodeCompletionExpr *CompletionExpr, DeclContext *DC)
6060
: CompletionExpr(CompletionExpr), DC(DC) {}
6161

62-
void deliverResults(DeclContext *DC, SourceLoc DotLoc,
63-
ide::CodeCompletionContext &CompletionCtx,
64-
CodeCompletionConsumer &Consumer);
62+
void collectResults(DeclContext *DC, SourceLoc DotLoc,
63+
ide::CodeCompletionContext &CompletionCtx);
6564
};
6665

6766
} // end namespace ide

0 commit comments

Comments
 (0)