in completing an anonymous class, the closing parenthesis should be inserted regardless of the pair bracket settings (IDEA-63392)

This commit is contained in:
peter.gromov
2010-12-23 18:32:16 +03:00
parent 11014fdc4e
commit 4240c6817e
6 changed files with 53 additions and 25 deletions
@@ -48,13 +48,16 @@ class ConstructorInsertHandler implements InsertHandler<LookupElementDecorator<L
final PsiAnonymousClass anonymousClass = PsiTreeUtil.getParentOfType(position, PsiAnonymousClass.class);
final boolean inAnonymous = anonymousClass != null && anonymousClass.getParent() == enclosing;
insertParentheses(context, delegate, delegate.getObject());
boolean withTail = item.getUserData(LookupItem.BRACKETS_COUNT_ATTR) == null && !inAnonymous;
boolean isAbstract = ((PsiClass)item.getObject()).hasModifierProperty(PsiModifier.ABSTRACT);
insertParentheses(context, delegate, delegate.getObject(), withTail && isAbstract);
DefaultInsertHandler.addImportForItem(context.getFile(), context.getStartOffset(), delegate);
if (item.getUserData(LookupItem.BRACKETS_COUNT_ATTR) == null && !inAnonymous) {
if (((PsiClass)item.getObject()).hasModifierProperty(PsiModifier.ABSTRACT)) {
if (withTail) {
if (isAbstract) {
if (mySmart) {
FeatureUsageTracker.getInstance().triggerFeatureUsed(JavaCompletionFeatures.AFTER_NEW_ANONYMOUS);
}
@@ -83,7 +86,10 @@ class ConstructorInsertHandler implements InsertHandler<LookupElementDecorator<L
}
}
public static boolean insertParentheses(InsertionContext context, LookupItem delegate, final PsiClass psiClass) {
public static boolean insertParentheses(InsertionContext context,
LookupItem delegate,
final PsiClass psiClass,
final boolean forAnonymous) {
final PsiElement place = context.getFile().findElementAt(context.getStartOffset());
final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(context.getProject()).getResolveHelper();
assert place != null;
@@ -96,7 +102,7 @@ class ConstructorInsertHandler implements InsertHandler<LookupElementDecorator<L
}
}
JavaCompletionUtil.insertParentheses(context, delegate, false, hasParams);
JavaCompletionUtil.insertParentheses(context, delegate, false, hasParams, forAnonymous);
return hasParams;
}
@@ -87,7 +87,7 @@ class JavaClassNameInsertHandler implements InsertHandler<JavaPsiClassReferenceE
}
if (completingRawConstructor(context, item) && !JavaCompletionUtil.hasAccessibleInnerClass(psiClass, file)) {
if (ConstructorInsertHandler.insertParentheses(context, item, psiClass)) {
if (ConstructorInsertHandler.insertParentheses(context, item, psiClass, false)) {
AutoPopupController.getInstance(project).autoPopupParameterInfo(editor, null);
}
}
@@ -921,7 +921,18 @@ public class JavaCompletionUtil {
return toDelete;
}
public static void insertParentheses(final InsertionContext context, final LookupElement item, boolean overloadsMatter, boolean hasParams) {
public static void insertParentheses(final InsertionContext context,
final LookupElement item,
boolean overloadsMatter,
boolean hasParams) {
insertParentheses(context, item, overloadsMatter, hasParams, false);
}
public static void insertParentheses(final InsertionContext context,
final LookupElement item,
boolean overloadsMatter,
boolean hasParams,
final boolean forceClosingParenthesis) {
final Editor editor = context.getEditor();
final char completionChar = context.getCompletionChar();
final PsiFile file = context.getFile();
@@ -933,28 +944,25 @@ public class JavaCompletionUtil {
final boolean addCompletionChar = context.shouldAddCompletionChar();
context.setAddCompletionChar(false);
final boolean needLeftParenth = isToInsertParenth(file.findElementAt(context.getStartOffset()));
final boolean needRightParenth = !smart && (CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET || hasTail);
final boolean needRightParenth = forceClosingParenthesis || !smart && (CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET || hasTail);
if (hasTail) {
hasParams = false;
}
if (needLeftParenth) {
final CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(context.getProject());
ParenthesesInsertHandler.getInstance(hasParams,
styleSettings.SPACE_BEFORE_METHOD_CALL_PARENTHESES,
styleSettings.SPACE_WITHIN_METHOD_CALL_PARENTHESES && hasParams,
needRightParenth,
styleSettings.METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE
).handleInsert(context, item);
}
final CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(context.getProject());
ParenthesesInsertHandler.getInstance(hasParams,
styleSettings.SPACE_BEFORE_METHOD_CALL_PARENTHESES,
styleSettings.SPACE_WITHIN_METHOD_CALL_PARENTHESES && hasParams,
needRightParenth,
styleSettings.METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE
).handleInsert(context, item);
if (needLeftParenth && hasParams) {
if (hasParams) {
// Invoke parameters popup
AutoPopupController.getInstance(file.getProject()).autoPopupParameterInfo(editor, overloadsMatter ? null : (PsiElement)item.getObject());
}
if (smart || needLeftParenth && needRightParenth && addCompletionChar) {
if (smart || needRightParenth && addCompletionChar) {
TailType toInsert = tailType;
LookupItem lookupItem = item.as(LookupItem.class);
if (lookupItem == null || lookupItem.getAttribute(LookupItem.TAIL_TYPE_ATTR) != TailType.UNKNOWN) {
@@ -970,11 +978,6 @@ public class JavaCompletionUtil {
}
}
public static boolean isToInsertParenth(PsiElement place){
if (place == null) return true;
return !(place.getParent() instanceof PsiImportStaticReferenceElement);
}
//need to shorten references in type argument list
public static void shortenReference(final PsiFile file, final int offset) throws IncorrectOperationException {
final PsiDocumentManager manager = PsiDocumentManager.getInstance(file.getProject());
@@ -0,0 +1,11 @@
public class Bar {
{
new Thread(new Runnable() {
@Override
public void run() {
<selection>//To change body of implemented methods use File | Settings | File Templates.</selection>
}
})
}
}
@@ -0,0 +1,6 @@
public class Bar {
{
new Thread(new Run<caret>)
}
}
@@ -481,6 +481,8 @@ public class SmartTypeCompletionTest extends LightFixtureCompletionTestCase {
public void testNoTailWhenNoPairBracket2() throws Throwable { doTestNoPairBracket(Lookup.NORMAL_SELECT_CHAR); }
public void testAnonymousNoPairBracket() throws Throwable { doTestNoPairBracket(Lookup.NORMAL_SELECT_CHAR); }
private void doTestNoPairBracket(final char c) throws Exception {
boolean old = CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET;
CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET = false;