Skip to content

Commit 09ca68e

Browse files
committed
[CodeCompletion] Don't attach attr to decl if blank line exists
``` @#^COMPLETE^# public func something() {} ``` In this case, we can't say the user is adding attribute to the func or starting a new declaration. So if there're one or more blank lines after the completion, suggest context free attribute list. rdar://problem/50441643
1 parent 16bc322 commit 09ca68e

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
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: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,13 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
13511351
void setAttrTargetDeclKind(Optional<DeclKind> DK) override {
13521352
if (DK == DeclKind::PatternBinding)
13531353
DK = DeclKind::Var;
1354+
1355+
// If the target is already set to 'Module', that means the completion
1356+
// should be performed as if it's not tied to any specific decl.
1357+
// see 'completeDeclAttrBeginning()'
1358+
if (AttTargetDK == DeclKind::Module)
1359+
DK = None;
1360+
13541361
AttTargetDK = DK;
13551362
}
13561363

@@ -1373,7 +1380,7 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
13731380
void completeCaseStmtKeyword() override;
13741381
void completeCaseStmtBeginning() override;
13751382
void completeCaseStmtDotPrefix() override;
1376-
void completeDeclAttrBeginning(bool Sil) override;
1383+
void completeDeclAttrBeginning(bool Sil, bool isIndependent) override;
13771384
void completeDeclAttrParam(DeclAttrKind DK, int Index) override;
13781385
void completeInPrecedenceGroup(SyntaxKind SK) override;
13791386
void completeNominalMemberBeginning(
@@ -4606,10 +4613,15 @@ void CodeCompletionCallbacksImpl::completeDeclAttrParam(DeclAttrKind DK,
46064613
CurDeclContext = P.CurDeclContext;
46074614
}
46084615

4609-
void CodeCompletionCallbacksImpl::completeDeclAttrBeginning(bool Sil) {
4616+
void CodeCompletionCallbacksImpl::completeDeclAttrBeginning(
4617+
bool Sil, bool isIndependent) {
46104618
Kind = CompletionKind::AttributeBegin;
46114619
IsInSil = Sil;
46124620
CurDeclContext = P.CurDeclContext;
4621+
4622+
// Use 'DeclKind::Module' as the indicator of "This is independent attribute".
4623+
if (isIndependent)
4624+
AttTargetDK = DeclKind::Module;
46134625
}
46144626

46154627
void CodeCompletionCallbacksImpl::completeInPrecedenceGroup(SyntaxKind SK) {
@@ -5373,6 +5385,8 @@ void CodeCompletionCallbacksImpl::doneParsing() {
53735385
}
53745386

53755387
case CompletionKind::AttributeBegin: {
5388+
assert(AttTargetDK != DeclKind::Module &&
5389+
"'setAttrTargetDeclKind()' hasn't been called");
53765390
Lookup.getAttributeDeclCompletions(IsInSil, AttTargetDK);
53775391

53785392
// TypeName at attribute position after '@'.

lib/Parse/ParseDecl.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,8 +1753,12 @@ 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+
auto ccLine = SourceMgr.getLineNumber(Tok.getLoc());
1758+
auto nextLine = SourceMgr.getLineNumber(peekToken().getLoc());
1759+
CodeCompletion->completeDeclAttrBeginning(isInSILMode(),
1760+
(nextLine - ccLine) > 1);
1761+
}
17581762
consumeToken(tok::code_complete);
17591763
return makeParserCodeCompletionStatus();
17601764
}

test/IDE/complete_decl_attribute.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
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
1212
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ON_PARAM | %FileCheck %s -check-prefix=ON_PARAM
13+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ON_MEMBER_INDEPENDENT | %FileCheck %s -check-prefix=ON_MEMBER_LAST
1314
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ON_MEMBER_LAST | %FileCheck %s -check-prefix=ON_MEMBER_LAST
15+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=KEYWORD_INDEPENDENT | %FileCheck %s -check-prefix=KEYWORD_LAST
1416
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=KEYWORD_LAST | %FileCheck %s -check-prefix=KEYWORD_LAST
1517

1618
struct MyStruct {}
@@ -160,6 +162,7 @@ struct _S {
160162
// ON_PROPERTY: End completions
161163

162164
@#^ON_METHOD^#
165+
private
163166
func foo()
164167
// ON_METHOD: Begin completions
165168
// ON_METHOD-DAG: Keyword/None: available[#Func Attribute#]; name=available
@@ -184,6 +187,10 @@ struct _S {
184187
// ON_PARAM-NOT: Keyword
185188
// ON_PARAM: End completions
186189

190+
@#^ON_MEMBER_INDEPENDENT^#
191+
192+
func dummy() {}
193+
187194
@#^ON_MEMBER_LAST^#
188195
// ON_MEMBER_LAST: Begin completions
189196
// ON_MEMBER_LAST-DAG: Keyword/None: available[#Declaration Attribute#]; name=available
@@ -216,6 +223,10 @@ struct _S {
216223
// ON_MEMBER_LAST: End completions
217224
}
218225

226+
@#^KEYWORD_INDEPENDENT^#
227+
228+
func dummy() {}
229+
219230
@#^KEYWORD_LAST^#
220231

221232
// KEYWORD_LAST: Begin completions

0 commit comments

Comments
 (0)