Skip to content

[5.1][CodeCompletion] Don't attach attr to decl if it's not on the same line #25707

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
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
2 changes: 1 addition & 1 deletion include/swift/Parse/CodeCompletionCallbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class CodeCompletionCallbacks {
virtual void completeAccessorBeginning(CodeCompletionExpr *E) {};

/// Complete the keyword in attribute, for instance, @available.
virtual void completeDeclAttrBeginning(bool Sil) {};
virtual void completeDeclAttrBeginning(bool Sil, bool isIndependent) {};

/// Complete the parameters in attribute, for instance, version specifier for
/// @available.
Expand Down
14 changes: 11 additions & 3 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
bool HasSpace = false;
bool ShouldCompleteCallPatternAfterParen = true;
bool PreferFunctionReferencesToCalls = false;
bool AttTargetIsIndependent = false;
Optional<DeclKind> AttTargetDK;
Optional<StmtKind> ParentStmtKind;

Expand Down Expand Up @@ -1353,7 +1354,12 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
void setAttrTargetDeclKind(Optional<DeclKind> DK) override {
if (DK == DeclKind::PatternBinding)
DK = DeclKind::Var;
AttTargetDK = DK;
else if (DK == DeclKind::Param)
// For params, consider the attribute is always for the decl.
AttTargetIsIndependent = false;

if (!AttTargetIsIndependent)
AttTargetDK = DK;
}

void completeExpr() override;
Expand All @@ -1375,7 +1381,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
void completeCaseStmtKeyword() override;
void completeCaseStmtBeginning() override;
void completeCaseStmtDotPrefix() override;
void completeDeclAttrBeginning(bool Sil) override;
void completeDeclAttrBeginning(bool Sil, bool isIndependent) override;
void completeDeclAttrParam(DeclAttrKind DK, int Index) override;
void completeInPrecedenceGroup(SyntaxKind SK) override;
void completeNominalMemberBeginning(
Expand Down Expand Up @@ -4612,10 +4618,12 @@ void CodeCompletionCallbacksImpl::completeDeclAttrParam(DeclAttrKind DK,
CurDeclContext = P.CurDeclContext;
}

void CodeCompletionCallbacksImpl::completeDeclAttrBeginning(bool Sil) {
void CodeCompletionCallbacksImpl::completeDeclAttrBeginning(
bool Sil, bool isIndependent) {
Kind = CompletionKind::AttributeBegin;
IsInSil = Sil;
CurDeclContext = P.CurDeclContext;
AttTargetIsIndependent = isIndependent;
}

void CodeCompletionCallbacksImpl::completeInPrecedenceGroup(SyntaxKind SK) {
Expand Down
9 changes: 7 additions & 2 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1753,8 +1753,13 @@ ParserStatus Parser::parseDeclAttribute(DeclAttributes &Attributes, SourceLoc At
Tok.isNot(tok::kw_inout)) {

if (Tok.is(tok::code_complete)) {
if (CodeCompletion)
CodeCompletion->completeDeclAttrBeginning(isInSILMode());
if (CodeCompletion) {
// If the next token is not on the same line, this attribute might be
// starting new declaration instead of adding attribute to existing
// decl.
auto isIndependent = peekToken().isAtStartOfLine();
CodeCompletion->completeDeclAttrBeginning(isInSILMode(), isIndependent);
}
consumeToken(tok::code_complete);
return makeParserCodeCompletionStatus();
}
Expand Down
61 changes: 42 additions & 19 deletions test/IDE/complete_decl_attribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ON_INIT | %FileCheck %s -check-prefix=ON_INIT
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ON_PROPERTY | %FileCheck %s -check-prefix=ON_PROPERTY
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ON_METHOD | %FileCheck %s -check-prefix=ON_METHOD
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ON_PARAM | %FileCheck %s -check-prefix=ON_PARAM
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ON_PARAM_1 | %FileCheck %s -check-prefix=ON_PARAM
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ON_PARAM_2 | %FileCheck %s -check-prefix=ON_PARAM
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ON_MEMBER_INDEPENDENT_1 | %FileCheck %s -check-prefix=ON_MEMBER_LAST
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ON_MEMBER_INDEPENDENT_2 | %FileCheck %s -check-prefix=ON_MEMBER_LAST
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ON_MEMBER_LAST | %FileCheck %s -check-prefix=ON_MEMBER_LAST
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=KEYWORD_INDEPENDENT_1 | %FileCheck %s -check-prefix=KEYWORD_LAST
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=KEYWORD_INDEPENDENT_2 | %FileCheck %s -check-prefix=KEYWORD_LAST
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=KEYWORD_LAST | %FileCheck %s -check-prefix=KEYWORD_LAST

struct MyStruct {}
Expand Down Expand Up @@ -44,8 +49,7 @@ struct MyStruct {}
// AVAILABILITY2-NEXT: Keyword/None: deprecated: [#Specify version number#]; name=deprecated{{$}}
// AVAILABILITY2-NEXT: End completions

@#^KEYWORD2^#
func method(){}
@#^KEYWORD2^# func method(){}

// KEYWORD2: Begin completions
// KEYWORD2-NEXT: Keyword/None: available[#Func Attribute#]; name=available{{$}}
Expand All @@ -63,8 +67,7 @@ func method(){}
// KEYWORD2: Decl[Struct]/CurrModule: MyStruct[#MyStruct#]; name=MyStruct
// KEYWORD2: End completions

@#^KEYWORD3^#
class C {}
@#^KEYWORD3^# class C {}

// KEYWORD3: Begin completions
// KEYWORD3-NEXT: Keyword/None: available[#Class Attribute#]; name=available{{$}}
Expand All @@ -81,12 +84,10 @@ class C {}
// KEYWORD3-NEXT: Keyword/None: _functionBuilder[#Class Attribute#]; name=_functionBuilder
// KEYWORD3-NEXT: End completions

@#^KEYWORD3_2^#IB
class C2 {}
@#^KEYWORD3_2^#IB class C2 {}
// Same as KEYWORD3.

@#^KEYWORD4^#
enum E {}
@#^KEYWORD4^# enum E {}
// KEYWORD4: Begin completions
// KEYWORD4-NEXT: Keyword/None: available[#Enum Attribute#]; name=available{{$}}
// KEYWORD4-NEXT: Keyword/None: objc[#Enum Attribute#]; name=objc{{$}}
Expand All @@ -98,8 +99,7 @@ enum E {}
// KEYWORD4-NEXT: End completions


@#^KEYWORD5^#
struct S{}
@#^KEYWORD5^# struct S{}
// KEYWORD5: Begin completions
// KEYWORD5-NEXT: Keyword/None: available[#Struct Attribute#]; name=available{{$}}
// KEYWORD5-NEXT: Keyword/None: dynamicCallable[#Struct Attribute#]; name=dynamicCallable
Expand All @@ -109,8 +109,7 @@ struct S{}
// KEYWORD5-NEXT: Keyword/None: _functionBuilder[#Struct Attribute#]; name=_functionBuilder
// KEYWORD5-NEXT: End completions

@#^ON_GLOBALVAR^#
var globalVar
@#^ON_GLOBALVAR^# var globalVar
// ON_GLOBALVAR: Begin completions
// ON_GLOBALVAR-DAG: Keyword/None: available[#Var Attribute#]; name=available
// ON_GLOBALVAR-DAG: Keyword/None: objc[#Var Attribute#]; name=objc
Expand All @@ -128,8 +127,7 @@ var globalVar
// ON_GLOBALVAR: End completions

struct _S {
@#^ON_INIT^#
init()
@#^ON_INIT^# init()
// ON_INIT: Begin completions
// ON_INIT-DAG: Keyword/None: available[#Constructor Attribute#]; name=available
// ON_INIT-DAG: Keyword/None: objc[#Constructor Attribute#]; name=objc
Expand All @@ -140,8 +138,7 @@ struct _S {
// ON_INIT-DAG: Keyword/None: discardableResult[#Constructor Attribute#]; name=discardableResult
// ON_INIT: End completions

@#^ON_PROPERTY^#
var foo
@#^ON_PROPERTY^# var foo
// ON_PROPERTY: Begin completions
// ON_PROPERTY-DAG: Keyword/None: available[#Var Attribute#]; name=available
// ON_PROPERTY-DAG: Keyword/None: objc[#Var Attribute#]; name=objc
Expand All @@ -159,7 +156,7 @@ struct _S {
// ON_PROPERTY-NOT: Decl[PrecedenceGroup]
// ON_PROPERTY: End completions

@#^ON_METHOD^#
@#^ON_METHOD^# private
func foo()
// ON_METHOD: Begin completions
// ON_METHOD-DAG: Keyword/None: available[#Func Attribute#]; name=available
Expand All @@ -177,13 +174,30 @@ struct _S {
// ON_METHOD: Decl[Struct]/CurrModule: MyStruct[#MyStruct#]; name=MyStruct
// ON_METHOD: End completions

func bar(@#^ON_PARAM^#)
func bar(@#^ON_PARAM_1^#)
// ON_PARAM: Begin completions
// ON_PARAM-NOT: Keyword
// ON_PARAM: Decl[Struct]/CurrModule: MyStruct[#MyStruct#]; name=MyStruct
// ON_PARAM-NOT: Keyword
// ON_PARAM: End completions

func bar(
@#^ON_PARAM_2^#

arg: Int
)
// Same as ON_PARAM.

@#^ON_MEMBER_INDEPENDENT_1^#

func dummy1() {}
// Same as ON_MEMBER_LAST.

@#^ON_MEMBER_INDEPENDENT_2^#
func dummy2() {}
// Same as ON_MEMBER_LAST.


@#^ON_MEMBER_LAST^#
// ON_MEMBER_LAST: Begin completions
// ON_MEMBER_LAST-DAG: Keyword/None: available[#Declaration Attribute#]; name=available
Expand Down Expand Up @@ -216,6 +230,15 @@ struct _S {
// ON_MEMBER_LAST: End completions
}

@#^KEYWORD_INDEPENDENT_1^#

func dummy1() {}
// Same as KEYWORD_LAST.

@#^KEYWORD_INDEPENDENT_2^#
func dummy2() {}
// Same as KEYWORD_LAST.

@#^KEYWORD_LAST^#

// KEYWORD_LAST: Begin completions
Expand Down
2 changes: 1 addition & 1 deletion test/IDE/complete_pound_decl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// MEMBER: End completions

// ATTR: Begin completions
// ATTR: available[#Func Attribute#]; name=available
// ATTR: available[#Declaration Attribute#]; name=available
// ATTR: End completions

// GLOBAL: Begin completions
Expand Down