mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
enable explicit type -> var for diamonds (IDEA-188771)
expand diamonds and spare on explicit type declaration
This commit is contained in:
+13
@@ -5,6 +5,7 @@ import com.intellij.openapi.project.Project;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.impl.PsiDiamondTypeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -58,6 +59,18 @@ public class RedundantExplicitVariableTypeInspection extends AbstractBaseJavaLoc
|
||||
}
|
||||
|
||||
private static PsiElement replaceExplicitTypeWithVar(PsiTypeElement typeElement, PsiElement context) {
|
||||
PsiElement parent = typeElement.getParent();
|
||||
if (parent instanceof PsiVariable) {
|
||||
PsiExpression copyVariableInitializer = ((PsiVariable)parent).getInitializer();
|
||||
if (copyVariableInitializer instanceof PsiNewExpression) {
|
||||
final PsiDiamondType.DiamondInferenceResult diamondResolveResult =
|
||||
PsiDiamondTypeImpl.resolveInferredTypesNoCheck((PsiNewExpression)copyVariableInitializer, copyVariableInitializer);
|
||||
if (!diamondResolveResult.getInferredTypes().isEmpty()) {
|
||||
PsiDiamondTypeUtil.expandTopLevelDiamondsInside(copyVariableInitializer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return typeElement.replace(JavaPsiFacade.getElementFactory(context.getProject()).createTypeElementFromText("var", context));
|
||||
}
|
||||
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Replace explicit type with 'var'" "true"
|
||||
class Main {
|
||||
{
|
||||
var i = new A<String>();
|
||||
}
|
||||
|
||||
static class A<T> {}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// "Replace explicit type with 'var'" "false"
|
||||
// "Replace explicit type with 'var'" "true"
|
||||
class Main {
|
||||
{
|
||||
<caret>A<String> i = new A<>();
|
||||
|
||||
Reference in New Issue
Block a user