Skip to content

Commit

Permalink
Add support to filter chain by passing the in the token (#1282)
Browse files Browse the repository at this point in the history
  • Loading branch information
gayanper committed Apr 30, 2024
1 parent a885b80 commit eae41a0
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IType;

import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.ui.text.ChainElement.ElementType;

public class ChainFinder {
Expand All @@ -40,11 +40,19 @@ public class ChainFinder {

private volatile boolean isCanceled;

private String token;

public ChainFinder(final List<ChainType> expectedTypes, final List<String> excludedTypes,
final IType receiverType) {
this(expectedTypes, excludedTypes, receiverType, null);
}

public ChainFinder(final List<ChainType> expectedTypes, final List<String> excludedTypes,
final IType receiverType, final String token) {
this.expectedTypes= expectedTypes;
this.excludedTypes= excludedTypes;
this.receiverType= receiverType;
this.token= token;
}

public void startChainSearch(final List<ChainElement> entrypoints, final int maxChains, final int minDepth,
Expand Down Expand Up @@ -73,7 +81,8 @@ private void searchChainsForExpectedType(final ChainType expectedType, final int
while (!incompleteChains.isEmpty() && !isCanceled) {
final LinkedList<ChainElement> chain= incompleteChains.poll();
final ChainElement edge= chain.getLast();
if (isValidEndOfChain(edge, expectedType, expectedDimensions)) {
final ChainElement start= chain.getFirst();
if (isValidEndOfChain(edge, start, expectedType, expectedDimensions)) {
if (chain.size() >= minDepth) {
chains.add(new Chain(chain, expectedDimensions));
if (chains.size() == maxChains) {
Expand Down Expand Up @@ -123,11 +132,15 @@ public static boolean isFromExcludedType(final List<String> excluded, final Chai
return excluded.contains(element.getPrimitiveType());
}

private boolean isValidEndOfChain(final ChainElement edge, final ChainType expectedType,
private boolean isValidEndOfChain(final ChainElement edge, ChainElement start, final ChainType expectedType,
final int expectedDimension) {
if (edge.getElementType() == ElementType.TYPE) {
return false;
}
if (token != null && !token.isBlank() && !CharOperation.subWordMatch(token.toCharArray(), edge.getElement().getElementName().toCharArray())
&& !CharOperation.subWordMatch(token.toCharArray(), start.getElement().getElementName().toCharArray())) {
return false;
}
if ((edge.getReturnType().getPrimitiveType() != null)) {
return edge.getReturnType().getPrimitiveType().equals(expectedType.getPrimitiveType());
}
Expand Down

0 comments on commit eae41a0

Please sign in to comment.