From eaeffca0b853c0a6cd43788a8455079e8e8c50bf Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Tue, 16 May 2017 21:20:53 +0300 Subject: [PATCH] method chain completion: simplify old code --- .../compiler/chainsSearch/ChainSearcher.java | 62 ++++++------------- .../compiler/chainsSearch/MaxSizeTreeSet.java | 46 -------------- .../compiler/chainsSearch/MethodChain.java | 11 +--- .../MethodChainLookupRangingHelper.java | 6 +- .../MethodChainsCompletionTest.java | 2 +- 5 files changed, 26 insertions(+), 101 deletions(-) delete mode 100644 java/compiler/impl/src/com/intellij/compiler/chainsSearch/MaxSizeTreeSet.java diff --git a/java/compiler/impl/src/com/intellij/compiler/chainsSearch/ChainSearcher.java b/java/compiler/impl/src/com/intellij/compiler/chainsSearch/ChainSearcher.java index 9e24f7521c9a..42ef614b8b73 100644 --- a/java/compiler/impl/src/com/intellij/compiler/chainsSearch/ChainSearcher.java +++ b/java/compiler/impl/src/com/intellij/compiler/chainsSearch/ChainSearcher.java @@ -59,65 +59,41 @@ public class ChainSearcher { int pathMaximalLength, int maxResultSize, ChainCompletionContext context) { - Map knownDistance = initializer.getChains(); LinkedList q = initializer.getChainQueue(); List result = new ArrayList<>(); while (!q.isEmpty()) { ProgressManager.checkCanceled(); - MethodChain currentVertex = q.poll(); - MethodIncompleteSignature headSignature = currentVertex.getHeadSignature(); - MethodChain currentVertexMethodChain = knownDistance.get(headSignature); - if (currentVertex.getChainWeight() != currentVertexMethodChain.getChainWeight()) { - continue; - } + MethodChain currentChain = q.poll(); + MethodIncompleteSignature headSignature = currentChain.getHeadSignature(); - // interrupt a chain if a head method is static or has suitable qualifier - if (headSignature.isStatic() || context.hasQualifier(context.resolveQualifierClass(headSignature))) { - addChainIfNotPresent(currentVertex, result); + // interrupt a chain if it can be terminal + if (headSignature.isStatic() || + context.hasQualifier(context.resolveQualifierClass(headSignature)) || + currentChain.length() >= pathMaximalLength) { + addChainIfNotPresent(currentChain, result); continue; } // otherwise try to find chain continuation + boolean updated = false; SortedSet candidates = referenceServiceEx.findMethodReferenceOccurrences(headSignature.getOwner(), SignatureData.ZERO_DIM); - MaxSizeTreeSet chosenCandidates = new MaxSizeTreeSet<>(maxResultSize); for (SignatureAndOccurrences candidate : candidates) { - if (candidate.getOccurrenceCount() * ChainSearchMagicConstants.FILTER_RATIO < currentVertex.getChainWeight()) { + if (candidate.getOccurrenceCount() * ChainSearchMagicConstants.FILTER_RATIO < currentChain.getChainWeight()) { break; } MethodIncompleteSignature sign = candidate.getSignature(); - if (sign.isStatic() || !sign.getOwner().equals(context.getTarget().getClassQName())) { - int vertexDistance = Math.min(currentVertex.getChainWeight(), candidate.getOccurrenceCount()); - MethodChain knownVertexMethodChain = knownDistance.get(sign); - if ((knownVertexMethodChain == null || knownVertexMethodChain.getChainWeight() < vertexDistance)) { - if ((chosenCandidates.isEmpty() || chosenCandidates.last().getOccurrenceCount() < vertexDistance) && currentVertexMethodChain.size() < pathMaximalLength - 1) { - MethodChain newBestMethodChain = currentVertexMethodChain.continuation(candidate.getSignature(), vertexDistance, context); - if (newBestMethodChain != null) { - chosenCandidates.add(new SignatureAndOccurrences(candidate.getSignature(), vertexDistance)); - knownDistance.put(sign, newBestMethodChain); - } + if ((sign.isStatic() || !sign.getOwner().equals(context.getTarget().getClassQName())) && + referenceServiceEx.mayHappen(candidate.getSignature().getRef(), headSignature.getRef(), ChainSearchMagicConstants.PROBABILITY_THRESHOLD)) { + MethodChain continuation = currentChain.continuation(candidate.getSignature(), candidate.getOccurrenceCount(), context); + if (continuation != null) { + boolean stopChain = candidate.getSignature().isStatic() || context.hasQualifier(context.resolveQualifierClass(candidate.getSignature())); + if (stopChain) { + addChainIfNotPresent(continuation, result); } - } - else { - break; - } - } - } - - boolean updated = false; - if (!chosenCandidates.isEmpty()) { - for (SignatureAndOccurrences candidate : chosenCandidates) { - if (referenceServiceEx.mayHappen(candidate.getSignature().getRef(), headSignature.getRef(), ChainSearchMagicConstants.PROBABILITY_THRESHOLD)) { - MethodChain continuation = currentVertex.continuation(candidate.getSignature(), candidate.getOccurrenceCount(), context); - if (continuation != null) { - boolean stopChain = candidate.getSignature().isStatic() || context.hasQualifier(context.resolveQualifierClass(candidate.getSignature())); - if (stopChain) { - addChainIfNotPresent(continuation, result); - } - else { - q.addFirst(continuation); - } + else { + q.addFirst(continuation); } updated = true; } @@ -126,7 +102,7 @@ public class ChainSearcher { // continuation is not found -> add this chain as result if (!updated && !context.getTarget().getClassQName().equals(headSignature.getOwner())) { - addChainIfNotPresent(currentVertex, result); + addChainIfNotPresent(currentChain, result); } if (result.size() > maxResultSize) { diff --git a/java/compiler/impl/src/com/intellij/compiler/chainsSearch/MaxSizeTreeSet.java b/java/compiler/impl/src/com/intellij/compiler/chainsSearch/MaxSizeTreeSet.java deleted file mode 100644 index 05c863412160..000000000000 --- a/java/compiler/impl/src/com/intellij/compiler/chainsSearch/MaxSizeTreeSet.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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. - */ -package com.intellij.compiler.chainsSearch; - -import java.util.Comparator; -import java.util.TreeSet; - -/** - * @author Dmitry Batkovich - */ -public class MaxSizeTreeSet> extends TreeSet { - private final int myMaxSize; - - public MaxSizeTreeSet(int maxSize) { - myMaxSize = maxSize; - } - - public boolean add(final E e) { - if (size() == myMaxSize) { - //noinspection ConstantConditions - final Comparator comparator = comparator(); - if ((comparator == null ? e.compareTo(last()) : comparator.compare(e, last())) < 0) { - final boolean isAdded = super.add(e); - if (isAdded) { - pollLast(); - return true; - } - } - return false; - } - return super.add(e); - } -} diff --git a/java/compiler/impl/src/com/intellij/compiler/chainsSearch/MethodChain.java b/java/compiler/impl/src/com/intellij/compiler/chainsSearch/MethodChain.java index 4110860db2d2..4f84e139e6cc 100644 --- a/java/compiler/impl/src/com/intellij/compiler/chainsSearch/MethodChain.java +++ b/java/compiler/impl/src/com/intellij/compiler/chainsSearch/MethodChain.java @@ -72,7 +72,7 @@ public class MethodChain { return mySignature; } - public int size() { + public int length() { return myRevertedPath.size(); } @@ -80,11 +80,6 @@ public class MethodChain { return myQualifierClass; } - @NotNull - public List getRevertedPath() { - return myRevertedPath; - } - @NotNull public PsiMethod[] getFirst() { return myRevertedPath.get(0); @@ -118,10 +113,10 @@ public class MethodChain { @SuppressWarnings("ConstantConditions") public static CompareResult compare(@NotNull MethodChain left, @NotNull MethodChain right) { - if (left.size() == 0) { + if (left.length() == 0) { return CompareResult.RIGHT_CONTAINS_LEFT; } - if (right.size() == 0) { + if (right.length() == 0) { return CompareResult.LEFT_CONTAINS_RIGHT; } Iterator leftIterator = left.myRevertedPath.iterator(); diff --git a/java/compiler/impl/src/com/intellij/compiler/chainsSearch/MethodChainLookupRangingHelper.java b/java/compiler/impl/src/com/intellij/compiler/chainsSearch/MethodChainLookupRangingHelper.java index d76bb906fe10..0737e1ef04af 100644 --- a/java/compiler/impl/src/com/intellij/compiler/chainsSearch/MethodChainLookupRangingHelper.java +++ b/java/compiler/impl/src/com/intellij/compiler/chainsSearch/MethodChainLookupRangingHelper.java @@ -41,8 +41,8 @@ public class MethodChainLookupRangingHelper { @Nullable public static LookupElement chainToWeightableLookupElement(MethodChain chain, ChainCompletionContext context) { - int chainSize = chain.size(); - assert chainSize != 0; + int chainLength = chain.length(); + assert chainLength != 0; int lastMethodWeight = chain.getChainWeight(); int unreachableParametersCount = 0; int notMatchedStringVars = 0; @@ -93,7 +93,7 @@ public class MethodChainLookupRangingHelper { } ChainRelevance relevance = - new ChainRelevance(chainSize, + new ChainRelevance(chainLength, lastMethodWeight, unreachableParametersCount, notMatchedStringVars, diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/MethodChainsCompletionTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/completion/MethodChainsCompletionTest.java index 28a0c2f1b52b..bb1cb377186f 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/MethodChainsCompletionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/MethodChainsCompletionTest.java @@ -95,7 +95,7 @@ public class MethodChainsCompletionTest extends AbstractCompilerAwareTest { } public void testMethodsWithParametersInContext() { - assertAdvisorLookupElementEquals("getInstance().findFile().findElementAt", 0, 5, 3, 0, assertOneElement(doCompletion())); + assertAdvisorLookupElementEquals("getInstance().findFile().findElementAt", 0, 5, 3, 0, doCompletion().get(0)); } public void testChainsWithIndependentCallings() {