reformat on replace type args with <> (IDEA-138079)

This commit is contained in:
Anna Kozlova
2015-03-24 19:00:26 +01:00
parent c3738d4cb7
commit 4b3cebcf53
4 changed files with 48 additions and 2 deletions

View File

@@ -22,8 +22,9 @@ import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.impl.PsiDiamondTypeUtil;
import com.intellij.psi.impl.source.tree.ChildRole;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
@@ -97,7 +98,11 @@ public class ExplicitTypeCanBeDiamondInspection extends BaseJavaBatchLocalInspec
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
if (!FileModificationService.getInstance().preparePsiElementForWrite(element)) return;
PsiDiamondTypeUtil.replaceExplicitWithDiamond(element);
final PsiNewExpression newExpression =
PsiTreeUtil.getParentOfType(PsiDiamondTypeUtil.replaceExplicitWithDiamond(element), PsiNewExpression.class);
if (newExpression != null) {
CodeStyleManager.getInstance(project).reformat(newExpression);
}
}
}
}

View File

@@ -0,0 +1,8 @@
// "Replace with <>" "true"
class Test<T> {
public Test(String s1, String s2) {}
Test<String> s = new Test<>("s1",
"s2");
}

View File

@@ -0,0 +1,8 @@
// "Replace with <>" "true"
class Test<T> {
public Test(String s1, String s2) {}
Test<String> s = new Test<Str<caret>ing>("s1",
"s2");
}

View File

@@ -17,12 +17,17 @@ package com.intellij.codeInsight.daemon.quickFix;
import com.intellij.codeInspection.ExplicitTypeCanBeDiamondInspection;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import org.jetbrains.annotations.NotNull;
//todo test3 should be checked if it compiles - as now javac infers Object instead of String?!
public class Simplify2DiamondInspectionsTest extends LightQuickFixParameterizedTestCase {
@NotNull
@Override
protected LocalInspectionTool[] configureLocalInspectionTools() {
@@ -31,6 +36,26 @@ public class Simplify2DiamondInspectionsTest extends LightQuickFixParameterizedT
};
}
private boolean myAlignment;
@Override
protected void setUp() throws Exception {
super.setUp();
CommonCodeStyleSettings settings = getSettings();
myAlignment = settings.ALIGN_MULTILINE_PARAMETERS_IN_CALLS;
settings.ALIGN_MULTILINE_PARAMETERS_IN_CALLS = true;
}
@Override
protected void tearDown() throws Exception {
getSettings().ALIGN_MULTILINE_PARAMETERS_IN_CALLS = myAlignment;
super.tearDown();
}
private CommonCodeStyleSettings getSettings() {
return CodeStyleSettingsManager.getSettings(getProject()).getCommonSettings(JavaLanguage.INSTANCE);
}
public void test() throws Exception { doAllTests(); }
@Override