Search in sources :

Example 1 with AbstractInplaceIntroducer

use of com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer in project intellij-community by JetBrains.

the class AbstractInplaceIntroduceTest method doTest.

protected void doTest(final Pass<AbstractInplaceIntroducer> pass) {
    String name = getTestName(true);
    configureByFile(getBasePath() + name + getExtension());
    final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled();
    try {
        TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
        getEditor().getSettings().setVariableInplaceRenameEnabled(true);
        final AbstractInplaceIntroducer introducer = invokeRefactoring();
        pass.pass(introducer);
        TemplateState state = TemplateManagerImpl.getTemplateState(InjectedLanguageUtil.getTopLevelEditor(getEditor()));
        assert state != null;
        state.gotoEnd(false);
        checkResultByFile(getBasePath() + name + "_after" + getExtension());
    } finally {
        getEditor().getSettings().setVariableInplaceRenameEnabled(enabled);
    }
}
Also used : AbstractInplaceIntroducer(com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Example 2 with AbstractInplaceIntroducer

use of com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer in project intellij-community by JetBrains.

the class IntroduceParameterHandler method introduceStrategy.

@VisibleForTesting
public boolean introduceStrategy(final Project project, final Editor editor, PsiFile file, final PsiElement[] elements) {
    if (elements.length > 0) {
        final AbstractInplaceIntroducer inplaceIntroducer = AbstractInplaceIntroducer.getActiveIntroducer(editor);
        if (inplaceIntroducer instanceof InplaceIntroduceParameterPopup) {
            return false;
        }
        final PsiMethod containingMethod = Util.getContainingMethod(elements[0]);
        if (containingMethod == null) {
            return false;
        }
        final List<PsiMethod> enclosingMethods = getEnclosingMethods(containingMethod);
        if (enclosingMethods.isEmpty()) {
            return false;
        }
        final PsiElement[] elementsCopy;
        if (!elements[0].isPhysical()) {
            elementsCopy = elements;
        } else {
            final PsiFile copy = PsiFileFactory.getInstance(project).createFileFromText(file.getName(), file.getFileType(), file.getText(), file.getModificationStamp(), false);
            final TextRange range = new TextRange(elements[0].getTextRange().getStartOffset(), elements[elements.length - 1].getTextRange().getEndOffset());
            final PsiExpression exprInRange = CodeInsightUtil.findExpressionInRange(copy, range.getStartOffset(), range.getEndOffset());
            elementsCopy = exprInRange != null ? new PsiElement[] { exprInRange } : CodeInsightUtil.findStatementsInRange(copy, range.getStartOffset(), range.getEndOffset());
        }
        final PsiMethod containingMethodCopy = Util.getContainingMethod(elementsCopy[0]);
        LOG.assertTrue(containingMethodCopy != null);
        final List<PsiMethod> enclosingMethodsInCopy = getEnclosingMethods(containingMethodCopy);
        final MyExtractMethodProcessor processor = new MyExtractMethodProcessor(project, editor, elementsCopy, enclosingMethodsInCopy.get(enclosingMethodsInCopy.size() - 1));
        try {
            if (!processor.prepare())
                return false;
            processor.showDialog();
            //provide context for generated method to check exceptions compatibility
            final PsiMethod emptyMethod = JavaPsiFacade.getElementFactory(project).createMethodFromText(processor.generateEmptyMethod("name").getText(), elements[0]);
            final Collection<? extends PsiType> types = FunctionalInterfaceSuggester.suggestFunctionalInterfaces(emptyMethod);
            if (types.isEmpty()) {
                return false;
            }
            if (types.size() == 1 || ApplicationManager.getApplication().isUnitTestMode()) {
                final PsiType next = types.iterator().next();
                functionalInterfaceSelected(next, enclosingMethods, project, editor, processor, elements);
            } else {
                final Map<PsiClass, PsiType> classes = new LinkedHashMap<>();
                for (PsiType type : types) {
                    classes.put(PsiUtil.resolveClassInType(type), type);
                }
                final PsiClass[] psiClasses = classes.keySet().toArray(new PsiClass[classes.size()]);
                final String methodSignature = PsiFormatUtil.formatMethod(emptyMethod, PsiSubstitutor.EMPTY, PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_TYPE);
                final PsiType returnType = emptyMethod.getReturnType();
                LOG.assertTrue(returnType != null);
                final String title = "Choose Applicable Functional Interface: " + methodSignature + " -> " + returnType.getPresentableText();
                NavigationUtil.getPsiElementPopup(psiClasses, new PsiClassListCellRenderer(), title, new PsiElementProcessor<PsiClass>() {

                    @Override
                    public boolean execute(@NotNull PsiClass psiClass) {
                        functionalInterfaceSelected(classes.get(psiClass), enclosingMethods, project, editor, processor, elements);
                        return true;
                    }
                }).showInBestPositionFor(editor);
                return true;
            }
            return true;
        } catch (IncorrectOperationException | PrepareFailedException ignore) {
        }
    }
    return false;
}
Also used : PrepareFailedException(com.intellij.refactoring.extractMethod.PrepareFailedException) AbstractInplaceIntroducer(com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) PsiClassListCellRenderer(com.intellij.ide.util.PsiClassListCellRenderer) IncorrectOperationException(com.intellij.util.IncorrectOperationException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with AbstractInplaceIntroducer

use of com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer in project intellij-community by JetBrains.

the class InplaceIntroduceVariableTest method doTestTypeChange.

private void doTestTypeChange(final String newType) {
    final Pass<AbstractInplaceIntroducer> typeChanger = new Pass<AbstractInplaceIntroducer>() {

        @Override
        public void pass(AbstractInplaceIntroducer inplaceIntroduceFieldPopup) {
            type(newType);
        }
    };
    String name = getTestName(true);
    configureByFile(getBasePath() + name + getExtension());
    final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled();
    try {
        TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
        getEditor().getSettings().setVariableInplaceRenameEnabled(true);
        final AbstractInplaceIntroducer introducer = invokeRefactoring();
        TemplateState state = TemplateManagerImpl.getTemplateState(getEditor());
        assert state != null;
        state.previousTab();
        typeChanger.pass(introducer);
        state.gotoEnd(false);
        checkResultByFile(getBasePath() + name + "_after" + getExtension());
    } finally {
        getEditor().getSettings().setVariableInplaceRenameEnabled(enabled);
    }
}
Also used : Pass(com.intellij.openapi.util.Pass) AbstractInplaceIntroducer(com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Example 4 with AbstractInplaceIntroducer

use of com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer in project intellij-community by JetBrains.

the class BaseExpressionToFieldHandler method invokeImpl.

protected boolean invokeImpl(final Project project, @NotNull final PsiExpression selectedExpr, final Editor editor) {
    final PsiElement element = getPhysicalElement(selectedExpr);
    final PsiFile file = element.getContainingFile();
    LOG.assertTrue(file != null, "expr.getContainingFile() == null");
    if (LOG.isDebugEnabled()) {
        LOG.debug("expression:" + selectedExpr);
    }
    final PsiType tempType = getTypeByExpression(selectedExpr);
    if (tempType == null || LambdaUtil.notInferredType(tempType)) {
        String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("unknown.expression.type"));
        CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), getHelpID());
        return false;
    }
    if (PsiType.VOID.equals(tempType)) {
        String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("selected.expression.has.void.type"));
        CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), getHelpID());
        return false;
    }
    myParentClass = getParentClass(selectedExpr);
    final List<PsiClass> classes = new ArrayList<>();
    PsiClass aClass = myParentClass;
    while (aClass != null) {
        classes.add(aClass);
        final PsiField psiField = ConvertToFieldRunnable.checkForwardRefs(selectedExpr, aClass);
        if (psiField != null && psiField.getParent() == aClass)
            break;
        aClass = PsiTreeUtil.getParentOfType(aClass, PsiClass.class, true);
    }
    final AbstractInplaceIntroducer activeIntroducer = AbstractInplaceIntroducer.getActiveIntroducer(editor);
    final boolean shouldSuggestDialog = activeIntroducer instanceof InplaceIntroduceConstantPopup && activeIntroducer.startsOnTheSameElement(selectedExpr, null);
    if (classes.size() == 1 || editor == null || ApplicationManager.getApplication().isUnitTestMode() || shouldSuggestDialog) {
        return !convertExpressionToField(selectedExpr, editor, file, project, tempType);
    } else if (!classes.isEmpty()) {
        PsiClass selection = AnonymousTargetClassPreselectionUtil.getPreselection(classes, myParentClass);
        NavigationUtil.getPsiElementPopup(classes.toArray(new PsiClass[classes.size()]), new PsiClassListCellRenderer(), "Choose class to introduce " + (myIsConstant ? "constant" : "field"), new PsiElementProcessor<PsiClass>() {

            @Override
            public boolean execute(@NotNull PsiClass aClass) {
                AnonymousTargetClassPreselectionUtil.rememberSelection(aClass, myParentClass);
                myParentClass = aClass;
                convertExpressionToField(selectedExpr, editor, file, project, tempType);
                return false;
            }
        }, selection).showInBestPositionFor(editor);
    }
    return true;
}
Also used : AbstractInplaceIntroducer(com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer) ArrayList(java.util.ArrayList) PsiClassListCellRenderer(com.intellij.ide.util.PsiClassListCellRenderer)

