Search in sources :

Example 1 with BaseRefactoringProcessor

use of com.intellij.refactoring.BaseRefactoringProcessor in project intellij-community by JetBrains.

the class PyChangeSignatureTest method changeSignature.

private void changeSignature(@Nullable String newName, @Nullable List<PyParameterInfo> parameters) {
    final PyChangeSignatureHandler changeSignatureHandler = new PyChangeSignatureHandler();
    final PyFunction function = (PyFunction) changeSignatureHandler.findTargetMember(myFixture.getFile(), myFixture.getEditor());
    assertNotNull(function);
    final PyFunction newFunction = PyChangeSignatureHandler.getSuperMethod(function);
    assertNotNull(newFunction);
    final PyMethodDescriptor method = new PyMethodDescriptor(newFunction);
    final TestPyChangeSignatureDialog dialog = new TestPyChangeSignatureDialog(newFunction.getProject(), method);
    try {
        if (newName != null) {
            dialog.setNewName(newName);
        }
        if (parameters != null) {
            dialog.setParameterInfos(parameters);
        }
        final String validationError = dialog.validateAndCommitData();
        assertTrue(validationError, validationError == null);
        final BaseRefactoringProcessor baseRefactoringProcessor = dialog.createRefactoringProcessor();
        assert baseRefactoringProcessor instanceof PyChangeSignatureProcessor;
        final PyChangeSignatureProcessor processor = (PyChangeSignatureProcessor) baseRefactoringProcessor;
        processor.run();
    } finally {
        Disposer.dispose(dialog.getDisposable());
    }
}
Also used : BaseRefactoringProcessor(com.intellij.refactoring.BaseRefactoringProcessor) PyFunction(com.jetbrains.python.psi.PyFunction)

Example 2 with BaseRefactoringProcessor

use of com.intellij.refactoring.BaseRefactoringProcessor in project intellij-community by JetBrains.

the class PyMoveSymbolDelegate method doMove.

public void doMove(@NotNull Project project, @NotNull List<PyElement> elements) {
    final PsiElement firstElement = elements.get(0);
    final String initialPath = StringUtil.notNullize(PyPsiUtils.getContainingFilePath(firstElement));
    final BaseRefactoringProcessor processor;
    if (isMovableLocalFunctionOrMethod(firstElement)) {
        final PyFunction function = (PyFunction) firstElement;
        final PyMakeFunctionTopLevelDialog dialog = new PyMakeFunctionTopLevelDialog(project, function, initialPath, initialPath);
        if (!dialog.showAndGet()) {
            return;
        }
        if (function.getContainingClass() != null) {
            processor = new PyMakeMethodTopLevelProcessor(function, dialog.getTargetPath());
        } else {
            processor = new PyMakeLocalFunctionTopLevelProcessor(function, dialog.getTargetPath());
        }
        processor.setPreviewUsages(dialog.isPreviewUsages());
    } else {
        final List<PsiNamedElement> initialElements = Lists.newArrayList();
        for (PsiElement element : elements) {
            final PsiNamedElement e = PyMoveModuleMembersHelper.extractNamedElement(element);
            if (e == null) {
                return;
            }
            initialElements.add(e);
        }
        final PyMoveModuleMembersDialog dialog = new PyMoveModuleMembersDialog(project, initialElements, initialPath, initialPath);
        if (!dialog.showAndGet()) {
            return;
        }
        final PsiNamedElement[] selectedElements = ContainerUtil.findAllAsArray(dialog.getSelectedTopLevelSymbols(), PsiNamedElement.class);
        processor = new PyMoveModuleMembersProcessor(selectedElements, dialog.getTargetPath());
        processor.setPreviewUsages(dialog.isPreviewUsages());
    }
    try {
        processor.run();
    } catch (IncorrectOperationException e) {
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            throw e;
        }
        CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("error.title"), e.getMessage(), null, project);
    }
}
Also used : PyMoveModuleMembersDialog(com.jetbrains.python.refactoring.move.moduleMembers.PyMoveModuleMembersDialog) BaseRefactoringProcessor(com.intellij.refactoring.BaseRefactoringProcessor) PyMoveModuleMembersProcessor(com.jetbrains.python.refactoring.move.moduleMembers.PyMoveModuleMembersProcessor) PyMakeFunctionTopLevelDialog(com.jetbrains.python.refactoring.move.makeFunctionTopLevel.PyMakeFunctionTopLevelDialog) PyMakeLocalFunctionTopLevelProcessor(com.jetbrains.python.refactoring.move.makeFunctionTopLevel.PyMakeLocalFunctionTopLevelProcessor) PyMakeMethodTopLevelProcessor(com.jetbrains.python.refactoring.move.makeFunctionTopLevel.PyMakeMethodTopLevelProcessor) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 3 with BaseRefactoringProcessor

use of com.intellij.refactoring.BaseRefactoringProcessor in project intellij-community by JetBrains.

the class DetectedJavaChangeInfo method perform.

