diff --git a/java/java-indexing-impl/src/META-INF/JavaIndexingPlugin.xml b/java/java-indexing-impl/src/META-INF/JavaIndexingPlugin.xml index f55d359cb5e2..59856a9a6d75 100644 --- a/java/java-indexing-impl/src/META-INF/JavaIndexingPlugin.xml +++ b/java/java-indexing-impl/src/META-INF/JavaIndexingPlugin.xml @@ -79,6 +79,7 @@ + \ No newline at end of file diff --git a/java/java-indexing-impl/src/com/intellij/psi/impl/search/JavaRecordComponentSearcher.java b/java/java-indexing-impl/src/com/intellij/psi/impl/search/JavaRecordComponentSearcher.java new file mode 100644 index 000000000000..d83faf418da4 --- /dev/null +++ b/java/java-indexing-impl/src/com/intellij/psi/impl/search/JavaRecordComponentSearcher.java @@ -0,0 +1,50 @@ +// Copyright 2000-2019 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 com.intellij.psi.impl.search; + +import com.intellij.openapi.application.QueryExecutorBase; +import com.intellij.openapi.application.ReadAction; +import com.intellij.psi.*; +import com.intellij.psi.search.UsageSearchContext; +import com.intellij.psi.search.searches.ReferencesSearch; +import com.intellij.util.Processor; +import org.jetbrains.annotations.NotNull; + +public class JavaRecordComponentSearcher extends QueryExecutorBase { + @Override + public void processQuery(@NotNull ReferencesSearch.SearchParameters queryParameters, @NotNull Processor consumer) { + PsiElement element = queryParameters.getElementToSearch(); + if (element instanceof PsiRecordComponent) { + RecordMethodNavigationInfo info = findNavigationInfo((PsiRecordComponent)element); + if (info != null) { + queryParameters.getOptimizer().searchWord(info.myName, + queryParameters.getEffectiveSearchScope(), + UsageSearchContext.IN_CODE, + false, + info.myLightMethod); + } + } + } + + private static RecordMethodNavigationInfo findNavigationInfo(PsiRecordComponent recordComponent) { + return ReadAction.compute(() -> { + String name = recordComponent.getName(); + if (name == null) return null; + PsiClass containingClass = recordComponent.getContainingClass(); + if (containingClass == null) return null; + PsiMethod[] methods = containingClass.findMethodsByName(name, false); + if (methods.length != 1) return null; + PsiMethod method = methods[0]; + return new RecordMethodNavigationInfo(method, name); + }); + } + + private static class RecordMethodNavigationInfo { + @NotNull final PsiMethod myLightMethod; + @NotNull final String myName; + + private RecordMethodNavigationInfo(@NotNull PsiMethod lightMethod, @NotNull String name) { + myLightMethod = lightMethod; + myName = name; + } + } +}