From 16a9e35d3bc3156bcf4c6ceb122cb22d5245815d Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Tue, 16 Apr 2024 11:15:49 +0200 Subject: [PATCH] [lombok] ensure to search for fields/components with generated members in the same way as without - but with additional secondary elements Otherwise, kotlin usages won't be found, because no one would call MethodReferenceSearch. Added test for kotlin+lombok setup, only k2 ^KTIJ-29440 fixed GitOrigin-RevId: a769e07571e34b8b07782f09254412c4466ad18a --- .idea/modules.xml | 1 + .../all-tests-fir/kotlin.fir-all-tests.iml | 1 + .../kotlin.k2.lombok.tests.iml | 15 +++++++ .../findUsages/KotlinLombokFindUsages.kt | 41 +++++++++++++++++++ .../LombokFieldFindUsagesHandlerFactory.java | 11 +++++ 5 files changed, 69 insertions(+) create mode 100644 plugins/kotlin/kotlin.lombok.tests/kotlin.k2.lombok.tests.iml create mode 100644 plugins/kotlin/kotlin.lombok.tests/test/org/jetbrains/kotlin/findUsages/KotlinLombokFindUsages.kt diff --git a/.idea/modules.xml b/.idea/modules.xml index ee330070738a..8ae0eb9e0fed 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -1459,6 +1459,7 @@ + diff --git a/plugins/kotlin/all-tests-fir/kotlin.fir-all-tests.iml b/plugins/kotlin/all-tests-fir/kotlin.fir-all-tests.iml index 3493f1f6ccc6..a8b403cfcd53 100644 --- a/plugins/kotlin/all-tests-fir/kotlin.fir-all-tests.iml +++ b/plugins/kotlin/all-tests-fir/kotlin.fir-all-tests.iml @@ -56,5 +56,6 @@ + \ No newline at end of file diff --git a/plugins/kotlin/kotlin.lombok.tests/kotlin.k2.lombok.tests.iml b/plugins/kotlin/kotlin.lombok.tests/kotlin.k2.lombok.tests.iml new file mode 100644 index 000000000000..9f9afb7af4c0 --- /dev/null +++ b/plugins/kotlin/kotlin.lombok.tests/kotlin.k2.lombok.tests.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/plugins/kotlin/kotlin.lombok.tests/test/org/jetbrains/kotlin/findUsages/KotlinLombokFindUsages.kt b/plugins/kotlin/kotlin.lombok.tests/test/org/jetbrains/kotlin/findUsages/KotlinLombokFindUsages.kt new file mode 100644 index 000000000000..020d1512a4e3 --- /dev/null +++ b/plugins/kotlin/kotlin.lombok.tests/test/org/jetbrains/kotlin/findUsages/KotlinLombokFindUsages.kt @@ -0,0 +1,41 @@ +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package org.jetbrains.kotlin.findUsages + +import com.intellij.testFramework.LightProjectDescriptor +import com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl +import de.plushnikov.intellij.plugin.LombokTestUtil +import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase + +class KotlinLombokFindUsages: KotlinLightCodeInsightFixtureTestCase() { + override fun getProjectDescriptor(): LightProjectDescriptor { + return LombokTestUtil.LOMBOK_NEW_DESCRIPTOR + } + + override fun isFirPlugin(): Boolean { + return true + } + + fun testFindUsagesForSetter() { + val aClass = myFixture.addClass( + """import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class SomeJavaClass { + + private int someValue; +}""" + ) + myFixture.addFileToProject("usage.kt", """ + fun main(val a: SomeJavaClass) { + a.someValue = 10 + } + """.trimIndent()) + val field = aClass.fields[0] + + val usages = (myFixture as CodeInsightTestFixtureImpl).findUsages(field) + + assertNotEmpty(usages) + } +} \ No newline at end of file diff --git a/plugins/lombok/src/main/java/de/plushnikov/intellij/plugin/extension/LombokFieldFindUsagesHandlerFactory.java b/plugins/lombok/src/main/java/de/plushnikov/intellij/plugin/extension/LombokFieldFindUsagesHandlerFactory.java index 7801b0aa4862..1dde36a84225 100644 --- a/plugins/lombok/src/main/java/de/plushnikov/intellij/plugin/extension/LombokFieldFindUsagesHandlerFactory.java +++ b/plugins/lombok/src/main/java/de/plushnikov/intellij/plugin/extension/LombokFieldFindUsagesHandlerFactory.java @@ -2,6 +2,8 @@ package de.plushnikov.intellij.plugin.extension; import com.intellij.find.findUsages.FindUsagesHandler; import com.intellij.find.findUsages.FindUsagesHandlerFactory; +import com.intellij.find.findUsages.FindUsagesOptions; +import com.intellij.find.findUsages.JavaFindUsagesHelper; import com.intellij.openapi.project.DumbService; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiElement; @@ -9,6 +11,8 @@ import com.intellij.psi.PsiField; import com.intellij.psi.PsiMember; import com.intellij.psi.PsiRecordComponent; import com.intellij.psi.util.PsiUtilCore; +import com.intellij.usageView.UsageInfo; +import com.intellij.util.Processor; import com.intellij.util.containers.ContainerUtil; import de.plushnikov.intellij.plugin.psi.LombokLightClassBuilder; import de.plushnikov.intellij.plugin.psi.LombokLightFieldBuilder; @@ -44,6 +48,13 @@ public final class LombokFieldFindUsagesHandlerFactory extends FindUsagesHandler @Override public FindUsagesHandler createFindUsagesHandler(@NotNull PsiElement element, boolean forHighlightUsages) { return new FindUsagesHandler(element) { + @Override + public boolean processElementUsages(@NotNull PsiElement element, + @NotNull Processor processor, + @NotNull FindUsagesOptions options) { + return JavaFindUsagesHelper.processElementUsages(element, options, processor); + } + @Override public PsiElement @NotNull [] getSecondaryElements() { final PsiMember psiMember = (PsiMember)getPsiElement();