Skip to content

Commit

Permalink
Use default sort order when custom sort order is identical (fixes #956)…
Browse files Browse the repository at this point in the history
… (#957)
  • Loading branch information
valepakh committed Apr 17, 2024
1 parent bef5cb1 commit b982407
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
24 changes: 13 additions & 11 deletions reader/src/main/java/org/jline/reader/impl/LineReaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5204,45 +5204,47 @@ protected PostResult computePost(
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<Object, Candidate>> sorted;
Map<String, List<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((customOrder ? cand.sort() : cand.value()), cand);
sorted.computeIfAbsent(group != null ? group : "", s -> new ArrayList<>())
.add(cand);
}
for (Map.Entry<String, Map<Object, Candidate>> entry : sorted.entrySet()) {
for (Map.Entry<String, List<Candidate>> entry : sorted.entrySet()) {
String group = entry.getKey();
if (group.isEmpty() && sorted.size() > 1) {
group = getOthersGroupName();
}
if (!group.isEmpty() && autoGroup) {
strings.add(group);
}
strings.add(new ArrayList<>(entry.getValue().values()));
List<Candidate> candidates = entry.getValue();
Collections.sort(candidates);
strings.add(candidates);
if (ordered != null) {
ordered.addAll(entry.getValue().values());
ordered.addAll(candidates);
}
}
} else {
Set<String> groups = new LinkedHashSet<>();
TreeMap<Object, Candidate> sorted = new TreeMap<>();
List<Candidate> sorted = new ArrayList<>();
for (Candidate cand : possible) {
String group = cand.group();
if (group != null) {
groups.add(group);
}
sorted.put((customOrder ? cand.sort() : cand.value()), cand);
sorted.add(cand);
}
if (autoGroup) {
strings.addAll(groups);
}
strings.add(new ArrayList<>(sorted.values()));
Collections.sort(sorted);
strings.add(sorted);
if (ordered != null) {
ordered.addAll(sorted.values());
ordered.addAll(sorted);
}
}
return toColumns(strings, selection, completed, wcwidth, width, rowsFirst);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;

import org.jline.reader.Candidate;
import org.jline.reader.LineReader.Option;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -80,4 +81,28 @@ public void testCompletionSort() throws Exception {
});
assertBuffer("foo", new TestBuffer("").tab().tab());
}

@Test
public void testCustomSortIdenticalSortValueGroups() {
List<Candidate> candidates = Arrays.asList(
new Candidate("foo", "foo", null, null, null, null, true, 1),
new Candidate("bar", "bar", null, null, null, null, true, 1),
new Candidate("zoo", "zoo", null, null, null, null, true, 1),
new Candidate("zzz", "zzz", null, null, null, null, true, 0));
reader.setOpt(Option.GROUP);
String postResult = reader.computePost(candidates, null, null, "").post.toString();
assertEquals("zzz bar foo zoo", postResult);
}

@Test
public void testCustomSortIdenticalSortValue() {
List<Candidate> candidates = Arrays.asList(
new Candidate("foo", "foo", null, null, null, null, true, 1),
new Candidate("bar", "bar", null, null, null, null, true, 1),
new Candidate("zoo", "zoo", null, null, null, null, true, 1),
new Candidate("zzz", "zzz", null, null, null, null, true, 0));
reader.unsetOpt(Option.GROUP);
String postResult = reader.computePost(candidates, null, null, "").post.toString();
assertEquals("zzz bar foo zoo", postResult);
}
}

0 comments on commit b982407

Please sign in to comment.