relevant method chain completion: do not suggest casts for introduced variable (EA-108521)

This commit is contained in:
Dmitry Batkovich
2017-10-26 16:36:55 +03:00
parent 4eb8c6faf5
commit 60921be2e3
5 changed files with 107 additions and 49 deletions
@@ -1,18 +1,4 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.compiler.chainsSearch;
import com.intellij.compiler.backwardRefs.CompilerReferenceServiceEx;
@@ -153,17 +139,20 @@ public class ChainSearcher {
}
}
private static boolean addChainIfTerminal(OperationChain currentChain, List<OperationChain> result, int pathMaximalLength,
private static boolean addChainIfTerminal(OperationChain currentChain, List<OperationChain> result,
int pathMaximalLength,
ChainCompletionContext context) {
RefChainOperation signature = currentChain.getHeadMethodCall();
if (signature == null) return false;
RefChainOperation head = currentChain.getHead();
if (((MethodCall)signature).isStatic() ||
context.hasQualifier(context.resolvePsiClass(head.getQualifierDef())) ||
currentChain.length() >= pathMaximalLength) {
if (((MethodCall)signature).isStatic() || context.hasQualifier(context.resolvePsiClass(head.getQualifierDef()))) {
addChainIfNotPresent(currentChain, result);
return true;
}
else if (currentChain.length() >= pathMaximalLength) {
addChainIfNotPresent(currentChain.getHead() == signature ? currentChain : currentChain.removeHeadCast(context), result);
return true;
}
return false;
}
@@ -1,21 +1,8 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.compiler.chainsSearch;
import com.intellij.compiler.chainsSearch.context.ChainCompletionContext;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifier;
@@ -27,11 +14,14 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
public class OperationChain {
private static final Logger LOG = Logger.getInstance(OperationChain.class);
@NotNull
private final ChainOperation[] myReverseOperations;
private final RefChainOperation myHeadOperation;
@@ -142,6 +132,18 @@ public class OperationChain {
return new OperationChain(head.getQualifierClass(), newReverseOperations, head.getHead(), myHeadMethodCall, getChainWeight());
}
@NotNull
OperationChain removeHeadCast(@NotNull ChainCompletionContext context) {
LOG.assertTrue(getHead() instanceof TypeCast);
ChainOperation[] newReverseOperations = new ChainOperation[length() - 1];
System.arraycopy(myReverseOperations, 0, newReverseOperations, 0, length() - 1);
return new OperationChain(Objects.requireNonNull(context.resolvePsiClass(myHeadMethodCall.getQualifierDef())),
newReverseOperations,
myHeadMethodCall,
myHeadMethodCall,
getChainWeight());
}
@Override
public String toString() {
ChainOperation[] path = getPath();
@@ -0,0 +1,31 @@
class Main {
void m() {
D d = <caret>
}
}
class A {
}
class AImpl extends A {
B getB() {
return new B();
}
}
class B {
C getC() {
return new C();
}
}
class C {
D getD() {
return new D();
}
}
class D {
}
@@ -0,0 +1,44 @@
class Main {
void m(A a) {
((AImpl) a).getB().getC().getD();
((AImpl) a).getB().getC().getD();
((AImpl) a).getB().getC().getD();
((AImpl) a).getB().getC().getD();
((AImpl) a).getB().getC().getD();
((AImpl) a).getB().getC().getD();
((AImpl) a).getB().getC().getD();
((AImpl) a).getB().getC().getD();
((AImpl) a).getB().getC().getD();
((AImpl) a).getB().getC().getD();
((AImpl) a).getB().getC().getD();
((AImpl) a).getB().getC().getD();
((AImpl) a).getB().getC().getD();
((AImpl) a).getB().getC().getD();
}
}
class A {
}
class AImpl extends A {
B getB() {
return new B();
}
}
class B {
C getC() {
return new C();
}
}
class C {
D getD() {
return new D();
}
}
class D {
}
@@ -1,18 +1,4 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.codeInsight.completion;
import com.intellij.JavaTestUtil;
@@ -21,6 +7,7 @@ import com.intellij.codeInsight.completion.CompletionType;
import com.intellij.codeInsight.lookup.Lookup;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.compiler.chainsSearch.ChainRelevance;
import com.intellij.compiler.chainsSearch.ChainSearchMagicConstants;
import com.intellij.compiler.chainsSearch.completion.MethodChainCompletionContributor;
import com.intellij.compiler.chainsSearch.completion.lookup.JavaRelevantChainLookupElement;
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
@@ -262,6 +249,11 @@ public class MethodChainsCompletionTest extends AbstractCompilerAwareTest {
assertEquals("(InspectionManagerEx)getInstance", element.toString());
}
public void testLongChainWithCast() {
assertEquals("the test should be modified when MAX_CHAIN_SIZE is changed", 4, ChainSearchMagicConstants.MAX_CHAIN_SIZE);
assertEquals("a.getB().getC().getD", assertOneElement(doCompletion()).toString());
}
public void assertAdvisorLookupElementEquals(String lookupText,
int unreachableParametersCount,
int chainSize,