Java: Fixed duplicate matching for complex expressions (IDEA-188127)

This commit is contained in:
Pavel Dolgov
2018-03-14 18:21:30 +03:00
parent c0e863d609
commit e0b966650e
4 changed files with 37 additions and 0 deletions
@@ -610,6 +610,9 @@ public class DuplicatesFinder {
if (!myWithExtractedParameters || !(pattern instanceof PsiReferenceExpression) || !(candidate instanceof PsiReferenceExpression)) {
return false;
}
if (myPattern.length == 1 && myPattern[0] == pattern) {
return false;
}
ExtractableExpressionPart part1 = ExtractableExpressionPart.matchVariable((PsiReferenceExpression)pattern, null);
if (part1 == null || part1.myVariable == null) {
return false;
@@ -0,0 +1,13 @@
class CallChainExpression {
String foo() {
String s = <selection>A.a().b().c</selection>;
System.out.println(s);
return s;
}
static class A {
static A a() { return new A(); }
A b() { return this; }
String c = "";
}
}
@@ -0,0 +1,17 @@
class CallChainExpression {
String foo() {
String s = newMethod();
System.out.println(s);
return s;
}
private String newMethod() {
return A.a().b().c;
}
static class A {
static A a() { return new A(); }
A b() { return this; }
String c = "";
}
}
@@ -1229,6 +1229,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
doExitPointsTest(false);
}
public void testCallChainExpression() throws Exception {
doTest();
}
private void doTestDisabledParam() throws PrepareFailedException {
final CommonCodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject()).getCommonSettings(JavaLanguage.INSTANCE);
settings.ELSE_ON_NEW_LINE = true;