void perform(final String oldText, Editor editor, boolean silently) {
    final PsiMethod method = getSuperMethod();
    Project project = getMethod().getProject();
    final PsiMethod currentMethod = getMethod();
    final TextRange signatureRange = JavaChangeSignatureDetector.getSignatureRange(currentMethod);
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    final Document document = documentManager.getDocument(currentMethod.getContainingFile());
    if (silently || ApplicationManager.getApplication().isUnitTestMode()) {
        final String currentSignature = currentMethod.getContainingFile().getText().substring(signatureRange.getStartOffset(), signatureRange.getEndOffset());
        InplaceChangeSignature.temporallyRevertChanges(JavaChangeSignatureDetector.getSignatureRange(currentMethod), document, oldText, project);
        PsiMethod prototype;
        if (isGenerateDelegate()) {
            for (JavaParameterInfo info : getNewParameters()) {
                if (info.getOldIndex() == -1) {
                    //to be replaced with template expr
                    ((ParameterInfoImpl) info).setDefaultValue("null");
                }
            }
            prototype = JavaChangeSignatureUsageProcessor.generateDelegatePrototype(this);
        } else {
            prototype = null;
        }
        createChangeSignatureProcessor(method).run();
        InplaceChangeSignature.temporallyRevertChanges(JavaChangeSignatureDetector.getSignatureRange(currentMethod), document, currentSignature, project);
        if (prototype != null) {
            WriteCommandAction.runWriteCommandAction(project, "Delegate", null, () -> {
                PsiMethod delegate = currentMethod.getContainingClass().findMethodBySignature(prototype, false);
                PsiExpression expression = delegate != null ? LambdaUtil.extractSingleExpressionFromBody(delegate.getBody()) : null;
                if (expression instanceof PsiMethodCallExpression) {
                    PsiExpression[] expressions = ((PsiMethodCallExpression) expression).getArgumentList().getExpressions();
                    JavaParameterInfo[] parameters = getNewParameters();
                    PsiExpression[] toBeDefault = Arrays.stream(parameters).filter(param -> param.getOldIndex() == -1).map(info -> {
                        int i = ArrayUtil.find(parameters, info);
                        return expressions[i];
                    }).toArray(PsiExpression[]::new);
                    DefineParamsDefaultValueAction.startTemplate(project, editor, toBeDefault, delegate);
                }
            });
        }
        return;
    }
    final JavaMethodDescriptor descriptor = new JavaMethodDescriptor(currentMethod) {

        @Override
        public String getReturnTypeText() {
            return getNewReturnType().getTypeText();
        }
    };
    final JavaChangeSignatureDialog dialog = new JavaChangeSignatureDialog(method.getProject(), descriptor, true, method) {

        protected BaseRefactoringProcessor createRefactoringProcessor() {
            return createChangeSignatureProcessor(method);
        }

        @Override
        protected void invokeRefactoring(final BaseRefactoringProcessor processor) {
            CommandProcessor.getInstance().executeCommand(myProject, () -> {
                InplaceChangeSignature.temporallyRevertChanges(JavaChangeSignatureDetector.getSignatureRange(currentMethod), document, oldText, project);
                doRefactor(processor);
            }, RefactoringBundle.message("changing.signature.of.0", DescriptiveNameUtil.getDescriptiveName(currentMethod)), null);
        }

        private void doRefactor(BaseRefactoringProcessor processor) {
            super.invokeRefactoring(processor);
        }
    };
    dialog.showAndGet();
}
Also used : DescriptiveNameUtil(com.intellij.lang.findUsages.DescriptiveNameUtil) CanonicalTypes(com.intellij.refactoring.util.CanonicalTypes) Arrays(java.util.Arrays) ArrayUtil(com.intellij.util.ArrayUtil) Document(com.intellij.openapi.editor.Document) UsageInfo(com.intellij.usageView.UsageInfo) HashMap(java.util.HashMap) RefactoringBundle(com.intellij.refactoring.RefactoringBundle) BaseRefactoringProcessor(com.intellij.refactoring.BaseRefactoringProcessor) HashSet(java.util.HashSet) Comparing(com.intellij.openapi.util.Comparing) Map(java.util.Map) Project(com.intellij.openapi.project.Project) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) InplaceChangeSignature(com.intellij.refactoring.changeSignature.inplace.InplaceChangeSignature) IncorrectOperationException(com.intellij.util.IncorrectOperationException) VisibilityUtil(com.intellij.util.VisibilityUtil) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) DefineParamsDefaultValueAction(com.intellij.codeInsight.daemon.impl.quickfix.DefineParamsDefaultValueAction) CommandProcessor(com.intellij.openapi.command.CommandProcessor) Nullable(org.jetbrains.annotations.Nullable) ApplicationManager(com.intellij.openapi.application.ApplicationManager) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) BaseRefactoringProcessor(com.intellij.refactoring.BaseRefactoringProcessor) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) Project(com.intellij.openapi.project.Project)

Example 4 with BaseRefactoringProcessor

use of com.intellij.refactoring.BaseRefactoringProcessor in project intellij-plugins by JetBrains.

the class FlexMoveClassDialog method doAction.

