From 095f4e73a7cda5d62d2ae683e15cdac70387bb66 Mon Sep 17 00:00:00 2001 From: Dmitry Avdeev Date: Fri, 15 May 2009 11:38:16 +0400 Subject: [PATCH] CreateMethodFromUsageFix refactored --- .../impl/quickfix/CreateMethodFromUsageFix.java | 113 +++++++++++---------- .../daemon/impl/quickfix/GuessTypeParameters.java | 3 +- 2 files changed, 60 insertions(+), 56 deletions(-) diff --git a/codeInsight/impl/com/intellij/codeInsight/daemon/impl/quickfix/CreateMethodFromUsageFix.java b/codeInsight/impl/com/intellij/codeInsight/daemon/impl/quickfix/CreateMethodFromUsageFix.java index d9cdd19bf1..196db586eb 100644 --- a/codeInsight/impl/com/intellij/codeInsight/daemon/impl/quickfix/CreateMethodFromUsageFix.java +++ b/codeInsight/impl/com/intellij/codeInsight/daemon/impl/quickfix/CreateMethodFromUsageFix.java @@ -94,7 +94,8 @@ public class CreateMethodFromUsageFix extends CreateFromUsageBaseFix { return result; } - protected void invokeImpl(PsiClass targetClass) { + protected void invokeImpl(final PsiClass targetClass) { + if (targetClass == null) return; PsiMethodCallExpression expression = getMethodCall(); if (expression == null) return; @@ -106,9 +107,6 @@ public class CreateMethodFromUsageFix extends CreateFromUsageBaseFix { PsiClass parentClass = PsiTreeUtil.getParentOfType(expression, PsiClass.class); PsiMember enclosingContext = PsiTreeUtil.getParentOfType(expression, PsiMethod.class, PsiField.class, PsiClassInitializer.class); - - final PsiFile targetFile = targetClass.getContainingFile(); - String methodName = ref.getReferenceName(); LOG.assertTrue(methodName != null); @@ -151,69 +149,74 @@ public class CreateMethodFromUsageFix extends CreateFromUsageBaseFix { PsiUtil.setModifierProperty(method, PsiModifier.STATIC, true); } - final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project); - final Document document = documentManager.getDocument(targetFile); - targetClass = method.getContainingClass(); final PsiElement context = PsiTreeUtil.getParentOfType(expression, PsiClass.class, PsiMethod.class); method = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(method); if (method == null) return; - body = method.getBody(); - TemplateBuilder builder = new TemplateBuilder(method); - - PsiMethodCallExpression methodCall = getMethodCall(); - if (methodCall == null) return; - PsiExpressionList argumentList = methodCall.getArgumentList(); - - final PsiSubstitutor substitutor = getTargetSubstitutor(methodCall); - CreateFromUsageUtils.setupMethodParameters(method, builder, argumentList, substitutor); - final ExpectedTypeInfo[] expectedTypes = CreateFromUsageUtils.guessExpectedTypes(methodCall, true); - new GuessTypeParameters(factory) - .setupTypeElement(method.getReturnTypeElement(), expectedTypes, substitutor, builder, context, targetClass); - builder.setEndVariableAfter(targetClass.isInterface() ? method : body.getLBrace()); - method = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(method); - if (method == null) return; - RangeMarker rangeMarker = document.createRangeMarker(method.getTextRange()); - final Editor newEditor = positionCursor(project, targetFile, method); - Template template = builder.buildTemplate(); - newEditor.getCaretModel().moveToOffset(rangeMarker.getStartOffset()); - newEditor.getDocument().deleteString(rangeMarker.getStartOffset(), rangeMarker.getEndOffset()); - - if (!targetClass.isInterface()) { - startTemplate(newEditor, template, project, new TemplateEditingAdapter() { - public void templateFinished(Template template) { - ApplicationManager.getApplication().runWriteAction(new Runnable() { - public void run() { - PsiDocumentManager.getInstance(project).commitDocument(newEditor.getDocument()); - final int offset = newEditor.getCaretModel().getOffset(); - PsiMethod method = PsiTreeUtil.findElementOfClassAtOffset(targetFile, offset - 1, PsiMethod.class, false); - - if (method != null) { - try { - CreateFromUsageUtils.setupMethodBody(method); - } - catch (IncorrectOperationException e) { - LOG.error(e); - } - - CreateFromUsageUtils.setupEditor(method, newEditor); - } - } - }); - } - }); - } - else { - startTemplate(newEditor, template, project); - } + doCreate(targetClass, method, expression.getArgumentList().getExpressions(), context, getTargetSubstitutor(expression), + CreateFromUsageUtils.guessExpectedTypes(expression, true)); } catch (IncorrectOperationException e) { LOG.error(e); } } + public static void doCreate(PsiClass targetClass, PsiMethod method, PsiExpression[] expressions, @Nullable PsiElement context, + PsiSubstitutor substitutor, ExpectedTypeInfo[] expectedTypes) { + + final Project project = targetClass.getProject(); + final PsiFile targetFile = targetClass.getContainingFile(); + Document document = PsiDocumentManager.getInstance(project).getDocument(targetFile); + + TemplateBuilder builder = new TemplateBuilder(method); + + CreateFromUsageUtils.setupMethodParameters(method, builder, context, substitutor, expressions); + new GuessTypeParameters(JavaPsiFacade.getInstance(project).getElementFactory()) + .setupTypeElement(method.getReturnTypeElement(), expectedTypes, substitutor, builder, context, targetClass); + PsiCodeBlock body = method.getBody(); + assert body != null; + builder.setEndVariableAfter(targetClass.isInterface() ? method : body.getLBrace()); + method = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(method); + if (method == null) return; + + assert document != null; + RangeMarker rangeMarker = document.createRangeMarker(method.getTextRange()); + final Editor newEditor = positionCursor(project, targetFile, method); + Template template = builder.buildTemplate(); + newEditor.getCaretModel().moveToOffset(rangeMarker.getStartOffset()); + newEditor.getDocument().deleteString(rangeMarker.getStartOffset(), rangeMarker.getEndOffset()); + + if (!targetClass.isInterface()) { + startTemplate(newEditor, template, project, new TemplateEditingAdapter() { + public void templateFinished(Template template) { + ApplicationManager.getApplication().runWriteAction(new Runnable() { + public void run() { + PsiDocumentManager.getInstance(project).commitDocument(newEditor.getDocument()); + final int offset = newEditor.getCaretModel().getOffset(); + PsiMethod method = PsiTreeUtil.findElementOfClassAtOffset(targetFile, offset - 1, PsiMethod.class, false); + + if (method != null) { + try { + CreateFromUsageUtils.setupMethodBody(method); + } + catch (IncorrectOperationException e) { + LOG.error(e); + } + + CreateFromUsageUtils.setupEditor(method, newEditor); + } + } + }); + } + }); + } + else { + startTemplate(newEditor, template, project); + } + } + protected boolean isValidElement(PsiElement element) { PsiMethodCallExpression callExpression = (PsiMethodCallExpression) element; PsiReferenceExpression referenceExpression = callExpression.getMethodExpression(); diff --git a/codeInsight/impl/com/intellij/codeInsight/daemon/impl/quickfix/GuessTypeParameters.java b/codeInsight/impl/com/intellij/codeInsight/daemon/impl/quickfix/GuessTypeParameters.java index 18611f5df5..038c6f7385 100644 --- a/codeInsight/impl/com/intellij/codeInsight/daemon/impl/quickfix/GuessTypeParameters.java +++ b/codeInsight/impl/com/intellij/codeInsight/daemon/impl/quickfix/GuessTypeParameters.java @@ -11,6 +11,7 @@ import com.intellij.psi.*; import com.intellij.psi.search.GlobalSearchScope; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Arrays; @@ -54,7 +55,7 @@ public class GuessTypeParameters { } public void setupTypeElement (PsiTypeElement typeElement, ExpectedTypeInfo[] infos, PsiSubstitutor substitutor, - TemplateBuilder builder, PsiElement context, PsiClass targetClass) { + TemplateBuilder builder, @Nullable PsiElement context, PsiClass targetClass) { LOG.assertTrue(typeElement.isValid()); ApplicationManager.getApplication().assertWriteAccessAllowed(); -- 2.11.4.GIT