extract to method reference: don't replace duplicates outside of target method scope (IDEA-195380)

This commit is contained in:
Anna.Kozlova
2018-07-11 11:51:40 +02:00
parent be60b913b2
commit 255a2cf537
2 changed files with 20 additions and 6 deletions

View File

@@ -214,7 +214,7 @@ public class MethodDuplicatesHandler implements RefactoringActionHandler, Contex
}
}
private static void replaceDuplicate(final Project project, final Map<PsiMember, List<Match>> duplicates, final Set<PsiMember> methods) {
public static void replaceDuplicate(final Project project, final Map<PsiMember, List<Match>> duplicates, final Set<PsiMember> methods) {
final ProgressIndicator progressIndicator = ProgressManager.getInstance().getProgressIndicator();
if (progressIndicator != null && progressIndicator.isCanceled()) return;
@@ -244,7 +244,7 @@ public class MethodDuplicatesHandler implements RefactoringActionHandler, Contex
ApplicationManager.getApplication().invokeLater(replaceRunnable, project.getDisposed());
}
public static List<Match> hasDuplicates(final PsiFile file, final PsiMember member) {
public static List<Match> hasDuplicates(final PsiElement file, final PsiMember member) {
final DuplicatesFinder duplicatesFinder = createDuplicatesFinder(member);
if (duplicatesFinder == null) {
return Collections.emptyList();

View File

@@ -3,7 +3,6 @@
*/
package com.siyeh.ipp.functional;
import com.intellij.analysis.AnalysisScope;
import com.intellij.codeInsight.CodeInsightUtilCore;
import com.intellij.codeInsight.intention.BaseElementAtCaretIntentionAction;
import com.intellij.codeInspection.LambdaCanBeMethodReferenceInspection;
@@ -24,6 +23,7 @@ import com.intellij.refactoring.rename.RenamePsiElementProcessor;
import com.intellij.refactoring.rename.inplace.MemberInplaceRenamer;
import com.intellij.refactoring.util.LambdaRefactoringUtil;
import com.intellij.refactoring.util.RefactoringUtil;
import com.intellij.refactoring.util.duplicates.Match;
import com.intellij.refactoring.util.duplicates.MethodDuplicatesHandler;
import com.intellij.util.ObjectUtils;
import com.intellij.util.text.UniqueNameGenerator;
@@ -169,13 +169,27 @@ public class ExtractToMethodReferenceIntention extends BaseElementAtCaretIntenti
}
private static void processMethodsDuplicates(PsiMethod method) {
Project project = method.getProject();
final Runnable runnable = () -> {
if (!method.isValid()) return;
MethodDuplicatesHandler
.invokeOnScope(method.getProject(), Collections.singleton(method), new AnalysisScope(method.getContainingFile()), true);
PsiClass containingClass = method.getContainingClass();
if (containingClass != null) {
final List<Match> duplicates = MethodDuplicatesHandler.hasDuplicates(containingClass, method);
for (Iterator<Match> iterator = duplicates.iterator(); iterator.hasNext(); ) {
Match match = iterator.next();
final PsiElement matchStart = match.getMatchStart();
if (PsiTreeUtil.isAncestor(method, matchStart, false)) {
iterator.remove();
break;
}
}
if (!duplicates.isEmpty()) {
MethodDuplicatesHandler.replaceDuplicate(project, Collections.singletonMap(method, duplicates), Collections.singleton(method));
}
}
};
ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> ApplicationManager.getApplication().runReadAction(runnable),
MethodDuplicatesHandler.REFACTORING_NAME, true, method.getProject());
MethodDuplicatesHandler.REFACTORING_NAME, true, project);
}
private static String getUniqueMethodName(PsiClass targetClass,