diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/ConstructorInsertHandler.java b/java/java-impl/src/com/intellij/codeInsight/completion/ConstructorInsertHandler.java index 73422a5172ce..aa82cc1db9ef 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/ConstructorInsertHandler.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/ConstructorInsertHandler.java @@ -38,33 +38,30 @@ class ConstructorInsertHandler implements InsertHandler item) { @SuppressWarnings({"unchecked"}) final LookupItem delegate = item.getDelegate(); + final PsiElement position = SmartCompletionDecorator.getPosition(context, delegate); + final PsiExpression enclosing = PsiTreeUtil.getContextOfType(position, PsiExpression.class, true); + final PsiAnonymousClass anonymousClass = PsiTreeUtil.getParentOfType(position, PsiAnonymousClass.class); + final boolean inAnonymous = anonymousClass != null && anonymousClass.getParent() == enclosing; + insertParentheses(context, delegate, delegate.getObject()); DefaultInsertHandler.addImportForItem(context.getFile(), context.getStartOffset(), delegate); - final PsiElement position = SmartCompletionDecorator.getPosition(context, delegate); - final PsiExpression enclosing = PsiTreeUtil.getContextOfType(position, PsiExpression.class, true); - if (item.getUserData(LookupItem.BRACKETS_COUNT_ATTR) == null) { - final PsiAnonymousClass anonymousClass = PsiTreeUtil.getParentOfType(position, PsiAnonymousClass.class); - if (anonymousClass == null || anonymousClass.getParent() != enclosing) { + if (item.getUserData(LookupItem.BRACKETS_COUNT_ATTR) == null && !inAnonymous) { + if (((PsiClass)item.getObject()).hasModifierProperty(PsiModifier.ABSTRACT)) { + FeatureUsageTracker.getInstance().triggerFeatureUsed("editing.completion.smarttype.anonymous"); - final PsiClass psiClass = (PsiClass)item.getObject(); + PostprocessReformattingAspect.getInstance(context.getProject()).doPostponedFormatting(context.getFile().getViewProvider()); - if (psiClass.hasModifierProperty(PsiModifier.ABSTRACT) || psiClass.isInterface()) { - FeatureUsageTracker.getInstance().triggerFeatureUsed("editing.completion.smarttype.anonymous"); - - PostprocessReformattingAspect.getInstance(context.getProject()).doPostponedFormatting(context.getFile().getViewProvider()); - - final Editor editor = context.getEditor(); - final int offset = context.getTailOffset(); - editor.getDocument().insertString(offset, " {}"); - editor.getCaretModel().moveToOffset(offset + 2); - context.setLaterRunnable(generateAnonymousBody(editor, context.getFile())); - } - else { - FeatureUsageTracker.getInstance().triggerFeatureUsed("editing.completion.smarttype.afternew"); - } + final Editor editor = context.getEditor(); + final int offset = context.getTailOffset(); + editor.getDocument().insertString(offset, " {}"); + editor.getCaretModel().moveToOffset(offset + 2); + context.setLaterRunnable(generateAnonymousBody(editor, context.getFile())); + } + else { + FeatureUsageTracker.getInstance().triggerFeatureUsed("editing.completion.smarttype.afternew"); } } } diff --git a/java/java-tests/testData/codeInsight/completion/smartType/NewAbstractInsideAnonymous-out.java b/java/java-tests/testData/codeInsight/completion/smartType/NewAbstractInsideAnonymous-out.java new file mode 100644 index 000000000000..3c290421b29b --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/NewAbstractInsideAnonymous-out.java @@ -0,0 +1,19 @@ +public class Foo { + + public Foo() { + Runnable r = new Runnable() { + public void run() { + Goo g = new Goo() { + @Override + void foo() { + //To change body of implemented methods use File | Settings | File Templates. + } + }; + } + }; + } +} + +abstract class Goo { + abstract void foo(); +} diff --git a/java/java-tests/testData/codeInsight/completion/smartType/NewAbstractInsideAnonymous.java b/java/java-tests/testData/codeInsight/completion/smartType/NewAbstractInsideAnonymous.java new file mode 100644 index 000000000000..b3a141712bd7 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/smartType/NewAbstractInsideAnonymous.java @@ -0,0 +1,14 @@ +public class Foo { + + public Foo() { + Runnable r = new Runnable() { + public void run() { + Goo g = new G + } + }; + } +} + +abstract class Goo { + abstract void foo(); +} diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionTest.java index 2eb7e0ff5d59..9932fd937875 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionTest.java @@ -951,6 +951,8 @@ public class SmartTypeCompletionTest extends LightCompletionTestCase { public void testEnumAsDefaultAnnotationParam() throws Throwable { doTest(); } + public void testNewAbstractInsideAnonymous() throws Throwable { doTest(); } + public void testFilterPrivateConstructors() throws Throwable { doTest(); } public void testExplicitMethodTypeParametersQualify() throws Throwable { doTest(); }