Skip to content

[CodeCompletion] Some more code cleanup #41601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions include/swift/IDE/DotExprCompletion.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,17 @@ class DotExprTypeCheckCompletionCallback : public TypeCheckCompletionCallback {
bool IsImplicitSingleExpressionReturn;
};

DeclContext *DC;
CodeCompletionExpr *CompletionExpr;
SmallVector<Result, 4> Results;
llvm::DenseMap<std::pair<Type, Decl *>, size_t> BaseToSolutionIdx;
bool GotCallback = false;

public:
DotExprTypeCheckCompletionCallback(DeclContext *DC,
CodeCompletionExpr *CompletionExpr)
: DC(DC), CompletionExpr(CompletionExpr) {}

/// True if at least one solution was passed via the \c sawSolution
/// callback.
bool gotCallback() const { return GotCallback; }
DotExprTypeCheckCompletionCallback(CodeCompletionExpr *CompletionExpr)
: CompletionExpr(CompletionExpr) {}

/// Typecheck the code completion expression in isolation, calling
/// \c sawSolution for each solution formed.
void fallbackTypeCheck();
void fallbackTypeCheck(DeclContext *DC) override;

void sawSolution(const constraints::Solution &solution) override;

Expand Down
9 changes: 0 additions & 9 deletions include/swift/IDE/UnresolvedMemberCompletion.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,12 @@ class UnresolvedMemberTypeCheckCompletionCallback
CodeCompletionExpr *CompletionExpr;
SmallVector<ExprResult, 4> ExprResults;
SmallVector<Type, 1> EnumPatternTypes;
bool GotCallback = false;

public:
UnresolvedMemberTypeCheckCompletionCallback(
CodeCompletionExpr *CompletionExpr)
: CompletionExpr(CompletionExpr) {}

/// True if at least one solution was passed via the \c sawSolution
/// callback.
bool gotCallback() const { return GotCallback; }

/// Typecheck the code completion expression in its outermost expression
/// context, calling \c sawSolution for each solution formed.
void fallbackTypeCheck(DeclContext *DC);

void sawSolution(const constraints::Solution &solution) override;

void deliverResults(DeclContext *DC, SourceLoc DotLoc,
Expand Down
17 changes: 15 additions & 2 deletions include/swift/Sema/CodeCompletionTypeChecking.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,24 @@ namespace swift {
}

class TypeCheckCompletionCallback {
bool GotCallback = false;

public:
virtual ~TypeCheckCompletionCallback() {}

/// Called for each solution produced while type-checking an expression
/// that the code completion expression participates in.
virtual void sawSolution(const constraints::Solution &solution) = 0;
virtual ~TypeCheckCompletionCallback() {}
virtual void sawSolution(const constraints::Solution &solution) {
GotCallback = true;
};

/// True if at least one solution was passed via the \c sawSolution
/// callback.
bool gotCallback() const { return GotCallback; }

/// Typecheck the code completion expression in its outermost expression
/// context, calling \c sawSolution for each solution formed.
virtual void fallbackTypeCheck(DeclContext *DC);
};
}

Expand Down
64 changes: 32 additions & 32 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,31 +132,6 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {

std::vector<std::pair<std::string, bool>> SubModuleNameVisibilityPairs;

void addSuperKeyword(CodeCompletionResultSink &Sink) {
auto *DC = CurDeclContext->getInnermostTypeContext();
if (!DC)
return;
auto *CD = DC->getSelfClassDecl();
if (!CD)
return;
Type ST = CD->getSuperclass();
if (ST.isNull() || ST->is<ErrorType>())
return;

CodeCompletionResultBuilder Builder(Sink, CodeCompletionResultKind::Keyword,
SemanticContextKind::CurrentNominal,
{});
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(CurDeclContext)) {
if (AFD->getOverriddenDecl() != nullptr) {
Builder.addFlair(CodeCompletionFlairBit::CommonKeywordAtCurrentPosition);
}
}

Builder.setKeywordKind(CodeCompletionKeywordKind::kw_super);
Builder.addKeyword("super");
Builder.addTypeAnnotation(ST, PrintOptions());
}

