Skip to content

Commit bfcc415

Browse files
committed
Candidates customized order lost when tabbing through candidates, fixes #763
1 parent cea9632 commit bfcc415

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5076,18 +5076,19 @@ protected PostResult computePost(List<Candidate> possible, Candidate selection,
50765076

50775077
protected PostResult computePost(List<Candidate> possible, Candidate selection, List<Candidate> ordered, String completed, Function<String, Integer> wcwidth, int width, boolean autoGroup, boolean groupName, boolean rowsFirst) {
50785078
List<Object> strings = new ArrayList<>();
5079+
boolean customOrder = possible.stream().anyMatch(c -> c.sort() != 0);
50795080
if (groupName) {
50805081
Comparator<String> groupComparator = getGroupComparator();
5081-
Map<String, Map<String, Candidate>> sorted;
5082+
Map<String, Map<Object, Candidate>> sorted;
50825083
sorted = groupComparator != null
50835084
? new TreeMap<>(groupComparator)
50845085
: new LinkedHashMap<>();
50855086
for (Candidate cand : possible) {
50865087
String group = cand.group();
50875088
sorted.computeIfAbsent(group != null ? group : "", s -> new LinkedHashMap<>())
5088-
.put(cand.value(), cand);
5089+
.put((customOrder ? cand.sort() : cand.value()), cand);
50895090
}
5090-
for (Map.Entry<String, Map<String, Candidate>> entry : sorted.entrySet()) {
5091+
for (Map.Entry<String, Map<Object, Candidate>> entry : sorted.entrySet()) {
50915092
String group = entry.getKey();
50925093
if (group.isEmpty() && sorted.size() > 1) {
50935094
group = getOthersGroupName();
@@ -5102,13 +5103,13 @@ protected PostResult computePost(List<Candidate> possible, Candidate selection,
51025103
}
51035104
} else {
51045105
Set<String> groups = new LinkedHashSet<>();
5105-
TreeMap<String, Candidate> sorted = new TreeMap<>();
5106+
TreeMap<Object, Candidate> sorted = new TreeMap<>();
51065107
for (Candidate cand : possible) {
51075108
String group = cand.group();
51085109
if (group != null) {
51095110
groups.add(group);
51105111
}
5111-
sorted.put(cand.value(), cand);
5112+
sorted.put((customOrder ? cand.sort() : cand.value()), cand);
51125113
}
51135114
if (autoGroup) {
51145115
strings.addAll(groups);

reader/src/test/java/org/jline/reader/CandidateTest.java renamed to reader/src/test/java/org/jline/reader/impl/CandidateCustomSortTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
*
77
* https://opensource.org/licenses/BSD-3-Clause
88
*/
9-
package org.jline.reader;
9+
package org.jline.reader.impl;
1010

11+
import org.jline.reader.Candidate;
1112
import org.junit.Test;
1213

1314
import java.util.Arrays;
@@ -16,7 +17,7 @@
1617

1718
import static org.junit.Assert.assertEquals;
1819

19-
public class CandidateTest {
20+
public class CandidateCustomSortTest extends ReaderTestSupport {
2021

2122
@Test
2223
public void testCandidatesSortedByValue() {
@@ -73,4 +74,17 @@ public void testCandidatesSortedByCornerSortProps() {
7374
assertEquals("cand1", candidates.get(1).value());
7475
assertEquals("cand2", candidates.get(2).value());
7576
}
77+
78+
@Test
79+
public void testCompletionSort() throws Exception {
80+
reader.setCompleter(
81+
(reader, line, candidates) -> {
82+
candidates.add(new Candidate("foo", "foo", null, null, null, null, true, 0));
83+
candidates.add(new Candidate("bar", "bar", null, null, null, null, true, 1));
84+
candidates.add(new Candidate("zoo", "zoo", null, null, null, null, true, 2));
85+
});
86+
assertBuffer("foo", new TestBuffer("").tab().tab());
87+
88+
}
89+
7690
}

0 commit comments

Comments
 (0)