Skip to content

Commit

Permalink
Tab completion: require at least one character to complete command
Browse files Browse the repository at this point in the history
options, fixes #487
  • Loading branch information
mattirn committed Dec 14, 2019
1 parent 10a8d99 commit daba558
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions builtins/src/test/java/org/jline/example/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
.variable(LineReader.SECONDARY_PROMPT_PATTERN, "%M%P > ")
.variable(LineReader.INDENTATION, 2)
.option(Option.INSERT_BRACKET, true)
.option(Option.EMPTY_WORD_OPTIONS, false)
.build();
//
// widgets
Expand Down
3 changes: 3 additions & 0 deletions reader/src/main/java/org/jline/reader/LineReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,9 @@ enum Option {

/** Automatic insertion of closing bracket */
INSERT_BRACKET,

/** Show command options tab completion candidates for zero length word */
EMPTY_WORD_OPTIONS(true),
;

private final boolean def;
Expand Down
16 changes: 11 additions & 5 deletions reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4461,11 +4461,17 @@ protected boolean doComplete(CompletionType lst, boolean useMenu, boolean prefix
} else {
String wd = line.word();
String wdi = caseInsensitive ? wd.toLowerCase() : wd;
matchers = Arrays.asList(
simpleMatcher(s -> (caseInsensitive ? s.toLowerCase() : s).startsWith(wdi)),
simpleMatcher(s -> (caseInsensitive ? s.toLowerCase() : s).contains(wdi)),
typoMatcher(wdi, errors, caseInsensitive)
);
if (isSet(Option.EMPTY_WORD_OPTIONS) || wd.length() > 0) {
matchers = Arrays.asList(
simpleMatcher(s -> (caseInsensitive ? s.toLowerCase() : s).startsWith(wdi)),
simpleMatcher(s -> (caseInsensitive ? s.toLowerCase() : s).contains(wdi)),
typoMatcher(wdi, errors, caseInsensitive)
);
} else {
matchers = Arrays.asList(
simpleMatcher(s -> !s.startsWith("-"))
);
}
exact = s -> caseInsensitive ? s.equalsIgnoreCase(wd) : s.equals(wd);
}
// Find matching candidates
Expand Down

0 comments on commit daba558

Please sign in to comment.