Optional<std::pair<Type, ConcreteDeclRef>> typeCheckParsedExpr() {
assert(ParsedExpr && "should have an expression");

Expand Down Expand Up @@ -879,6 +854,32 @@ static void addExprKeywords(CodeCompletionResultSink &Sink, DeclContext *DC) {
addKeyword(Sink, "await", CodeCompletionKeywordKind::None, "", flair);
}

static void addSuperKeyword(CodeCompletionResultSink &Sink, DeclContext *DC) {
if (!DC)
return;
auto *TC = DC->getInnermostTypeContext();
if (!TC)
return;
auto *CD = TC->getSelfClassDecl();
if (!CD)
return;
Type ST = CD->getSuperclass();
if (ST.isNull() || ST->is<ErrorType>())
return;

CodeCompletionResultBuilder Builder(Sink, CodeCompletionResultKind::Keyword,
SemanticContextKind::CurrentNominal, {});
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(DC)) {
if (AFD->getOverriddenDecl() != nullptr) {
Builder.addFlair(CodeCompletionFlairBit::CommonKeywordAtCurrentPosition);
}
}

Builder.setKeywordKind(CodeCompletionKeywordKind::kw_super);
Builder.addKeyword("super");
Builder.addTypeAnnotation(ST, PrintOptions());
}

