IDEA-62753 Completing method call with ';' when pair bracket autoinsertion is off

This commit is contained in:
peter.gromov
2010-12-15 18:54:42 +03:00
parent e457bfc021
commit c09bf152d6
8 changed files with 58 additions and 52 deletions
@@ -923,14 +923,21 @@ public class JavaCompletionUtil {
public static void insertParentheses(final InsertionContext context, final LookupElement item, boolean overloadsMatter, boolean hasParams) {
final Editor editor = context.getEditor();
final TailType tailType = getTailType(item, context);
final char completionChar = context.getCompletionChar();
final PsiFile file = context.getFile();
final TailType tailType = completionChar == '(' ? TailType.NONE : LookupItem.handleCompletionChar(context.getEditor(), item, completionChar);
final boolean hasTail = tailType != TailType.NONE && tailType != TailType.UNKNOWN;
final boolean smart = completionChar == Lookup.COMPLETE_STATEMENT_SELECT_CHAR;
final boolean addCompletionChar = context.shouldAddCompletionChar();
context.setAddCompletionChar(false);
final boolean needLeftParenth = isToInsertParenth(file.findElementAt(context.getStartOffset()));
final boolean needRightParenth = tailType != TailType.SMART_COMPLETION && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET;
final boolean needRightParenth = !smart && (CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET || hasTail);
if (hasTail) {
hasParams = false;
}
if (needLeftParenth) {
final CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(context.getProject());
@@ -947,42 +954,20 @@ public class JavaCompletionUtil {
AutoPopupController.getInstance(file.getProject()).autoPopupParameterInfo(editor, overloadsMatter ? null : (PsiElement)item.getObject());
}
if (tailType == TailType.SEMICOLON) {
if (!needRightParenth) {
return;
}
PsiDocumentManager.getInstance(file.getProject()).commitAllDocuments();
if (psiElement().beforeLeaf(psiElement().withText(".")).accepts(file.findElementAt(context.getTailOffset() - 1))) {
return;
if (smart || needLeftParenth && needRightParenth && addCompletionChar) {
TailType toInsert = tailType;
LookupItem lookupItem = item.as(LookupItem.class);
if (lookupItem == null || lookupItem.getAttribute(LookupItem.TAIL_TYPE_ATTR) != TailType.UNKNOWN) {
if (!hasTail && item.getObject() instanceof PsiMethod && ((PsiMethod)item.getObject()).getReturnType() == PsiType.VOID) {
PsiDocumentManager.getInstance(file.getProject()).commitAllDocuments();
if (psiElement().beforeLeaf(psiElement().withText(".")).accepts(file.findElementAt(context.getTailOffset() - 1))) {
return;
}
toInsert = TailType.SEMICOLON;
}
}
toInsert.processTail(editor, context.getTailOffset());
}
if (tailType == TailType.SMART_COMPLETION || needLeftParenth && needRightParenth) {
tailType.processTail(editor, context.getTailOffset());
}
}
@NotNull
public static TailType getTailType(final LookupElement item, InsertionContext context) {
final char completionChar = context.getCompletionChar();
if (completionChar == '!') return item instanceof LookupItem ? ((LookupItem)item).getTailType() : TailType.NONE;
if (completionChar == '(') {
final Object o = item.getObject();
if (o instanceof PsiMethod) {
final PsiMethod psiMethod = (PsiMethod)o;
return psiMethod.getParameterList().getParameters().length > 0 || psiMethod.getReturnType() != PsiType.VOID
? TailType.NONE : TailType.SEMICOLON;
} else if (o instanceof PsiClass) { // it may be a constructor
return TailType.NONE;
}
}
if (completionChar == Lookup.COMPLETE_STATEMENT_SELECT_CHAR) return TailType.SMART_COMPLETION;
if (!context.shouldAddCompletionChar()) {
return TailType.NONE;
}
return LookupItem.handleCompletionChar(context.getEditor(), item, completionChar);
}
public static boolean isToInsertParenth(PsiElement place){
@@ -17,7 +17,6 @@ package com.intellij.codeInsight.completion;
import com.intellij.codeInsight.ExpectedTypeInfo;
import com.intellij.codeInsight.ExpectedTypesProvider;
import com.intellij.codeInsight.TailType;
import com.intellij.codeInsight.completion.util.MethodParenthesesHandler;
import com.intellij.codeInsight.lookup.*;
import com.intellij.codeInsight.lookup.impl.JavaElementLookupRenderer;
@@ -57,8 +56,6 @@ public class JavaMethodCallElement extends LookupItem<PsiMethod> implements Type
LOG.error(method.getName());
}
myCanImportStatic = canImportStatic;
PsiType type = method.getReturnType();
setTailType(PsiType.VOID.equals(type) ? TailType.SEMICOLON : TailType.NONE);
}
public PsiType getType() {
@@ -3,6 +3,6 @@ class MyClass {
void foo(int a) {}
{
foo(<caret>)
foo(<caret>);
}
}
@@ -0,0 +1,5 @@
class A {
{
String c = new String();<caret>
}
}
@@ -0,0 +1,5 @@
class A {
{
String c = new Stri<caret>
}
}
@@ -0,0 +1,8 @@
class A {
void foo(String bar, int a) {}
String zoo(int b) {}
{
foo(zoo(), <caret>)
}
}
@@ -0,0 +1,8 @@
class A {
void foo(String bar, int a) {}
String zoo(int b) {}
{
foo(zo<caret>)
}
}
@@ -477,28 +477,26 @@ public class SmartTypeCompletionTest extends LightFixtureCompletionTestCase {
public void testSameNamedFieldAndLocal() throws Throwable { doTest(); }
public void testNoTailWhenNoPairBracket() throws Throwable {
doTestNoPairBracket();
}
public void testNoTailWhenNoPairBracket() throws Throwable { doTestNoPairBracket(Lookup.NORMAL_SELECT_CHAR); }
public void testNoTailWhenNoPairBracket2() throws Throwable {
doTestNoPairBracket();
}
public void testNoTailWhenNoPairBracket2() throws Throwable { doTestNoPairBracket(Lookup.NORMAL_SELECT_CHAR); }
private void doTestNoPairBracket() throws Exception {
private void doTestNoPairBracket(final char c) throws Exception {
boolean old = CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET;
CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET = false;
try {
doTest();
doTest(c);
}
finally {
CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET = old;
}
}
public void testNoConstructorTailWhenNoPairBracket() throws Throwable {
doTestNoPairBracket();
}
public void testNoConstructorTailWhenNoPairBracket() throws Throwable { doTestNoPairBracket(Lookup.NORMAL_SELECT_CHAR); }
public void testConstructorNoPairBracketSemicolon() throws Throwable { doTestNoPairBracket(';'); }
public void testMethodNoPairBracketComma() throws Throwable { doTestNoPairBracket(','); }
public void testAbstractClassTwice() throws Throwable {
configureByTestName();