@Override
protected void doAction() {
    myTargetPackageField.updateRecents();
    PsiElement firstElement = myElements.iterator().next();
    PsiDirectory baseDir;
    if (myTargetContainer instanceof PsiDirectory) {
        baseDir = (PsiDirectory) myTargetContainer;
    } else {
        baseDir = PlatformPackageUtil.getDirectory(firstElement);
    }
    String nameToCheck = myFileLocal ? myClassNameField.getText() : null;
    PsiDirectory targetDirectory = JSRefactoringUtil.chooseOrCreateDirectoryForClass(myProject, ModuleUtilCore.findModuleForPsiElement(firstElement), GlobalSearchScope.projectScope(myProject), myTargetPackageField.getText(), nameToCheck, baseDir, myCbMoveToAnotherSourceFolder.isSelected() ? ThreeState.YES : ThreeState.NO);
    if (targetDirectory == null) {
        return;
    }
    // file-local case already checked by JSRefactoringUtil.chooseOrCreateDirectoryForClass (see nameToCheck)
    if (!myFileLocal) {
        try {
            for (PsiElement element : myElements) {
                MoveFilesOrDirectoriesUtil.checkMove(element.getContainingFile(), targetDirectory);
            }
        } catch (IncorrectOperationException e) {
            CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("error.title"), e.getMessage(), getHelpId(), myProject);
            return;
        }
    }
    BaseRefactoringProcessor processor;
    if (myFileLocal) {
        processor = new FlexMoveInnerClassProcessor(myElements.iterator().next(), targetDirectory, myClassNameField.getText(), myTargetPackageField.getText(), myCbSearchInComments.isSelected(), myCbSearchTextOccurences.isSelected(), myCallback);
    } else {
        processor = new FlexMoveClassProcessor(myElements, targetDirectory, myTargetPackageField.getText(), myCbSearchInComments.isSelected(), myCbSearchTextOccurences.isSelected(), myCallback);
    }
    invokeRefactoring(processor);
}
Also used : BaseRefactoringProcessor(com.intellij.refactoring.BaseRefactoringProcessor) PsiDirectory(com.intellij.psi.PsiDirectory) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiElement(com.intellij.psi.PsiElement)

Example 5 with BaseRefactoringProcessor

use of com.intellij.refactoring.BaseRefactoringProcessor in project intellij-plugins by JetBrains.

the class JSChangeSignatureDetector method performChange.

@Override
public void performChange(JSChangeInfo changeInfo, Editor editor, @NotNull final String oldText) {
    final JSChangeInfo jsChangeInfo = changeInfo;
    JSMethodDescriptor descriptor = new JSMethodDescriptor(jsChangeInfo.getMethod(), false) {

        @Override
        public String getName() {
            return jsChangeInfo.getNewName();
        }

        @Override
        public List<JSParameterInfo> getParameters() {
            return Arrays.asList(jsChangeInfo.getNewParameters());
        }

        @Override
        public int getParametersCount() {
            return getParameters().size();
        }

        @Override
        public String getVisibility() {
            return jsChangeInfo.getNewVisibility().name();
        }

        @Override
        public JSFunction getMethod() {
            return jsChangeInfo.getMethod();
        }
    };
    JSChangeSignatureDialog d = new JSChangeSignatureDialog(descriptor, changeInfo.getMethod()) {

        @Override
        protected void invokeRefactoring(BaseRefactoringProcessor processor) {
            revertChanges(jsChangeInfo.getMethod(), oldText);
            super.invokeRefactoring(processor);
        }
    };
    d.showAndGet();
}
Also used : BaseRefactoringProcessor(com.intellij.refactoring.BaseRefactoringProcessor) JSChangeSignatureDialog(com.intellij.lang.javascript.refactoring.changeSignature.JSChangeSignatureDialog) JSMethodDescriptor(com.intellij.lang.javascript.refactoring.changeSignature.JSMethodDescriptor) JSParameterInfo(com.intellij.lang.javascript.refactoring.changeSignature.JSParameterInfo)

Aggregations

BaseRefactoringProcessor (com.intellij.refactoring.BaseRefactoringProcessor)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 DefineParamsDefaultValueAction (com.intellij.codeInsight.daemon.impl.quickfix.DefineParamsDefaultValueAction)1 DescriptiveNameUtil (com.intellij.lang.findUsages.DescriptiveNameUtil)1 JSChangeSignatureDialog (com.intellij.lang.javascript.refactoring.changeSignature.JSChangeSignatureDialog)1 JSMethodDescriptor (com.intellij.lang.javascript.refactoring.changeSignature.JSMethodDescriptor)1 JSParameterInfo (com.intellij.lang.javascript.refactoring.changeSignature.JSParameterInfo)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 CommandProcessor (com.intellij.openapi.command.CommandProcessor)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1 Comparing (com.intellij.openapi.util.Comparing)1 TextRange (com.intellij.openapi.util.TextRange)1 com.intellij.psi (com.intellij.psi)1 PsiDirectory (com.intellij.psi.PsiDirectory)1 PsiElement (com.intellij.psi.PsiElement)1 RefactoringBundle (com.intellij.refactoring.RefactoringBundle)1 InplaceChangeSignature (com.intellij.refactoring.changeSignature.inplace.InplaceChangeSignature)1