constructor completion: insert the chosen constructor and position the caret accordingly (IDEA-CR-14717)

This commit is contained in:
peter
2016-10-19 19:27:57 +02:00
parent 87a28d8d6e
commit 5b863d7817
5 changed files with 28 additions and 5 deletions
@@ -214,9 +214,11 @@ public class ConstructorInsertHandler implements InsertHandler<LookupElementDeco
return false;
}
PsiMethod constructor = JavaConstructorCallElement.extractCalledConstructor(delegate);
final PsiElement place = context.getFile().findElementAt(context.getStartOffset());
assert place != null;
boolean hasParams = hasConstructorParameters(psiClass, place);
boolean hasParams = constructor != null ? constructor.getParameterList().getParametersCount() > 0 : hasConstructorParameters(psiClass, place);
JavaCompletionUtil.insertParentheses(context, delegate, false, hasParams, forAnonymous);
@@ -49,8 +49,6 @@ public class JavaConstructorCallElement extends LookupElementDecorator<LookupEle
myConstructor = constructor;
myType = type;
mySubstitutor = myType.resolveGenerics().getSubstitutor();
markClassItemWrapped(classItem);
}
private void markClassItemWrapped(@NotNull LookupElement classItem) {
@@ -62,6 +60,12 @@ public class JavaConstructorCallElement extends LookupElementDecorator<LookupEle
}
}
@Override
public void handleInsert(InsertionContext context) {
markClassItemWrapped(getDelegate());
super.handleInsert(context);
}
@NotNull
@Override
public PsiMethod getObject() {
@@ -1,10 +1,10 @@
class Foo{
Foo(int arg) {
}
Foo(boolean arg) {
}
Foo() {
}
Foo(boolean arg) {
}
{
Foo f = new F<caret>
@@ -0,0 +1,12 @@
class Foo{
Foo(int arg) {
}
Foo() {
}
Foo(boolean arg) {
}
{
Foo f = new Foo();<caret>
}
}
@@ -18,6 +18,8 @@ package com.intellij.codeInsight.completion
import com.intellij.JavaTestUtil
import com.intellij.codeInsight.template.impl.TemplateManagerImpl
import com.intellij.openapi.util.registry.Registry
import com.intellij.psi.PsiMethod
/**
* @author peter
*/
@@ -64,6 +66,9 @@ class SignatureCompletionTest extends LightFixtureCompletionTestCase {
myFixture.complete(CompletionType.SMART)
def items = myFixture.lookup.items
assert items.size() == 3
assert ((PsiMethod) items[0].object).parameterList.parametersCount == 0
myFixture.type('\n')
checkResult()
}
}