correctly generate anonymos body when completing after new inside another anonymous

This commit is contained in:
peter
2010-05-31 21:44:06 +01:00
parent 46bd89c489
commit 7c51c98375
4 changed files with 52 additions and 20 deletions
@@ -38,33 +38,30 @@ class ConstructorInsertHandler implements InsertHandler<LookupElementDecorator<L
public void handleInsert(InsertionContext context, LookupElementDecorator<LookupItem> item) {
@SuppressWarnings({"unchecked"}) final LookupItem<PsiClass> 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");
}
}
}
@@ -0,0 +1,19 @@
public class Foo {
public Foo() {
Runnable r = new Runnable() {
public void run() {
Goo<String> g = new Goo<String>() {
@Override
void foo() {
<selection>//To change body of implemented methods use File | Settings | File Templates.</selection>
}
};
}
};
}
}
abstract class Goo<T> {
abstract void foo();
}
@@ -0,0 +1,14 @@
public class Foo {
public Foo() {
Runnable r = new Runnable() {
public void run() {
Goo<String> g = new G<caret>
}
};
}
}
abstract class Goo<T> {
abstract void foo();
}
@@ -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(); }