Skip to content

[Sema] Skip adding solutions to completion callback if needed #61162

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 1 commit into from
Sep 19, 2022
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
4 changes: 4 additions & 0 deletions include/swift/Sema/CompletionContextFinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ class CompletionContextFinder : public ASTWalker {
return CompletionNode.dyn_cast<const KeyPathExpr *>() != nullptr;
}

bool hasCompletion() const {
return !CompletionNode.isNull();
}

/// If we are completing in a key path, returns the \c KeyPath that contains
/// the code completion component.
const KeyPathExpr *getKeyPathContainingCompletionComponent() const {
Expand Down
8 changes: 5 additions & 3 deletions lib/Sema/BuilderTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2361,9 +2361,11 @@ Optional<BraceStmt *> TypeChecker::applyResultBuilderBodyTransform(
cs.solveForCodeCompletion(solutions);

CompletionContextFinder analyzer(func, func->getDeclContext());
filterSolutionsForCodeCompletion(solutions, analyzer);
for (const auto &solution : solutions) {
cs.getASTContext().CompletionCallback->sawSolution(solution);
if (analyzer.hasCompletion()) {
filterSolutionsForCodeCompletion(solutions, analyzer);
for (const auto &solution : solutions) {
cs.getASTContext().CompletionCallback->sawSolution(solution);
}
}
return nullptr;
}
Expand Down
1 change: 0 additions & 1 deletion lib/Sema/CompletionContextFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ CompletionContextFinder::walkToExprPost(Expr *E) {
}

size_t CompletionContextFinder::getKeyPathCompletionComponentIndex() const {
assert(hasCompletionKeyPathComponent());
size_t ComponentIndex = 0;
auto Components = getKeyPathContainingCompletionComponent()->getComponents();
for (auto &Component : Components) {
Expand Down
4 changes: 1 addition & 3 deletions lib/Sema/TypeCheckCodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,6 @@ static bool hasTypeForCompletion(Solution &solution,
if (contextAnalyzer.hasCompletionExpr()) {
return solution.hasType(contextAnalyzer.getCompletionExpr());
} else {
assert(contextAnalyzer.hasCompletionKeyPathComponent());
return solution.hasType(
contextAnalyzer.getKeyPathContainingCompletionComponent(),
contextAnalyzer.getKeyPathCompletionComponentIndex());
Expand Down Expand Up @@ -578,8 +577,7 @@ bool TypeChecker::typeCheckForCodeCompletion(

// If there was no completion expr (e.g. if the code completion location was
// among tokens that were skipped over during parser error recovery) bail.
if (!contextAnalyzer.hasCompletionExpr() &&
!contextAnalyzer.hasCompletionKeyPathComponent())
if (!contextAnalyzer.hasCompletion())
return false;

// Interpolation components are type-checked separately.
Expand Down