Search in sources :

Example 6 with AbstractInplaceIntroducer

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

the class IntroduceConstantHandler method showRefactoringDialog.

protected Settings showRefactoringDialog(Project project, final Editor editor, PsiClass parentClass, PsiExpression expr, PsiType type, PsiExpression[] occurrences, PsiElement anchorElement, PsiElement anchorElementIfAll) {
    final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(expr != null ? expr : anchorElement, PsiMethod.class);
    PsiLocalVariable localVariable = null;
    if (expr instanceof PsiReferenceExpression) {
        PsiElement ref = ((PsiReferenceExpression) expr).resolve();
        if (ref instanceof PsiLocalVariable) {
            localVariable = (PsiLocalVariable) ref;
        }
    } else if (anchorElement instanceof PsiLocalVariable) {
        localVariable = (PsiLocalVariable) anchorElement;
    }
    String enteredName = null;
    boolean replaceAllOccurrences = true;
    final AbstractInplaceIntroducer activeIntroducer = AbstractInplaceIntroducer.getActiveIntroducer(editor);
    if (activeIntroducer != null) {
        activeIntroducer.stopIntroduce(editor);
        expr = (PsiExpression) activeIntroducer.getExpr();
        localVariable = (PsiLocalVariable) activeIntroducer.getLocalVariable();
        occurrences = (PsiExpression[]) activeIntroducer.getOccurrences();
        enteredName = activeIntroducer.getInputName();
        replaceAllOccurrences = activeIntroducer.isReplaceAllOccurrences();
        type = ((InplaceIntroduceConstantPopup) activeIntroducer).getType();
    }
    for (PsiExpression occurrence : occurrences) {
        if (RefactoringUtil.isAssignmentLHS(occurrence)) {
            String message = RefactoringBundle.getCannotRefactorMessage("Selected expression is used for write");
            CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, getHelpID());
            highlightError(project, editor, occurrence);
            return null;
        }
    }
    if (localVariable == null) {
        final PsiElement errorElement = isStaticFinalInitializer(expr);
        if (errorElement != null) {
            String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("selected.expression.cannot.be.a.constant.initializer"));
            CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, getHelpID());
            highlightError(project, editor, errorElement);
            return null;
        }
    } else {
        final PsiExpression initializer = localVariable.getInitializer();
        if (initializer == null) {
            String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("variable.does.not.have.an.initializer", localVariable.getName()));
            CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, getHelpID());
            return null;
        }
        final PsiElement errorElement = isStaticFinalInitializer(initializer);
        if (errorElement != null) {
            String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("initializer.for.variable.cannot.be.a.constant.initializer", localVariable.getName()));
            CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, getHelpID());
            highlightError(project, editor, errorElement);
            return null;
        }
    }
    final TypeSelectorManagerImpl typeSelectorManager = new TypeSelectorManagerImpl(project, type, containingMethod, expr, occurrences);
    if (editor != null && editor.getSettings().isVariableInplaceRenameEnabled() && (expr == null || expr.isPhysical()) && activeIntroducer == null) {
        myInplaceIntroduceConstantPopup = new InplaceIntroduceConstantPopup(project, editor, parentClass, expr, localVariable, occurrences, typeSelectorManager, anchorElement, anchorElementIfAll, expr != null ? createOccurrenceManager(expr, parentClass) : null);
        if (myInplaceIntroduceConstantPopup.startInplaceIntroduceTemplate()) {
            return null;
        }
    }
    final IntroduceConstantDialog dialog = new IntroduceConstantDialog(project, parentClass, expr, localVariable, localVariable != null, occurrences, getParentClass(), typeSelectorManager, enteredName);
    dialog.setReplaceAllOccurrences(replaceAllOccurrences);
    if (!dialog.showAndGet()) {
        if (occurrences.length > 1) {
            WindowManager.getInstance().getStatusBar(project).setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting"));
        }
        return null;
    }
    return new Settings(dialog.getEnteredName(), expr, occurrences, dialog.isReplaceAllOccurrences(), true, true, InitializationPlace.IN_FIELD_DECLARATION, dialog.getFieldVisibility(), localVariable, dialog.getSelectedType(), dialog.isDeleteVariable(), dialog.getDestinationClass(), dialog.isAnnotateAsNonNls(), dialog.introduceEnumConstant());
}
Also used : AbstractInplaceIntroducer(com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer) TypeSelectorManagerImpl(com.intellij.refactoring.ui.TypeSelectorManagerImpl)

