Skip to content

Commit b982407

Browse files
authored
Use default sort order when custom sort order is identical (fixes #956) (#957)
1 parent bef5cb1 commit b982407

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5204,45 +5204,47 @@ protected PostResult computePost(
52045204
boolean groupName,
52055205
boolean rowsFirst) {
52065206
List<Object> strings = new ArrayList<>();
5207-
boolean customOrder = possible.stream().anyMatch(c -> c.sort() != 0);
52085207
if (groupName) {
52095208
Comparator<String> groupComparator = getGroupComparator();
5210-
Map<String, Map<Object, Candidate>> sorted;
5209+
Map<String, List<Candidate>> sorted;
52115210
sorted = groupComparator != null ? new TreeMap<>(groupComparator) : new LinkedHashMap<>();
52125211
for (Candidate cand : possible) {
52135212
String group = cand.group();
5214-
sorted.computeIfAbsent(group != null ? group : "", s -> new LinkedHashMap<>())
5215-
.put((customOrder ? cand.sort() : cand.value()), cand);
5213+
sorted.computeIfAbsent(group != null ? group : "", s -> new ArrayList<>())
5214+
.add(cand);
52165215
}
5217-
for (Map.Entry<String, Map<Object, Candidate>> entry : sorted.entrySet()) {
5216+
for (Map.Entry<String, List<Candidate>> entry : sorted.entrySet()) {
52185217
String group = entry.getKey();
52195218
if (group.isEmpty() && sorted.size() > 1) {
52205219
group = getOthersGroupName();
52215220
}
52225221
if (!group.isEmpty() && autoGroup) {
52235222
strings.add(group);
52245223
}
5225-
strings.add(new ArrayList<>(entry.getValue().values()));
5224+
List<Candidate> candidates = entry.getValue();
5225+
Collections.sort(candidates);
5226+
strings.add(candidates);
52265227
if (ordered != null) {
5227-
ordered.addAll(entry.getValue().values());
5228+
ordered.addAll(candidates);
52285229
}
52295230
}
52305231
} else {
52315232
Set<String> groups = new LinkedHashSet<>();
5232-
TreeMap<Object, Candidate> sorted = new TreeMap<>();
5233+
List<Candidate> sorted = new ArrayList<>();
52335234
for (Candidate cand : possible) {
52345235
String group = cand.group();
52355236
if (group != null) {
52365237
groups.add(group);
52375238
}
5238-
sorted.put((customOrder ? cand.sort() : cand.value()), cand);
5239+
sorted.add(cand);
52395240
}
52405241
if (autoGroup) {
52415242
strings.addAll(groups);
52425243
}
5243-
strings.add(new ArrayList<>(sorted.values()));
5244+
Collections.sort(sorted);
5245+
strings.add(sorted);
52445246
if (ordered != null) {
5245-
ordered.addAll(sorted.values());
5247+
ordered.addAll(sorted);
52465248
}
52475249
}
52485250
return toColumns(strings, selection, completed, wcwidth, width, rowsFirst);

reader/src/test/java/org/jline/reader/impl/CandidateCustomSortTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.List;
1414

1515
import org.jline.reader.Candidate;
16+
import org.jline.reader.LineReader.Option;
1617
import org.junit.jupiter.api.Test;
1718

1819
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -80,4 +81,28 @@ public void testCompletionSort() throws Exception {
8081
});
8182
assertBuffer("foo", new TestBuffer("").tab().tab());
8283
}
84+
85+
@Test
86+
public void testCustomSortIdenticalSortValueGroups() {
87+
List<Candidate> candidates = Arrays.asList(
88+
new Candidate("foo", "foo", null, null, null, null, true, 1),
89+
new Candidate("bar", "bar", null, null, null, null, true, 1),
90+
new Candidate("zoo", "zoo", null, null, null, null, true, 1),
91+
new Candidate("zzz", "zzz", null, null, null, null, true, 0));
92+
reader.setOpt(Option.GROUP);
93+
String postResult = reader.computePost(candidates, null, null, "").post.toString();
94+
assertEquals("zzz bar foo zoo", postResult);
95+
}
96+
97+
@Test
98+
public void testCustomSortIdenticalSortValue() {
99+
List<Candidate> candidates = Arrays.asList(
100+
new Candidate("foo", "foo", null, null, null, null, true, 1),
101+
new Candidate("bar", "bar", null, null, null, null, true, 1),
102+
new Candidate("zoo", "zoo", null, null, null, null, true, 1),
103+
new Candidate("zzz", "zzz", null, null, null, null, true, 0));
104+
reader.unsetOpt(Option.GROUP);
105+
String postResult = reader.computePost(candidates, null, null, "").post.toString();
106+
assertEquals("zzz bar foo zoo", postResult);
107+
}
83108
}

0 commit comments

Comments
 (0)