diff --git a/src/main/java/jline/console/completer/ArgumentCompleter.java b/src/main/java/jline/console/completer/ArgumentCompleter.java index 21e035b4..9e988e28 100644 --- a/src/main/java/jline/console/completer/ArgumentCompleter.java +++ b/src/main/java/jline/console/completer/ArgumentCompleter.java @@ -140,11 +140,29 @@ public int complete(final String buffer, final int cursor, final List subCandidates = new LinkedList(); - if (sub.complete(arg, arg.length(), subCandidates) == -1) { + int offset = sub.complete(arg, arg.length(), subCandidates); + if (offset == -1) { return -1; } - if (!subCandidates.contains(arg)) { + // for strict matching, one of the candidates must equal the current argument "arg", + // starting from offset within arg, but the suitable candidate may actually also have a + // delimiter at then end. + boolean candidateMatches = false; + for (CharSequence subCandidate: subCandidates) { + // each SUbcandidate may end with the delimiter. + // That it contains the delimiter is possible, but not plausible. + String[] candidateDelimList = delim.delimit(subCandidate, 0).getArguments(); + if (candidateDelimList.length == 0) { + continue; + } + String trimmedCand = candidateDelimList[0]; + if (trimmedCand.equals(arg.substring(offset))) { + candidateMatches = true; + break; + } + } + if (!candidateMatches) { return -1; } } diff --git a/src/test/java/jline/console/completer/ArgumentCompleterTest.java b/src/test/java/jline/console/completer/ArgumentCompleterTest.java index 2da519a8..e11b03be 100644 --- a/src/test/java/jline/console/completer/ArgumentCompleterTest.java +++ b/src/test/java/jline/console/completer/ArgumentCompleterTest.java @@ -13,6 +13,8 @@ import jline.console.completer.StringsCompleter; import org.junit.Test; +import java.util.List; + /** * Tests for {@link jline.console.completer.ArgumentCompleter}. * @@ -65,4 +67,34 @@ public void test2() throws Exception { assertBuffer("some foo ", new Buffer("some fo").tab()); } -} \ No newline at end of file + + @Test + public void testMultipleRelative() throws Exception { + ArgumentCompleter argCompleter = new ArgumentCompleter( + new Completer() { + public int complete(String buffer, int cursor, List candidates) { + candidates.add("bar"); + return 3; + } + }, + new StringsCompleter("foo")); + console.addCompleter(argCompleter); + assertBuffer("thebar foo ", new Buffer("thebar ").tab()); + assertBuffer("thebar foo ", new Buffer("thebar f").tab()); + } + + @Test + public void testMultipleRelativeWithDelim() throws Exception { + ArgumentCompleter argCompleter = new ArgumentCompleter( + new Completer() { + public int complete(String buffer, int cursor, List candidates) { + candidates.add("bar "); + return 3; + } + }, + new StringsCompleter("foo")); + console.addCompleter(argCompleter); + assertBuffer("thebar foo ", new Buffer("thebar ").tab()); + assertBuffer("thebar foo ", new Buffer("thebar f").tab()); + } +}