Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix strict Argument checking broken with relative completion offset #202

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/main/java/jline/console/completer/ArgumentCompleter.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,29 @@ public int complete(final String buffer, final int cursor, final List<CharSequen

List<CharSequence> subCandidates = new LinkedList<CharSequence>();

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;
}
}
Expand Down
34 changes: 33 additions & 1 deletion src/test/java/jline/console/completer/ArgumentCompleterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import jline.console.completer.StringsCompleter;
import org.junit.Test;

import java.util.List;

/**
* Tests for {@link jline.console.completer.ArgumentCompleter}.
*
Expand Down Expand Up @@ -65,4 +67,34 @@ public void test2() throws Exception {

assertBuffer("some foo ", new Buffer("some fo").tab());
}
}

@Test
public void testMultipleRelative() throws Exception {
ArgumentCompleter argCompleter = new ArgumentCompleter(
new Completer() {
public int complete(String buffer, int cursor, List<CharSequence> 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<CharSequence> 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());
}
}