From 7f0b84b0f2eaf041d2005d7cb3d755efad12f541 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Tue, 11 Nov 2025 12:43:48 +0100 Subject: [PATCH] IDEA-331486 [Java]: check search scope for enum constant declarations when searching constructor references GitOrigin-RevId: f85d71a8fd56dea17266354c912cb13292083b5a --- .../ConstructorReferencesSearchHelper.java | 7 ++++--- .../codeVision/JavaCodeVisionProviderTest.kt | 21 ++++++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/java/java-indexing-impl/src/com/intellij/psi/impl/search/ConstructorReferencesSearchHelper.java b/java/java-indexing-impl/src/com/intellij/psi/impl/search/ConstructorReferencesSearchHelper.java index f8c12beb9956..3a063ccb1d45 100644 --- a/java/java-indexing-impl/src/com/intellij/psi/impl/search/ConstructorReferencesSearchHelper.java +++ b/java/java-indexing-impl/src/com/intellij/psi/impl/search/ConstructorReferencesSearchHelper.java @@ -56,7 +56,7 @@ class ConstructorReferencesSearchHelper { }); if (isEnum[0]) { - if (!processEnumReferences(processor, constructor, project, containingClass)) return false; + if (!processEnumReferences(processor, constructor, project, containingClass, searchScope)) return false; } // search usages like "new XXX(..)" @@ -114,10 +114,11 @@ class ConstructorReferencesSearchHelper { private static boolean processEnumReferences(@NotNull Processor processor, @NotNull PsiMethod constructor, @NotNull Project project, - @NotNull PsiClass aClass) { + @NotNull PsiClass aClass, + @NotNull SearchScope searchScope) { return DumbService.getInstance(project).runReadActionInSmartMode(() -> { for (PsiField field : aClass.getFields()) { - if (field instanceof PsiEnumConstant) { + if (field instanceof PsiEnumConstant && PsiSearchScopeUtil.isInScope(searchScope, field)) { PsiReference reference = field.getReference(); if (reference != null && reference.isReferenceTo(constructor) && !processor.process(reference)) { return false; diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/codeVision/JavaCodeVisionProviderTest.kt b/java/java-tests/testSrc/com/intellij/java/codeInsight/codeVision/JavaCodeVisionProviderTest.kt index 72003d99edcc..a9cbd11f7c4f 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/codeVision/JavaCodeVisionProviderTest.kt +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/codeVision/JavaCodeVisionProviderTest.kt @@ -1,4 +1,4 @@ -// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.java.codeInsight.codeVision import com.intellij.codeInsight.daemon.impl.JavaInheritorsCodeVisionProvider @@ -91,6 +91,25 @@ class JavaCodeVisionProviderTest : CodeVisionTestCase() { } """.trimIndent(), JavaReferencesCodeVisionProvider().groupId) + fun testEnumConstructor() = doTest(""" + enum LocationType {/*<# [no usages] #>*/ + FOREST("Forest"),/*<# [no usages] #>*/ + PLAIN("Plain"),/*<# [no usages] #>*/ + DESERT("Desert"),/*<# [no usages] #>*/ + HILLS("hills");/*<# [no usages] #>*/ + + private final String typeName;/*<# [2 usages] #>*/ + + LocationType(String typeName) {/*<# [4 usages] #>*/ + this.typeName = typeName; + } + + public String getTypeName() {/*<# [no usages] #>*/ + return typeName; + } + } + """.trimIndent(), JavaReferencesCodeVisionProvider().groupId) + fun testClassAtZeroOffset() = doTest(""" class A { /*<# [no usages] #>*/ enum E { /*<# [6 usages] #>*/