[java] IDEA-345355 Refactor HighlightingFeature (in progress)

JavaFeature.isAvailable moved to PsiUtil, as we cannot get PsiElement language level inside frontback-api module

GitOrigin-RevId: 8092b1f55f0d90af5bc570f2fb49cdf9ef94485a
This commit is contained in:
Tagir Valeev
2024-02-07 17:08:06 +01:00
committed by intellij-monorepo-bot
parent cd071eaae4
commit 340c4d40ba
137 changed files with 284 additions and 218 deletions

View File

@@ -3,9 +3,6 @@ package com.intellij.pom.java;
import com.intellij.core.JavaPsiBundle;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.ThreeState;
import org.jetbrains.annotations.*;
/**
@@ -168,20 +165,10 @@ public enum JavaFeature {
}
/**
* @param element a valid PsiElement to check (it's better to supply PsiFile if already known; any element is accepted for convenience)
* @return true if this feature is available in the PsiFile the supplied element belongs to
* @return true if the availability of this feature can be additionally filtered using {@link LanguageFeatureProvider}.
*/
public boolean isAvailable(@NotNull PsiElement element) {
if (!isSufficient(PsiUtil.getLanguageLevel(element))) return false;
if (!myCanBeCustomized) return true;
PsiFile file = element.getContainingFile();
if (file == null) return true;
for (LanguageFeatureProvider extension : LanguageFeatureProvider.EXTENSION_POINT_NAME.getExtensionList()) {
ThreeState threeState = extension.isFeatureSupported(this, file);
if (threeState != ThreeState.UNSURE)
return threeState.toBoolean();
}
return true;
public boolean canBeCustomized() {
return myCanBeCustomized;
}
public boolean isSufficient(@NotNull LanguageLevel useSiteLevel) {

View File

@@ -106,7 +106,7 @@ public final class PsiPolyExpressionUtil {
}
private static boolean isVarContext(PsiVariable variable) {
if (JavaFeature.LVTI.isAvailable(variable)) {
if (PsiUtil.isAvailable(JavaFeature.LVTI, variable)) {
PsiTypeElement typeElement = variable.getTypeElement();
if (typeElement != null && typeElement.isInferredType()) {
return true;

View File

@@ -146,12 +146,12 @@ public final class PsiTypesUtil {
case CommonClassNames.JAVA_LANG_DOUBLE:
return "0.0";
case CommonClassNames.JAVA_UTIL_SET:
return JavaFeature.COLLECTION_FACTORIES.isAvailable(psiClass) ? "java.util.Set.of()" : "java.util.Collections.emptySet()";
return PsiUtil.isAvailable(JavaFeature.COLLECTION_FACTORIES, psiClass) ? "java.util.Set.of()" : "java.util.Collections.emptySet()";
case CommonClassNames.JAVA_UTIL_COLLECTION:
case CommonClassNames.JAVA_UTIL_LIST:
return JavaFeature.COLLECTION_FACTORIES.isAvailable(psiClass) ? "java.util.List.of()" : "java.util.Collections.emptyList()";
return PsiUtil.isAvailable(JavaFeature.COLLECTION_FACTORIES, psiClass) ? "java.util.List.of()" : "java.util.Collections.emptyList()";
case CommonClassNames.JAVA_UTIL_MAP:
return JavaFeature.COLLECTION_FACTORIES.isAvailable(psiClass) ? "java.util.Map.of()" : "java.util.Collections.emptyMap()";
return PsiUtil.isAvailable(JavaFeature.COLLECTION_FACTORIES, psiClass) ? "java.util.Map.of()" : "java.util.Collections.emptyMap()";
}
}
}

View File

@@ -16,6 +16,8 @@ import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.pom.java.JavaFeature;
import com.intellij.pom.java.LanguageFeatureProvider;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.infos.ClassCandidateInfo;
@@ -28,6 +30,7 @@ import com.intellij.psi.search.ProjectScope;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.ThreeState;
import com.intellij.util.TimeoutUtil;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.JBIterable;
@@ -1068,9 +1071,33 @@ public final class PsiUtil extends PsiUtilCore {
}
/**
* @param element element to get Java language level for
* @return the language level. If {@linkplain LanguageLevel#isUnsupported() unsupported} language level
* @param feature feature to check
* @param element a valid PsiElement to check (it's better to supply PsiFile if already known; any element is accepted for convenience)
* @return true if the feature is available in the PsiFile the supplied element belongs to
*/
public static boolean isAvailable(@NotNull JavaFeature feature, @NotNull PsiElement element) {
if (!feature.isSufficient(getLanguageLevel(element))) return false;
if (!feature.canBeCustomized()) return true;
PsiFile file = element.getContainingFile();
if (file == null) return true;
for (LanguageFeatureProvider extension : LanguageFeatureProvider.EXTENSION_POINT_NAME.getExtensionList()) {
ThreeState threeState = extension.isFeatureSupported(feature, file);
if (threeState != ThreeState.UNSURE)
return threeState.toBoolean();
}
return true;
}
/**
* Returns the element language level. If {@linkplain LanguageLevel#isUnsupported() unsupported} language level
* is selected for project or module, this method returns the supported alias.
* <p>
* Note that it's a rare case when one may need a language level. Usually, it's interesting to check
* whether a particular language feature is available at a given context.
* Consider using {@link #isAvailable(JavaFeature, PsiElement)} instead of this method.
*
* @param element element to get Java language level for
* @return the language level.
*/
@NotNull
public static LanguageLevel getLanguageLevel(@NotNull PsiElement element) {