Skip to content

Commit

Permalink
Candidates customized order lost when tabbing through candidates, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Dec 23, 2021
1 parent cea9632 commit bfcc415
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
11 changes: 6 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 @@ -5076,18 +5076,19 @@ protected PostResult computePost(List<Candidate> possible, Candidate selection,

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) {
List<Object> strings = new ArrayList<>();
boolean customOrder = possible.stream().anyMatch(c -> c.sort() != 0);
if (groupName) {
Comparator<String> groupComparator = getGroupComparator();
Map<String, Map<String, Candidate>> sorted;
Map<String, Map<Object, Candidate>> sorted;
sorted = groupComparator != null
? new TreeMap<>(groupComparator)
: new LinkedHashMap<>();
for (Candidate cand : possible) {
String group = cand.group();
sorted.computeIfAbsent(group != null ? group : "", s -> new LinkedHashMap<>())
.put(cand.value(), cand);
.put((customOrder ? cand.sort() : cand.value()), cand);
}
for (Map.Entry<String, Map<String, Candidate>> entry : sorted.entrySet()) {
for (Map.Entry<String, Map<Object, Candidate>> entry : sorted.entrySet()) {
String group = entry.getKey();
if (group.isEmpty() && sorted.size() > 1) {
group = getOthersGroupName();
Expand All @@ -5102,13 +5103,13 @@ protected PostResult computePost(List<Candidate> possible, Candidate selection,
}
} else {
Set<String> groups = new LinkedHashSet<>();
TreeMap<String, Candidate> sorted = new TreeMap<>();
TreeMap<Object, Candidate> sorted = new TreeMap<>();
for (Candidate cand : possible) {
String group = cand.group();
if (group != null) {
groups.add(group);
}
sorted.put(cand.value(), cand);
sorted.put((customOrder ? cand.sort() : cand.value()), cand);
}
if (autoGroup) {
strings.addAll(groups);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
*
* https://opensource.org/licenses/BSD-3-Clause
*/
package org.jline.reader;
package org.jline.reader.impl;

import org.jline.reader.Candidate;
import org.junit.Test;

import java.util.Arrays;
Expand All @@ -16,7 +17,7 @@

import static org.junit.Assert.assertEquals;

public class CandidateTest {
public class CandidateCustomSortTest extends ReaderTestSupport {

@Test
public void testCandidatesSortedByValue() {
Expand Down Expand Up @@ -73,4 +74,17 @@ public void testCandidatesSortedByCornerSortProps() {
assertEquals("cand1", candidates.get(1).value());
assertEquals("cand2", candidates.get(2).value());
}

@Test
public void testCompletionSort() throws Exception {
reader.setCompleter(
(reader, line, candidates) -> {
candidates.add(new Candidate("foo", "foo", null, null, null, null, true, 0));
candidates.add(new Candidate("bar", "bar", null, null, null, null, true, 1));
candidates.add(new Candidate("zoo", "zoo", null, null, null, null, true, 2));
});
assertBuffer("foo", new TestBuffer("").tab().tab());

}

}

0 comments on commit bfcc415

Please sign in to comment.