Example 7 with AbstractInplaceIntroducer

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

the class IntroduceFieldHandler method showRefactoringDialog.

protected Settings showRefactoringDialog(Project project, Editor editor, PsiClass parentClass, PsiExpression expr, PsiType type, PsiExpression[] occurrences, PsiElement anchorElement, PsiElement anchorElementIfAll) {
    final AbstractInplaceIntroducer activeIntroducer = AbstractInplaceIntroducer.getActiveIntroducer(editor);
    PsiLocalVariable localVariable = null;
    if (anchorElement instanceof PsiLocalVariable) {
        localVariable = (PsiLocalVariable) anchorElement;
    } else if (expr instanceof PsiReferenceExpression) {
        PsiElement ref = ((PsiReferenceExpression) expr).resolve();
        if (ref instanceof PsiLocalVariable) {
            localVariable = (PsiLocalVariable) ref;
        }
    }
    String enteredName = null;
    boolean replaceAll = false;
    if (activeIntroducer != null) {
        activeIntroducer.stopIntroduce(editor);
        expr = (PsiExpression) activeIntroducer.getExpr();
        localVariable = (PsiLocalVariable) activeIntroducer.getLocalVariable();
        occurrences = (PsiExpression[]) activeIntroducer.getOccurrences();
        enteredName = activeIntroducer.getInputName();
        replaceAll = activeIntroducer.isReplaceAllOccurrences();
        type = ((AbstractJavaInplaceIntroducer) activeIntroducer).getType();
        IntroduceFieldDialog.ourLastInitializerPlace = ((InplaceIntroduceFieldPopup) activeIntroducer).getInitializerPlace();
    }
    final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(expr != null ? expr : anchorElement, PsiMethod.class);
    final PsiModifierListOwner staticParentElement = PsiUtil.getEnclosingStaticElement(getElement(expr, anchorElement), parentClass);
    boolean declareStatic = staticParentElement != null;
    boolean isInSuperOrThis = false;
    if (!declareStatic) {
        for (int i = 0; !declareStatic && i < occurrences.length; i++) {
            PsiExpression occurrence = occurrences[i];
            isInSuperOrThis = isInSuperOrThis(occurrence);
            declareStatic = isInSuperOrThis;
        }
    }
    int occurrencesNumber = occurrences.length;
    final boolean currentMethodConstructor = containingMethod != null && containingMethod.isConstructor();
    final boolean allowInitInMethod = (!currentMethodConstructor || !isInSuperOrThis) && (anchorElement instanceof PsiLocalVariable || anchorElement instanceof PsiStatement);
    final boolean allowInitInMethodIfAll = (!currentMethodConstructor || !isInSuperOrThis) && anchorElementIfAll instanceof PsiStatement;
    if (editor != null && editor.getSettings().isVariableInplaceRenameEnabled() && (expr == null || expr.isPhysical()) && activeIntroducer == null) {
        myInplaceIntroduceFieldPopup = new InplaceIntroduceFieldPopup(localVariable, parentClass, declareStatic, currentMethodConstructor, occurrences, expr, new TypeSelectorManagerImpl(project, type, containingMethod, expr, occurrences), editor, allowInitInMethod, allowInitInMethodIfAll, anchorElement, anchorElementIfAll, expr != null ? createOccurrenceManager(expr, parentClass) : null, project);
        if (myInplaceIntroduceFieldPopup.startInplaceIntroduceTemplate()) {
            return null;
        }
    }
    IntroduceFieldDialog dialog = new IntroduceFieldDialog(project, parentClass, expr, localVariable, currentMethodConstructor, localVariable != null, declareStatic, occurrences, allowInitInMethod, allowInitInMethodIfAll, new TypeSelectorManagerImpl(project, type, containingMethod, expr, occurrences), enteredName);
    dialog.setReplaceAllOccurrences(replaceAll);
    if (!dialog.showAndGet()) {
        if (occurrencesNumber > 1) {
            WindowManager.getInstance().getStatusBar(project).setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting"));
        }
        return null;
    }
    if (!dialog.isDeleteVariable()) {
        localVariable = null;
    }
    return new Settings(dialog.getEnteredName(), expr, occurrences, dialog.isReplaceAllOccurrences(), declareStatic, dialog.isDeclareFinal(), dialog.getInitializerPlace(), dialog.getFieldVisibility(), localVariable, dialog.getFieldType(), localVariable != null, (TargetDestination) null, false, false);
}
Also used : AbstractInplaceIntroducer(com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer) TypeSelectorManagerImpl(com.intellij.refactoring.ui.TypeSelectorManagerImpl)

