Search in sources :

Example 1 with DuplicatesFinder

use of com.intellij.refactoring.util.duplicates.DuplicatesFinder 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 2 with DuplicatesFinder

use of com.intellij.refactoring.util.duplicates.DuplicatesFinder 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

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