Example 5 with AbstractInplaceIntroducer

use of com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer in project intellij-community by JetBrains.

the class AbstractInplaceIntroduceTest method doTestEscape.

protected void doTestEscape(Pass<AbstractInplaceIntroducer> pass) {
    String name = getTestName(true);
    configureByFile(getBasePath() + name + getExtension());
    final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled();
    try {
        TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
        getEditor().getSettings().setVariableInplaceRenameEnabled(true);
        final AbstractInplaceIntroducer introducer = invokeRefactoring();
        if (pass != null) {
            pass.pass(introducer);
        }
        TemplateState state = TemplateManagerImpl.getTemplateState(getEditor());
        assert state != null;
        state.gotoEnd(true);
        checkResultByFile(getBasePath() + name + "_after" + getExtension());
    } finally {
        getEditor().getSettings().setVariableInplaceRenameEnabled(enabled);
    }
}
Also used : AbstractInplaceIntroducer(com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer) TemplateState(com.intellij.codeInsight.template.impl.TemplateState)

Aggregations

AbstractInplaceIntroducer (com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer)13 TemplateState (com.intellij.codeInsight.template.impl.TemplateState)6 PsiClassListCellRenderer (com.intellij.ide.util.PsiClassListCellRenderer)3 Pass (com.intellij.openapi.util.Pass)2 PsiElementProcessor (com.intellij.psi.search.PsiElementProcessor)2 TypeSelectorManagerImpl (com.intellij.refactoring.ui.TypeSelectorManagerImpl)2 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 TextRange (com.intellij.openapi.util.TextRange)1 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)1 PrepareFailedException (com.intellij.refactoring.extractMethod.PrepareFailedException)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1