Example 8 with AbstractInplaceIntroducer

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

the class LocalToFieldHandler method convertLocalToField.

public boolean convertLocalToField(final PsiLocalVariable local, final Editor editor) {
    boolean tempIsStatic = myIsConstant;
    PsiElement parent = local.getParent();
    final List<PsiClass> classes = new ArrayList<>();
    while (parent != null && parent.getContainingFile() != null) {
        if (parent instanceof PsiClass && !(myIsConstant && parent instanceof PsiAnonymousClass)) {
            classes.add((PsiClass) parent);
        }
        if (parent instanceof PsiFile && FileTypeUtils.isInServerPageFile(parent)) {
            String message = RefactoringBundle.message("error.not.supported.for.jsp", REFACTORING_NAME);
            CommonRefactoringUtil.showErrorHint(myProject, editor, message, REFACTORING_NAME, HelpID.LOCAL_TO_FIELD);
            return false;
        }
        if (parent instanceof PsiModifierListOwner && ((PsiModifierListOwner) parent).hasModifierProperty(PsiModifier.STATIC)) {
            tempIsStatic = true;
        }
        parent = parent.getParent();
    }
    if (classes.isEmpty())
        return false;
    final AbstractInplaceIntroducer activeIntroducer = AbstractInplaceIntroducer.getActiveIntroducer(editor);
    final boolean shouldSuggestDialog = activeIntroducer instanceof InplaceIntroduceConstantPopup && activeIntroducer.startsOnTheSameElement(null, local);
    if (classes.size() == 1 || ApplicationManager.getApplication().isUnitTestMode() || shouldSuggestDialog) {
        if (convertLocalToField(local, classes.get(getChosenClassIndex(classes)), editor, tempIsStatic))
            return false;
    } else {
        final boolean isStatic = tempIsStatic;
        final PsiClass firstClass = classes.get(0);
        final PsiClass preselection = AnonymousTargetClassPreselectionUtil.getPreselection(classes, firstClass);
        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, aClass);
                convertLocalToField(local, aClass, editor, isStatic);
                return false;
            }
        }, preselection).showInBestPositionFor(editor);
    }
    return true;
}
Also used : AbstractInplaceIntroducer(com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) PsiClassListCellRenderer(com.intellij.ide.util.PsiClassListCellRenderer)

Example 9 with AbstractInplaceIntroducer

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

the class InplaceIntroduceParameterTest method testLocalInsideAnonymous1.

public void testLocalInsideAnonymous1() throws Exception {
    final Pass<AbstractInplaceIntroducer> pass = new Pass<AbstractInplaceIntroducer>() {

        @Override
        public void pass(AbstractInplaceIntroducer inplaceIntroducePopup) {
        }
    };
    String name = getTestName(true);
    configureByFile(getBasePath() + name + getExtension());
    final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled();
    try {
        TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
        getEditor().getSettings().setVariableInplaceRenameEnabled(true);
        //ensure extract local var
        final MyIntroduceHandler introduceHandler = createIntroduceHandler();
        introduceHandler.invokeImpl(LightPlatformTestCase.getProject(), getLocalVariableFromEditor(), getEditor());
        final AbstractInplaceIntroducer introducer = introduceHandler.getInplaceIntroducer();
        pass.pass(introducer);
        TemplateState state = TemplateManagerImpl.getTemplateState(getEditor());
        assert state != null;
        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 10 with AbstractInplaceIntroducer

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

the class InplaceIntroduceVariableTest method doTestReplaceChoice.

private void doTestReplaceChoice(IntroduceVariableBase.JavaReplaceChoice choice, 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);
        MyIntroduceHandler handler = createIntroduceHandler();
        ((MyIntroduceVariableHandler) handler).setChoice(choice);
        final AbstractInplaceIntroducer introducer = invokeRefactoring(handler);
        if (pass != null) {
            pass.pass(introducer);
        }
        TemplateState state = TemplateManagerImpl.getTemplateState(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)

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