[kotlin] Do not show inlay hints for java method parameters annotated with comments

Relates to #KTIJ-19379

GitOrigin-RevId: a9301a33f29f0b05cd0ce4501e2b4aab842d966f
This commit is contained in:
Vladimir Dolzhenko
2021-08-31 05:14:49 +00:00
committed by intellij-monorepo-bot
parent 51c2ebdd98
commit fef9437a4a
9 changed files with 100 additions and 5 deletions
@@ -24,8 +24,9 @@ import org.jetbrains.kotlin.idea.codeInsight.generate.AbstractCodeInsightActionT
import org.jetbrains.kotlin.idea.codeInsight.generate.AbstractGenerateHashCodeAndEqualsActionTest
import org.jetbrains.kotlin.idea.codeInsight.generate.AbstractGenerateTestSupportMethodActionTest
import org.jetbrains.kotlin.idea.codeInsight.generate.AbstractGenerateToStringActionTest
import org.jetbrains.kotlin.idea.codeInsight.hints.AbstractKotlinArgumentsHintsProviderTest
import org.jetbrains.kotlin.idea.codeInsight.hints.AbstractKotlinLambdasHintsProvider
import org.jetbrains.kotlin.idea.codeInsight.hints.AbstractKotlinRangeHintsProviderTest
import org.jetbrains.kotlin.idea.codeInsight.hints.AbstractKotlinRangesHintsProviderTest
import org.jetbrains.kotlin.idea.codeInsight.moveUpDown.AbstractMoveLeftRightTest
import org.jetbrains.kotlin.idea.codeInsight.moveUpDown.AbstractMoveStatementTest
import org.jetbrains.kotlin.idea.codeInsight.postfix.AbstractPostfixTemplateProviderTest
@@ -895,10 +896,13 @@ private fun assembleWorkspace(): TWorkspace = workspace {
model("codeInsight/postfix")
}
testClass<AbstractKotlinArgumentsHintsProviderTest> {
model("codeInsight/hints/arguments")
}
testClass<AbstractKotlinLambdasHintsProvider> {
model("codeInsight/hints/lambda")
}
testClass<AbstractKotlinRangeHintsProviderTest> {
testClass<AbstractKotlinRangesHintsProviderTest> {
model("codeInsight/hints/ranges")
}
@@ -3,6 +3,9 @@
package org.jetbrains.kotlin.idea.parameterInfo
import com.intellij.codeInsight.hints.InlayInfo
import com.intellij.psi.PsiComment
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.util.prevLeafs
import org.jetbrains.kotlin.builtins.extractParameterNameFromFunctionTypeArgument
import org.jetbrains.kotlin.builtins.functions.FunctionInvokeDescriptor
import org.jetbrains.kotlin.descriptors.CallableDescriptor
@@ -11,6 +14,8 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.core.resolveCandidates
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.load.java.descriptors.JavaClassConstructorDescriptor
import org.jetbrains.kotlin.load.java.descriptors.JavaMethodDescriptor
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.resolve.calls.callUtil.getCall
@@ -62,7 +67,7 @@ private fun getArgumentNameHintsForCallCandidate(
resolvedArg.arguments.firstOrNull()?.let { arg ->
arg.getArgumentExpression()?.let { argExp ->
if (!arg.isNamed() && !valueParam.name.isSpecial && argExp.isUnclearExpression()) {
if (!arg.isNamed() && !argExp.isAnnotatedWithComment(resultingDescriptor) && !valueParam.name.isSpecial && argExp.isUnclearExpression()) {
val prefix = if (valueParam.varargElementType != null) "..." else ""
val offset = if (arg == valueArgumentList?.arguments?.firstOrNull() && valueParam.varargElementType != null)
valueArgumentList.leftParenthesis?.textRange?.endOffset ?: argExp.startOffset
@@ -81,3 +86,9 @@ private fun KtExpression.isUnclearExpression() = when (this) {
is KtPrefixExpression -> baseExpression is KtConstantExpression && (operationToken == KtTokens.PLUS || operationToken == KtTokens.MINUS)
else -> false
}
private fun KtExpression.isAnnotatedWithComment(descriptor: CallableDescriptor): Boolean =
(descriptor is JavaMethodDescriptor || descriptor is JavaClassConstructorDescriptor) &&
prevLeafs
.takeWhile { it is PsiWhiteSpace || it is PsiComment }
.any { it is PsiComment }
@@ -0,0 +1,30 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.kotlin.idea.codeInsight.hints
import com.intellij.testFramework.LightProjectDescriptor
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
import java.io.File
abstract class AbstractKotlinArgumentsHintsProviderTest : KotlinLightCodeInsightFixtureTestCase() {
override fun getProjectDescriptor(): LightProjectDescriptor {
return KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE
}
fun doTest(unused: String) { // named according to the convention imposed by GenerateTests
val dependencySuffixes = listOf(".dependency.kt", ".dependency.java", ".dependency1.kt", ".dependency2.kt")
for (suffix in dependencySuffixes) {
val dependencyPath = fileName().replace(".kt", suffix)
if (File(testDataDirectory, dependencyPath).exists()) {
myFixture.configureByFile(dependencyPath)
}
}
val ktFile = myFixture.configureByFile(testDataFile())
myFixture.testInlays()
}
}
@@ -8,7 +8,7 @@ import com.intellij.testFramework.utils.inlays.InlayHintsProviderTestCase
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
import java.io.File
abstract class AbstractKotlinRangeHintsProviderTest :
abstract class AbstractKotlinRangesHintsProviderTest :
InlayHintsProviderTestCase() { // Abstract- prefix is just a convention for GenerateTests
override fun getProjectDescriptor(): LightProjectDescriptor {
@@ -0,0 +1,35 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.kotlin.idea.codeInsight.hints;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.TestMetadata;
import org.jetbrains.kotlin.test.TestRoot;
import org.junit.runner.RunWith;
/**
* This class is generated by {@link org.jetbrains.kotlin.testGenerator.generator.TestGenerator}.
* DO NOT MODIFY MANUALLY.
*/
@SuppressWarnings("all")
@TestRoot("idea/tests")
@TestDataPath("$CONTENT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("testData/codeInsight/hints/arguments")
public class KotlinArgumentsHintsProviderTestGenerated extends AbstractKotlinArgumentsHintsProviderTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
@TestMetadata("javaParameters.kt")
public void testJavaParameters() throws Exception {
runTest("testData/codeInsight/hints/arguments/javaParameters.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("testData/codeInsight/hints/arguments/simple.kt");
}
}
@@ -18,7 +18,7 @@ import org.junit.runner.RunWith;
@TestDataPath("$CONTENT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("testData/codeInsight/hints/ranges")
public class KotlinRangeHintsProviderTestGenerated extends AbstractKotlinRangeHintsProviderTest {
public class KotlinRangesHintsProviderTestGenerated extends AbstractKotlinRangesHintsProviderTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
@@ -0,0 +1,5 @@
public class SomeClass {
public void invokeMe(int index, String name) {
}
}
@@ -0,0 +1,5 @@
fun javaParameters() {
val some = SomeClass()
some.invokeMe(<hint text="index:"/>0, <hint text="name:"/>"me")
some.invokeMe(/* index = */ 0, /* name = */ "me")
}
@@ -0,0 +1,5 @@
fun foo(name: String, index: Int) {
}
val f = foo(<hint text="name:"/>"name", <hint text="index:"/>42)