Skip to content

Commit

Permalink
add subword match on edge and start
Browse files Browse the repository at this point in the history
  • Loading branch information
gayanper committed Nov 15, 2023
1 parent 79642ad commit c02861e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
Expand Up @@ -8,6 +8,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.Chain;
import org.eclipse.jdt.internal.ui.text.ChainElement;
import org.eclipse.jdt.internal.ui.text.ChainElement.ElementType;
Expand Down Expand Up @@ -65,7 +66,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 @@ -116,11 +118,12 @@ public static boolean isFromExcludedType(final List<String> excluded, final Chai
return excluded.contains(element.getPrimitiveType());
}

private boolean isValidEndOfChain(final ChainElement edge, final ChainType expectedType, final int expectedDimension) {
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() && !edge.getElement().getElementName().startsWith(token)) {
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)) {
Expand Down
Expand Up @@ -360,20 +360,41 @@ public static void main(String[] args) {
}

@Test
public void testChainCompletionsOnVariableWithToken() throws Exception {
public void testChainCompletionsOnVariableWithTokenMatchingEdge() throws Exception {
//@formatter:off
ICompilationUnit unit = getWorkingCopy(
"src/java/Foo.java",
"""
import java.util.Collection;
import java.util.List;
public class Foo {
public static void main(String[] args) {
Collection<String> names = emptyL
List<String> names = emptyL
}
}
""");
//@formatter:on
CompletionList list = requestCompletions(unit, "names = emptyL");
assertTrue("all start with token", list.getItems().stream().allMatch(i -> i.getLabel().matches(".*\\.emptyList()")));
assertEquals(2, list.getItems().size());
assertTrue("emptyList", list.getItems().stream().anyMatch(i -> i.getLabel().matches(".*\\.emptyList().*")));
assertTrue("EMPTY_LIST", list.getItems().stream().anyMatch(i -> i.getLabel().matches(".*\\.EMPTY_LIST.*")));
}

@Test
public void testChainCompletionsOnVariableWithTokenMatchingStart() throws Exception {
//@formatter:off
ICompilationUnit unit = getWorkingCopy(
"src/java/Foo.java",
"""
import java.util.List;
public class Foo {
public static void main(String[] args) {
List<String> names = Coll
}
}
""");
//@formatter:on
CompletionList list = requestCompletions(unit, "names = Coll");
assertTrue(list.getItems().size() > 0);
assertTrue("All Collections.*", list.getItems().stream().anyMatch(i -> i.getLabel().matches("Collections\\..*")));
}
}

0 comments on commit c02861e

Please sign in to comment.