Skip to content

[CodeCompletion] Revert: "Restrict ancestor search to brace" #20323

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
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
39 changes: 10 additions & 29 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5241,7 +5241,6 @@ namespace {
friend class CodeCompletionTypeContextAnalyzer;
Expr *ChildExpr;
llvm::function_ref<bool(ParentTy, ParentTy)> Predicate;
bool AncestorsWithinBrace;

bool arePositionsSame(Expr *E1, Expr *E2) {
return E1->getSourceRange().Start == E2->getSourceRange().Start &&
Expand All @@ -5250,15 +5249,9 @@ namespace {

public:
llvm::SmallVector<ParentTy, 5> Ancestors;
llvm::SmallVector<size_t, 1> FarthestAncestorIndex;
ParentTy ParentClosest;
ParentTy ParentFarthest;
ExprParentFinder(Expr* ChildExpr,
llvm::function_ref<bool(ParentTy, ParentTy)> Predicate,
bool AncestorsWithinBrace = false) :
ChildExpr(ChildExpr), Predicate(Predicate),
AncestorsWithinBrace(AncestorsWithinBrace),
FarthestAncestorIndex({0}) {}
llvm::function_ref<bool(ParentTy, ParentTy)> Predicate) :
ChildExpr(ChildExpr), Predicate(Predicate) {}

std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
// Finish if we found the target. 'ChildExpr' might have been replaced
Expand All @@ -5270,13 +5263,6 @@ namespace {
Ancestors.push_back(E);
return { true, E };
}
if (E == ChildExpr || arePositionsSame(E, ChildExpr)) {
if (!Ancestors.empty()) {
ParentClosest = Ancestors.back();
ParentFarthest = Ancestors[FarthestAncestorIndex.back()];
}
return {false, nullptr};
}
return { true, E };
}

Expand All @@ -5289,14 +5275,10 @@ namespace {
std::pair<bool, Stmt *> walkToStmtPre(Stmt *S) override {
if (Predicate(S, Parent))
Ancestors.push_back(S);
if (AncestorsWithinBrace && isa<BraceStmt>(S))
FarthestAncestorIndex.push_back(Ancestors.size());
return { true, S };
}

Stmt *walkToStmtPost(Stmt *S) override {
if (AncestorsWithinBrace && isa<BraceStmt>(S))
FarthestAncestorIndex.pop_back();
if (Predicate(S, Parent))
Ancestors.pop_back();
return S;
Expand Down Expand Up @@ -5338,8 +5320,7 @@ class CodeCompletionTypeContextAnalyzer {
ExprParentFinder Finder;

public:
CodeCompletionTypeContextAnalyzer(DeclContext *DC, Expr *ParsedExpr,
bool AncestorsWithinBrace = false) : DC(DC),
CodeCompletionTypeContextAnalyzer(DeclContext *DC, Expr *ParsedExpr) : DC(DC),
ParsedExpr(ParsedExpr), SM(DC->getASTContext().SourceMgr),
Context(DC->getASTContext()),
Finder(ParsedExpr, [](ASTWalker::ParentTy Node, ASTWalker::ParentTy Parent) {
Expand Down Expand Up @@ -5389,7 +5370,7 @@ class CodeCompletionTypeContextAnalyzer {
}
} else
return false;
}, /*AncestorsWithinBrace=*/AncestorsWithinBrace) {}
}) {}

void analyzeExpr(Expr *Parent, llvm::function_ref<void(Type)> Callback,
SmallVectorImpl<StringRef> &PossibleNames) {
Expand Down Expand Up @@ -5647,7 +5628,8 @@ void CodeCompletionCallbacksImpl::doneParsing() {
if (isa<BindOptionalExpr>(ParsedExpr) || isa<ForceValueExpr>(ParsedExpr))
Lookup.setIsUnwrappedOptional(true);

::CodeCompletionTypeContextAnalyzer TypeAnalyzer(CurDeclContext, ParsedExpr);
::CodeCompletionTypeContextAnalyzer TypeAnalyzer(CurDeclContext,
ParsedExpr);
llvm::SmallVector<Type, 2> PossibleTypes;
if (TypeAnalyzer.Analyze(PossibleTypes)) {
Lookup.setExpectedTypes(PossibleTypes);
Expand Down Expand Up @@ -5692,7 +5674,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
case CompletionKind::ForEachSequence:
case CompletionKind::PostfixExprBeginning: {
::CodeCompletionTypeContextAnalyzer Analyzer(CurDeclContext,
CodeCompleteTokenExpr);
CodeCompleteTokenExpr);
llvm::SmallVector<Type, 1> Types;
if (Analyzer.Analyze(Types)) {
Lookup.setExpectedTypes(Types);
Expand Down Expand Up @@ -5720,7 +5702,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
Lookup.setHaveLParen(true);

::CodeCompletionTypeContextAnalyzer TypeAnalyzer(CurDeclContext,
CodeCompleteTokenExpr);
CodeCompleteTokenExpr);
SmallVector<Type, 2> PossibleTypes;
SmallVector<StringRef, 2> PossibleNames;
if (TypeAnalyzer.Analyze(PossibleTypes, PossibleNames)) {
Expand Down Expand Up @@ -5844,9 +5826,8 @@ void CodeCompletionCallbacksImpl::doneParsing() {
case CompletionKind::UnresolvedMember: {
Lookup.setHaveDot(DotLoc);
SmallVector<Type, 2> PossibleTypes;
::CodeCompletionTypeContextAnalyzer
TypeAnalyzer(CurDeclContext, CodeCompleteTokenExpr,
/*AncestorsWithinBrace=*/true);
::CodeCompletionTypeContextAnalyzer TypeAnalyzer(CurDeclContext,
CodeCompleteTokenExpr);
if (TypeAnalyzer.Analyze(PossibleTypes))
Lookup.setExpectedTypes(PossibleTypes);
Lookup.getUnresolvedMemberCompletions(PossibleTypes);
Expand Down