Skip to content

Commit ae0aeaf

Browse files
authored
Merge pull request #25707 from rintaro/5.1-ide-completion-declattr-independent-rdar50441643
[5.1][CodeCompletion] Don't attach attr to decl if it's not on the same line
2 parents 8bd32dd + 34e52bf commit ae0aeaf

File tree

5 files changed

+62
-26
lines changed

5 files changed

+62
-26
lines changed

include/swift/Parse/CodeCompletionCallbacks.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class CodeCompletionCallbacks {
189189
virtual void completeAccessorBeginning(CodeCompletionExpr *E) {};
190190

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

194194
/// Complete the parameters in attribute, for instance, version specifier for
195195
/// @available.

lib/IDE/CodeCompletion.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
12631263
bool HasSpace = false;
12641264
bool ShouldCompleteCallPatternAfterParen = true;
12651265
bool PreferFunctionReferencesToCalls = false;
1266+
bool AttTargetIsIndependent = false;
12661267
Optional<DeclKind> AttTargetDK;
12671268
Optional<StmtKind> ParentStmtKind;
12681269

@@ -1353,7 +1354,12 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
13531354
void setAttrTargetDeclKind(Optional<DeclKind> DK) override {
13541355
if (DK == DeclKind::PatternBinding)
13551356
DK = DeclKind::Var;
1356-
AttTargetDK = DK;
1357+
else if (DK == DeclKind::Param)
1358+
// For params, consider the attribute is always for the decl.
1359+
AttTargetIsIndependent = false;
1360+
1361+
if (!AttTargetIsIndependent)
1362+
AttTargetDK = DK;
13571363
}
13581364

13591365
void completeExpr() override;
@@ -1375,7 +1381,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
13751381
void completeCaseStmtKeyword() override;
13761382
void completeCaseStmtBeginning() override;
13771383
void completeCaseStmtDotPrefix() override;
1378-
void completeDeclAttrBeginning(bool Sil) override;
1384+
void completeDeclAttrBeginning(bool Sil, bool isIndependent) override;
13791385
void completeDeclAttrParam(DeclAttrKind DK, int Index) override;
13801386
void completeInPrecedenceGroup(SyntaxKind SK) override;
13811387
void completeNominalMemberBeginning(
@@ -4612,10 +4618,12 @@ void CodeCompletionCallbacksImpl::completeDeclAttrParam(DeclAttrKind DK,
46124618
CurDeclContext = P.CurDeclContext;
46134619
}
46144620

4615-
void CodeCompletionCallbacksImpl::completeDeclAttrBeginning(bool Sil) {
4621+
void CodeCompletionCallbacksImpl::completeDeclAttrBeginning(
4622+
bool Sil, bool isIndependent) {
46164623
Kind = CompletionKind::AttributeBegin;
46174624
IsInSil = Sil;
46184625
CurDeclContext = P.CurDeclContext;
4626+
AttTargetIsIndependent = isIndependent;
46194627
}
46204628

46214629
void CodeCompletionCallbacksImpl::completeInPrecedenceGroup(SyntaxKind SK) {

lib/Parse/ParseDecl.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,8 +1753,13 @@ ParserStatus Parser::parseDeclAttribute(DeclAttributes &Attributes, SourceLoc At
17531753
Tok.isNot(tok::kw_inout)) {
17541754

17551755
if (Tok.is(tok::code_complete)) {
1756-
if (CodeCompletion)
1757-
CodeCompletion->completeDeclAttrBeginning(isInSILMode());
1756+
if (CodeCompletion) {
1757+
// If the next token is not on the same line, this attribute might be
1758+
// starting new declaration instead of adding attribute to existing
1759+
// decl.
1760+
auto isIndependent = peekToken().isAtStartOfLine();
1761+
CodeCompletion->completeDeclAttrBeginning(isInSILMode(), isIndependent);
1762+
}
17581763
consumeToken(tok::code_complete);
17591764
return makeParserCodeCompletionStatus();
17601765
}

test/IDE/complete_decl_attribute.swift

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ON_INIT | %FileCheck %s -check-prefix=ON_INIT
1010
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ON_PROPERTY | %FileCheck %s -check-prefix=ON_PROPERTY
1111
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ON_METHOD | %FileCheck %s -check-prefix=ON_METHOD
12-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ON_PARAM | %FileCheck %s -check-prefix=ON_PARAM
12+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ON_PARAM_1 | %FileCheck %s -check-prefix=ON_PARAM
13+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ON_PARAM_2 | %FileCheck %s -check-prefix=ON_PARAM
14+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ON_MEMBER_INDEPENDENT_1 | %FileCheck %s -check-prefix=ON_MEMBER_LAST
15+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ON_MEMBER_INDEPENDENT_2 | %FileCheck %s -check-prefix=ON_MEMBER_LAST
1316
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ON_MEMBER_LAST | %FileCheck %s -check-prefix=ON_MEMBER_LAST
17+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=KEYWORD_INDEPENDENT_1 | %FileCheck %s -check-prefix=KEYWORD_LAST
18+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=KEYWORD_INDEPENDENT_2 | %FileCheck %s -check-prefix=KEYWORD_LAST
1419
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=KEYWORD_LAST | %FileCheck %s -check-prefix=KEYWORD_LAST
1520

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

47-
@#^KEYWORD2^#
48-
func method(){}
52+
@#^KEYWORD2^# func method(){}
4953

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

66-
@#^KEYWORD3^#
67-
class C {}
70+
@#^KEYWORD3^# class C {}
6871

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

84-
@#^KEYWORD3_2^#IB
85-
class C2 {}
87+
@#^KEYWORD3_2^#IB class C2 {}
8688
// Same as KEYWORD3.
8789

88-
@#^KEYWORD4^#
89-
enum E {}
90+
@#^KEYWORD4^# enum E {}
9091
// KEYWORD4: Begin completions
9192
// KEYWORD4-NEXT: Keyword/None: available[#Enum Attribute#]; name=available{{$}}
9293
// KEYWORD4-NEXT: Keyword/None: objc[#Enum Attribute#]; name=objc{{$}}
@@ -98,8 +99,7 @@ enum E {}
9899
// KEYWORD4-NEXT: End completions
99100

100101

101-
@#^KEYWORD5^#
102-
struct S{}
102+
@#^KEYWORD5^# struct S{}
103103
// KEYWORD5: Begin completions
104104
// KEYWORD5-NEXT: Keyword/None: available[#Struct Attribute#]; name=available{{$}}
105105
// KEYWORD5-NEXT: Keyword/None: dynamicCallable[#Struct Attribute#]; name=dynamicCallable
@@ -109,8 +109,7 @@ struct S{}
109109
// KEYWORD5-NEXT: Keyword/None: _functionBuilder[#Struct Attribute#]; name=_functionBuilder
110110
// KEYWORD5-NEXT: End completions
111111

112-
@#^ON_GLOBALVAR^#
113-
var globalVar
112+
@#^ON_GLOBALVAR^# var globalVar
114113
// ON_GLOBALVAR: Begin completions
115114
// ON_GLOBALVAR-DAG: Keyword/None: available[#Var Attribute#]; name=available
116115
// ON_GLOBALVAR-DAG: Keyword/None: objc[#Var Attribute#]; name=objc
@@ -128,8 +127,7 @@ var globalVar
128127
// ON_GLOBALVAR: End completions
129128

130129
struct _S {
131-
@#^ON_INIT^#
132-
init()
130+
@#^ON_INIT^# init()
133131
// ON_INIT: Begin completions
134132
// ON_INIT-DAG: Keyword/None: available[#Constructor Attribute#]; name=available
135133
// ON_INIT-DAG: Keyword/None: objc[#Constructor Attribute#]; name=objc
@@ -140,8 +138,7 @@ struct _S {
140138
// ON_INIT-DAG: Keyword/None: discardableResult[#Constructor Attribute#]; name=discardableResult
141139
// ON_INIT: End completions
142140

143-
@#^ON_PROPERTY^#
144-
var foo
141+
@#^ON_PROPERTY^# var foo
145142
// ON_PROPERTY: Begin completions
146143
// ON_PROPERTY-DAG: Keyword/None: available[#Var Attribute#]; name=available
147144
// ON_PROPERTY-DAG: Keyword/None: objc[#Var Attribute#]; name=objc
@@ -159,7 +156,7 @@ struct _S {
159156
// ON_PROPERTY-NOT: Decl[PrecedenceGroup]
160157
// ON_PROPERTY: End completions
161158

162-
@#^ON_METHOD^#
159+
@#^ON_METHOD^# private
163160
func foo()
164161
// ON_METHOD: Begin completions
165162
// ON_METHOD-DAG: Keyword/None: available[#Func Attribute#]; name=available
@@ -177,13 +174,30 @@ struct _S {
177174
// ON_METHOD: Decl[Struct]/CurrModule: MyStruct[#MyStruct#]; name=MyStruct
178175
// ON_METHOD: End completions
179176

180-
func bar(@#^ON_PARAM^#)
177+
func bar(@#^ON_PARAM_1^#)
181178
// ON_PARAM: Begin completions
182179
// ON_PARAM-NOT: Keyword
183180
// ON_PARAM: Decl[Struct]/CurrModule: MyStruct[#MyStruct#]; name=MyStruct
184181
// ON_PARAM-NOT: Keyword
185182
// ON_PARAM: End completions
186183

184+
func bar(
185+
@#^ON_PARAM_2^#
186+
187+
arg: Int
188+
)
189+
// Same as ON_PARAM.
190+
191+
@#^ON_MEMBER_INDEPENDENT_1^#
192+
193+
func dummy1() {}
194+
// Same as ON_MEMBER_LAST.
195+
196+
@#^ON_MEMBER_INDEPENDENT_2^#
197+
func dummy2() {}
198+
// Same as ON_MEMBER_LAST.
199+
200+
187201
@#^ON_MEMBER_LAST^#
188202
// ON_MEMBER_LAST: Begin completions
189203
// ON_MEMBER_LAST-DAG: Keyword/None: available[#Declaration Attribute#]; name=available
@@ -216,6 +230,15 @@ struct _S {
216230
// ON_MEMBER_LAST: End completions
217231
}
218232

233+
@#^KEYWORD_INDEPENDENT_1^#
234+
235+
func dummy1() {}
236+
// Same as KEYWORD_LAST.
237+
238+
@#^KEYWORD_INDEPENDENT_2^#
239+
func dummy2() {}
240+
// Same as KEYWORD_LAST.
241+
219242
@#^KEYWORD_LAST^#
220243

221244
// KEYWORD_LAST: Begin completions

test/IDE/complete_pound_decl.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// MEMBER: End completions
1616

1717
// ATTR: Begin completions
18-
// ATTR: available[#Func Attribute#]; name=available
18+
// ATTR: available[#Declaration Attribute#]; name=available
1919
// ATTR: End completions
2020

2121
// GLOBAL: Begin completions

0 commit comments

Comments
 (0)