Skip to content

Commit

Permalink
Add option to disable typo completion matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
retronym committed Jan 27, 2021
1 parent 859bc82 commit b1e83bb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
2 changes: 2 additions & 0 deletions reader/src/main/java/org/jline/reader/LineReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ enum Option {
COMPLETE_IN_WORD,
/** use camel case completion matcher */
COMPLETE_MATCHER_CAMELCASE,
/** use type completion matcher */
COMPLETE_MATCHER_TYPO(true),
DISABLE_EVENT_EXPANSION,
HISTORY_VERIFY,
HISTORY_IGNORE_SPACE(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ protected void defaultMatchers(Map<LineReader.Option, Boolean> options, boolean
String wp = wdi.substring(0, line.wordCursor());
matchers = new ArrayList<>(Arrays.asList(
simpleMatcher(s -> (caseInsensitive ? s.toLowerCase() : s).startsWith(wp)),
simpleMatcher(s -> (caseInsensitive ? s.toLowerCase() : s).contains(wp)),
typoMatcher(wp, errors, caseInsensitive, originalGroupName)
simpleMatcher(s -> (caseInsensitive ? s.toLowerCase() : s).contains(wp))
));
if (LineReader.Option.COMPLETE_MATCHER_TYPO.isSet(options)) {
matchers.add(typoMatcher(wp, errors, caseInsensitive, originalGroupName));
}
exact = s -> caseInsensitive ? s.equalsIgnoreCase(wp) : s.equals(wp);
} else if (LineReader.Option.COMPLETE_IN_WORD.isSet(options)) {
String wd = line.word();
Expand All @@ -114,19 +116,24 @@ protected void defaultMatchers(Map<LineReader.Option, Boolean> options, boolean
Pattern p2 = Pattern.compile(".*" + Pattern.quote(wp) + ".*" + Pattern.quote(ws) + ".*");
matchers = new ArrayList<>(Arrays.asList(
simpleMatcher(s -> p1.matcher(caseInsensitive ? s.toLowerCase() : s).matches()),
simpleMatcher(s -> p2.matcher(caseInsensitive ? s.toLowerCase() : s).matches()),
typoMatcher(wdi, errors, caseInsensitive, originalGroupName)
simpleMatcher(s -> p2.matcher(caseInsensitive ? s.toLowerCase() : s).matches())
));
if (LineReader.Option.COMPLETE_MATCHER_TYPO.isSet(options)) {
matchers.add(typoMatcher(wdi, errors, caseInsensitive, originalGroupName));
}

exact = s -> caseInsensitive ? s.equalsIgnoreCase(wd) : s.equals(wd);
} else {
String wd = line.word();
String wdi = caseInsensitive ? wd.toLowerCase() : wd;
if (LineReader.Option.EMPTY_WORD_OPTIONS.isSet(options) || wd.length() > 0) {
matchers = new ArrayList<>(Arrays.asList(
simpleMatcher(s -> (caseInsensitive ? s.toLowerCase() : s).startsWith(wdi)),
simpleMatcher(s -> (caseInsensitive ? s.toLowerCase() : s).contains(wdi)),
typoMatcher(wdi, errors, caseInsensitive, originalGroupName)
simpleMatcher(s -> (caseInsensitive ? s.toLowerCase() : s).contains(wdi))
));
if (LineReader.Option.COMPLETE_MATCHER_TYPO.isSet(options)) {
matchers.add(typoMatcher(wdi, errors, caseInsensitive, originalGroupName));
}
} else {
matchers = new ArrayList<>(Collections.singletonList(simpleMatcher(s -> !s.startsWith("-"))));
}
Expand Down
13 changes: 13 additions & 0 deletions reader/src/test/java/org/jline/reader/impl/CompletionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,20 @@ public void testListAndMenu() throws IOException {
assertBuffer("foo", new TestBuffer("fo\t\t\t"));
assertTrue(reader.list);
assertTrue(reader.menu);
}

@Test
public void testTypoMatcher() throws Exception {
reader.setCompleter(new StringsCompleter("foo", "foobar"));

reader.unsetOpt(Option.MENU_COMPLETE);
reader.unsetOpt(Option.AUTO_LIST);
reader.unsetOpt(Option.AUTO_MENU);
reader.unsetOpt(Option.LIST_AMBIGUOUS);

assertBuffer("foobar ", new TestBuffer("foobaZ\t"));
reader.unsetOpt(Option.COMPLETE_MATCHER_TYPO);
assertBuffer("foobaZ", new TestBuffer("foobaZ\t"));
}

@Test
Expand Down

0 comments on commit b1e83bb

Please sign in to comment.