IDEA-124609 Unexpected indentation of anonymous classes

This commit is contained in:
Yaroslav Lepenkin
2014-05-26 15:32:57 +04:00
parent 41c5b8817e
commit 775e4cc2d3
6 changed files with 106 additions and 3 deletions
@@ -14,6 +14,7 @@ import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.command.UndoConfirmationPolicy;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ScrollType;
import com.intellij.openapi.project.Project;
@@ -22,7 +23,7 @@ import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.impl.source.PostprocessReformattingAspect;
import com.intellij.psi.infos.CandidateInfo;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.PsiUtilCore;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -111,13 +112,19 @@ public class ConstructorInsertHandler implements InsertHandler<LookupElementDeco
PostprocessReformattingAspect.getInstance(context.getProject()).doPostponedFormatting(context.getFile().getViewProvider());
final Editor editor = context.getEditor();
final Document document = editor.getDocument();
final int offset = context.getTailOffset();
editor.getDocument().insertString(offset, " {}");
document.insertString(offset, " {}");
editor.getCaretModel().moveToOffset(offset + 2);
final PsiFile file = context.getFile();
PsiDocumentManager.getInstance(file.getProject()).commitDocument(document);
reformatEnclosingExpressionListAtOffset(file, offset);
if (fillTypeArgs && JavaCompletionUtil.promptTypeArgs(context, context.getOffset(insideRef))) return;
context.setLaterRunnable(generateAnonymousBody(editor, context.getFile()));
context.setLaterRunnable(generateAnonymousBody(editor, file));
}
else {
PsiDocumentManager.getInstance(context.getProject()).commitAllDocuments();
@@ -136,6 +143,28 @@ public class ConstructorInsertHandler implements InsertHandler<LookupElementDeco
}
}
private static void reformatEnclosingExpressionListAtOffset(@NotNull PsiFile file, int offset) {
final PsiElement elementAtOffset = PsiUtilCore.getElementAtOffset(file, offset);
PsiExpressionList listToReformat = getEnclosingExpressionList(elementAtOffset.getParent());
if (listToReformat != null) {
CodeStyleManager.getInstance(file.getProject()).reformat(listToReformat);
}
}
@Nullable
private static PsiExpressionList getEnclosingExpressionList(@NotNull PsiElement element) {
if (!(element instanceof PsiAnonymousClass)) {
return null;
}
PsiElement e = element.getParent();
if (e instanceof PsiNewExpression && e.getParent() instanceof PsiExpressionList) {
return (PsiExpressionList)e.getParent();
}
return null;
}
static boolean isRawTypeExpected(InsertionContext context, PsiTypeLookupItem delegate) {
PsiNewExpression newExpr =
PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), PsiNewExpression.class, false);
@@ -0,0 +1,15 @@
public class Test {
public static void run(Runnable runnable) {
}
public static void main(String[] args) {
run(
new Runnable() {
@Override
public void run() {
<caret>
}
});
}
}
@@ -0,0 +1,9 @@
public class Test {
public static void run(Runnable runnable) {
}
public static void main(String[] args) {
run(new <caret>)
}
}
@@ -0,0 +1,16 @@
public class Test {
public static void run(int times, Runnable runnable) {
}
public static void main(String[] args) {
run(
123, new Runnable() {
@Override
public void run() {
<caret>
}
}
);
}
}
@@ -0,0 +1,9 @@
public class Test {
public static void run(int times, Runnable runnable) {
}
public static void main(String[] args) {
run(123, new <caret>)
}
}
@@ -665,6 +665,31 @@ public class SmartTypeCompletionTest extends LightFixtureCompletionTestCase {
public void testNewAnonymousFunction() throws Throwable { doTest(); }
public void testNewRunnableInsideMethod() throws Throwable {
CommonCodeStyleSettings settings = getCodeStyleSettings();
boolean lParenOnNextLine = settings.CALL_PARAMETERS_LPAREN_ON_NEXT_LINE;
try {
settings.CALL_PARAMETERS_LPAREN_ON_NEXT_LINE = true;
doTest();
} finally {
settings.CALL_PARAMETERS_LPAREN_ON_NEXT_LINE = lParenOnNextLine;
}
}
public void testNewRunnableInsideMethodMultiParams() throws Throwable {
CommonCodeStyleSettings settings = getCodeStyleSettings();
boolean lParenOnNextLine = settings.CALL_PARAMETERS_LPAREN_ON_NEXT_LINE;
boolean rParenOnNextLine = settings.CALL_PARAMETERS_RPAREN_ON_NEXT_LINE;
try {
settings.CALL_PARAMETERS_LPAREN_ON_NEXT_LINE = true;
settings.CALL_PARAMETERS_RPAREN_ON_NEXT_LINE = true;
doTest();
} finally {
settings.CALL_PARAMETERS_LPAREN_ON_NEXT_LINE = lParenOnNextLine;
settings.CALL_PARAMETERS_RPAREN_ON_NEXT_LINE = rParenOnNextLine;
}
}
public void testUseIntConstantsFromTargetClass() throws Throwable { doTest(); }
public void testUseIntConstantsFromTargetClassReturnValue() throws Throwable { doTest(); }
public void testUseIntConstantsFromConstructedClass() throws Throwable { doTest(); }