Skip to content

[CodeComplete] Offer completion suggestions for default argument values inside the macro declaration + argument label inside #externalMacro #65526

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
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
4 changes: 4 additions & 0 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8871,6 +8871,10 @@ const ParamDecl *getParameterAt(ConcreteDeclRef declRef, unsigned index);
/// nullptr if the source does not have a parameter list.
const ParamDecl *getParameterAt(const ValueDecl *source, unsigned index);

/// Retrieve parameter declaration from the given source at given index, or
/// nullptr if the source does not have a parameter list.
const ParamDecl *getParameterAt(const DeclContext *source, unsigned index);

void simple_display(llvm::raw_ostream &out,
OptionSet<NominalTypeDecl::LookupDirectFlags> options);

Expand Down
8 changes: 8 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8281,6 +8281,14 @@ const ParamDecl *swift::getParameterAt(const ValueDecl *source,
return nullptr;
}

const ParamDecl *swift::getParameterAt(const DeclContext *source,
unsigned index) {
if (auto *params = getParameterList(const_cast<DeclContext *>(source))) {
return index < params->size() ? params->get(index) : nullptr;
}
return nullptr;
}

Type AbstractFunctionDecl::getMethodInterfaceType() const {
assert(getDeclContext()->isTypeContext());
auto Ty = getInterfaceType();
Expand Down
3 changes: 2 additions & 1 deletion lib/SIL/IR/TypeLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3959,7 +3959,8 @@ TypeConverter::getLoweredLocalCaptures(SILDeclRef fn) {
fn.getAsRegularLocation(), Context);

if (auto *afd = dyn_cast<AbstractFunctionDecl>(curFn.getDecl())) {
auto *param = getParameterAt(afd, curFn.defaultArgIndex);
auto *param = getParameterAt(static_cast<ValueDecl *>(afd),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why's the static_cast needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because otherwise it’s ambiguous whether this should call getParameterAt(DeclContext *, …) or getParameterAt(ValueDecl *, …)

curFn.defaultArgIndex);
if (param->hasDefaultExpr()) {
auto dc = afd->getInnermostDeclContext();
collectCaptures(param->getDefaultArgumentCaptureInfo(), dc);
Expand Down
7 changes: 7 additions & 0 deletions lib/Sema/TypeCheckMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ MacroDefinition MacroDefinitionRequest::evaluate(
free(replacements);
};

if (checkResult < 0 && ctx.CompletionCallback) {
// If the macro failed to check and we are in code completion mode, pretend
// it's an arbitrary macro. This allows us to get call argument completions
// inside `#externalMacro`.
checkResult = BridgedMacroDefinitionKind::BridgedExpandedMacro;
}

if (checkResult < 0)
return MacroDefinition::forInvalid();

Expand Down
9 changes: 2 additions & 7 deletions lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2366,13 +2366,8 @@ bool TypeCheckASTNodeAtLocRequest::evaluate(
}
}
} else if (auto *defaultArg = dyn_cast<DefaultArgumentInitializer>(DC)) {
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(defaultArg->getParent())) {
auto *Param = AFD->getParameters()->get(defaultArg->getIndex());
(void)Param->getTypeCheckedDefaultExpr();
return false;
}
if (auto *SD = dyn_cast<SubscriptDecl>(defaultArg->getParent())) {
auto *Param = SD->getIndices()->get(defaultArg->getIndex());
if (const ParamDecl *Param =
getParameterAt(defaultArg->getParent(), defaultArg->getIndex())) {
(void)Param->getTypeCheckedDefaultExpr();
return false;
}
Expand Down
22 changes: 22 additions & 0 deletions test/IDE/complete_macro_declaration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// REQUIRES: swift_swift_parser
// RUN: %empty-directory(%t)
// RUN: %target-swift-ide-test -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t

let globalVar = 1
macro expect(file: Int = #^DEFAULT_ARG^#) = #externalMacro(module: "MyModule", type: "MyMacro")
// DEFAULT_ARG: Decl[GlobalVar]/CurrModule/TypeRelation[Convertible]: globalVar[#Int#]; name=globalVar

@freestanding(expression)
macro externalMacro() = ##^EXTERNAL_MACRO^#
// EXTERNAL_MACRO: Decl[Macro]/OtherModule[Swift]/IsSystem: externalMacro({#module: String#}, {#type: String#})[#T#]; name=externalMacro(module:type:)

@freestanding(expression)
macro externalMacroWithTrailing() = ##^EXTERNAL_MACRO_WITH_TRAILING?check=EXTERNAL_MACRO^#externalMacro

@freestanding(expression)
macro externalMacroCallPattern() = #externalMacro(#^EXTERNAL_MACRO_CALL_PATTERN^#)
// EXTERNAL_MACRO_CALL_PATTERN: Pattern/None/Flair[ArgLabels]/TypeRelation[Convertible]: ['(']{#module: String#}, {#type: String#}[')'][#Void#]; name=module:type:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's module:type: there?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s how the call pattern shows up in code completion, I think. It’s the same that shows up for normal function calls

$ cat /tmp/a.swift
func foo(module: String, type: String) {}

foo(#^COMPLETE^#)
$ swift-ide-complete /tmp/a.swift
+ /Users/alex/sbin/swift-ide-test -code-completion -source-filename /private/tmp/a.swift -code-completion-token COMPLETE
found code completion token COMPLETE at offset 47
Begin completions, 1 items
Decl[FreeFunction]/CurrModule/Flair[ArgLabels]: ['(']{#module: String#}, {#type: String#}[')'][#Void#]; name=module:type:
End completions


@freestanding(expression)
macro externalMacroCallPattern() = #externalMacro(module: "MyModule", #^EXTERNAL_MACRO_TYPE_ARG_LABEL^#)
// EXTERNAL_MACRO_TYPE_ARG_LABEL: Pattern/Local/Flair[ArgLabels]: {#type: String#}[#String#]; name=type: