Skip to content

Commit

Permalink
JLine option AUTO_MENU_LIST: candidate list is wrongly positioned, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Nov 10, 2020
1 parent ea8d0d3 commit a5686ab
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java
Expand Up @@ -5030,6 +5030,7 @@ protected boolean doList(List<Candidate> possible

boolean caseInsensitive = isSet(Option.CASE_INSENSITIVE);
StringBuilder sb = new StringBuilder();
candidateStartPosition = 0;
while (true) {
String current = completed + sb.toString();
List<Candidate> cands;
Expand All @@ -5045,7 +5046,9 @@ protected boolean doList(List<Candidate> possible
.sorted(getCandidateComparator(caseInsensitive, current))
.collect(Collectors.toList());
}
candidateStartPosition = candidateStartPosition();
if (isSet(Option.AUTO_MENU_LIST) && candidateStartPosition == 0) {
candidateStartPosition = candidateStartPosition(cands);
}
post = () -> {
AttributedString t = insertSecondaryPrompts(AttributedStringBuilder.append(prompt, buf.toString()), new ArrayList<>());
int pl = t.columnSplitLength(size.getColumns(), false, display.delayLineWrap()).size();
Expand Down Expand Up @@ -5181,7 +5184,13 @@ protected PostResult computePost(List<Candidate> possible, Candidate selection,
private static final int MARGIN_BETWEEN_COLUMNS = 3;
private static final int MENU_LIST_WIDTH = 25;

private int candidateStartPosition() {
private int candidateStartPosition(List<Candidate> cands) {
List<String> values = cands.stream().map(c -> AttributedString.stripAnsi(c.displ()))
.filter(c -> !c.matches("\\w+")).collect(Collectors.toList());
Set<String> notDelimiters = new HashSet<>();
values.forEach(v -> v.substring(0, v.length() - 1).chars()
.filter(c -> !Character.isDigit(c) && !Character.isAlphabetic(c))
.forEach(c -> notDelimiters.add(Character.toString((char)c))));
int out = prompt != null ? prompt.length() : 0;
String buffer = buf.substring(0, buf.cursor());
buffer = buffer.substring(buffer.lastIndexOf('\n') + 1);
Expand All @@ -5199,7 +5208,8 @@ private int candidateStartPosition() {
out = 0;
}
for (int i = buffer.length(); i > 0; i--) {
if (buffer.substring(0, i).matches(".*\\W")) {
if (buffer.substring(0, i).matches(".*\\W")
&& !notDelimiters.contains(buffer.substring(i - 1, i))) {
out += i;
break;
}
Expand Down

0 comments on commit a5686ab

Please sign in to comment.