Search in sources :

Example 1 with Match

use of com.intellij.refactoring.util.duplicates.Match in project intellij-community by JetBrains.

the class ExtractMethodTest method performExtractMethod.

public static boolean performExtractMethod(boolean doRefactor, boolean replaceAllDuplicates, Editor editor, PsiFile file, Project project, final boolean extractChainedConstructor, PsiType returnType, boolean makeStatic, String newNameOfFirstParam, PsiClass targetClass, int... disabledParams) throws PrepareFailedException, IncorrectOperationException {
    int startOffset = editor.getSelectionModel().getSelectionStart();
    int endOffset = editor.getSelectionModel().getSelectionEnd();
    PsiElement[] elements;
    PsiExpression expr = CodeInsightUtil.findExpressionInRange(file, startOffset, endOffset);
    if (expr != null) {
        elements = new PsiElement[] { expr };
    } else {
        elements = CodeInsightUtil.findStatementsInRange(file, startOffset, endOffset);
    }
    if (elements.length == 0) {
        final PsiExpression expression = IntroduceVariableBase.getSelectedExpression(project, file, startOffset, endOffset);
        if (expression != null) {
            elements = new PsiElement[] { expression };
        }
    }
    assertTrue(elements.length > 0);
    final ExtractMethodProcessor processor = new ExtractMethodProcessor(project, editor, elements, null, "Extract Method", "newMethod", null);
    processor.setShowErrorDialogs(false);
    processor.setChainedConstructor(extractChainedConstructor);
    if (!processor.prepare()) {
        return false;
    }
    if (doRefactor) {
        processor.testTargetClass(targetClass);
        processor.testPrepare(returnType, makeStatic);
        processor.testNullness();
        if (disabledParams != null) {
            for (int param : disabledParams) {
                processor.doNotPassParameter(param);
            }
        }
        if (newNameOfFirstParam != null) {
            processor.changeParamName(0, newNameOfFirstParam);
        }
        ExtractMethodHandler.run(project, editor, processor);
    }
    if (replaceAllDuplicates) {
        final Boolean hasDuplicates = processor.hasDuplicates();
        if (hasDuplicates == null || hasDuplicates.booleanValue()) {
            final List<Match> duplicates = processor.getDuplicates();
            for (final Match match : duplicates) {
                if (!match.getMatchStart().isValid() || !match.getMatchEnd().isValid())
                    continue;
                PsiDocumentManager.getInstance(project).commitAllDocuments();
                ApplicationManager.getApplication().runWriteAction(() -> {
                    processor.processMatch(match);
                });
            }
        }
    }
    return true;
}
Also used : ExtractMethodProcessor(com.intellij.refactoring.extractMethod.ExtractMethodProcessor) Match(com.intellij.refactoring.util.duplicates.Match)

Example 2 with Match

use of com.intellij.refactoring.util.duplicates.Match in project intellij-community by JetBrains.

the class ExtractMethodSignatureSuggester method collectParamValues.

private static boolean collectParamValues(List<Match> duplicates, VariableData variableData, THashSet<PsiExpression> map) {
    for (Match duplicate : duplicates) {
        final List<PsiElement> values = duplicate.getParameterValues(variableData.variable);
        if (values == null || values.isEmpty()) {
            return false;
        }
        boolean found = false;
        for (PsiElement value : values) {
            if (value instanceof PsiExpression) {
                map.add((PsiExpression) value);
                found = true;
                break;
            }
        }
        if (!found)
            return false;
    }
    return true;
}
Also used : Match(com.intellij.refactoring.util.duplicates.Match)

Example 3 with Match

use of com.intellij.refactoring.util.duplicates.Match in project intellij-community by JetBrains.

the class ExtractMethodSignatureSuggester method getDuplicates.

public List<Match> getDuplicates(final PsiMethod method, final PsiMethodCallExpression methodCall, ParametersFolder folder) {
    final List<Match> duplicates = findDuplicatesSignature(method, folder);
    if (duplicates != null && !duplicates.isEmpty()) {
        if (ApplicationManager.getApplication().isUnitTestMode() || new PreviewDialog(method, myExtractedMethod, methodCall, myMethodCall, duplicates.size()).showAndGet()) {
            PsiDocumentManager.getInstance(myProject).commitAllDocuments();
            WriteCommandAction.runWriteCommandAction(myProject, () -> {
                myMethodCall = (PsiMethodCallExpression) methodCall.replace(myMethodCall);
                myExtractedMethod = (PsiMethod) method.replace(myExtractedMethod);
            });
            final DuplicatesFinder finder = MethodDuplicatesHandler.createDuplicatesFinder(myExtractedMethod);
            if (finder != null) {
                final List<VariableData> datas = finder.getParameters().getInputVariables();
                myVariableData = datas.toArray(new VariableData[datas.size()]);
                return finder.findDuplicates(myExtractedMethod.getContainingClass());
            }
        }
    }
    return null;
}
Also used : DuplicatesFinder(com.intellij.refactoring.util.duplicates.DuplicatesFinder) VariableData(com.intellij.refactoring.util.VariableData) Match(com.intellij.refactoring.util.duplicates.Match)

Example 4 with Match

use of com.intellij.refactoring.util.duplicates.Match in project intellij-community by JetBrains.

the class ExtractMethodSignatureSuggester method findDuplicatesSignature.

@Nullable
public List<Match> findDuplicatesSignature(final PsiMethod method, ParametersFolder folder) {
    final List<PsiExpression> copies = new ArrayList<>();
    final InputVariables variables = detectTopLevelExpressionsToReplaceWithParameters(copies);
    if (variables == null) {
        return null;
    }
    final DuplicatesFinder defaultFinder = MethodDuplicatesHandler.createDuplicatesFinder(myExtractedMethod);
    if (defaultFinder == null) {
        return null;
    }
    final DuplicatesFinder finder = new DuplicatesFinder(defaultFinder.getPattern(), variables, defaultFinder.getReturnValue(), new ArrayList<>()) {

        @Override
        protected boolean isSelf(PsiElement candidate) {
            return PsiTreeUtil.isAncestor(method, candidate, true);
        }
    };
    List<Match> duplicates = finder.findDuplicates(method.getContainingClass());
    if (duplicates != null && !duplicates.isEmpty()) {
        restoreRenamedParams(copies, folder);
        if (!myMethodCall.isValid()) {
            return null;
        }
        myMethodCall = (PsiMethodCallExpression) myMethodCall.copy();
        inlineSameArguments(method, copies, variables, duplicates);
        for (PsiExpression expression : copies) {
            myMethodCall.getArgumentList().add(expression);
        }
        return duplicates;
    } else {
        return null;
    }
}
Also used : DuplicatesFinder(com.intellij.refactoring.util.duplicates.DuplicatesFinder) Match(com.intellij.refactoring.util.duplicates.Match) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Match (com.intellij.refactoring.util.duplicates.Match)4 DuplicatesFinder (com.intellij.refactoring.util.duplicates.DuplicatesFinder)2 ExtractMethodProcessor (com.intellij.refactoring.extractMethod.ExtractMethodProcessor)1 VariableData (com.intellij.refactoring.util.VariableData)1 Nullable (org.jetbrains.annotations.Nullable)1