IDEA-73717 Completion after 'new' and generics

This commit is contained in:
peter
2011-09-02 18:30:37 +02:00
parent 0dbf6b4864
commit c8e59d711c
7 changed files with 33 additions and 3 deletions

View File

@@ -26,10 +26,12 @@ import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.highlighter.HighlighterIterator;
import com.intellij.openapi.project.Project;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.filters.FilterPositionUtil;
import com.intellij.psi.javadoc.PsiDocTag;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.containers.hash.HashSet;
import java.util.Set;
@@ -81,8 +83,12 @@ class JavaClassNameInsertHandler implements InsertHandler<JavaPsiClassReferenceE
}
}
OffsetKey refEnd = context.trackOffset(context.getTailOffset(), false);
boolean fillTypeArgs = false;
if (shouldInsertParentheses(psiClass, position)) {
if (ConstructorInsertHandler.insertParentheses(context, item, psiClass, false)) {
fillTypeArgs = psiClass.hasTypeParameters() && PsiUtil.getLanguageLevel(file).isAtLeast(LanguageLevel.JDK_1_5);
AutoPopupController.getInstance(project).autoPopupParameterInfo(editor, null);
}
}
@@ -118,6 +124,13 @@ class JavaClassNameInsertHandler implements InsertHandler<JavaPsiClassReferenceE
}
}
if (fillTypeArgs) {
int typeArgs = context.getOffset(refEnd);
if (typeArgs >= 0) {
context.getDocument().insertString(typeArgs, "<>");
context.getEditor().getCaretModel().moveToOffset(typeArgs + 1);
}
}
}
private static boolean shouldInsertParentheses(PsiClass psiClass, PsiElement position) {

View File

@@ -0,0 +1,5 @@
public class Driver {
{
new ArrayLi<caret>
}
}

View File

@@ -0,0 +1,7 @@
import java.util.ArrayList;
public class Driver {
{
new ArrayList<<caret>>()
}
}

View File

@@ -899,6 +899,7 @@ public class NormalCompletionTest extends LightFixtureCompletionTestCase {
public void testMethodSquareBracket() throws Throwable { doTest('[') }
public void testMethodParameterTypeDot() throws Throwable { doAntiTest() }
public void testNewGenericClass() throws Throwable { doTest('\n') }
public void testSuperProtectedMethod() throws Throwable {
myFixture.addClass """package foo;

View File

@@ -83,7 +83,7 @@ public class GroovyClassNameInsertHandler implements InsertHandler<JavaPsiClassR
GroovyPsiElement place = PsiTreeUtil.findElementOfClassAtOffset(context.getFile(), context.getStartOffset(), GroovyPsiElement.class, false);
JavaCompletionUtil.insertParentheses(context, item, false, place != null && GroovyCompletionUtil.hasConstructorParameters(psiClass, place));
if (context.getCompletionChar() == '<') {
if (context.getCompletionChar() == '<' || psiClass.hasTypeParameters()) {
context.getDocument().insertString(identifierEnd, "<>");
context.setAddCompletionChar(false);
context.getEditor().getCaretModel().moveToOffset(identifierEnd + 1);

View File

@@ -236,9 +236,9 @@ new Fxoo()<caret>\n"""
}
public void testNewImportedClassName() {
myFixture.configureByText("a.groovy", "new ArrayLi<caret>\n")
myFixture.configureByText("a.groovy", "new ArrayIndexOut<caret>\n")
myFixture.completeBasic()
myFixture.checkResult "new ArrayList(<caret>)\n"
myFixture.checkResult "new ArrayIndexOutOfBoundsException(<caret>)\n"
}
public void testOnlyAnnotationsAfterAt() {

View File

@@ -830,6 +830,10 @@ class X {
doBasicTest()
}
public void testNewClassGenerics() {
checkSingleItemCompletion 'new ArrayLi<caret>', 'new ArrayList<<caret>>()'
}
public void testPropertyBeforeAccessor() {
doVariantableTest 'soSe', 'setSoSe'
}