Skip to content

Add @ClosureParams to Closure method parameters that already have a @DelegatesTo #27077

@PaintNinja

Description

@PaintNinja

Expected Behavior

Various methods that take Closures exist with an appropriate @DelegatesTo annotation attached to them, which is great for improving IDE support and aiding less verbose @TypeChecked or even @CompileStatic Groovy plugin code.

However, I noticed it is seldom combined with @ClosureParams, which if added, would show the param type in the IDE, like this:
image

Current Behavior (optional)

Currently in Gradle 8.4, the lack of that annotation means the implicit it type isn't automatically known by the IDE and typing a quick it. to get suggestions doesn't show anything specific:
image

But you can verify the type with a simple instanceof check on the param first...

subprojects {
    println it instanceof Project // prints true
}

...and then manually look at Project's methods.

Alternatively, hovering over the subprojects part and looking at the type of the @DelegatesTo annotation also works:
image

Context

I did a fair bit of investigating before filing this feature request...

My first thought was it might be a bug in IntelliJ and that the closure param type hint shown should use the @DelegatesTo type if the @ClosureParams annotation is missing, however after playing around with some code I discovered this isn't always the case (a @DelegatesTo-only annotated Closure isn't guaranteed to have its type available in the implicit it).

I then started looking at Gradle code to verify that the methods set both the param and delegate and it looks like it does in various cases, as internally it seems to end up here which sets the delegate and also calls the closure with it as the param if possible.

With all that confirmed and out of the way, it seems trivial to improve IDE support for the Groovy DSL in this specific scenario (and likely for various other methods that have a @DelegatesTo-only annotated Closure):

void subprojects(@DelegatesTo(Project.class) @ClosureParams(value = SimpleType.class, options = "org.gradle.api.Project") Closure configureClosure);

Metadata

Metadata

Assignees

Labels

a:featureA new functionalityin:groovy-dslin:ideIssues related to using Gradle with an IDE like IntelliJ, Eclipse or Android Studiore:comprehensibilityreasonable errors and warnings, clear dsl, mental overload

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions