cleanup: rename

This commit is contained in:
Dmitry Batkovich
2017-05-15 15:53:05 +03:00
parent e8cb69649d
commit d4277c4ab3
8 changed files with 87 additions and 90 deletions
@@ -29,21 +29,18 @@ import org.jetbrains.jps.backwardRefs.SignatureData;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author Dmitry Batkovich
*/
public class ChainsSearcher {
private static final Logger LOG = Logger.getInstance(ChainsSearcher.class);
public class ChainSearcher {
private static final Logger LOG = Logger.getInstance(ChainSearcher.class);
private ChainsSearcher() {
private ChainSearcher() {
}
@NotNull
public static List<MethodsChain> search(int pathMaximalLength,
ChainSearchTarget searchTarget,
int maxResultSize,
ChainCompletionContext context,
CompilerReferenceServiceEx compilerReferenceServiceEx) {
public static List<MethodChain> search(int pathMaximalLength,
ChainSearchTarget searchTarget,
int maxResultSize,
ChainCompletionContext context,
CompilerReferenceServiceEx compilerReferenceServiceEx) {
SearchInitializer initializer = createInitializer(searchTarget, compilerReferenceServiceEx, context);
return search(compilerReferenceServiceEx,
initializer,
@@ -70,20 +67,20 @@ public class ChainsSearcher {
}
@NotNull
private static List<MethodsChain> search(CompilerReferenceServiceEx indexReader,
SearchInitializer initializer,
int pathMaximalLength,
int maxResultSize,
ChainCompletionContext context) {
private static List<MethodChain> search(CompilerReferenceServiceEx indexReader,
SearchInitializer initializer,
int pathMaximalLength,
int maxResultSize,
ChainCompletionContext context) {
SearchInitializer.InitResult initResult = initializer.init(Collections.emptySet());
Map<MethodIncompleteSignature, MethodsChain> knownDistance = initResult.getChains();
Map<MethodIncompleteSignature, MethodChain> knownDistance = initResult.getChains();
LinkedList<OccurrencesAware<MethodsChain>> q = initResult
LinkedList<OccurrencesAware<MethodChain>> q = initResult
.getVertices()
.stream()
.map(
signAndWeight -> new OccurrencesAware<>(MethodsChain.create(signAndWeight.getUnderlying(), signAndWeight.getOccurrences(), context),
signAndWeight -> new OccurrencesAware<>(MethodChain.create(signAndWeight.getUnderlying(), signAndWeight.getOccurrences(), context),
signAndWeight.getOccurrences()))
.filter(Objects::nonNull)
.collect(Collectors.toCollection(LinkedList::new));
@@ -91,12 +88,12 @@ public class ChainsSearcher {
ResultHolder result = new ResultHolder();
while (!q.isEmpty()) {
ProgressManager.checkCanceled();
OccurrencesAware<MethodsChain> currentVertex = q.poll();
OccurrencesAware<MethodChain> currentVertex = q.poll();
int currentVertexDistance = currentVertex.getOccurrences();
MethodsChain currentChain = currentVertex.getUnderlying();
MethodChain currentChain = currentVertex.getUnderlying();
MethodIncompleteSignature headSignature = currentChain.getHeadSignature();
MethodsChain currentVertexMethodsChain = knownDistance.get(headSignature);
if (currentVertexDistance != currentVertexMethodsChain.getChainWeight()) {
MethodChain currentVertexMethodChain = knownDistance.get(headSignature);
if (currentVertexDistance != currentVertexMethodChain.getChainWeight()) {
continue;
}
if (headSignature.isStatic() || context.hasQualifier(context.resolveQualifierClass(headSignature))) {
@@ -113,16 +110,16 @@ public class ChainsSearcher {
int occurrences = indexValue.getOccurrences();
if (vertex.isStatic() || !vertex.getOwner().equals(targetQName)) {
int vertexDistance = Math.min(currentVertexDistance, occurrences);
MethodsChain knownVertexMethodsChain = knownDistance.get(vertex);
if ((knownVertexMethodsChain == null || knownVertexMethodsChain.getChainWeight() < vertexDistance)) {
MethodChain knownVertexMethodChain = knownDistance.get(vertex);
if ((knownVertexMethodChain == null || knownVertexMethodChain.getChainWeight() < vertexDistance)) {
if (currentSignatures.isEmpty() || currentSignatures.last().getOccurrences() < vertexDistance) {
if (currentVertexMethodsChain.size() < pathMaximalLength - 1) {
MethodsChain newBestMethodsChain =
currentVertexMethodsChain.continuation(indexValue.getUnderlying(), vertexDistance, context);
if (newBestMethodsChain != null) {
if (currentVertexMethodChain.size() < pathMaximalLength - 1) {
MethodChain newBestMethodChain =
currentVertexMethodChain.continuation(indexValue.getUnderlying(), vertexDistance, context);
if (newBestMethodChain != null) {
currentSignatures
.add(new OccurrencesAware<>(indexValue.getUnderlying(), vertexDistance));
knownDistance.put(vertex, newBestMethodsChain);
knownDistance.put(vertex, newBestMethodChain);
}
}
}
@@ -141,7 +138,7 @@ public class ChainsSearcher {
boolean stopChain = sign.getUnderlying().isStatic() || context.hasQualifier(context.resolveQualifierClass(sign.getUnderlying()));
if (stopChain) {
updated = true;
MethodsChain continuation = currentChain.continuation(sign.getUnderlying(), sign.getOccurrences(), context);
MethodChain continuation = currentChain.continuation(sign.getUnderlying(), sign.getOccurrences(), context);
if (continuation != null) {
result.add(continuation);
}
@@ -149,10 +146,10 @@ public class ChainsSearcher {
}
else {
updated = true;
MethodsChain methodsChain =
MethodChain methodChain =
currentChain.continuation(sign.getUnderlying(), sign.getOccurrences(), context);
if (methodsChain != null) {
q.addFirst(new OccurrencesAware<>(methodsChain, sign.getOccurrences()));
if (methodChain != null) {
q.addFirst(new OccurrencesAware<>(methodChain, sign.getOccurrences()));
continue;
}
}
@@ -173,19 +170,19 @@ public class ChainsSearcher {
return result.getResult();
}
private static MethodsChain createChainFromFirstElement(MethodsChain chain, PsiClass newQualifierClass) {
private static MethodChain createChainFromFirstElement(MethodChain chain, PsiClass newQualifierClass) {
//TODO
return new MethodsChain(newQualifierClass, Collections.singletonList(chain.getFirst()), null, chain.getChainWeight());
return new MethodChain(newQualifierClass, Collections.singletonList(chain.getFirst()), null, chain.getChainWeight());
}
private static class ResultHolder {
private final List<MethodsChain> myResult;
private final List<MethodChain> myResult;
private ResultHolder() {
myResult = new ArrayList<>();
}
public void add(MethodsChain newChain) {
public void add(MethodChain newChain) {
if (myResult.isEmpty()) {
myResult.add(newChain);
return;
@@ -193,9 +190,9 @@ public class ChainsSearcher {
boolean doAdd = true;
Stack<Integer> indexesToRemove = new Stack<>();
for (int i = 0; i < myResult.size(); i++) {
MethodsChain chain = myResult.get(i);
MethodChain chain = myResult.get(i);
//
MethodsChain.CompareResult r = MethodsChain.compare(chain, newChain);
MethodChain.CompareResult r = MethodChain.compare(chain, newChain);
switch (r) {
case LEFT_CONTAINS_RIGHT:
indexesToRemove.add(i);
@@ -216,11 +213,11 @@ public class ChainsSearcher {
}
}
public List<MethodsChain> getRawResult() {
public List<MethodChain> getRawResult() {
return myResult;
}
public List<MethodsChain> getResult() {
public List<MethodChain> getResult() {
return findSimilar(reduceChainsSize(myResult));
}
@@ -228,7 +225,7 @@ public class ChainsSearcher {
return myResult.size();
}
private static List<MethodsChain> reduceChainsSize(List<MethodsChain> chains) {
private static List<MethodChain> reduceChainsSize(List<MethodChain> chains) {
return ContainerUtil.map(chains, chain -> {
Iterator<PsiMethod[]> chainIterator = chain.iterator();
if (!chainIterator.hasNext()) {
@@ -268,9 +265,9 @@ public class ChainsSearcher {
});
}
private static List<MethodsChain> findSimilar(List<MethodsChain> chains) {
private static List<MethodChain> findSimilar(List<MethodChain> chains) {
ResultHolder resultHolder = new ResultHolder();
for (MethodsChain chain : chains) {
for (MethodChain chain : chains) {
resultHolder.add(chain);
}
return resultHolder.getRawResult();
@@ -30,16 +30,16 @@ import java.util.stream.Collectors;
import static com.intellij.util.containers.ContainerUtil.newArrayList;
import static com.intellij.util.containers.ContainerUtil.reverse;
public class MethodsChain {
public class MethodChain {
private final List<PsiMethod[]> myRevertedPath;
private final MethodIncompleteSignature mySignature;
private final int myWeight;
private final PsiClass myQualifierClass;
@Nullable
public static MethodsChain create(@NotNull MethodIncompleteSignature signature,
int weight,
@NotNull ChainCompletionContext context) {
public static MethodChain create(@NotNull MethodIncompleteSignature signature,
int weight,
@NotNull ChainCompletionContext context) {
PsiClass qualifier = context.resolveQualifierClass(signature);
if (qualifier == null || (!signature.isStatic() && InheritanceUtil.isInheritorOrSelf(context.getTarget().getTargetClass(), qualifier, true))) {
return null;
@@ -55,13 +55,13 @@ public class MethodsChain {
return null;
}
classes.add(contextClass);
return new MethodsChain(qualifier, Collections.singletonList(methods), signature, weight);
return new MethodChain(qualifier, Collections.singletonList(methods), signature, weight);
}
public MethodsChain(@NotNull PsiClass qualifierClass,
@NotNull List<PsiMethod[]> revertedPath,
MethodIncompleteSignature signature,
int weight) {
public MethodChain(@NotNull PsiClass qualifierClass,
@NotNull List<PsiMethod[]> revertedPath,
MethodIncompleteSignature signature,
int weight) {
myQualifierClass = qualifierClass;
myRevertedPath = revertedPath;
mySignature = signature;
@@ -99,16 +99,16 @@ public class MethodsChain {
}
public MethodsChain continuation(@NotNull MethodIncompleteSignature signature,
int weight,
@NotNull ChainCompletionContext context) {
MethodsChain head = create(signature, weight, context);
public MethodChain continuation(@NotNull MethodIncompleteSignature signature,
int weight,
@NotNull ChainCompletionContext context) {
MethodChain head = create(signature, weight, context);
if (head == null) return null;
ArrayList<PsiMethod[]> newRevertedPath = newArrayList();
newRevertedPath.addAll(myRevertedPath);
newRevertedPath.add(head.getPath().get(0));
return new MethodsChain(head.getQualifierClass(), newRevertedPath, head.getHeadSignature(), weight);
return new MethodChain(head.getQualifierClass(), newRevertedPath, head.getHeadSignature(), weight);
}
@Override
@@ -117,7 +117,7 @@ public class MethodsChain {
}
@SuppressWarnings("ConstantConditions")
public static CompareResult compare(@NotNull MethodsChain left, @NotNull MethodsChain right) {
public static CompareResult compare(@NotNull MethodChain left, @NotNull MethodChain right) {
if (left.size() == 0) {
return CompareResult.RIGHT_CONTAINS_LEFT;
}
@@ -45,7 +45,7 @@ import static com.intellij.psi.CommonClassNames.JAVA_LANG_STRING;
public class MethodsChainLookupRangingHelper {
@Nullable
public static LookupElement chainToWeightableLookupElement(MethodsChain chain,
public static LookupElement chainToWeightableLookupElement(MethodChain chain,
ChainCompletionContext context) {
int chainSize = chain.size();
assert chainSize != 0;
@@ -22,7 +22,7 @@ import com.intellij.openapi.util.Pair;
import java.util.*;
public class SearchInitializer {
private final LinkedHashMap<MethodIncompleteSignature, Pair<MethodsChain, Integer>> myChains;
private final LinkedHashMap<MethodIncompleteSignature, Pair<MethodChain, Integer>> myChains;
private final ChainCompletionContext myContext;
public SearchInitializer(SortedSet<OccurrencesAware<MethodIncompleteSignature>> indexValues,
@@ -51,9 +51,9 @@ public class SearchInitializer {
private boolean add(OccurrencesAware<MethodIncompleteSignature> indexValue) {
MethodIncompleteSignature methodInvocation = indexValue.getUnderlying();
int occurrences = indexValue.getOccurrences();
MethodsChain methodsChain = MethodsChain.create(indexValue.getUnderlying(), occurrences, myContext);
if (methodsChain != null) {
myChains.put(methodInvocation, Pair.create(methodsChain, occurrences));
MethodChain methodChain = MethodChain.create(indexValue.getUnderlying(), occurrences, myContext);
if (methodChain != null) {
myChains.put(methodInvocation, Pair.create(methodChain, occurrences));
return true;
}
return false;
@@ -62,14 +62,14 @@ public class SearchInitializer {
public InitResult init(Set<String> excludedEdgeNames) {
int size = myChains.size();
List<OccurrencesAware<MethodIncompleteSignature>> initedVertexes = new ArrayList<>(size);
LinkedHashMap<MethodIncompleteSignature, MethodsChain> initedChains =
LinkedHashMap<MethodIncompleteSignature, MethodChain> initedChains =
new LinkedHashMap<>(size);
for (Map.Entry<MethodIncompleteSignature, Pair<MethodsChain, Integer>> entry : myChains.entrySet()) {
for (Map.Entry<MethodIncompleteSignature, Pair<MethodChain, Integer>> entry : myChains.entrySet()) {
MethodIncompleteSignature signature = entry.getKey();
if (!excludedEdgeNames.contains(signature.getName())) {
initedVertexes.add(new OccurrencesAware<>(entry.getKey(), entry.getValue().getSecond()));
MethodsChain methodsChain = entry.getValue().getFirst();
initedChains.put(signature, methodsChain);
MethodChain methodChain = entry.getValue().getFirst();
initedChains.put(signature, methodChain);
}
}
return new InitResult(initedVertexes, initedChains);
@@ -77,10 +77,10 @@ public class SearchInitializer {
public static class InitResult {
private final List<OccurrencesAware<MethodIncompleteSignature>> myVertexes;
private final LinkedHashMap<MethodIncompleteSignature, MethodsChain> myChains;
private final LinkedHashMap<MethodIncompleteSignature, MethodChain> myChains;
private InitResult(List<OccurrencesAware<MethodIncompleteSignature>> vertexes,
LinkedHashMap<MethodIncompleteSignature, MethodsChain> chains) {
LinkedHashMap<MethodIncompleteSignature, MethodChain> chains) {
myVertexes = vertexes;
myChains = chains;
}
@@ -89,7 +89,7 @@ public class SearchInitializer {
return myVertexes;
}
public LinkedHashMap<MethodIncompleteSignature, MethodsChain> getChains() {
public LinkedHashMap<MethodIncompleteSignature, MethodChain> getChains() {
return myChains;
}
}
@@ -21,8 +21,8 @@ import com.intellij.compiler.CompilerReferenceService;
import com.intellij.compiler.backwardRefs.CompilerReferenceServiceEx;
import com.intellij.compiler.backwardRefs.ReferenceIndexUnavailableException;
import com.intellij.compiler.chainsSearch.ChainSearchMagicConstants;
import com.intellij.compiler.chainsSearch.ChainsSearcher;
import com.intellij.compiler.chainsSearch.MethodsChain;
import com.intellij.compiler.chainsSearch.ChainSearcher;
import com.intellij.compiler.chainsSearch.MethodChain;
import com.intellij.compiler.chainsSearch.MethodsChainLookupRangingHelper;
import com.intellij.compiler.chainsSearch.context.ChainCompletionContext;
import com.intellij.compiler.chainsSearch.context.ChainSearchTarget;
@@ -50,14 +50,14 @@ import static com.intellij.patterns.PsiJavaPatterns.psiElement;
/**
* @author Dmitry Batkovich
*/
public class MethodsChainsCompletionContributor extends CompletionContributor {
public class MethodChainCompletionContributor extends CompletionContributor {
public static final String REGISTRY_KEY = "compiler.ref.chain.search";
private static final Logger LOG = Logger.getInstance(MethodsChainsCompletionContributor.class);
private static final Logger LOG = Logger.getInstance(MethodChainCompletionContributor.class);
private static final boolean UNIT_TEST_MODE = ApplicationManager.getApplication().isUnitTestMode();
public static final CompletionType COMPLETION_TYPE = UNIT_TEST_MODE ? CompletionType.BASIC : CompletionType.SMART;
@SuppressWarnings("unchecked")
public MethodsChainsCompletionContributor() {
public MethodChainCompletionContributor() {
ElementPattern<PsiElement> pattern = or(patternForMethodCallParameter(), patternForVariableAssignment());
extend(COMPLETION_TYPE, pattern, new CompletionProvider<CompletionParameters>() {
@Override
@@ -100,13 +100,13 @@ public class MethodsChainsCompletionContributor extends CompletionContributor {
private static List<LookupElement> searchForLookups(ChainCompletionContext context) {
CompilerReferenceServiceEx methodsUsageIndexReader = (CompilerReferenceServiceEx)CompilerReferenceService.getInstance(context.getProject());
ChainSearchTarget target = context.getTarget();
List<MethodsChain> searchResult =
ChainsSearcher.search(ChainSearchMagicConstants.MAX_CHAIN_SIZE,
target,
ChainSearchMagicConstants.MAX_SEARCH_RESULT_SIZE,
context,
methodsUsageIndexReader);
int maxWeight = searchResult.stream().mapToInt(MethodsChain::getChainWeight).max().orElse(0);
List<MethodChain> searchResult =
ChainSearcher.search(ChainSearchMagicConstants.MAX_CHAIN_SIZE,
target,
ChainSearchMagicConstants.MAX_SEARCH_RESULT_SIZE,
context,
methodsUsageIndexReader);
int maxWeight = searchResult.stream().mapToInt(MethodChain::getChainWeight).max().orElse(0);
return searchResult
.stream()
@@ -25,7 +25,7 @@ import org.jetbrains.annotations.NotNull;
/**
* @author Dmitry Batkovich
*/
public class MethodsChainsWeigher extends CompletionWeigher {
public class MethodChainWeigher extends CompletionWeigher {
@Override
public Comparable weigh(@NotNull final LookupElement element, @NotNull final CompletionLocation location) {
if (element instanceof WeightableChainLookupElement) {
@@ -19,7 +19,7 @@ import com.intellij.JavaTestUtil;
import com.intellij.codeInsight.lookup.Lookup;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.compiler.chainsSearch.ChainRelevance;
import com.intellij.compiler.chainsSearch.completion.MethodsChainsCompletionContributor;
import com.intellij.compiler.chainsSearch.completion.MethodChainCompletionContributor;
import com.intellij.compiler.chainsSearch.completion.lookup.ChainCompletionMethodCallLookupElement;
import com.intellij.compiler.chainsSearch.completion.lookup.WeightableChainLookupElement;
import com.intellij.ide.util.PropertiesComponent;
@@ -43,13 +43,13 @@ public class MethodChainsCompletionTest extends AbstractCompilerAwareTest {
protected void setUp() throws Exception {
super.setUp();
installCompiler();
Registry.get(MethodsChainsCompletionContributor.REGISTRY_KEY).setValue(true);
Registry.get(MethodChainCompletionContributor.REGISTRY_KEY).setValue(true);
}
@Override
protected void tearDown() throws Exception {
try {
Registry.get(MethodsChainsCompletionContributor.REGISTRY_KEY).setValue(false);
Registry.get(MethodChainCompletionContributor.REGISTRY_KEY).setValue(false);
} finally {
super.tearDown();
}
+2 -2
View File
@@ -1880,9 +1880,9 @@
<gotoDeclarationHandler implementation="com.intellij.codeInsight.navigation.actions.GotoLambdaParameterHandler"/>
<completion.contributor language="JAVA" id="methodsChainsCompletionContributor" order="first"
implementationClass="com.intellij.compiler.chainsSearch.completion.MethodsChainsCompletionContributor"/>
implementationClass="com.intellij.compiler.chainsSearch.completion.MethodChainCompletionContributor"/>
<weigher order="first" key="completion" id="methodsChains"
implementationClass="com.intellij.compiler.chainsSearch.completion.MethodsChainsWeigher"/>
implementationClass="com.intellij.compiler.chainsSearch.completion.MethodChainWeigher"/>
<applicationService serviceInterface="org.jetbrains.java.generate.template.toString.ToStringTemplatesManager"
serviceImplementation="org.jetbrains.java.generate.template.toString.ToStringTemplatesManager"/>