Skip to content

[CodeCompletion] Don't suggest values before declaration #20677

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

Closed
Closed
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
5 changes: 5 additions & 0 deletions include/swift/Parse/CodeCompletionCallbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class CodeCompletionCallbacks {
ObjCSelectorContext CompleteExprSelectorContext = ObjCSelectorContext::None;

std::vector<Expr *> leadingSequenceExprs;
std::vector<VarDecl *> disabledVars;

public:
CodeCompletionCallbacks(Parser &P)
Expand Down Expand Up @@ -80,6 +81,10 @@ class CodeCompletionCallbacks {
leadingSequenceExprs.assign(exprs.begin(), exprs.end());
}

void setDisabledVars(ArrayRef<VarDecl *> vars) {
disabledVars.assign(vars.begin(), vars.end());
}

class InEnumElementRawValueRAII {
CodeCompletionCallbacks *Callbacks;

Expand Down
27 changes: 20 additions & 7 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5606,8 +5606,23 @@ void CodeCompletionCallbacksImpl::doneParsing() {
Lookup.setPreferFunctionReferencesToCalls();

auto DoPostfixExprBeginning = [&] (){
SourceLoc Loc = P.Context.SourceMgr.getCodeCompletionLoc();
Lookup.getValueCompletionsInDeclContext(Loc);
SourceManager &SM = P.Context.SourceMgr;
SourceLoc Loc = SM.getCodeCompletionLoc();
llvm::SmallPtrSet<ValueDecl *, 4> disabledVarSet(disabledVars.begin(),
disabledVars.end());
auto localFilter = [&](ValueDecl *VD, DeclVisibilityKind Kind) -> bool {
if (Kind != DeclVisibilityKind::LocalVariable)
return true;

if (disabledVarSet.count(VD))
return false;
if (isa<TypeDecl>(VD))
return true;
if (SM.isBeforeInBuffer(VD->getLoc(), Loc))
return true;
return false;
};
Lookup.getValueCompletionsInDeclContext(Loc, localFilter);
};

switch (Kind) {
Expand Down Expand Up @@ -5780,7 +5795,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {

case CompletionKind::CaseStmtBeginning: {
SourceLoc Loc = P.Context.SourceMgr.getCodeCompletionLoc();
Lookup.getValueCompletionsInDeclContext(Loc);
DoPostfixExprBeginning();
Lookup.getTypeContextEnumElementCompletions(Loc);
break;
}
Expand Down Expand Up @@ -5836,10 +5851,9 @@ void CodeCompletionCallbacksImpl::doneParsing() {
break;
}
case CompletionKind::AssignmentRHS : {
SourceLoc Loc = P.Context.SourceMgr.getCodeCompletionLoc();
if (auto destType = ParsedExpr->getType())
Lookup.setExpectedTypes(destType->getRValueType());
Lookup.getValueCompletionsInDeclContext(Loc, DefaultFilter);
DoPostfixExprBeginning();
break;
}
case CompletionKind::CallArg : {
Expand All @@ -5858,9 +5872,8 @@ void CodeCompletionCallbacksImpl::doneParsing() {
}

case CompletionKind::ReturnStmtExpr : {
SourceLoc Loc = P.Context.SourceMgr.getCodeCompletionLoc();
Lookup.setExpectedTypes(getReturnTypeFromContext(CurDeclContext));
Lookup.getValueCompletionsInDeclContext(Loc);
DoPostfixExprBeginning();
break;
}

Expand Down
5 changes: 4 additions & 1 deletion lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ ParserResult<Expr> Parser::parseExprImpl(Diag<> Message,

auto expr = parseExprSequence(Message, isExprBasic,
/*forConditionalDirective*/false);
if (expr.hasCodeCompletion())
if (expr.hasCodeCompletion()) {
if (CodeCompletion)
CodeCompletion->setDisabledVars(DisabledVars);
return expr;
}
if (expr.isNull())
return nullptr;

Expand Down
48 changes: 40 additions & 8 deletions test/IDE/complete_expr_postfix_begin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=IN_TUPLE_1 | %FileCheck %s -check-prefix=IN_TUPLE_1
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=IN_TUPLE_2 | %FileCheck %s -check-prefix=IN_TUPLE_2

// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_INIT_1 | %FileCheck %s -check-prefix=OWN_INIT_1
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_INIT_2 | %FileCheck %s -check-prefix=OWN_INIT_2
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_INIT_3 | %FileCheck %s -check-prefix=OWN_INIT_3
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_INIT_4 | %FileCheck %s -check-prefix=OWN_INIT_4
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_INIT_5 | %FileCheck %s -check-prefix=OWN_INIT_5
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_INIT_6 | %FileCheck %s -check-prefix=OWN_INIT_6
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=OWN_INIT_7 | %FileCheck %s -check-prefix=OWN_INIT_7

//
// Test code completion at the beginning of expr-postfix.
//
Expand Down Expand Up @@ -435,8 +443,7 @@ func testInForEach1(arg: Int) {
let after = 4
// IN_FOR_EACH_1-NOT: Decl[LocalVar]
// IN_FOR_EACH_1: Decl[LocalVar]/Local: local[#Int#];
// FIXME: shouldn't show 'after' here.
// IN_FOR_EACH_1: Decl[LocalVar]/Local: after[#Int#];
// IN_FOR_EACH_1-NOT: after
// IN_FOR_EACH_1: Decl[LocalVar]/Local: arg[#Int#];
// IN_FOR_EACH_1-NOT: Decl[LocalVar]
}
Expand All @@ -448,8 +455,7 @@ func testInForEach2(arg: Int) {
let after = 4
// IN_FOR_EACH_2-NOT: Decl[LocalVar]
// IN_FOR_EACH_2: Decl[LocalVar]/Local/TypeRelation[Identical]: local[#Int#];
// FIXME: shouldn't show 'after' here.
// IN_FOR_EACH_2: Decl[LocalVar]/Local/TypeRelation[Identical]: after[#Int#];
// IN_FOR_EACH_2-NOT: after
// IN_FOR_EACH_2: Decl[LocalVar]/Local/TypeRelation[Identical]: arg[#Int#];
// IN_FOR_EACH_2-NOT: Decl[LocalVar]
}
Expand All @@ -463,8 +469,7 @@ func testInForEach3(arg: Int) {
// IN_FOR_EACH_3: Decl[LocalVar]/Local: index[#Int#];
// IN_FOR_EACH_3-NOT: Decl[LocalVar]
// IN_FOR_EACH_3: Decl[LocalVar]/Local: local[#Int#];
// FIXME: shouldn't show 'after' here.
// IN_FOR_EACH_3: Decl[LocalVar]/Local: after[#Int#];
// IN_FOR_EACH_3-NOT: after
// IN_FOR_EACH_3: Decl[LocalVar]/Local: arg[#Int#];
// IN_FOR_EACH_3-NOT: Decl[LocalVar]
}
Expand Down Expand Up @@ -503,8 +508,7 @@ func testInForEach9(arg: Int) {
// NOTE: [Convertible] to AnyHashable.
// IN_FOR_EACH_4-NOT: Decl[LocalVar]
// IN_FOR_EACH_4: Decl[LocalVar]/Local/TypeRelation[Convertible]: local[#Int#];
// FIXME: shouldn't show 'after' here.
// IN_FOR_EACH_4: Decl[LocalVar]/Local/TypeRelation[Convertible]: after[#Int#];
// IN_FOR_EACH_4-NOT: after
// IN_FOR_EACH_4: Decl[LocalVar]/Local/TypeRelation[Convertible]: arg[#Int#];
// IN_FOR_EACH_4-NOT: Decl[LocalVar]
}
Expand Down Expand Up @@ -555,3 +559,31 @@ func testTuple(localInt: Int) {
// IN_TUPLE_2: Decl[LocalVar]/Local: localStr[#String#]; name=localStr
// IN_TUPLE_2: Decl[LocalVar]/Local/TypeRelation[Identical]: localInt[#Int#]; name=localInt
// IN_TUPLE_2: End completions

var ownInit1: Int = #^OWN_INIT_1^#
// OWN_INIT_1: Begin completions
// OWN_INIT_1-NOT: ownInit1
var ownInit2: () -> Void = { #^OWN_INIT_2^# }
// OWN_INIT_2: Begin completions
// OWN_INIT_2-NOT: ownInit2
struct OwnInitTester {
var ownInit3: Int = #^OWN_INIT_3^#
// OWN_INIT_3: Begin completions
// OWN_INIT_3-NOT: ownInit3
var ownInit4: () -> Void = { #^OWN_INIT_4^# }
// OWN_INIT_4: Begin completions
// OWN_INIT_4-NOT: ownInit4
}
func ownInitTesting() {
var ownInit5: Int = #^OWN_INIT_5^#
// OWN_INIT_5: Begin completions
// OWN_INIT_5-NOT: ownInit5
var ownInit6: () -> Void = { #^OWN_INIT_6^# }
// OWN_INIT_6: Begin completions
// OWN_INIT_6-NOT: ownInit6
}
func ownInitTestingShadow(ownInit7: Int) {
var ownInit7: Int = #^OWN_INIT_7^#
// OWN_INIT_7: Begin completions
// OWN_INIT_7: Decl[LocalVar]/Local/TypeRelation[Identical]: ownInit7[#Int#];
}