[groovy] IDEA-251327: Don't show hint under @CompileStatic

GitOrigin-RevId: 0f3048483289a144662f3bd59fd70745a89f3cd8
This commit is contained in:
Konstantin Nisht
2020-10-21 17:35:36 +00:00
committed by intellij-monorepo-bot
parent cac739f4df
commit b5e457fd57
3 changed files with 49 additions and 11 deletions
@@ -1457,4 +1457,31 @@ public final class PsiUtil {
return GrLiteralImpl.getLiteralValue(element);
}
public static boolean isEligibleForInvocationWithNull(@NotNull GrCall call) {
if (isCompileStatic(call) || call.hasClosureArguments()) {
return false;
}
var argumentList = call.getArgumentList();
if (argumentList == null || !argumentList.isEmpty()) {
return false;
}
PsiMethod method = call.resolveMethod();
if (method == null) {
return false;
}
PsiParameterList parameterList = method.getParameterList();
if (parameterList.getParametersCount() != 1) {
return false;
}
PsiParameter parameter = parameterList.getParameter(0);
if (parameter == null || parameter.isVarArgs() || (parameter instanceof GrParameter && ((GrParameter)parameter).isOptional())) {
return false;
}
PsiTypeElement typeElement = parameter.getTypeElement();
if (typeElement != null && !(typeElement.getType() instanceof PsiClassType)) {
return false;
}
return true;
}
}
@@ -5,14 +5,13 @@ import com.intellij.codeInsight.hints.FactoryInlayHintsCollector
import com.intellij.codeInsight.hints.InlayHintsSink
import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiPrimitiveType
import com.intellij.psi.util.parentOfType
import com.intellij.refactoring.suggested.endOffset
import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall
import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil
class GroovyImplicitNullArgumentCollector(editor: Editor) :
FactoryInlayHintsCollector(editor) {
class GroovyImplicitNullArgumentCollector(editor: Editor) : FactoryInlayHintsCollector(editor) {
override fun collect(element: PsiElement, editor: Editor, sink: InlayHintsSink): Boolean {
if (element !is GrArgumentList) {
@@ -22,15 +21,9 @@ class GroovyImplicitNullArgumentCollector(editor: Editor) :
return true
}
val methodCall = element.parentOfType<GrCall>()?.takeIf { it.argumentList === element } ?: return true
if (methodCall.hasClosureArguments()) {
return true
if (PsiUtil.isEligibleForInvocationWithNull(methodCall)) {
sink.addInlineElement(element.firstChild.endOffset, true, factory.roundWithBackground(factory.smallText("null")), false)
}
val resolvedMethod = methodCall.resolveMethod() ?: return true
if (resolvedMethod.parameterList.parametersCount != 1) return true
if (resolvedMethod.isVarArgs) return true
val parameterTypeElement = resolvedMethod.parameterList.getParameter(0)?.typeElement
if (parameterTypeElement != null && parameterTypeElement.type is PsiPrimitiveType) return true
sink.addInlineElement(element.firstChild.endOffset, true, factory.roundWithBackground(factory.smallText("null")), false)
return true
}
}
@@ -1,12 +1,19 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.groovy.codeInsight.hint
import com.intellij.testFramework.LightProjectDescriptor
import com.intellij.testFramework.utils.inlays.InlayHintsProviderTestCase
import groovy.transform.CompileStatic
import org.jetbrains.plugins.groovy.GroovyProjectDescriptors
@CompileStatic
class GroovyImplicitNullArgumentHintProviderTest extends InlayHintsProviderTestCase {
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return GroovyProjectDescriptors.GROOVY_2_5
}
def doTest(String text) {
testProvider("test.groovy", text, new GroovyImplicitNullArgumentHintProvider(), new GroovyImplicitNullArgumentHintProvider.Settings())
}
@@ -50,6 +57,17 @@ class Foo {
}
new Foo(<# null #>)
"""
}
void 'test don\'t show hint with @CompileStatic'() {
doTest """
@groovy.transform.CompileStatic
class Foo {
def foo(String s) {}
def bar() { foo<error>()</error> }
}
"""
}
}