Skip to content

Commit

Permalink
[JLINE-699] Make candidates for completion unique
Browse files Browse the repository at this point in the history
  • Loading branch information
snuyanzin authored and mattirn committed Sep 18, 2021
1 parent 9ca636f commit 74c97a2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
13 changes: 13 additions & 0 deletions reader/src/main/java/org/jline/reader/Candidate.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ public int compareTo(Candidate o) {
return value.compareTo(o.value);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Candidate candidate = (Candidate) o;
return Objects.equals(value, candidate.value);
}

@Override
public int hashCode() {
return Objects.hash(value);
}

@Override
public String toString() {
return "Candidate{" + value + "}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public List<Candidate> matches(List<Candidate> candidates) {
break;
}
}
return !matching.isEmpty() ? matching.entrySet().stream().flatMap(e -> e.getValue().stream()).collect(Collectors.toList())
return !matching.isEmpty() ? matching.entrySet().stream().flatMap(e -> e.getValue().stream()).distinct().collect(Collectors.toList())
: new ArrayList<>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import static org.junit.Assert.assertEquals;


public class CompletionWithCustomMatcherTest extends ReaderTestSupport {

Expand All @@ -30,6 +33,8 @@ public void compile(Map<LineReader.Option, Boolean> options, boolean prefix, Com
// add custom matcher before typo matcher
int pos = matchers.size() + (LineReader.Option.COMPLETE_MATCHER_TYPO.isSet(options) ? -1 : 0);
matchers.add(pos, simpleMatcher(candidate -> camelMatch(line.word(), 0, candidate, 0)));
Candidate c = new Candidate(line.word());
assertEquals("Expected only one element", 1, matches(Arrays.asList(c, c)).size());
}
}
});
Expand Down

0 comments on commit 74c97a2

Please sign in to comment.