Skip to content

Commit ce823b2

Browse files
authored
Merge pull request #64112 from DougGregor/code-complete-attached-macros
2 parents b159576 + 7177a16 commit ce823b2

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

include/swift/IDE/CodeCompletionResultType.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ enum class CustomAttributeKind : uint8_t {
2828
ResultBuilder = 1 << 1,
2929
/// A type that can be used as a global actor.
3030
GlobalActor = 1 << 2,
31+
/// A macro can be used as a custom attribute.
32+
Macro = 1 << 3,
3133
};
3234

3335
/// The expected contextual type(s) for code-completion.

lib/IDE/CodeCompletion.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1807,18 +1807,24 @@ void CodeCompletionCallbacksImpl::doneParsing(SourceFile *SrcFile) {
18071807
ExpectedCustomAttributeKinds |= CustomAttributeKind::ResultBuilder;
18081808
ExpectedCustomAttributeKinds |= CustomAttributeKind::GlobalActor;
18091809
}
1810+
ExpectedCustomAttributeKinds |= CustomAttributeKind::Macro;
1811+
18101812
Lookup.setExpectedTypes(/*Types=*/{},
18111813
/*isImplicitSingleExpressionReturn=*/false,
18121814
/*preferNonVoid=*/false,
18131815
ExpectedCustomAttributeKinds);
18141816

18151817
// TypeName at attribute position after '@'.
18161818
// - VarDecl: Property Wrappers.
1817-
// - ParamDecl/VarDecl/FuncDecl: Function Builders.
1819+
// - ParamDecl/VarDecl/FuncDecl: Result Builders.
18181820
if (!AttTargetDK || *AttTargetDK == DeclKind::Var ||
18191821
*AttTargetDK == DeclKind::Param || *AttTargetDK == DeclKind::Func)
18201822
Lookup.getTypeCompletionsInDeclContext(
18211823
P.Context.SourceMgr.getIDEInspectionTargetLoc());
1824+
1825+
// Macro name at attribute position after '@'.
1826+
Lookup.getToplevelCompletions(
1827+
/*OnlyTypes=*/false, /*OnlyMacros=*/true);
18221828
break;
18231829
}
18241830
case CompletionKind::AttributeDeclParen: {

lib/IDE/CompletionLookup.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,16 +1783,23 @@ void CompletionLookup::addEnumElementRef(const EnumElementDecl *EED,
17831783
void CompletionLookup::addMacroExpansion(const MacroDecl *MD,
17841784
DeclVisibilityKind Reason) {
17851785
if (!MD->hasName() || !MD->isAccessibleFrom(CurrDeclContext) ||
1786-
MD->shouldHideFromEditor() ||
1787-
!isFreestandingMacro(MD->getMacroRoles()))
1786+
MD->shouldHideFromEditor())
1787+
return;
1788+
1789+
// If this is the wrong kind of macro, we don't need it.
1790+
bool wantAttachedMacro =
1791+
expectedTypeContext.getExpectedCustomAttributeKinds()
1792+
.contains(CustomAttributeKind::Macro);
1793+
if ((wantAttachedMacro && !isAttachedMacro(MD->getMacroRoles())) ||
1794+
(!wantAttachedMacro && !isFreestandingMacro(MD->getMacroRoles())))
17881795
return;
17891796

17901797
CodeCompletionResultBuilder Builder(
17911798
Sink, CodeCompletionResultKind::Declaration,
17921799
getSemanticContext(MD, Reason, DynamicLookupInfo()));
17931800
Builder.setAssociatedDecl(MD);
17941801

1795-
if (NeedLeadingMacroPound) {
1802+
if (NeedLeadingMacroPound && !wantAttachedMacro) {
17961803
Builder.addTextChunk("#");
17971804
}
17981805

test/IDE/complete_decl_attribute.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ actor MyGenericGlobalActor<T> {
5757
static let shared = MyGenricGlobalActor<T>()
5858
}
5959

60+
@attached(member)
61+
macro MyMacro() = #externalMacro(module: "Macros", type: "MyMacro")
6062

6163
@available(#^AVAILABILITY1^#)
6264

@@ -117,6 +119,7 @@ actor MyGenericGlobalActor<T> {
117119
// KEYWORD2-DAG: Decl[Struct]/CurrModule: MyPropertyWrapper[#MyPropertyWrapper#]; name=MyPropertyWrapper
118120
// KEYWORD2-DAG: Decl[Struct]/CurrModule/TypeRelation[Convertible]: MyResultBuilder[#MyResultBuilder#]; name=MyResultBuilder
119121
// KEYWORD2-DAG: Decl[Actor]/CurrModule/TypeRelation[Convertible]: MyGlobalActor[#MyGlobalActor#]; name=MyGlobalActor
122+
// KEYWORD2-DAG: Decl[Macro]/CurrModule: MyMacro[#Void#]; name=MyMacro
120123
// KEYWORD2: End completions
121124

122125
@#^KEYWORD3^# class C {}
@@ -138,6 +141,7 @@ actor MyGenericGlobalActor<T> {
138141
// KEYWORD3-NEXT: Keyword/None: globalActor[#Class Attribute#]; name=globalActor
139142
// KEYWORD3-NEXT: Keyword/None: preconcurrency[#Class Attribute#]; name=preconcurrency
140143
// KEYWORD3-NEXT: Keyword/None: runtimeMetadata[#Class Attribute#]; name=runtimeMetadata
144+
// KEYWORD3-NEXT: Decl[Macro]/CurrModule: MyMacro[#Void#]; name=MyMacro
141145
// KEYWORD3-NEXT: End completions
142146

143147
@#^KEYWORD3_2^#IB class C2 {}
@@ -157,6 +161,7 @@ actor MyGenericGlobalActor<T> {
157161
// KEYWORD4-NEXT: Keyword/None: globalActor[#Enum Attribute#]; name=globalActor
158162
// KEYWORD4-NEXT: Keyword/None: preconcurrency[#Enum Attribute#]; name=preconcurrency
159163
// KEYWORD4-NEXT: Keyword/None: runtimeMetadata[#Enum Attribute#]; name=runtimeMetadata
164+
// KEYWORD4-NEXT: Decl[Macro]/CurrModule: MyMacro[#Void#]; name=MyMacro
160165
// KEYWORD4-NEXT: End completions
161166

162167
@#^KEYWORD5^# struct S{}
@@ -172,6 +177,7 @@ actor MyGenericGlobalActor<T> {
172177
// KEYWORD5-NEXT: Keyword/None: globalActor[#Struct Attribute#]; name=globalActor
173178
// KEYWORD5-NEXT: Keyword/None: preconcurrency[#Struct Attribute#]; name=preconcurrency
174179
// KEYWORD5-NEXT: Keyword/None: runtimeMetadata[#Struct Attribute#]; name=runtimeMetadata
180+
// KEYWORD5-NEXT: Decl[Macro]/CurrModule: MyMacro[#Void#]; name=MyMacro
175181
// KEYWORD5-NEXT: End completions
176182

177183
@#^ON_GLOBALVAR^# var globalVar

0 commit comments

Comments
 (0)