Skip to content

Commit

Permalink
CompletionMatcherImpl: add max search time for camelMatch()
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jan 17, 2021
1 parent ac4cdc1 commit 4ba4649
Showing 1 changed file with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import java.util.stream.Collectors;

public class CompletionMatcherImpl implements CompletionMatcher {
private static final int MAX_SEARCH_TIME = 1000;
protected Predicate<String> exact;
protected List<Function<Map<String, List<Candidate>>, Map<String, List<Candidate>>>> matchers;
private Map<String, List<Candidate>> matching;
private boolean caseInsensitive;
private long searchMaxTime;

public CompletionMatcherImpl() {
}
Expand All @@ -43,16 +45,13 @@ public void compile(Map<LineReader.Option, Boolean> options, boolean prefix, Com
defaultMatchers(options, prefix, line, caseInsensitive, errors, originalGroupName);
if (LineReader.Option.COMPLETE_MATCHER_CAMELCASE.isSet(options)) {
matchers.add(simpleMatcher(candidate -> camelMatch(line.word(), line.word().indexOf('=') + 1
, candidate, countUpperCase(line.word()) < 15 ? 0 : candidate.length())));
, candidate, 0)));
}
}

private long countUpperCase(String word) {
return word.substring(word.indexOf('=') + 1).chars().filter(Character::isUpperCase).count();
}

@Override
public List<Candidate> matches(List<Candidate> candidates) {
searchMaxTime = new Date().getTime() + MAX_SEARCH_TIME;
matching = Collections.emptyMap();
Map<String, List<Candidate>> sortedCandidates = sort(candidates);
for (Function<Map<String, List<Candidate>>,
Expand Down Expand Up @@ -157,7 +156,9 @@ Map<String, List<Candidate>>> typoMatcher(String word, int errors, boolean caseI
}

protected boolean camelMatch(String word, int i, String candidate, int j) {
if (word.length() <= i) {
if (new Date().getTime() > searchMaxTime) {
return false;
} else if (word.length() <= i) {
return true;
} else if (candidate.length() <= j) {
return false;
Expand Down

0 comments on commit 4ba4649

Please sign in to comment.