create method from usage: create method type params if needed (IDEA-55505)

This commit is contained in:
anna
2010-11-12 16:11:22 +03:00
parent 9de3abe249
commit edc0799f69
3 changed files with 56 additions and 2 deletions

View File

@@ -31,12 +31,14 @@ import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.RangeMarker;
import com.intellij.openapi.editor.ex.RangeMarkerEx;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.refactoring.util.FieldConflictsResolver;
import com.intellij.refactoring.util.RefactoringUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.Processor;
import com.intellij.util.containers.ContainerUtil;
@@ -202,7 +204,7 @@ public class CreateMethodFromUsageFix extends CreateFromUsageBaseFix {
List<Pair<PsiExpression, PsiType>> arguments,
PsiSubstitutor substitutor,
ExpectedTypeInfo[] expectedTypes,
@Nullable PsiElement context) {
@Nullable final PsiElement context) {
method = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(method);
@@ -239,7 +241,16 @@ public class CreateMethodFromUsageFix extends CreateFromUsageBaseFix {
PsiDocumentManager.getInstance(project).commitDocument(newEditor.getDocument());
final int offset = newEditor.getCaretModel().getOffset();
PsiMethod method = PsiTreeUtil.findElementOfClassAtOffset(targetFile, offset - 1, PsiMethod.class, false);
if (context instanceof PsiMethod) {
final PsiTypeParameter[] typeParameters = ((PsiMethod)context).getTypeParameters();
if (typeParameters.length > 0) {
for (PsiTypeParameter typeParameter : typeParameters) {
if (checkTypeParam( method, typeParameter)) {
method.getTypeParameterList().add(typeParameter);
}
}
}
}
if (method != null) {
try {
CreateFromUsageUtils.setupMethodBody(method);
@@ -260,6 +271,29 @@ public class CreateMethodFromUsageFix extends CreateFromUsageBaseFix {
}
}
private static boolean checkTypeParam(final PsiElement typeElement,
final PsiTypeParameter typeParameter) {
final String typeParameterName = typeParameter.getName();
final boolean[] found = new boolean[] {false};
typeElement.accept(new JavaRecursiveElementWalkingVisitor(){
@Override
public void visitElement(PsiElement element) {
if (found[0]) return;
super.visitElement(element);
}
@Override
public void visitTypeElement(PsiTypeElement type) {
super.visitTypeElement(type);
if (Comparing.strEqual(typeParameterName, type.getText())) {
found[0] = true;
}
}
});
return found[0];
}
protected boolean shouldBeAbstract(PsiClass targetClass) {
return shouldBeAbstractImpl(targetClass);
}

View File

@@ -0,0 +1,12 @@
// "Create Method 'f'" "true"
class A {
<T> T foo(){
B<T> x = f();
}
private <T> B<T> f() {
<selection>return null; //To change body of created methods use File | Settings | File Templates.</selection>
}
}
class B<K>{}

View File

@@ -0,0 +1,8 @@
// "Create Method 'f'" "true"
class A {
<T> T foo(){
B<T> x = f<caret>();
}
}
class B<K>{}