static void addOpaqueTypeKeyword(CodeCompletionResultSink &Sink) {
addKeyword(Sink, "some", CodeCompletionKeywordKind::None, "some");
}
Expand Down Expand Up @@ -950,7 +951,7 @@ void CodeCompletionCallbacksImpl::addKeywords(CodeCompletionResultSink &Sink,
case CompletionKind::YieldStmtExpr:
case CompletionKind::PostfixExprBeginning:
case CompletionKind::ForEachSequence:
addSuperKeyword(Sink);
addSuperKeyword(Sink, CurDeclContext);
addLetVarKeywords(Sink);
addExprKeywords(Sink, CurDeclContext);
addAnyTypeKeyword(Sink, CurDeclContext->getASTContext().TheAnyType);
Expand Down Expand Up @@ -1333,8 +1334,7 @@ bool CodeCompletionCallbacksImpl::trySolverCompletion(bool MaybeFuncBody) {
assert(CodeCompleteTokenExpr);
assert(CurDeclContext);

DotExprTypeCheckCompletionCallback Lookup(CurDeclContext,
CodeCompleteTokenExpr);
DotExprTypeCheckCompletionCallback Lookup(CodeCompleteTokenExpr);
llvm::SaveAndRestore<TypeCheckCompletionCallback*>
CompletionCollector(Context.CompletionCallback, &Lookup);
typeCheckContextAt(CurDeclContext, CompletionLoc);
Expand All @@ -1345,7 +1345,7 @@ bool CodeCompletionCallbacksImpl::trySolverCompletion(bool MaybeFuncBody) {
// typechecking still resolve even these cases would be beneficial for
// tooling in general though.
if (!Lookup.gotCallback())
Lookup.fallbackTypeCheck();
Lookup.fallbackTypeCheck(CurDeclContext);

addKeywords(CompletionContext.getResultSink(), MaybeFuncBody);

Expand Down Expand Up @@ -1566,7 +1566,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
Lookup.setHaveLParen(false);

// Add any keywords that can be used in an argument expr position.
addSuperKeyword(CompletionContext.getResultSink());
addSuperKeyword(CompletionContext.getResultSink(), CurDeclContext);
addExprKeywords(CompletionContext.getResultSink(), CurDeclContext);

DoPostfixExprBeginning();
Expand Down Expand Up @@ -1712,7 +1712,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
ContextInfo.isImplicitSingleExpressionReturn());

// Add any keywords that can be used in an argument expr position.
addSuperKeyword(CompletionContext.getResultSink());
addSuperKeyword(CompletionContext.getResultSink(), CurDeclContext);
addExprKeywords(CompletionContext.getResultSink(), CurDeclContext);

DoPostfixExprBeginning();
Expand Down Expand Up @@ -1770,7 +1770,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
Context.LangOpts.EnableExperimentalConcurrency,
Context.LangOpts.EnableExperimentalDistributed);
addStmtKeywords(Sink, CurDeclContext, MaybeFuncBody);
addSuperKeyword(Sink);
addSuperKeyword(Sink, CurDeclContext);
addLetVarKeywords(Sink);
addExprKeywords(Sink, CurDeclContext);
addAnyTypeKeyword(Sink, Context.TheAnyType);
Expand Down
4 changes: 2 additions & 2 deletions lib/IDE/DotExprCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using namespace swift;
using namespace swift::constraints;
using namespace swift::ide;

void DotExprTypeCheckCompletionCallback::fallbackTypeCheck() {
void DotExprTypeCheckCompletionCallback::fallbackTypeCheck(DeclContext *DC) {
assert(!gotCallback());

// Default to checking the completion expression in isolation.
Expand All @@ -46,7 +46,7 @@ void DotExprTypeCheckCompletionCallback::fallbackTypeCheck() {

void DotExprTypeCheckCompletionCallback::sawSolution(
const constraints::Solution &S) {
GotCallback = true;
TypeCheckCompletionCallback::sawSolution(S);
auto &CS = S.getConstraintSystem();
auto *ParsedExpr = CompletionExpr->getBase();
auto *SemanticExpr = ParsedExpr->getSemanticsProvidingExpr();
Expand Down
2 changes: 2 additions & 0 deletions lib/IDE/KeyPathCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ using namespace swift::ide;

void KeyPathTypeCheckCompletionCallback::sawSolution(
const constraints::Solution &S) {
TypeCheckCompletionCallback::sawSolution(S);

// Determine the code completion.
size_t ComponentIndex = 0;
for (auto &Component : KeyPath->getComponents()) {
Expand Down
21 changes: 1 addition & 20 deletions lib/IDE/UnresolvedMemberCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static VarDecl *getMatchVarIfInPatternMatch(CodeCompletionExpr *CompletionExpr,

void UnresolvedMemberTypeCheckCompletionCallback::sawSolution(
const constraints::Solution &S) {
GotCallback = true;
TypeCheckCompletionCallback::sawSolution(S);

auto &CS = S.getConstraintSystem();
Type ExpectedTy = getTypeForCompletion(S, CompletionExpr);
Expand Down Expand Up @@ -119,25 +119,6 @@ void UnresolvedMemberTypeCheckCompletionCallback::sawSolution(
}
}

void UnresolvedMemberTypeCheckCompletionCallback::fallbackTypeCheck(
DeclContext *DC) {
assert(!gotCallback());

CompletionContextFinder finder(DC);
if (!finder.hasCompletionExpr())
return;

auto fallback = finder.getFallbackCompletionExpr();
if (!fallback)
return;

SolutionApplicationTarget completionTarget(fallback->E, fallback->DC,
CTP_Unused, Type(),
/*isDiscared=*/true);
typeCheckForCodeCompletion(completionTarget, /*needsPrecheck*/ true,
[&](const Solution &S) { sawSolution(S); });
}

void UnresolvedMemberTypeCheckCompletionCallback::deliverResults(
DeclContext *DC, SourceLoc DotLoc,
ide::CodeCompletionContext &CompletionCtx,
Expand Down
19 changes: 19 additions & 0 deletions lib/Sema/TypeCheckCodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -848,3 +848,22 @@ bool swift::isImplicitSingleExpressionReturn(ConstraintSystem &CS,
}
return false;
}

void TypeCheckCompletionCallback::fallbackTypeCheck(DeclContext *DC) {
assert(!GotCallback);

CompletionContextFinder finder(DC);
if (!finder.hasCompletionExpr())
return;

auto fallback = finder.getFallbackCompletionExpr();
if (!fallback)
return;

SolutionApplicationTarget completionTarget(fallback->E, fallback->DC,
CTP_Unused, Type(),
/*isDiscared=*/true);
TypeChecker::typeCheckForCodeCompletion(
completionTarget, /*needsPrecheck*/ true,
[&](const Solution &S) { sawSolution(S); });
}