From fccb200100f7782bd7306d3def321955db547f06 Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Mon, 26 Mar 2018 18:19:37 +0200 Subject: [PATCH] suggest to increase language level for unresolved `var` references IDEA-188912 --- ...bleTypeInferenceUnresolvedFixProvider.java | 53 +++++++++++++++++++ .../src/META-INF/InspectionGadgets.xml | 1 + 2 files changed, 54 insertions(+) create mode 100644 java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/LocalVariableTypeInferenceUnresolvedFixProvider.java diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/LocalVariableTypeInferenceUnresolvedFixProvider.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/LocalVariableTypeInferenceUnresolvedFixProvider.java new file mode 100644 index 000000000000..4b1b5f1e9268 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/LocalVariableTypeInferenceUnresolvedFixProvider.java @@ -0,0 +1,53 @@ +// Copyright 2000-2018 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.codeInsight.daemon.impl.analysis; + +import com.intellij.codeInsight.daemon.QuickFixActionRegistrar; +import com.intellij.codeInsight.intention.QuickFixFactory; +import com.intellij.codeInsight.quickfix.UnresolvedReferenceQuickFixProvider; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.pom.java.LanguageLevel; +import com.intellij.psi.*; +import com.intellij.psi.util.PsiUtil; +import org.jetbrains.annotations.NotNull; + +public class LocalVariableTypeInferenceUnresolvedFixProvider extends UnresolvedReferenceQuickFixProvider { + @Override + public void registerFixes(@NotNull PsiJavaCodeReferenceElement ref, @NotNull QuickFixActionRegistrar registrar) { + PsiElement typeElement = ref.getParent(); + PsiElement parent = typeElement instanceof PsiTypeElement ? typeElement.getParent() : null; + boolean increaseLanguageLevel = true; + LanguageLevel targetLanguageLevel; + if (parent instanceof PsiParameter && ((PsiParameter)parent).getDeclarationScope() instanceof PsiLambdaExpression) { + //early-draft specification support to be enabled after release + if (LanguageLevel.HIGHEST.isAtLeast(LanguageLevel.JDK_X)) { + targetLanguageLevel = LanguageLevel.JDK_X; + } + else if (ApplicationManager.getApplication().isEAP()) { + targetLanguageLevel = LanguageLevel.JDK_X; + + //show module options with ability to explicitly agree with legal notice + increaseLanguageLevel = false; + } + else { + return; + } + } + else { + targetLanguageLevel = LanguageLevel.JDK_10; + } + + if (PsiUtil.getLanguageLevel(ref).isAtLeast(targetLanguageLevel)) return; + if (!PsiKeyword.VAR.equals(ref.getReferenceName())) return; + + if (increaseLanguageLevel) { + registrar.register(QuickFixFactory.getInstance().createIncreaseLanguageLevelFix(targetLanguageLevel)); + } + registrar.register(QuickFixFactory.getInstance().createShowModulePropertiesFix(ref)); + } + + @NotNull + @Override + public Class getReferenceClass() { + return PsiJavaCodeReferenceElement.class; + } +} diff --git a/plugins/InspectionGadgets/src/META-INF/InspectionGadgets.xml b/plugins/InspectionGadgets/src/META-INF/InspectionGadgets.xml index bde1f1faa55a..23d1eced4650 100644 --- a/plugins/InspectionGadgets/src/META-INF/InspectionGadgets.xml +++ b/plugins/InspectionGadgets/src/META-INF/InspectionGadgets.xml @@ -2798,6 +2798,7 @@ bundle="com.siyeh.InspectionGadgetsBundle" key="inspection.constant.expression.display.name" groupBundle="messages.InspectionsBundle" groupKey="group.names.code.style.issues" implementationClass="com.siyeh.ig.style.ConstantExpressionInspection"/> +